Expose _.property.

This commit is contained in:
John-David Dalton
2013-11-16 20:31:18 -08:00
parent 96819628d6
commit fe2ee64ec9
9 changed files with 392 additions and 241 deletions

View File

@@ -1209,22 +1209,22 @@
}
/**
* Checks if the specified object `property` exists and is a direct property,
* Checks if the specified property name exists as a direct property of `object`,
* instead of an inherited property.
*
* @static
* @memberOf _
* @category Objects
* @param {Object} object The object to check.
* @param {string} property The property to check for.
* @param {Object} object The object to inspect.
* @param {string} prop The name of the property to check.
* @returns {boolean} Returns `true` if key is a direct property, else `false`.
* @example
*
* _.has({ 'a': 1, 'b': 2, 'c': 3 }, 'b');
* // => true
*/
function has(object, property) {
return object ? hasOwnProperty.call(object, property) : false;
function has(object, prop) {
return object ? hasOwnProperty.call(object, prop) : false;
}
/**
@@ -2435,7 +2435,7 @@
* @type Function
* @category Collections
* @param {Array|Object|string} collection The collection to iterate over.
* @param {string} property The property to pluck.
* @param {string} property The name of the property to pluck.
* @returns {Array} Returns a new array of property values.
* @example
*
@@ -2447,18 +2447,7 @@
* _.pluck(characters, 'name');
* // => ['barney', 'fred']
*/
function pluck(collection, property) {
var index = -1,
length = collection ? collection.length : 0;
if (typeof length == 'number') {
var result = Array(length);
while (++index < length) {
result[index] = collection[index][property];
}
}
return result || map(collection, property);
}
var pluck = map;
/**
* Reduces a collection to a value which is the accumulated result of running
@@ -2826,7 +2815,7 @@
* @type Function
* @category Collections
* @param {Array|Object|string} collection The collection to iterate over.
* @param {Object} properties The object of property values to filter by.
* @param {Object} props The object of property values to filter by.
* @returns {Array} Returns a new array of elements that have the given properties.
* @example
*
@@ -3792,9 +3781,7 @@
}
// handle "_.pluck" style callback shorthands
if (type != 'object') {
return function(object) {
return object[func];
};
return property(func);
}
var props = keys(func);
return function(object) {
@@ -4104,6 +4091,36 @@
return createWrapper(func, 16, 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
@@ -4333,7 +4350,7 @@
}
/**
* Resolves the value of `property` on `object`. If `property` is a function
* Resolves the value of `prop` on `object`. If `prop` is a function
* it will be invoked with the `this` binding of `object` and its result returned,
* else the property value is returned. If `object` is falsey then `undefined`
* is returned.
@@ -4342,7 +4359,7 @@
* @memberOf _
* @category Utilities
* @param {Object} object The object to inspect.
* @param {string} property The property to get the value of.
* @param {string} prop The name of the property to resolve.
* @returns {*} Returns the resolved value.
* @example
*
@@ -4359,10 +4376,10 @@
* _.result(object, 'stuff');
* // => 'nonsense'
*/
function result(object, property) {
function result(object, prop) {
if (object) {
var value = object[property];
return isFunction(value) ? object[property]() : value;
var value = object[prop];
return isFunction(value) ? object[prop]() : value;
}
}