Add _.functionsIn and _.pairsIn.

This commit is contained in:
John-David Dalton
2015-10-27 00:45:32 -07:00
parent 6b2645b310
commit 557af10067
3 changed files with 95 additions and 15 deletions

101
lodash.js
View File

@@ -712,7 +712,22 @@
} }
/** /**
* The base implementation of `_.reduce` and `_.reduceRight` without support * The base implementation of `_.pairs` and `_.pairsIn` which creates an array
* of key-value pairs for `object` corresponding to the property names of `props`.
*
* @private
* @param {Object} object The object to query.
* @param {Array} props The property names to get values for.
* @returns {Object} Returns the new array of key-value pairs.
*/
function basePairs(object, props) {
return arrayMap(props, function(key) {
return [key, object[key]];
});
}
/**
* The base implementation of `_.reduce` and `_.reduceRight`, without support
* for callback shorthands, which iterates over `collection` using the provided * for callback shorthands, which iterates over `collection` using the provided
* `eachFunc`. * `eachFunc`.
* *
@@ -10174,8 +10189,8 @@
} }
/** /**
* Creates an array of function property names from all enumerable properties, * Creates an array of function property names from own enumerable properties
* own and inherited, of `object`. * of `object`.
* *
* @static * @static
* @memberOf _ * @memberOf _
@@ -10184,10 +10199,42 @@
* @returns {Array} Returns the new array of property names. * @returns {Array} Returns the new array of property names.
* @example * @example
* *
* _.functions(_); * function Foo() {
* // => ['after', 'ary', 'assign', ...] * this.a = _.constant('a');
* this.b = _.constant('b');
* }
*
* Foo.prototype.c = _.constant('c');
*
* _.functions(new Foo);
* // => ['a', 'b']
*/ */
function functions(object) { function functions(object) {
return object == null ? [] : baseFunctions(object, keys(object));
}
/**
* Creates an array of function property names from own and inherited
* enumerable properties of `object`.
*
* @static
* @memberOf _
* @category Object
* @param {Object} object The object to inspect.
* @returns {Array} Returns the new array of property names.
* @example
*
* function Foo() {
* this.a = _.constant('a');
* this.b = _.constant('b');
* }
*
* Foo.prototype.c = _.constant('c');
*
* _.functionsIn(new Foo);
* // => ['a', 'b', 'c']
*/
function functionsIn(object) {
return object == null ? [] : baseFunctions(object, keysIn(object)); return object == null ? [] : baseFunctions(object, keysIn(object));
} }
@@ -10593,8 +10640,7 @@
} }
/** /**
* Creates an array of the key-value pairs for `object`, * Creates an array of own enumerable key-value pairs for `object`.
* e.g. `[[key1, value1], [key2, value2]]`.
* *
* @static * @static
* @memberOf _ * @memberOf _
@@ -10603,13 +10649,42 @@
* @returns {Array} Returns the new array of key-value pairs. * @returns {Array} Returns the new array of key-value pairs.
* @example * @example
* *
* _.pairs({ 'barney': 36, 'fred': 40 }); * function Foo() {
* // => [['barney', 36], ['fred', 40]] (iteration order is not guaranteed) * this.a = 1;
* this.b = 2;
* }
*
* Foo.prototype.c = 3;
*
* _.pairs(new Foo);
* // => [['a', 1], ['b', 2]] (iteration order is not guaranteed)
*/ */
function pairs(object) { function pairs(object) {
return arrayMap(keys(object), function(key) { return basePairs(object, keys(object));
return [key, object[key]]; }
});
/**
* Creates an array of own and inherited enumerable key-value pairs for `object`.
*
* @static
* @memberOf _
* @category Object
* @param {Object} object The object to query.
* @returns {Array} Returns the new array of key-value pairs.
* @example
*
* function Foo() {
* this.a = 1;
* this.b = 2;
* }
*
* Foo.prototype.c = 3;
*
* _.pairsIn(new Foo);
* // => [['a', 1], ['b', 2], ['c', 1]] (iteration order is not guaranteed)
*/
function pairsIn(object) {
return basePairs(object, keysIn(object));
} }
/** /**
@@ -13050,6 +13125,7 @@
lodash.flow = flow; lodash.flow = flow;
lodash.flowRight = flowRight; lodash.flowRight = flowRight;
lodash.functions = functions; lodash.functions = functions;
lodash.functionsIn = functionsIn;
lodash.groupBy = groupBy; lodash.groupBy = groupBy;
lodash.initial = initial; lodash.initial = initial;
lodash.intersection = intersection; lodash.intersection = intersection;
@@ -13080,6 +13156,7 @@
lodash.omitBy = omitBy; lodash.omitBy = omitBy;
lodash.once = once; lodash.once = once;
lodash.pairs = pairs; lodash.pairs = pairs;
lodash.pairsIn = pairsIn;
lodash.partial = partial; lodash.partial = partial;
lodash.partialRight = partialRight; lodash.partialRight = partialRight;
lodash.partition = partition; lodash.partition = partition;

View File

@@ -6041,7 +6041,7 @@
assert.deepEqual(_.functions(object).sort(), ['b', 'd']); assert.deepEqual(_.functions(object).sort(), ['b', 'd']);
}); });
QUnit.test('should include inherited functions', function(assert) { QUnit.test('should not include inherited functions', function(assert) {
assert.expect(1); assert.expect(1);
function Foo() { function Foo() {
@@ -6049,7 +6049,7 @@
this.b = 'b'; this.b = 'b';
} }
Foo.prototype.c = noop; Foo.prototype.c = noop;
assert.deepEqual(_.functions(new Foo).sort(), ['a', 'c']); assert.deepEqual(_.functions(new Foo).sort(), ['a']);
}); });
}()); }());
@@ -21997,7 +21997,7 @@
var acceptFalsey = lodashStable.difference(allMethods, rejectFalsey); var acceptFalsey = lodashStable.difference(allMethods, rejectFalsey);
QUnit.test('should accept falsey arguments', function(assert) { QUnit.test('should accept falsey arguments', function(assert) {
assert.expect(263); assert.expect(265);
var emptyArrays = lodashStable.map(falsey, lodashStable.constant([])); var emptyArrays = lodashStable.map(falsey, lodashStable.constant([]));

View File

@@ -277,6 +277,9 @@
'assigning non-objects results in returning the non-object value', 'assigning non-objects results in returning the non-object value',
'assigning undefined results in undefined' 'assigning undefined results in undefined'
], ],
'functions': [
'also looks up functions on the prototype'
],
'isEqual': [ 'isEqual': [
'`0` is not equal to `-0`', '`0` is not equal to `-0`',
'Commutative equality is implemented for `0` and `-0`', 'Commutative equality is implemented for `0` and `-0`',