Split _.fromPairs into _.zipObject.

This commit is contained in:
John-David Dalton
2015-11-21 00:02:08 -08:00
parent bce8f498b4
commit 7745c91c69
3 changed files with 89 additions and 53 deletions

View File

@@ -5989,26 +5989,6 @@
var object = { 'barney': 36, 'fred': 40 },
array = [['barney', 36], ['fred', 40]];
QUnit.test('should skip falsey elements in a given two dimensional array', function(assert) {
assert.expect(1);
var actual = _.fromPairs(array.concat(falsey));
assert.deepEqual(actual, object);
});
QUnit.test('should zip together key/value arrays into an object', function(assert) {
assert.expect(1);
var actual = _.fromPairs(['barney', 'fred'], [36, 40]);
assert.deepEqual(actual, object);
});
QUnit.test('should ignore extra `values`', function(assert) {
assert.expect(1);
assert.deepEqual(_.fromPairs(['a'], [1, 2]), { 'a': 1 });
});
QUnit.test('should accept a two dimensional array', function(assert) {
assert.expect(1);
@@ -6016,13 +5996,6 @@
assert.deepEqual(actual, object);
});
QUnit.test('should not assume `keys` is two dimensional if `values` is not provided', function(assert) {
assert.expect(1);
var actual = _.fromPairs(['barney', 'fred']);
assert.deepEqual(actual, { 'barney': undefined, 'fred': undefined });
});
QUnit.test('should accept a falsey `array` argument', function(assert) {
assert.expect(1);
@@ -6040,7 +6013,7 @@
QUnit.test('should support deep paths', function(assert) {
assert.expect(1);
var actual = _.fromPairs(['a.b.c'], [1]);
var actual = _.fromPairs([['a.b.c', 1]]);
assert.deepEqual(actual, { 'a': { 'b': { 'c': 1 } } });
});
@@ -21437,6 +21410,50 @@
/*--------------------------------------------------------------------------*/
QUnit.module('lodash.zipObject');
(function() {
var object = { 'barney': 36, 'fred': 40 },
array = [['barney', 36], ['fred', 40]];
QUnit.test('should zip together key/value arrays into an object', function(assert) {
assert.expect(1);
var actual = _.zipObject(['barney', 'fred'], [36, 40]);
assert.deepEqual(actual, object);
});
QUnit.test('should ignore extra `values`', function(assert) {
assert.expect(1);
assert.deepEqual(_.zipObject(['a'], [1, 2]), { 'a': 1 });
});
QUnit.test('should support deep paths', function(assert) {
assert.expect(1);
var actual = _.zipObject(['a.b.c'], [1]);
assert.deepEqual(actual, { 'a': { 'b': { 'c': 1 } } });
});
QUnit.test('should work in a lazy chain sequence', function(assert) {
assert.expect(1);
if (!isNpm) {
var values = lodashStable.range(LARGE_ARRAY_SIZE),
props = lodashStable.map(values, function(value) { return 'key' + value; }),
actual = _(props).zipObject(values).map(square).filter(isEven).take().value();
assert.deepEqual(actual, _.take(_.filter(_.map(_.zipObject(props, values), square), isEven)));
}
else {
skipTest(assert);
}
});
}());
/*--------------------------------------------------------------------------*/
QUnit.module('lodash.zipWith');
(function() {
@@ -22562,7 +22579,7 @@
var acceptFalsey = lodashStable.difference(allMethods, rejectFalsey);
QUnit.test('should accept falsey arguments', function(assert) {
assert.expect(280);
assert.expect(281);
var emptyArrays = lodashStable.map(falsey, lodashStable.constant([]));

View File

@@ -73,6 +73,10 @@
'should treat non-number `fromIndex` values as `array.length`',
'[0,-1,-1]'
],
'object': [
'an array of pairs zipped together into an object',
'an object converted to pairs and back to an object'
],
'rest': [
'working rest(0)',
'rest can take an index',
@@ -343,13 +347,12 @@
'mapObject': 'mapValues',
'matcher': 'matches',
'methods': 'functions',
'object': 'fromPairs',
'object': 'zipObject',
'pairs': 'toPairs',
'pluck': 'map',
'restParam': 'restArgs',
'select': 'filter',
'where': 'filter',
'zipObject': 'fromPairs'
'where': 'filter'
};
var keyMap = {