Expanded _.createCallback and _.after docs and ensure _.times calls lodash.createCallback.

Former-commit-id: 2d6a480a5ee449295ec40acd5e8bbef6fe955473
This commit is contained in:
John-David Dalton
2013-03-09 12:08:49 -08:00
parent afde4804c9
commit 63a6aac2d8
8 changed files with 194 additions and 120 deletions

View File

@@ -3264,9 +3264,11 @@
/*--------------------------------------------------------------------------*/
/**
* Creates a function that is restricted to executing `func` only after it is
* called `n` times. The `func` is executed with the `this` binding of the
* created function.
* If `n` is greater than `0`, a function is created that is restricted to
* executing `func`, with the `this` binding and arguments of the created
* function, only after it is called `n` times. If `n` is less than `1`,
* `func` is executed immediately, without a `this` binding or additional
* arguments, and its result is returned.
*
* @static
* @memberOf _
@@ -3421,6 +3423,19 @@
*
* _.filter(stooges, 'age__gt45');
* // => [{ 'name': 'larry', 'age': 50 }]
*
* // create mixins with support for "_.pluck" and "_.where" callback shorthands
* _.mixin({
* 'toLookup': function(collection, callback, thisArg) {
* callback = _.createCallback(callback, thisArg);
* return _.reduce(collection, function(result, value, index, collection) {
* return (result[callback(value, index, collection)] = value, result);
* }, {});
* }
* });
*
* _.toLookup(stooges, 'name');
* // => { 'moe': { 'name': 'moe', 'age': 40 }, 'larry': { 'name': 'larry', 'age': 50 } }
*/
function createCallback(func, thisArg, argCount) {
if (func == null) {