Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita80d0bc

Browse files
committed
Ensure_#reverse doesn't augment the original array if it was sliced before. [closes#1323]
1 parent2e50d11 commita80d0bc

File tree

2 files changed

+62
-36
lines changed

2 files changed

+62
-36
lines changed

‎lodash.src.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,7 +1205,7 @@
12051205
takeCount=nativeMin(length,this.__takeCount__);
12061206

12071207
if(!isArr||arrLength<LARGE_ARRAY_SIZE||(arrLength==length&&takeCount==length)){
1208-
returnbaseWrapperValue((isRight&&isArr) ?array.reverse() :array,this.__actions__);
1208+
returnbaseWrapperValue(array,this.__actions__);
12091209
}
12101210
varresult=[];
12111211

@@ -6230,7 +6230,7 @@
62306230
varvalue=this.__wrapped__;
62316231

62326232
varinterceptor=function(value){
6233-
return(wrapped&&wrapped.__dir__<0) ?value :value.reverse();
6233+
returnvalue.reverse();
62346234
};
62356235
if(valueinstanceofLazyWrapper){
62366236
varwrapped=value;

‎test/test.js‎

Lines changed: 60 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9588,7 +9588,7 @@
95889588
test('should provide the correct `predicate` arguments in a lazy chain sequence', 5, function() {
95899589
if (!isNpm) {
95909590
var args,
9591-
array = _.range(0, LARGE_ARRAY_SIZE),
9591+
array = _.range(0, LARGE_ARRAY_SIZE + 1),
95929592
expected = [1, 0, _.map(array.slice(1), square)];
95939593

95949594
_(array).slice(1).map(function(value, index, array) {
@@ -17092,14 +17092,14 @@
1709217092

1709317093
test('should work when in a lazy chain sequence before `first` or `last`', 1, function() {
1709417094
if (!isNpm) {
17095-
var array = _.range(0, LARGE_ARRAY_SIZE),
17096-
wrapped = _(array).slice(1).xor([LARGE_ARRAY_SIZE - 1, LARGE_ARRAY_SIZE]);
17095+
var array = _.range(0, LARGE_ARRAY_SIZE + 1),
17096+
wrapped = _(array).slice(1).xor([LARGE_ARRAY_SIZE, LARGE_ARRAY_SIZE + 1]);
1709717097

1709817098
var actual = _.map(['first', 'last'], function(methodName) {
1709917099
return wrapped[methodName]();
1710017100
});
1710117101

17102-
deepEqual(actual, [1, LARGE_ARRAY_SIZE]);
17102+
deepEqual(actual, [1, LARGE_ARRAY_SIZE + 1]);
1710317103
}
1710417104
else {
1710517105
skipTest();
@@ -17501,80 +17501,106 @@
1750117501
QUnit.module('lodash(...).reverse');
1750217502

1750317503
(function() {
17504-
test('should return the wrapped reversed `array`', 3, function() {
17504+
var largeArray = _.range(0, LARGE_ARRAY_SIZE).concat(null),
17505+
smallArray = [0, 1, 2, null];
17506+
17507+
test('should return the wrapped reversed `array`', 6, function() {
1750517508
if (!isNpm) {
17506-
var array = [1, 2, 3],
17507-
wrapped = _(array).reverse(),
17508-
actual = wrapped.value();
17509+
_.times(2, function(index) {
17510+
var array = (index ? largeArray : smallArray).slice(),
17511+
clone = array.slice(),
17512+
wrapped = _(array).reverse(),
17513+
actual = wrapped.value();
1750917514

17510-
ok(wrapped instanceof _);
17511-
strictEqual(actual, array);
17512-
deepEqual(actual, [3, 2, 1]);
17515+
ok(wrapped instanceof _);
17516+
strictEqual(actual, array);
17517+
deepEqual(actual, clone.slice().reverse());
17518+
});
1751317519
}
1751417520
else {
17515-
skipTest(3);
17521+
skipTest(6);
1751617522
}
1751717523
});
1751817524

17519-
test('should work in a lazy chain sequence',1, function() {
17525+
test('should work in a lazy chain sequence',4, function() {
1752017526
if (!isNpm) {
17521-
var array = _.range(0, LARGE_ARRAY_SIZE).concat(null),
17522-
actual = _(array).slice(1).reverse().value();
17527+
_.times(2, function(index) {
17528+
var array = (index ? largeArray : smallArray).slice(),
17529+
expected = array.slice(),
17530+
actual = _(array).slice(1).reverse().value();
1752317531

17524-
deepEqual(actual, array.slice(1).reverse());
17532+
deepEqual(actual, expected.slice(1).reverse());
17533+
deepEqual(array, expected);
17534+
});
1752517535
}
1752617536
else {
17527-
skipTest();
17537+
skipTest(4);
1752817538
}
1752917539
});
1753017540

17531-
test('should be lazy when in a lazy chain sequence',2, function() {
17541+
test('should be lazy when in a lazy chain sequence',3, function() {
1753217542
if (!isNpm) {
1753317543
var spy = {
1753417544
'toString': function() {
1753517545
throw new Error('spy was revealed');
1753617546
}
1753717547
};
1753817548

17549+
var array = largeArray.concat(spy),
17550+
expected = array.slice();
17551+
1753917552
try {
17540-
var array = _.range(0, LARGE_ARRAY_SIZE).concat(spy),
17541-
wrapped = _(array).slice(1).map(String).reverse(),
17553+
var wrapped = _(array).slice(1).map(String).reverse(),
1754217554
actual = wrapped.last();
1754317555
} catch(e) {}
1754417556

1754517557
ok(wrapped instanceof _);
1754617558
strictEqual(actual, '1');
17559+
deepEqual(array, expected);
1754717560
}
1754817561
else {
17549-
skipTest(2);
17562+
skipTest(3);
1755017563
}
1755117564
});
1755217565

17553-
test('should work in a hybrid chain sequence',4, function() {
17566+
test('should work in a hybrid chain sequence',8, function() {
1755417567
if (!isNpm) {
17555-
var array = [1, 2, 3, null];
17568+
_.times(2, function(index) {
17569+
var clone = (index ? largeArray : smallArray).slice();
1755617570

17557-
_.each(['map', 'filter'], function(methodName) {
17558-
var actual = _(array)[methodName](_.identity).thru(_.compact).reverse().value();
17559-
deepEqual(actual, [3, 2, 1]);
17571+
_.each(['map', 'filter'], function(methodName) {
17572+
var array = clone.slice(),
17573+
expected = clone.slice(1, -1).reverse(),
17574+
actual = _(array)[methodName](_.identity).thru(_.compact).reverse().value();
1756017575

17561-
actual = _(array).thru(_.compact)[methodName](_.identity).pull(1).push(4).reverse().value();
17562-
deepEqual(actual, [4, 3, 2]);
17576+
deepEqual(actual, expected);
17577+
17578+
array = clone.slice();
17579+
actual = _(array).thru(_.compact)[methodName](_.identity).pull(1).push(3).reverse().value();
17580+
17581+
deepEqual(actual, [3].concat(expected.slice(0, -1)));
17582+
});
1756317583
});
1756417584
}
1756517585
else {
17566-
skipTest(4);
17586+
skipTest(8);
1756717587
}
1756817588
});
1756917589

17570-
test('should track the `__chain__` value of a wrapper',2, function() {
17590+
test('should track the `__chain__` value of a wrapper',6, function() {
1757117591
if (!isNpm) {
17572-
var wrapped = _([1, 2, 3]).chain().reverse().first();
17573-
ok(wrapped instanceof _);
17574-
strictEqual(wrapped.value(), 3);
17592+
_.times(2, function(index) {
17593+
var array = (index ? largeArray : smallArray).slice(),
17594+
expected = array.slice().reverse(),
17595+
wrapped = _(array).chain().reverse().first();
17596+
17597+
ok(wrapped instanceof _);
17598+
strictEqual(wrapped.value(), _.first(expected));
17599+
deepEqual(array, expected);
17600+
});
1757517601
}
1757617602
else {
17577-
skipTest(2);
17603+
skipTest(6);
1757817604
}
1757917605
});
1758017606
}());

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp