equalsBy
Usage
The equalsBy function determines whether two values represent "the same item". It first takes fast paths for referential equality and null/undefined, unwraps Vue reactive wrappers via toRaw, then compares using by or keys configuration.
import { equalsBy } from '@movk/core'
// referential equality or primitive fast path
equalsBy(1, 1) // true
equalsBy('a', 'b') // false
by string path
When both values are objects, the path is used to extract and compare values. Setting by is exclusive — a hit or miss does not fall through to keys.
equalsBy({ id: 1, name: 'A' }, { id: 1, name: 'B' }, { by: 'id' }) // true
equalsBy({ meta: { id: 1 } }, { meta: { id: 2 } }, { by: 'meta.id' }) // false
by predicate function
equalsBy(
{ tenant: 't1', user: 'u1' },
{ tenant: 't1', user: 'u1' },
{ by: (a, b) => a.tenant === b.tenant && a.user === b.user },
) // true
keys heuristic fallback
Only used when by is not set and both values are objects. Candidate keys are iterated in order; the first key where both sides yield a non-null, non-object scalar is used as the comparison basis.
equalsBy(
{ label: 'HSL', value: 'hsl' },
{ label: 'HSL', value: 'hsl' },
{ keys: ['value', 'label'] },
) // true (first candidate 'value' matches)
createEqualsBy
Use createEqualsBy to create an equality function with pre-bound options, convenient for reuse in .filter / .some / deduplication callbacks.
import { createEqualsBy } from '@movk/core'
const sameUser = createEqualsBy<{ id: number }>({ by: 'id' })
users.some(u => sameUser(u, target))
API
equalsBy<T>(a, b, options?)
Determines whether two values represent the same item.
Parameters
Returns
EqualsByOptions
keys.by is not set and both values are objects.createEqualsBy<T>(options)
Creates a pre-bound equalsBy function.