Add support for arrays
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Michel Weststrate · committed by Michel Weststrate · parent 46e1d5d · Browse files · 3 files·+56 added−9 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+56 added−9 removed
- tests:+39 added−1 removed
- other:+17 added−8 removed
Provenance
- Commit
d3696cb- Tree
c2099b8- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
46e1d5d
3 files changed+56 added−9 removed
modified:__tests__/base.js+39 added-1 removed
76 unmodified lines7778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126891271281291301315 unmodified lines76 unmodified lines expect(nextState).not.toBe(baseState) expect(nextState).toEqual(baseState) })
it("should support reading arrays", () => { const nextState = immer(baseState, s => { s.anArray.slice() }) expect(nextState.anArray).toBe(baseState.anArray) expect(nextState).toBe(baseState) })
it("should support changing arrays", () => { const nextState = immer(baseState, s => { s.anArray[3] = true }) expect(nextState).not.toBe(baseState) expect(nextState.anArray).not.toBe(baseState.anArray) expect(nextState.anArray[3]).toEqual(true) })
it("should support changing arrays - 2", () => { const nextState = immer(baseState, s => { s.anArray.splice(1, 1, "a", "b") }) expect(nextState).not.toBe(baseState) expect(nextState.anArray).not.toBe(baseState.anArray)
expect(nextState.anArray).toEqual([3, "a", "b", { c: 3 }, 1]) })
it("should support sorting arrays", () => { const nextState = immer(baseState, s => { s.anArray[2].c = 4 s.anArray.sort() s.anArray[3].c = 5 }) expect(nextState).not.toBe(baseState) expect(nextState.anArray).not.toBe(baseState.anArray) expect(nextState.anArray).toEqual([1, 2, 3, { c: 5 }]) })
afterEach(() => { expect(baseState).toBe(origBaseState) expect(baseState).toEqual(createBaseState()) })})
function createBaseState() { return { anArray: [1, 2, { c: 3 }], anArray: [3, 2, { c: 3 }, 1], aProp: "hi", anObject: { nested: { yummie: true5 unmodified linesmodified:immer.js+16 added-7 removed
29 unmodified lines30 set(target, prop, value) {31 const current = proxyThing(getCurrentSource(target)[prop])32 const newValue = proxyThing(value)33 if (current !== newValue) {34- const copy = getOrCreateObjectCopy(target)34+ const copy = getOrCreateCopy(target)35 copy[prop] = newValue36 }37 return true38 },39 deleteProperty(target, property) {40- const copy = getOrCreateObjectCopy(target)40+ const copy = getOrCreateCopy(target)41 delete copy[property]42 return true43 }44 }45 46- function getOrCreateObjectCopy(object) {46+ function getOrCreateCopy(object) {47 let copy = copies.get(object)48 if (!copy) {49- copy = Object.assign({}, object)49+ copy = Array.isArray(object) ? object.slice() : Object.assign({}, object)50 copies.set(object, copy)51 }52 return copy53 }3 unmodified lines57 return copy || object58 }59 60 function proxyThing(thing) {61- if (isPlainObject(thing)) return proxyObject(thing)62- // TODO: array61+ if (isPlainObject(thing) || Array.isArray(thing)) return proxyObject(thing)62 return thing63 }64 65 function proxyObject(object) {5 unmodified lines71 }72 73 function finalizeThing(thing) {74 if (isPlainObject(thing)) return finalizeObject(thing)75+ if (Array.isArray(thing)) return finalizeArray(thing)76 return thing77 }78 79 function hasObjectChanges(thing) {10 unmodified lines90 }91 92 function finalizeObject(thing) {93 if (!hasObjectChanges(thing)) return thing94- const copy = getOrCreateObjectCopy(thing)94+ const copy = getOrCreateCopy(thing)95 Object.keys(copy).forEach(prop => {96 copy[prop] = finalizeThing(copy[prop])97 })98 return copy99 }100 101+ function finalizeArray(thing) {102+ if (!hasObjectChanges(thing)) return thing103+ const copy = getOrCreateCopy(thing)104+ copy.forEach((value, index) => {105+ copy[index] = finalizeThing(copy[index])106+ })107+ return copy108+ }109+110 const rootClone = proxyThing(baseState)111 thunk(rootClone)112 return finalizeThing(baseState)113 }8 unmodified linesmodified:package.json+1 added-1 removed
1233456732 unmodified lines{ "name": "immer", "version": "1.0.0", "version": "0.0.1", "description": "Create your next immutable state by mutating the current one", "main": "immer.js", "scripts": { "test": "jest"32 unmodified lines