shallow.ts
This file
Path
src/vanilla/shallow.ts
74 lines · 1.9 KiB · TypeScript
Last changed by
fix(shallow): undefined value (#3205)Daishi Katobefore adoption2cc1988
History of this fileTake it
The walk stopped after 200 commits: lines older than f85898f point at it.
chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
lines 1–21why, one step back1const isIterable = (obj: object): obj is Iterable<unknown> =>2 Symbol.iterator in obj34const hasIterableEntries = (5 value: Iterable<unknown>,6): value is Iterable<unknown> & {7 entries(): Iterable<[unknown, unknown]>8} =>9 // HACK: avoid checking entries type10 'entries' in value1112const compareEntries = (13 valueA: { entries(): Iterable<[unknown, unknown]> },14 valueB: { entries(): Iterable<[unknown, unknown]> },15) => {16 const mapA = valueA instanceof Map ? valueA : new Map(valueA.entries())17 const mapB = valueB instanceof Map ? valueB : new Map(valueB.entries())18 if (mapA.size !== mapB.size) {19 return false20 }21 for (const [key, value] of mapA) {fix(shallow): undefined value (#3205)Daishi Katobefore adoption2cc1988
line 22why, one step back22 if (!mapB.has(key) || !Object.is(value, mapB.get(key))) {chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
lines 23–59why, one step back23 return false24 }25 }26 return true27}2829// Ordered iterables30const compareIterables = (31 valueA: Iterable<unknown>,32 valueB: Iterable<unknown>,33) => {34 const iteratorA = valueA[Symbol.iterator]()35 const iteratorB = valueB[Symbol.iterator]()36 let nextA = iteratorA.next()37 let nextB = iteratorB.next()38 while (!nextA.done && !nextB.done) {39 if (!Object.is(nextA.value, nextB.value)) {40 return false41 }42 nextA = iteratorA.next()43 nextB = iteratorB.next()44 }45 return !!nextA.done && !!nextB.done46}4748export function shallow<T>(valueA: T, valueB: T): boolean {49 if (Object.is(valueA, valueB)) {50 return true51 }52 if (53 typeof valueA !== 'object' ||54 valueA === null ||55 typeof valueB !== 'object' ||56 valueB === null57 ) {58 return false59 }feat(vanilla): update shallow compare function and tests (#3108)Danilo Brittobefore adoptiona56a3e4
lines 60–61why, one step back60 if (Object.getPrototypeOf(valueA) !== Object.getPrototypeOf(valueB)) {61 return falsechore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
line 62why, one step back62 }feat(vanilla): update shallow compare function and tests (#3108)Danilo Brittobefore adoptiona56a3e4
lines 63–67why, one step back63 if (isIterable(valueA) && isIterable(valueB)) {64 if (hasIterableEntries(valueA) && hasIterableEntries(valueB)) {65 return compareEntries(valueA, valueB)66 }67 return compareIterables(valueA, valueB)chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
line 68why, one step back68 }feat(vanilla): update shallow compare function and tests (#3108)Danilo Brittobefore adoptiona56a3e4
lines 69–73why, one step back69 // assume plain objects70 return compareEntries(71 { entries: () => Object.entries(valueA) },72 { entries: () => Object.entries(valueB) },73 )chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
line 74why, one step back74}