Skip to content

Commit 83fae9c

Browse files
committed
retain non int array keys
1 parent 532f6d3 commit 83fae9c

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var getAllKeys = typeof Object.getOwnPropertySymbols === 'function' ?
1919
/* istanbul ignore next */
2020
function copy(object) {
2121
if (object instanceof Array) {
22-
return object.slice();
22+
return assign(object.constructor(object.length), object)
2323
} else if (object && typeof object === 'object') {
2424
var prototype = object.constructor && object.constructor.prototype
2525
return assign(Object.create(prototype || null), object);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "immutability-helper",
3-
"version": "2.2.3",
3+
"version": "2.3.0",
44
"description": "mutate a copy of data without changing the original source",
55
"main": "index.js",
66
"scripts": {

test.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,16 @@ describe('update', function() {
412412
expect(a).toBe(b)
413413
expect(a).toBe(c)
414414
expect(a).toBe(d)
415-
})
415+
});
416+
417+
it('does not lose non integer keys of an array', function() {
418+
var state = a = { items: [
419+
{ name: 'Superman', strength: 1000 },
420+
{ name: 'Jim', strength: 2 },
421+
] };
422+
state.items.top = 0
423+
var state2 = update(state, { items: { 1: { strength: { $set: 3 } } } });
424+
expect(state2.items.top).toBe(0)
425+
});
416426

417427
});

0 commit comments

Comments
 (0)