From f7c4410f41030ca9eab4794573f8f5a67662c211 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sun, 24 Apr 2016 23:56:00 -0700 Subject: [PATCH] Add support for converting maps to `_.toPairs` and `_.toPairsIn`. --- lodash.js | 26 +++++++++++++++++--------- test/test.js | 34 ++++++++++++++++++++++++---------- 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/lodash.js b/lodash.js index 4d77d51ab..f71afa301 100644 --- a/lodash.js +++ b/lodash.js @@ -2674,9 +2674,7 @@ */ function baseGetAllKeys(object, keysFunc, symbolsFunc) { var result = keysFunc(object); - return isArray(object) - ? result - : arrayPush(result, symbolsFunc(object)); + return isArray(object) ? result : arrayPush(result, symbolsFunc(object)); } /** @@ -4879,6 +4877,20 @@ return new Set(values); }; + /** + * Creates a `_.toPairs` or `_.toPairsIn` function. + * + * @private + * @param {Function} keysFunc The function to get the keys of a given object. + * @returns {Function} Returns the new pairs function. + */ + function createToPairs(keysFunc) { + return function(object) { + var tag = getTag(object); + return tag == mapTag ? mapToArray(object) : baseToPairs(object, keysFunc(object)); + }; + } + /** * Creates a function that either curries or invokes `func` with optional * `this` binding and partially applied arguments. @@ -12893,9 +12905,7 @@ * _.toPairs(new Foo); * // => [['a', 1], ['b', 2]] (iteration order is not guaranteed) */ - function toPairs(object) { - return baseToPairs(object, keys(object)); - } + var toPairs = createToPairs(keys); /** * Creates an array of own and inherited enumerable string keyed-value pairs @@ -12920,9 +12930,7 @@ * _.toPairsIn(new Foo); * // => [['a', 1], ['b', 2], ['c', 1]] (iteration order is not guaranteed) */ - function toPairsIn(object) { - return baseToPairs(object, keysIn(object)); - } + var toPairsIn = createToPairs(keysIn); /** * An alternative to `_.reduce`; this method transforms `object` to a new diff --git a/test/test.js b/test/test.js index 0ecdcd572..af1b71ff3 100644 --- a/test/test.js +++ b/test/test.js @@ -23256,15 +23256,6 @@ assert.deepEqual(actual, [['a', 1], ['b', 2]]); }); - QUnit.test('`_.' + methodName + '` should work with an object that has a `length` property', function(assert) { - assert.expect(1); - - var object = { '0': 'a', '1': 'b', 'length': 2 }, - actual = lodashStable.sortBy(func(object), 0); - - assert.deepEqual(actual, [['0', 'a'], ['1', 'b'], ['length', 2]]); - }); - QUnit.test('`_.' + methodName + '` should ' + (isToPairs ? 'not ' : '') + 'include inherited string keyed property values', function(assert) { assert.expect(1); @@ -23279,7 +23270,30 @@ assert.deepEqual(actual, expected); }); - QUnit.test('`_.' + methodName + '` should work with strings', function(assert) { + QUnit.test('`_.' + methodName + '` should convert objects with a `length` property', function(assert) { + assert.expect(1); + + var object = { '0': 'a', '1': 'b', 'length': 2 }, + actual = lodashStable.sortBy(func(object), 0); + + assert.deepEqual(actual, [['0', 'a'], ['1', 'b'], ['length', 2]]); + }); + + QUnit.test('`_.' + methodName + '` should convert maps', function(assert) { + assert.expect(1); + + if (Map) { + var map = new Map; + map.set('a', 1); + map.set('b', 2); + assert.deepEqual(func(map), [['a', 1], ['b', 2]]); + } + else { + skipAssert(assert); + } + }); + + QUnit.test('`_.' + methodName + '` should convert strings', function(assert) { assert.expect(2); lodashStable.each(['xo', Object('xo')], function(string) {