mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 19:07:49 +00:00
Add _.transform tests.
This commit is contained in:
68
test/test.js
68
test/test.js
@@ -5184,7 +5184,7 @@
|
|||||||
|
|
||||||
QUnit.module('exit early');
|
QUnit.module('exit early');
|
||||||
|
|
||||||
_.each(['_baseEach', 'forEach', 'forEachRight', 'forIn', 'forInRight', 'forOwn', 'forOwnRight'], function(methodName) {
|
_.each(['_baseEach', 'forEach', 'forEachRight', 'forIn', 'forInRight', 'forOwn', 'forOwnRight', 'transform'], function(methodName) {
|
||||||
var func = _[methodName];
|
var func = _[methodName];
|
||||||
if (!func) {
|
if (!func) {
|
||||||
return;
|
return;
|
||||||
@@ -5193,7 +5193,11 @@
|
|||||||
var array = [1, 2, 3],
|
var array = [1, 2, 3],
|
||||||
values = [];
|
values = [];
|
||||||
|
|
||||||
func(array, function(value) { values.push(value); return false; });
|
func(array, function(value, other) {
|
||||||
|
values.push(_.isArray(value) ? other : value);
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
|
||||||
deepEqual(values, [_.endsWith(methodName, 'Right') ? 3 : 1]);
|
deepEqual(values, [_.endsWith(methodName, 'Right') ? 3 : 1]);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -5201,7 +5205,11 @@
|
|||||||
var object = { 'a': 1, 'b': 2, 'c': 3 },
|
var object = { 'a': 1, 'b': 2, 'c': 3 },
|
||||||
values = [];
|
values = [];
|
||||||
|
|
||||||
func(object, function(value) { values.push(value); return false; });
|
func(object, function(value, other) {
|
||||||
|
values.push(_.isArray(value) ? other : value);
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
|
||||||
strictEqual(values.length, 1);
|
strictEqual(values.length, 1);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -13021,13 +13029,13 @@
|
|||||||
QUnit.module('lodash.transform');
|
QUnit.module('lodash.transform');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
test('should create an object with the same `[[Prototype]]` as `object` when `accumulator` is `null` or `undefined`', 4, function() {
|
function Foo() {
|
||||||
function Foo() {
|
this.a = 1;
|
||||||
this.a = 1;
|
this.b = 2;
|
||||||
this.b = 2;
|
this.c = 3;
|
||||||
this.c = 3;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
test('should create an object with the same `[[Prototype]]` as `object` when `accumulator` is `null` or `undefined`', 4, function() {
|
||||||
var accumulators = [, null, undefined],
|
var accumulators = [, null, undefined],
|
||||||
expected = _.map(accumulators, _.constant(true)),
|
expected = _.map(accumulators, _.constant(true)),
|
||||||
object = new Foo;
|
object = new Foo;
|
||||||
@@ -13065,6 +13073,43 @@
|
|||||||
deepEqual(actual, expected);
|
deepEqual(actual, expected);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('should support an `accumulator` value', 4, function() {
|
||||||
|
var values = [new Foo, [1, 2, 3], { 'a': 1, 'b': 2, 'c': 3 }],
|
||||||
|
expected = _.map(values, _.constant([0, 1, 4, 9]));
|
||||||
|
|
||||||
|
var actual = _.map(values, function(value) {
|
||||||
|
return _.transform(value, function(result, value) {
|
||||||
|
result.push(value * value);
|
||||||
|
}, [0]);
|
||||||
|
});
|
||||||
|
|
||||||
|
deepEqual(actual, expected);
|
||||||
|
|
||||||
|
var object = { '_': 0, 'a': 1, 'b': 4, 'c': 9 };
|
||||||
|
expected = [object, { '_': 0, '0': 1, '1': 4, '2': 9 }, object];
|
||||||
|
actual = _.map(values, function(value) {
|
||||||
|
return _.transform(value, function(result, value, key) {
|
||||||
|
result[key] = value * value;
|
||||||
|
}, { '_': 0 });
|
||||||
|
});
|
||||||
|
|
||||||
|
deepEqual(actual, expected);
|
||||||
|
|
||||||
|
object = {};
|
||||||
|
expected = _.map(values, _.constant(object));
|
||||||
|
actual = _.map(values, function(value) {
|
||||||
|
return _.transform(value, _.noop, object);
|
||||||
|
});
|
||||||
|
|
||||||
|
deepEqual(actual, expected);
|
||||||
|
|
||||||
|
actual = _.map(values, function(value) {
|
||||||
|
return _.transform(null, null, object);
|
||||||
|
});
|
||||||
|
|
||||||
|
deepEqual(actual, expected);
|
||||||
|
});
|
||||||
|
|
||||||
test('should treat sparse arrays as dense', 1, function() {
|
test('should treat sparse arrays as dense', 1, function() {
|
||||||
var actual = _.transform(Array(1), function(result, value, index) {
|
var actual = _.transform(Array(1), function(result, value, index) {
|
||||||
result[index] = String(value);
|
result[index] = String(value);
|
||||||
@@ -13074,7 +13119,6 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('should work without an `iteratee` argument', 1, function() {
|
test('should work without an `iteratee` argument', 1, function() {
|
||||||
function Foo() {}
|
|
||||||
ok(_.transform(new Foo) instanceof Foo);
|
ok(_.transform(new Foo) instanceof Foo);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -13083,8 +13127,8 @@
|
|||||||
values = [true, false, 0, 1, NaN, '', 'a'],
|
values = [true, false, 0, 1, NaN, '', 'a'],
|
||||||
expected = _.map(values, _.constant({}));
|
expected = _.map(values, _.constant({}));
|
||||||
|
|
||||||
var results = _.map(values, function(value, index) {
|
var results = _.map(values, function(value) {
|
||||||
return index ? _.transform(value) : _.transform();
|
return _.transform(value);
|
||||||
});
|
});
|
||||||
|
|
||||||
deepEqual(results, expected);
|
deepEqual(results, expected);
|
||||||
|
|||||||
Reference in New Issue
Block a user