@traversable/schema-deep-equal
v0.0.19
Published
<br /> <h1 align="center">แฏ๐๐ฟ๐ฎ๐๐ฒ๐ฟ๐๐ฎ๐ฏ๐น๐ฒ/๐๐ฐ๐ต๐ฒ๐บ๐ฎ-๐ฑ๐ฒ๐ฒ๐ฝ-๐ฒ๐พ๐๐ฎ๐น</h1> <br />
Downloads
23
Readme
Overview
An "deep equal" function" is a function that accepts 2 values, and returns a boolean indicating whether the values are made up of the same component parts.
The
@traversable/schema-deep-equalpackage can be used in 2 ways:
As a side-effect import, which will install a
.equalsmethod to all schemas.To derive a deep equal function from a schema recursively, by walking the schema's AST
Notable features
Portable
Derived deep equal functions are "isomorphic" -- JS-speak for "works on both the client and on the server".
That means you can use @traversable/schema-deep-equal anywhere: in the browser (they work great with state
libraries that use an equality function to determine when to re-render!), in a BFF-architecture, with
CloudFlare workers -- anywhere.
Performance
Our deep equal functions are:
- 3-4x faster than
node:assert/deepStrictEqual, and - 4-6x faster than
lodash.deepEqual
If you'd like to check our math, the benchmarks are public and available here.
Drop-in replacement
- Deep equal functions generated by
@traversable/schema-deep-equalhave been thoroughly fuzz-tested against millions of randomly generated inputs (viafast-check) to make the transition seamless for our users
Quick Start
You can install .equals on all schemas with a single line:
Example
import { t } from '@traversable/schema'
import '@traversable/schema-deep-equal/install'
// โโ importing `@traversable/schema-deep-equal/install` installs `.equal` on all schemas
const Schema = t.object({
abc: t.boolean,
def: t.optional(t.number.min(3)),
})
let x = { abc: true, def: 10 }
let y = { ...x }
let z = { ...x, abc: false }
console.log(Object.is(x, y)) // => false ๐ญ
console.log(Schema.equals(x, y)) // => true ๐
console.log(Schema.equals(y, z)) // => false ๐Usage
If you'd prefer not to install .equals to all schemas, use Eq.fromSchema
(or you can import fromSchema directly, if you're bundle-phobic).
Example
import { t } from '@traversable/schema'
import { Eq } from '@traversable/schema-deep-equal'
const Schema = t.object({
abc: t.boolean,
def: t.optional(t.number.min(3)),
})
const equalsFn = Eq.fromSchema(Schema)
let x = { abc: true, def: 10 }
let y = { ...x }
let z = { ...x, abc: false }
console.log(Object.is(x, y)) // => false ๐ญ
console.log(equalsFn(x, y)) // => true ๐
console.log(equalsFn(y, z)) // => false ๐