Skip to content
Myr

simplify es5 object key diffing

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by aleclarson · committed by aleclarson · parent 4613b06 · Browse files · 1 file+18 added−28 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+18 added−28 removed

  • other:+18 added−28 removed

1 file+18 added−28 removed

modified:src/es5.js+18 added-28 removed
6 unmodified lines
7 isProxy,8 PROXY_STATE,9 shallowCopy,10 RETURNED_AND_MODIFIED_ERROR,11 has,12 each,13 finalize14} from "./common"15
152 unmodified lines
168 }169}170171function hasObjectChanges(state) {171 const baseKeys = Object.keys(state.base)172 const keys = Object.keys(state.proxy)173 return !shallowEqual(baseKeys, keys)172 const {base, proxy} = state173174 // Search for added keys. Start at the back, because non-numeric keys175 // are ordered by time of definition on the object.176 const keys = Object.keys(proxy)177 for (let i = keys.length; i !== 0; ) {178 const key = keys[--i]179180 // The `undefined` check is a fast path for pre-existing keys.181 if (base[key] === undefined && !has(base, key)) {182 return true183 }184 }185186 // Since no keys have been added, we can compare lengths to know if an187 // object has been deleted.188 return keys.length !== Object.keys(base).length189}190191function hasArrayChanges(state) {192 const {proxy} = state
57 unmodified lines
250 states = prevStates251 }252}253239function shallowEqual(objA, objB) {240 //From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js241 if (is(objA, objB)) return true242 if (243 typeof objA !== "object" ||244 objA === null ||245 typeof objB !== "object" ||246 objB === null247 ) {248 return false249 }250 const keysA = Object.keys(objA)251 const keysB = Object.keys(objB)252 if (keysA.length !== keysB.length) return false253 for (let i = 0; i < keysA.length; i++) {254 if (255 !hasOwnProperty.call(objB, keysA[i]) ||256 !is(objA[keysA[i]], objB[keysA[i]])257 ) {258 return false259 }260 }261 return true262}263254function createHiddenProperty(target, prop, value) {255 Object.defineProperty(target, prop, {256 value: value,257 enumerable: false,
3 unmodified lines