mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-07 18:07:49 +00:00
Move _.createCallback and _.property to the "Utilities" category.
This commit is contained in:
192
lodash.js
192
lodash.js
@@ -5539,72 +5539,6 @@
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Produces a callback bound to an optional `thisArg`. If `func` is a property
|
||||
* name the created callback will return the property value for a given element.
|
||||
* If `func` is an object the created callback will return `true` for elements
|
||||
* that contain the equivalent object properties, otherwise it will return `false`.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Functions
|
||||
* @param {*} [func=identity] The value to convert to a callback.
|
||||
* @param {*} [thisArg] The `this` binding of the created callback.
|
||||
* @param {number} [argCount] The number of arguments the callback accepts.
|
||||
* @returns {Function} Returns a callback function.
|
||||
* @example
|
||||
*
|
||||
* var characters = [
|
||||
* { 'name': 'barney', 'age': 36 },
|
||||
* { 'name': 'fred', 'age': 40 }
|
||||
* ];
|
||||
*
|
||||
* // wrap to create custom callback shorthands
|
||||
* _.createCallback = _.wrap(_.createCallback, function(func, callback, thisArg) {
|
||||
* var match = /^(.+?)__([gl]t)(.+)$/.exec(callback);
|
||||
* return !match ? func(callback, thisArg) : function(object) {
|
||||
* return match[2] == 'gt' ? object[match[1]] > match[3] : object[match[1]] < match[3];
|
||||
* };
|
||||
* });
|
||||
*
|
||||
* _.filter(characters, 'age__gt38');
|
||||
* // => [{ 'name': 'fred', 'age': 40 }]
|
||||
*/
|
||||
function createCallback(func, thisArg, argCount) {
|
||||
var type = typeof func;
|
||||
if (func == null || type == 'function') {
|
||||
return baseCreateCallback(func, thisArg, argCount);
|
||||
}
|
||||
// handle "_.pluck" style callback shorthands
|
||||
if (type != 'object') {
|
||||
return property(func);
|
||||
}
|
||||
var props = keys(func),
|
||||
key = props[0],
|
||||
a = func[key];
|
||||
|
||||
// handle "_.where" style callback shorthands
|
||||
if (props.length == 1 && a === a && !isObject(a)) {
|
||||
// fast path the common case of providing an object with a single
|
||||
// property containing a primitive value
|
||||
return function(object) {
|
||||
var b = object[key];
|
||||
return a === b && (a !== 0 || (1 / a == 1 / b));
|
||||
};
|
||||
}
|
||||
return function(object) {
|
||||
var length = props.length,
|
||||
result = false;
|
||||
|
||||
while (length--) {
|
||||
if (!(result = baseIsEqual(object[props[length]], func[props[length]], null, true))) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a function which accepts one or more arguments of `func` that when
|
||||
* invoked either executes `func` returning its result, if all `func` arguments
|
||||
@@ -5977,36 +5911,6 @@
|
||||
return createWrapper(func, 32, null, slice(arguments, 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a "_.pluck" style function, which returns the `prop` value of a
|
||||
* given object.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Functions
|
||||
* @param {string} prop The name of the property to retrieve.
|
||||
* @returns {*} Returns the new function.
|
||||
* @example
|
||||
*
|
||||
* var characters = [
|
||||
* { 'name': 'fred', 'age': 40 },
|
||||
* { 'name': 'barney', 'age': 36 }
|
||||
* ];
|
||||
*
|
||||
* var getName = _.property('name');
|
||||
*
|
||||
* _.map(characters, getName);
|
||||
* // => ['barney', 'fred']
|
||||
*
|
||||
* _.sortBy(characters, getName);
|
||||
* // => [{ 'name': 'barney', 'age': 36 }, { 'name': 'fred', 'age': 40 }]
|
||||
*/
|
||||
function property(prop) {
|
||||
return function(object) {
|
||||
return object[prop];
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a function that, when executed, will only call the `func` function
|
||||
* at most once per every `wait` milliseconds. Provide an options object to
|
||||
@@ -6085,6 +5989,72 @@
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
* Produces a callback bound to an optional `thisArg`. If `func` is a property
|
||||
* name the created callback will return the property value for a given element.
|
||||
* If `func` is an object the created callback will return `true` for elements
|
||||
* that contain the equivalent object properties, otherwise it will return `false`.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Utilities
|
||||
* @param {*} [func=identity] The value to convert to a callback.
|
||||
* @param {*} [thisArg] The `this` binding of the created callback.
|
||||
* @param {number} [argCount] The number of arguments the callback accepts.
|
||||
* @returns {Function} Returns a callback function.
|
||||
* @example
|
||||
*
|
||||
* var characters = [
|
||||
* { 'name': 'barney', 'age': 36 },
|
||||
* { 'name': 'fred', 'age': 40 }
|
||||
* ];
|
||||
*
|
||||
* // wrap to create custom callback shorthands
|
||||
* _.createCallback = _.wrap(_.createCallback, function(func, callback, thisArg) {
|
||||
* var match = /^(.+?)__([gl]t)(.+)$/.exec(callback);
|
||||
* return !match ? func(callback, thisArg) : function(object) {
|
||||
* return match[2] == 'gt' ? object[match[1]] > match[3] : object[match[1]] < match[3];
|
||||
* };
|
||||
* });
|
||||
*
|
||||
* _.filter(characters, 'age__gt38');
|
||||
* // => [{ 'name': 'fred', 'age': 40 }]
|
||||
*/
|
||||
function createCallback(func, thisArg, argCount) {
|
||||
var type = typeof func;
|
||||
if (func == null || type == 'function') {
|
||||
return baseCreateCallback(func, thisArg, argCount);
|
||||
}
|
||||
// handle "_.pluck" style callback shorthands
|
||||
if (type != 'object') {
|
||||
return property(func);
|
||||
}
|
||||
var props = keys(func),
|
||||
key = props[0],
|
||||
a = func[key];
|
||||
|
||||
// handle "_.where" style callback shorthands
|
||||
if (props.length == 1 && a === a && !isObject(a)) {
|
||||
// fast path the common case of providing an object with a single
|
||||
// property containing a primitive value
|
||||
return function(object) {
|
||||
var b = object[key];
|
||||
return a === b && (a !== 0 || (1 / a == 1 / b));
|
||||
};
|
||||
}
|
||||
return function(object) {
|
||||
var length = props.length,
|
||||
result = false;
|
||||
|
||||
while (length--) {
|
||||
if (!(result = baseIsEqual(object[props[length]], func[props[length]], null, true))) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the characters `&`, `<`, `>`, `"`, and `'` in `string` to their
|
||||
* corresponding HTML entities.
|
||||
@@ -6247,6 +6217,36 @@
|
||||
return nativeParseInt(isString(value) ? value.replace(reLeadingSpacesAndZeros, '') : value, radix || 0);
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a "_.pluck" style function, which returns the `prop` value of a
|
||||
* given object.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Utilities
|
||||
* @param {string} prop The name of the property to retrieve.
|
||||
* @returns {*} Returns the new function.
|
||||
* @example
|
||||
*
|
||||
* var characters = [
|
||||
* { 'name': 'fred', 'age': 40 },
|
||||
* { 'name': 'barney', 'age': 36 }
|
||||
* ];
|
||||
*
|
||||
* var getName = _.property('name');
|
||||
*
|
||||
* _.map(characters, getName);
|
||||
* // => ['barney', 'fred']
|
||||
*
|
||||
* _.sortBy(characters, getName);
|
||||
* // => [{ 'name': 'barney', 'age': 36 }, { 'name': 'fred', 'age': 40 }]
|
||||
*/
|
||||
function property(prop) {
|
||||
return function(object) {
|
||||
return object[prop];
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Produces a random number between `min` and `max` (inclusive). If only one
|
||||
* argument is provided a number between `0` and the given number will be
|
||||
|
||||
Reference in New Issue
Block a user