Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-regexp-comparison.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/store': patch
---

Compare regular expression patterns and flags in `compareDeep` and `compareShallow` so changes are not silently skipped.
10 changes: 10 additions & 0 deletions packages/store/src/compare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ function _evaluate<T>(
)
}

// A RegExp stores its pattern and flags outside its enumerable properties.
// Leave the mutable matching cursor (lastIndex) out of the comparison.
if (
objA instanceof RegExp &&
objB instanceof RegExp &&
(objA.source !== objB.source || objA.flags !== objB.flags)
) {
return false
}

const keysA = getOwnKeys(objA as object)
const keysB = getOwnKeys(objB as object)

Expand Down
46 changes: 45 additions & 1 deletion packages/store/tests/compare.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { describe, expect, it, vi } from 'vitest'
import { compareDeep, compareShallow } from '../src/compare'
import { compareDeep, compareShallow, createAtom } from '../src'

const comparators = [
{ name: 'compareShallow', compare: compareShallow },
{ name: 'compareDeep', compare: compareDeep },
]

describe('evaluate', () => {
it('should test equality between primitives', () => {
Expand All @@ -19,6 +24,45 @@ describe('evaluate', () => {
expect(undefinedFalse).toEqual(false)
})

it.each(comparators)(
'$name compares regular expression patterns and flags',
({ compare }) => {
expect(compare(/search/i, /search/i)).toBe(true)
expect(compare(/search/i, /replace/i)).toBe(false)
expect(compare(/search/i, /search/)).toBe(false)

// Matching advances lastIndex without changing the pattern or flags.
const used = /search/g
used.test('search')
expect(compare(used, /search/g)).toBe(true)

const taggedA = Object.assign(/search/, { tag: 'a' })
const taggedB = Object.assign(/search/, { tag: 'b' })
expect(compare(taggedA, taggedB)).toBe(false)
},
)

it.each(comparators)(
'$name updates an atom when its regular expression changes',
({ compare }) => {
const atom = createAtom(/^before/, { compare })
const onChange = vi.fn()
const subscription = atom.subscribe(onChange)

try {
atom.set(/^before/)
expect(onChange).not.toHaveBeenCalled()

const next = /^after/
atom.set(next)
expect(atom.get()).toBe(next)
expect(onChange).toHaveBeenCalledExactlyOnceWith(next)
} finally {
subscription.unsubscribe()
}
},
)

it('should return false for runtime cross-type comparisons', () => {
expect(compareShallow<any>(new Date(), new Map())).toEqual(false)
expect(compareShallow<any>(new Date(), new Set())).toEqual(false)
Expand Down