Fixed finalizing array
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by iruca3 · committed by iruca3 · parent 7290670 · Browse files · 3 files·+44 added−2 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+44 added−2 removed
- tests:+34 added−0 removed
- other:+10 added−2 removed
Provenance
- Commit
4202907- Tree
d302985- Author
iruca3 <dolphin.orca@gmail.com>- Committer
iruca3 <dolphin.orca@gmail.com>- Parent
7290670
3 files changed+44 added−2 removed
modified:__tests__/base.js+34 added
504 unmodified lines505 room: {elephant: {kiddo: {age: 10, legs: 5}}}506 })507 })508 509+ it("should not throw error", () => {510+ const base = {511+ arr: [512+ {513+ id: "100",514+ arr: [{id: "1", no: 1}, {id: "2", no: 2}]515+ },516+ {id: "3", no: 3}517+ ]518+ }519+ const result = produce(base, draft => {520+ const base = draft.arr[0]521+ draft.arr = draft.arr.slice(0, 1)522+ const newArr = {523+ id: "101",524+ arr: [base, {id: "3"}]525+ }526+ draft.arr.splice(0, 1, newArr)527+ draft.arr[0].arr[1].no = 3528+ })529+ expect(result).toEqual({530+ arr: [{531+ id: "101",532+ arr: [533+ {534+ id: "100",535+ arr: [{id: "1", no: 1}, {id: "2", no: 2}]536+ },537+ {id: "3", no: 3}538+ ]539+ }]540+ })541+ })542+543 it("should handle dates correctly", () => {544 const data = {date: new Date()}545 const next = produce(data, draft => {546 draft.x = true176 unmodified linesmodified:src/es5.js+5 added-1 removed
154 unmodified lines155 export function finalizeArray(proxy, state) {156 const res = (state.copy = shallowCopy(proxy))157 const base = state.base158 each(res, (i, value) => {159- if (value !== base[i]) res[i] = finalize(value)159+ if (value !== base[i]) {160+ res[i] = finalize(value)161+ } else {162+ res[i] = finalize(res[i])163+ }164 })165 return freeze(res)166 }167 58 unmodified linesmodified:src/proxy.js+5 added-1 removed
136 unmodified linesExpand all13713813914014114114214314414514614714814917 unmodified lines136 unmodified linesExpand allexport function finalizeArray(state) { const copy = state.copy const base = state.base each(copy, (i, value) => { if (value !== base[i]) copy[i] = finalize(value) if (value !== base[i]) { copy[i] = finalize(value) } else { copy[i] = finalize(copy[i]) } }) return freeze(copy)}
17 unmodified lines