Skip to content
toekneestuck edited this page Feb 21, 2012 · 61 revisions

This page serves to catalog shared underscore mixins, here and elsewhere.

Background: Mixin Syntax
_.mixin(object)

Allows you to extend Underscore with your own utility functions. Pass a hash of {name: function} definitions to have your functions added to the Underscore object, as well as the OOP wrapper.

_.mixin({
  capitalize : function(string) {
    return string.charAt(0).toUpperCase() + string.substring(1).toLowerCase();
  }
});
_("fabio").capitalize();
=> "Fabio"

Mixins in the Wild

(update me!)

  • Esa-Matti Suuronen has a sweet git repo named underscore.string which adds many string functions to Underscore.js through mixins.
  • Tim Wood created a mixin called underscore.date which is a javascript date library for parsing, manipulating, and formatting dates, without modifying the Date.prototype.
  • Omar Yasin published a mixin in this gist that provides a ‘printf’-like functionality
  • Xavier Spriet has this gist that has a mixin to emulate ruby’s Array#each_slice method.
  • Francesco Agati provides this gist with _.selectIf
  • Steven Black, in this gist _.filterObj: Return an object who’s members are culled to match a reference array of keys.
  • thegrandpoobah suggests this _.retry mixin.
  • Arnau Sanchez: Mixins: merge, mash, takeWhile, mapDetect, uniqWith, flatten1.
  • Paul Harper (benekastah): underscore.loop – adds scheme/clojure like recursive looping with tail-call optimization.
  • Kevin Malakoff (kmalakoff): underscore-awesomer – many “awesome” functions like ‘toJSON’/‘parseJSON’ for instance serializaion/deserializaion, ‘compare’, ‘keypath’ for accessing nested properties, ‘remove’, ‘own’/‘disown’, ‘cloneToDepth’, ‘toType’ conversions using to{X} function as a fallback if the types don’t match, etc.
  • Julian Burgess (aubergene): _.rotate – Array rotate mixin
  • Trevor Parscal (trevorparscal): more-underscore – More stuff for underscore, such as ‘extendClass’ (prototypical inheritance), ‘objectify’ (combine array values with property names) and ‘traverse’ (get a value from a multi-level collection using an array of keys and indexes).
  • tobias104 provides this _.indexBy gist – Combination of _.find and _.indexOf, that returns the index of the first item that passes the test.
  • Mark Rushakoff (mark-rushakoff): function binding helpers – provides two functions similar to _.bindAll. _.bindFiltered takes a filter function that operates on function names, and _.bindMatching takes a regex for the names of which functions to bind.
  • Adrian Rossouw (Vertice): Partial function application binders – provides a function called _.partial which allows you to bind specific arguments, leaving a closure. Also contains a _.wrapAsync, which turns non-async calls into the format node.js expects. _.partialAsync does both.
  • Simon Kågedal Reimer (skagedal): underscore-pickrandom.js — provides a function, _.pickRandom(array, [n]) that picks random elements from an array. It works similar to _.first(_.shuffle(array, n)), but is more efficient; it operates in time proportional to n rather than the length of the whole array.
  • Tony Stuck (toekneestuck): underscore.nl2br – A simple port of nl2br to Underscore for string manipulation.

Discussion

This page is a bit hard to browse and quickly find what you need. I’d suggest: 1. group mixins by functionality; 2. emphasize what functions each mixin provides with a short description for each function (more like the underscore main page); 3. put less emphasis on the author. /skagedal (sorry if this isn’t the proper way of discussing wiki pages)

Clone this wiki locally