Skip to content

Instantly share code, notes, and snippets.

@chrisdickinson
Forked from dshaw/array.goodies.js
Created July 21, 2011 21:45
Show Gist options
  • Save chrisdickinson/1098296 to your computer and use it in GitHub Desktop.
Save chrisdickinson/1098296 to your computer and use it in GitHub Desktop.
Array goodies from twitter rap with David Herman, see wrap up here: http://twitter.com/#!/littlecalculist/status/89855977838485504
// rbind provides "right binding" to Function.prototype --
// allows for currying arguments *after* incoming args.
var rbind = function(to) {
var args = [].slice.call(arguments),
fn = this,
_to = to;
_to === undefined && (_to = {});
return function() {
return fn.apply(to, [].slice.call(arguments).concat(args));
};
};
Function.prototype.rbind = rbind;
// method allows for the common case where you're iterating over a list
// of items and want to call a method on each item.
Function.method = function(name) {
return function(obj) {
var fn = obj[name];
return fn.apply(obj, [].slice.call(arguments, 1));
};
};
// mask allows for fine-grained control over incoming arguments --
// e.g., [].forEach(parseInt) sends the `idx` param to `parseInt`, but
// you'd like to mask that param with '10', so each array item gets parsed
// as base 10.
var mask = function(to, mask) {
var fn = this,
_mask,
_to;
_mask = mask === undefined ? to : mask;
_to = mask === undefined ? {} : to;
return function() {
var args = [].slice.call(arguments);
for(var key in _mask) if (!isNaN(key)) {
args[key] = _mask[key];
}
return fn.apply(_to, args);
};
};
Function.prototype.mask = mask;
var hex = parseInt.rbind(16);
console.log(
'hex example:',
hex('ff')
);
console.log(
'mask example:',
['0f','ff','ca'].map(parseInt.mask({1:16}))
);
console.log(
'method example',
['0f','ff','ca'].map(parseInt.mask({1:16})).map(Function.method('toString').mask({1:'2'}))
);
// http://twitter.com/#!/littlecalculist/status/89848378682392576
// http://twitter.com/#!/littlecalculist/status/89855977838485504
// Unary Array.from()
Array.from = function( arrayish ) {
return [].slice.call( arrayish );
};
var divs = document.querySelectorAll("div");
console.log(
Array.from( divs )
);
// see console
Array.from( divs ).forEach(function( node ) {
console.log( node );
});
var filtered = Array.from( divs ).filter(function( node ) {
return !!node.classList.length;
});
console.log( "filtered", filtered );
// filtered [<div class="some classes" data-info="12"></div>]
var reduced = Array.from( divs ).reduce(function( prev, current ) {
return ( +prev.dataset.info ) + ( +current.dataset.info );
});
console.log( "reduced", reduced );
// reduced 22
console.log(
Array.from( divs[0].classList )
);
var a = Array;
console.log(
// Now shorter then [].foo.call :)
a.from( divs )
);
// Variable Arity Array.of()
Array.of = function() {
return [].slice.call( arguments );
};
console.log(
Array.of( "things", "that", "aren't", "currently", "an", "array" )
);
// ["things", "that", "aren't", "currently", "an", "array"]
console.log(
Array.of.apply( null, [ "foo", "bar", "baz" ] )
);
// [ "foo", "bar", "baz" ]
<div class="some classes" data-info="12"></div>
<div data-info="10"></div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment