allthethings
Advanced tools
Comparing version 0.0.1-alpha-3 to 0.0.1-alpha-4
@@ -1,2 +0,2 @@ | ||
/*! allthethings v0.0.1-alpha-3 | ||
/*! allthethings v0.0.1-alpha-4 | ||
* https://github.com/markdalgleish/allthethings.js | ||
@@ -9,3 +9,4 @@ * Copyright (c) 2012 Mark Dalgleish; Licensed MIT */ | ||
reduce: /^(reduce|add|calculate)/, | ||
filter: /^(filter|is)/ | ||
filter: /^(filter|is)/, | ||
some: /^(some|contains)/ | ||
}, | ||
@@ -20,7 +21,11 @@ | ||
); | ||
}, | ||
allthethings = function(things) { | ||
return resolveFunc(this, things).call(things, this); | ||
}; | ||
Function.prototype.allThe = Function.prototype.fromThe = function(things) { | ||
return resolveFunc(this, things).call(things, this); | ||
}; | ||
['allThe', 'fromThe', 'inThe'].forEach(function(alias) { | ||
Function.prototype[alias] = allthethings; | ||
}); | ||
@@ -27,0 +32,0 @@ exports.rules = rules; |
@@ -1,2 +0,2 @@ | ||
/*! allthethings v0.0.1-alpha-3 | (c) 2012 Mark Dalgleish | https://github.com/markdalgleish/allthethings.js | Licensed MIT */ | ||
(function(e){var t={reduce:/^(reduce|add|calculate)/,filter:/^(filter|is)/},n=function(e,n){var r="_allthethings_func";return e[r]||(e[r]=n[Object.keys(t).reduce(function(n,r){return t[r].test(e.name)?r:n},"map")])};Function.prototype.allThe=Function.prototype.fromThe=function(e){return n(this,e).call(e,this)},e.rules=t})(typeof exports=="object"&&exports||(this.allthethings={})); | ||
/*! allthethings v0.0.1-alpha-4 | (c) 2012 Mark Dalgleish | https://github.com/markdalgleish/allthethings.js | Licensed MIT */ | ||
(function(e){var t={reduce:/^(reduce|add|calculate)/,filter:/^(filter|is)/,some:/^(some|contains)/},n=function(e,n){var r="_allthethings_func";return e[r]||(e[r]=n[Object.keys(t).reduce(function(n,r){return t[r].test(e.name)?r:n},"map")])},r=function(e){return n(this,e).call(e,this)};["allThe","fromThe","inThe"].forEach(function(e){Function.prototype[e]=r}),e.rules=t})(typeof exports=="object"&&exports||(this.allthethings={})); |
@@ -5,3 +5,4 @@ (function(exports) { | ||
reduce: /^(reduce|add|calculate)/, | ||
filter: /^(filter|is)/ | ||
filter: /^(filter|is)/, | ||
some: /^(some|contains)/ | ||
}, | ||
@@ -16,7 +17,11 @@ | ||
); | ||
}, | ||
allthethings = function(things) { | ||
return resolveFunc(this, things).call(things, this); | ||
}; | ||
Function.prototype.allThe = Function.prototype.fromThe = function(things) { | ||
return resolveFunc(this, things).call(things, this); | ||
}; | ||
['allThe', 'fromThe', 'inThe'].forEach(function(alias) { | ||
Function.prototype[alias] = allthethings; | ||
}); | ||
@@ -23,0 +28,0 @@ exports.rules = rules; |
{ | ||
"name": "allthethings", | ||
"description": "Let your iterations read like actual sentences.", | ||
"version": "0.0.1-alpha-3", | ||
"version": "0.0.1-alpha-4", | ||
"homepage": "https://github.com/markdalgleish/allthethings.js", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -10,12 +10,12 @@ [![Build Status](https://secure.travis-ci.org/markdalgleish/allthethings.js.png)](http://travis-ci.org/markdalgleish/allthethings.js) | ||
``` js | ||
doSomethingTo.allThe(things); | ||
``` | ||
var things = ['all', 'the', 'things']; | ||
Instead of coding like Yoda... | ||
function log(thing) { | ||
console.log(thing); | ||
} | ||
``` js | ||
things.forEach(doSomething); | ||
log.allThe(things); | ||
``` | ||
For no extra cost, you also get 'fromThe': | ||
For no extra cost, you also get the 'fromThe' and 'inThe' aliases, for your grammatical needs: | ||
@@ -26,2 +26,6 @@ ``` js | ||
``` js | ||
containsProHackers.inThe(hackers); // true | ||
``` | ||
And all for less than 1KB, with no dependencies*. Delicious. | ||
@@ -140,2 +144,75 @@ | ||
## some | ||
If your named function starts with 'some' or 'contains', then a 'some' is performed. | ||
When using 'some', it's idiomatic to use 'inThe' instead of 'allThe': | ||
```js | ||
var numbers = [-2,-1,0,1,2]; | ||
function someNegatives(number) { | ||
return number < 0; | ||
} | ||
someNegatives.inThe(numbers); // true | ||
``` | ||
```js | ||
var numbers = [-2,-1,0,1,2]; | ||
function containsNegatives(number) { | ||
return number < 0; | ||
} | ||
containsNegatives.inThe(numbers); // true | ||
``` | ||
## Elite hacker tips | ||
### Aliased functions | ||
Assign a named function expression to a variable of a different name: | ||
``` js | ||
var things = [false, true, false]; | ||
var myAliasedFunction = function filter(thing) { | ||
return thing === true; | ||
}; | ||
myAliasedFunction.allThe(things); // [true] | ||
``` | ||
### Custom function name rules | ||
You can create new rules or override existing ones: | ||
#### In Node.js | ||
``` js | ||
var allthethings = require('allthethings'); | ||
allthethings.rules.filter = /foobar/; | ||
``` | ||
#### In the browser | ||
``` js | ||
window.allthethings.rules.filter = /foobar/; | ||
``` | ||
#### Which allows: | ||
``` js | ||
var numbers = [1,2,3,4]; | ||
function foobar(number) { | ||
return number % 2 === 0; | ||
} | ||
// This now performs a filter: | ||
foobar.allThe(numbers); // [2,4] | ||
``` | ||
## Contributing | ||
@@ -142,0 +219,0 @@ In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [grunt](http://gruntjs.com/). |
/*global require:true */ | ||
require('../lib/allthethings.js'); | ||
var allthethings = require('../lib/allthethings.js'); | ||
@@ -65,3 +65,37 @@ exports['ALL THE THINGS'] = { | ||
test.done(); | ||
}, | ||
'some': function(test) { | ||
var numbers = [-1,0,1], | ||
positiveNumbers = [1,2,3]; | ||
function someNegatives(number) { | ||
return number < 0; | ||
} | ||
function containsNegatives(number) { | ||
return number < 0; | ||
} | ||
test.strictEqual(someNegatives.inThe(numbers), true); | ||
test.strictEqual(someNegatives.inThe(positiveNumbers), false); | ||
test.strictEqual(containsNegatives.inThe(numbers), true); | ||
test.strictEqual(containsNegatives.inThe(positiveNumbers), false); | ||
test.done(); | ||
}, | ||
'custom rules': function(test) { | ||
allthethings.rules.filter = /foobar/; | ||
var numbers = [1,2,3,4]; | ||
function foobar(number) { | ||
return number % 2 === 0; | ||
} | ||
test.deepEqual(foobar.fromThe(numbers), [2,4]); | ||
test.done(); | ||
} | ||
}; |
108901
181
221