Skip to content

Commit

Permalink
fix: updating Map & Set objects should now work as-expected
Browse files Browse the repository at this point in the history
  • Loading branch information
richhost authored Nov 25, 2024
1 parent 3b481ec commit 1cf9549
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/angular-store/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ function shallow<T>(objA: T, objB: T) {
return false
}

if (objA instanceof Map && objB instanceof Map) {
if (objA.size !== objB.size) return false
for (const [k, v] of objA) {
if (!objB.has(k) || !Object.is(v, objB.get(k))) return false
}
return true
}

if (objA instanceof Set && objB instanceof Set) {
if (objA.size !== objB.size) return false
for (const v of objA) {
if (!objB.has(v)) return false
}
return true
}

const keysA = Object.keys(objA)
if (keysA.length !== Object.keys(objB).length) {
return false
Expand Down
16 changes: 16 additions & 0 deletions packages/react-store/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ export function shallow<T>(objA: T, objB: T) {
return false
}

if (objA instanceof Map && objB instanceof Map) {
if (objA.size !== objB.size) return false
for (const [k, v] of objA) {
if (!objB.has(k) || !Object.is(v, objB.get(k))) return false
}
return true
}

if (objA instanceof Set && objB instanceof Set) {
if (objA.size !== objB.size) return false
for (const v of objA) {
if (!objB.has(v)) return false
}
return true
}

const keysA = Object.keys(objA)
if (keysA.length !== Object.keys(objB).length) {
return false
Expand Down
24 changes: 24 additions & 0 deletions packages/react-store/tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,28 @@ describe('shallow', () => {
// @ts-expect-error
expect(shallow(objA, objB)).toBe(false)
})

test('should return true for shallowly equal maps', () => {
const objA = new Map([['1', 'hello']])
const objB = new Map([['1', 'hello']])
expect(shallow(objA, objB)).toBe(true)
})

test('should return false for maps with different values', () => {
const objA = new Map([['1', 'hello']])
const objB = new Map([['1', 'world']])
expect(shallow(objA, objB)).toBe(false)
})

test('should return true for shallowly equal sets', () => {
const objA = new Set([1])
const objB = new Set([1])
expect(shallow(objA, objB)).toBe(true)
})

test('should return false for sets with different values', () => {
const objA = new Set([1])
const objB = new Set([2])
expect(shallow(objA, objB)).toBe(false)
})
})
16 changes: 16 additions & 0 deletions packages/svelte-store/src/index.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ export function shallow<T>(objA: T, objB: T) {
return false
}

if (objA instanceof Map && objB instanceof Map) {
if (objA.size !== objB.size) return false
for (const [k, v] of objA) {
if (!objB.has(k) || !Object.is(v, objB.get(k))) return false
}
return true
}

if (objA instanceof Set && objB instanceof Set) {
if (objA.size !== objB.size) return false
for (const v of objA) {
if (!objB.has(v)) return false
}
return true
}

const keysA = Object.keys(objA)
if (keysA.length !== Object.keys(objB).length) {
return false
Expand Down
16 changes: 16 additions & 0 deletions packages/vue-store/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ export function shallow<T>(objA: T, objB: T) {
return false
}

if (objA instanceof Map && objB instanceof Map) {
if (objA.size !== objB.size) return false
for (const [k, v] of objA) {
if (!objB.has(k) || !Object.is(v, objB.get(k))) return false
}
return true
}

if (objA instanceof Set && objB instanceof Set) {
if (objA.size !== objB.size) return false
for (const v of objA) {
if (!objB.has(v)) return false
}
return true
}

const keysA = Object.keys(objA)
if (keysA.length !== Object.keys(objB).length) {
return false
Expand Down
24 changes: 24 additions & 0 deletions packages/vue-store/tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,28 @@ describe('shallow', () => {
// @ts-expect-error
expect(shallow(objA, objB)).toBe(false)
})

test('should return true for shallowly equal maps', () => {
const objA = new Map([['1', 'hello']])
const objB = new Map([['1', 'hello']])
expect(shallow(objA, objB)).toBe(true)
})

test('should return false for maps with different values', () => {
const objA = new Map([['1', 'hello']])
const objB = new Map([['1', 'world']])
expect(shallow(objA, objB)).toBe(false)
})

test('should return true for shallowly equal sets', () => {
const objA = new Set([1])
const objB = new Set([1])
expect(shallow(objA, objB)).toBe(true)
})

test('should return false for sets with different values', () => {
const objA = new Set([1])
const objB = new Set([2])
expect(shallow(objA, objB)).toBe(false)
})
})

0 comments on commit 1cf9549

Please sign in to comment.