dittoly 1.0.0-test → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/README.md +467 -4
- package/dist/index.cjs +4 -0
- package/dist/index.d.cts +132 -0
- package/dist/index.d.ts +132 -0
- package/dist/index.js +3 -0
- package/package.json +58 -7
- package/index.d.ts +0 -2
- package/index.js +0 -8
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this package are documented here. This project follows [Semantic Versioning](https://semver.org/).
|
|
4
|
+
|
|
5
|
+
## 1.0.0 - Unreleased
|
|
6
|
+
|
|
7
|
+
First release.
|
|
8
|
+
|
|
9
|
+
- `dittoly(a, b, options?)` (default export), `equal()` and `isEqual()`: deep equality for primitives (`NaN` equals `NaN`), plain objects, class instances, arrays (incl. sparse), `Map` (object keys matched deeply), `Set` (object members matched deeply), `Date`, `RegExp`, typed arrays, `ArrayBuffer`, `SharedArrayBuffer`, `DataView`, `Error` (incl. `cause` and `AggregateError.errors`), boxed primitives, `URL`, `URLSearchParams` and Temporal objects.
|
|
10
|
+
- Circular references are handled without stack overflows, at no cost for ordinary data.
|
|
11
|
+
- `strict: true` matches Node's `util.isDeepStrictEqual` (`+0`/`-0`, array holes, symbol keys, exact prototypes, extra properties).
|
|
12
|
+
- Options: `prototype`, `signedZero`, `nanEqual`, `symbols`, `ignoreUndefined`, `ignoreKeys` (list or function) and `customizer`.
|
|
13
|
+
- `createEqual(options)` builds a reusable comparator.
|
|
14
|
+
- `diff(a, b, options?)` lists differences as `{ kind: 'changed' | 'added' | 'removed', path, a, b }`, and returns `[]` exactly when the values are equal.
|
|
15
|
+
- `assertEqual(actual, expected, options?)` throws a `NotEqualError` with a readable list of differences, and narrows `actual` in TypeScript.
|
|
16
|
+
- `formatPath(path)` turns `['user', 'tags', 1]` into `'user.tags[1]'`.
|
|
17
|
+
- Promises, WeakMaps, WeakSets, WeakRefs and Blobs are only equal to themselves.
|
|
18
|
+
- Getters on prototypes are never invoked; `valueOf`/`toString` are never called on plain objects.
|
|
19
|
+
- ESM + CommonJS, TypeScript types, zero dependencies.
|
package/README.md
CHANGED
|
@@ -1,18 +1,481 @@
|
|
|
1
1
|
# dittoly
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Check if two values are deeply equal, and see exactly where they differ.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
[](https://www.npmjs.com/package/dittoly) [](https://bundlephobia.com/package/dittoly) [](LICENSE)
|
|
6
|
+
|
|
7
|
+
`dittoly` compares two values by their contents, not by reference. This is called deep equality. It works with objects, arrays, `Map`, `Set`, `Date` and many other built-in types, and it handles circular references (objects that point back to themselves). Use it in tests, to skip work when data has not changed, or anywhere you would use `fast-deep-equal`, `dequal`, `lodash.isequal` or `util.isDeepStrictEqual`.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- **Compares many types**: plain objects, class instances, arrays (also sparse arrays), `Map`, `Set`, `Date`, `RegExp`, typed arrays, `ArrayBuffer`, `DataView`, `Error`, boxed primitives, `URL`, `URLSearchParams`.
|
|
12
|
+
- **Deep `Map` keys and `Set` members**: object keys and members are matched by content.
|
|
13
|
+
- **Circular references** work with no stack overflow.
|
|
14
|
+
- **`diff()` with paths** shows where two values differ, and `assertEqual()` throws a clear message like `user.tags[1]: got "b", expected "c"`.
|
|
15
|
+
- **Two modes**: simple defaults, or `strict: true` to match Node's `util.isDeepStrictEqual`.
|
|
16
|
+
- **Options when you need them**: `ignoreKeys`, `ignoreUndefined`, `customizer`, `prototype`, `signedZero`, `nanEqual`, `symbols`.
|
|
17
|
+
- **Fast**: about as fast as `dequal`, 3-5x faster than `util.isDeepStrictEqual`, 4-15x faster than `lodash.isequal` (see [Performance](#performance)).
|
|
18
|
+
- **Safe**: never calls `valueOf` or `toString` on your objects, never runs getters on their prototypes, and does not hang on unusual inputs.
|
|
19
|
+
- **Small**: zero dependencies, about 4.5 KB gzipped.
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
6
22
|
|
|
7
23
|
```sh
|
|
8
24
|
npm install dittoly
|
|
9
25
|
```
|
|
10
26
|
|
|
27
|
+
```sh
|
|
28
|
+
pnpm add dittoly
|
|
29
|
+
yarn add dittoly
|
|
30
|
+
bun add dittoly
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Quick start
|
|
34
|
+
|
|
35
|
+
```js
|
|
36
|
+
import dittoly, { diff } from 'dittoly'
|
|
37
|
+
// CommonJS: const dittoly = require('dittoly')
|
|
38
|
+
|
|
39
|
+
dittoly({ a: [1, { b: 2 }] }, { a: [1, { b: 2 }] }) // => true
|
|
40
|
+
dittoly(new Set([1, 2]), new Set([2, 1])) // => true
|
|
41
|
+
dittoly(new Map([[{ id: 1 }, 'x']]), new Map([[{ id: 1 }, 'x']])) // => true
|
|
42
|
+
dittoly(NaN, NaN) // => true
|
|
43
|
+
dittoly(0, -0, { strict: true }) // => false
|
|
44
|
+
|
|
45
|
+
// Skip keys that always change
|
|
46
|
+
dittoly({ id: 1, updatedAt: 1 }, { id: 1, updatedAt: 2 }, { ignoreKeys: ['updatedAt'] }) // => true
|
|
47
|
+
|
|
48
|
+
// Find where two values differ
|
|
49
|
+
diff({ user: { tags: ['a', 'b'] } }, { user: { tags: ['a', 'c'] } })
|
|
50
|
+
// => [{ kind: 'changed', path: ['user', 'tags', 1], a: 'b', b: 'c' }]
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Table of contents
|
|
54
|
+
|
|
55
|
+
- [Usage](#usage)
|
|
56
|
+
- [API](#api)
|
|
57
|
+
- [How values are compared](#how-values-are-compared)
|
|
58
|
+
- [Comparison](#comparison)
|
|
59
|
+
- [Performance](#performance)
|
|
60
|
+
- [Migrating from other packages](#migrating-from-other-packages)
|
|
61
|
+
- [Notes and limitations](#notes-and-limitations)
|
|
62
|
+
- [Requirements](#requirements)
|
|
63
|
+
- [License](#license)
|
|
64
|
+
|
|
65
|
+
## Usage
|
|
66
|
+
|
|
67
|
+
### Compare two values
|
|
68
|
+
|
|
69
|
+
The default export, `equal` and `isEqual` are the same function. Use the name you like.
|
|
70
|
+
|
|
71
|
+
```js
|
|
72
|
+
import dittoly, { equal, isEqual } from 'dittoly'
|
|
73
|
+
|
|
74
|
+
dittoly({ a: 1, b: [1, 2] }, { b: [1, 2], a: 1 }) // => true (key order does not matter)
|
|
75
|
+
equal([1, 2], [2, 1]) // => false (array order matters)
|
|
76
|
+
isEqual(new Date(0), new Date(0)) // => true
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Ignore keys that always change
|
|
80
|
+
|
|
81
|
+
`ignoreKeys` skips object keys at any depth. Pass a list of keys, or a function that returns `true` for keys to skip.
|
|
82
|
+
|
|
83
|
+
```js
|
|
84
|
+
import { equal } from 'dittoly'
|
|
85
|
+
|
|
86
|
+
equal({ id: 1, meta: { updatedAt: 1 } }, { id: 1, meta: { updatedAt: 2 } }, { ignoreKeys: ['updatedAt'] })
|
|
87
|
+
// => true
|
|
88
|
+
|
|
89
|
+
equal({ _rev: 'a', name: 'Ann' }, { _rev: 'b', name: 'Ann' }, { ignoreKeys: (key) => key.startsWith('_') })
|
|
90
|
+
// => true
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Treat `undefined` properties as missing
|
|
94
|
+
|
|
95
|
+
By default, `{ a: undefined }` and `{}` are different. With `ignoreUndefined: true`, a property set to `undefined` counts as missing.
|
|
96
|
+
|
|
97
|
+
```js
|
|
98
|
+
import { equal } from 'dittoly'
|
|
99
|
+
|
|
100
|
+
equal({ a: 1, b: undefined }, { a: 1 }) // => false
|
|
101
|
+
equal({ a: 1, b: undefined }, { a: 1 }, { ignoreUndefined: true }) // => true
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Compare class instances by their properties
|
|
105
|
+
|
|
106
|
+
By default, the prototype (the class an object was made from) must match. Set `prototype: false` to compare class instances by their properties only.
|
|
107
|
+
|
|
108
|
+
```js
|
|
109
|
+
import { equal } from 'dittoly'
|
|
110
|
+
|
|
111
|
+
class Point {
|
|
112
|
+
constructor(x, y) {
|
|
113
|
+
this.x = x
|
|
114
|
+
this.y = y
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
equal(new Point(1, 2), { x: 1, y: 2 }) // => false
|
|
119
|
+
equal(new Point(1, 2), { x: 1, y: 2 }, { prototype: false }) // => true
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### Use your own rule for some values
|
|
123
|
+
|
|
124
|
+
A `customizer` is a function that runs before the built-in rules. Return `true` or `false` to decide, or `undefined` to let `dittoly` continue.
|
|
125
|
+
|
|
126
|
+
```js
|
|
127
|
+
import { equal } from 'dittoly'
|
|
128
|
+
|
|
129
|
+
// Compare strings without case
|
|
130
|
+
equal({ name: 'ANN' }, { name: 'ann' }, {
|
|
131
|
+
customizer: (a, b) => (typeof a === 'string' && typeof b === 'string' ? a.toLowerCase() === b.toLowerCase() : undefined),
|
|
132
|
+
}) // => true
|
|
133
|
+
|
|
134
|
+
// Allow small number errors
|
|
135
|
+
equal({ total: 0.1 + 0.2 }, { total: 0.3 }, {
|
|
136
|
+
customizer: (a, b) => (typeof a === 'number' && typeof b === 'number' ? Math.abs(a - b) < 1e-9 : undefined),
|
|
137
|
+
}) // => true
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### Reuse the same options
|
|
141
|
+
|
|
142
|
+
`createEqual()` builds a comparator with the options built in. Use it when you call with the same `customizer` or `ignoreKeys` many times. Options made only of booleans are cached for you, so you do not need `createEqual` for those.
|
|
143
|
+
|
|
144
|
+
```js
|
|
145
|
+
import { createEqual } from 'dittoly'
|
|
146
|
+
|
|
147
|
+
const sameRecord = createEqual({ ignoreKeys: ['updatedAt', '_rev'] })
|
|
148
|
+
|
|
149
|
+
const rows = [{ id: 1, updatedAt: 5 }, { id: 2, _rev: 'x' }]
|
|
150
|
+
const previous = [{ id: 1, updatedAt: 9 }, { id: 3 }]
|
|
151
|
+
|
|
152
|
+
rows.filter((row, i) => !sameRecord(row, previous[i])) // => [{ id: 2, _rev: 'x' }]
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### Find where two values differ
|
|
156
|
+
|
|
157
|
+
`diff()` returns a list of differences. Each one has a `kind` (`'changed'`, `'added'` or `'removed'`) and a `path` (the keys and indexes that lead to the value). `formatPath()` turns a path into a readable string.
|
|
158
|
+
|
|
159
|
+
```js
|
|
160
|
+
import { diff, formatPath } from 'dittoly'
|
|
161
|
+
|
|
162
|
+
const changes = diff(
|
|
163
|
+
{ user: { name: 'Ann', tags: ['x', 'y'], age: 30 } },
|
|
164
|
+
{ user: { name: 'Ann', tags: ['x', 'z', 'w'], email: 'a@b.c' } },
|
|
165
|
+
)
|
|
166
|
+
// => [
|
|
167
|
+
// { kind: 'changed', path: ['user', 'tags', 1], a: 'y', b: 'z' },
|
|
168
|
+
// { kind: 'added', path: ['user', 'tags', 2], b: 'w' },
|
|
169
|
+
// { kind: 'removed', path: ['user', 'age'], a: 30 },
|
|
170
|
+
// { kind: 'added', path: ['user', 'email'], b: 'a@b.c' },
|
|
171
|
+
// ]
|
|
172
|
+
|
|
173
|
+
changes.map((d) => formatPath(d.path))
|
|
174
|
+
// => ['user.tags[1]', 'user.tags[2]', 'user.age', 'user.email']
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
`diff(a, b, options)` returns `[]` exactly when `equal(a, b, options)` is `true`.
|
|
178
|
+
|
|
179
|
+
### Get a clear error in tests
|
|
180
|
+
|
|
181
|
+
`assertEqual()` throws a `NotEqualError` that lists every difference. The error also has `diffs`, `actual` and `expected` properties.
|
|
182
|
+
|
|
183
|
+
```js
|
|
184
|
+
import { assertEqual } from 'dittoly'
|
|
185
|
+
|
|
186
|
+
try {
|
|
187
|
+
assertEqual({ user: { name: 'Ann', age: 3 } }, { user: { name: 'Bob' } })
|
|
188
|
+
} catch (error) {
|
|
189
|
+
console.log(error.message)
|
|
190
|
+
// Expected values to be deeply equal (2 differences):
|
|
191
|
+
// user.name: got "Ann", expected "Bob"
|
|
192
|
+
// user.age: got 3, expected (missing)
|
|
193
|
+
}
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
In TypeScript, `actual` has the type of `expected` after the call:
|
|
197
|
+
|
|
198
|
+
```ts
|
|
199
|
+
import { assertEqual } from 'dittoly'
|
|
200
|
+
|
|
201
|
+
const body: unknown = await response.json()
|
|
202
|
+
assertEqual(body, { ok: true })
|
|
203
|
+
body.ok // TypeScript now knows this is a boolean
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
### Match Node's `util.isDeepStrictEqual`
|
|
207
|
+
|
|
208
|
+
The default mode compares what most people mean by "the same data". `strict: true` follows Node's `util.isDeepStrictEqual`.
|
|
209
|
+
|
|
210
|
+
| Case | Default | `strict: true` |
|
|
211
|
+
|---|---|---|
|
|
212
|
+
| `NaN` vs `NaN` | Equal | Equal |
|
|
213
|
+
| `+0` vs `-0` | Equal | Different |
|
|
214
|
+
| `{ a: undefined }` vs `{}` | Different (use `ignoreUndefined`) | Different |
|
|
215
|
+
| `[, 1]` (empty slot) vs `[undefined, 1]` | Equal | Different |
|
|
216
|
+
| `{}` vs `Object.create(null)` vs an object from another realm (iframe or `vm` context) | Equal | Different |
|
|
217
|
+
| `new Point(1, 2)` vs `{ x: 1, y: 2 }` | Different (use `prototype: false`) | Different |
|
|
218
|
+
| Symbol keys | Ignored | Compared |
|
|
219
|
+
| Extra properties on arrays, Dates, Maps, Sets, RegExps | Ignored | Compared |
|
|
220
|
+
| `RegExp` `lastIndex` | Ignored | Compared |
|
|
221
|
+
|
|
222
|
+
You can change each row with an option. For example, `{ strict: true, signedZero: false }` is strict mode where `0` equals `-0`.
|
|
223
|
+
|
|
224
|
+
```js
|
|
225
|
+
import { equal } from 'dittoly'
|
|
226
|
+
|
|
227
|
+
equal([, 1], [undefined, 1]) // => true
|
|
228
|
+
equal([, 1], [undefined, 1], { strict: true }) // => false
|
|
229
|
+
equal(0, -0, { strict: true, signedZero: false }) // => true
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
## API
|
|
233
|
+
|
|
234
|
+
### dittoly(a, b, options?)
|
|
235
|
+
|
|
236
|
+
```ts
|
|
237
|
+
function dittoly(a: unknown, b: unknown, options?: EqualOptions): boolean
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
The default export. Returns `true` if `a` and `b` are deeply equal. All named exports are also attached to it: `dittoly.equal`, `dittoly.isEqual`, `dittoly.createEqual`, `dittoly.diff`, `dittoly.assertEqual`, `dittoly.formatPath` and `dittoly.NotEqualError`. In CommonJS, `require('dittoly')` returns this function.
|
|
241
|
+
|
|
242
|
+
### equal(a, b, options?) and isEqual(a, b, options?)
|
|
243
|
+
|
|
244
|
+
```ts
|
|
245
|
+
function equal(a: unknown, b: unknown, options?: EqualOptions): boolean
|
|
246
|
+
const isEqual: typeof equal
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
The same as `dittoly`, as named exports. `isEqual` is an alias for code coming from lodash.
|
|
250
|
+
|
|
251
|
+
| Option | Type | Default | Description |
|
|
252
|
+
|---|---|---|---|
|
|
253
|
+
| `strict` | `boolean` | `false` | Match `util.isDeepStrictEqual` (see [the table above](#match-nodes-utilisdeepstrictequal)). Sets the defaults of `signedZero`, `symbols` and `prototype`. |
|
|
254
|
+
| `prototype` | `boolean` | `true` | Compare prototypes. Outside strict mode, plain objects, `Object.create(null)` objects and objects from other realms count as the same. `false` compares class instances by properties only (arrays, Maps, Dates and so on still only equal their own kind). |
|
|
255
|
+
| `signedZero` | `boolean` | same as `strict` | `+0` and `-0` are different (also inside float typed arrays). |
|
|
256
|
+
| `nanEqual` | `boolean` | `true` | `NaN` equals `NaN`. |
|
|
257
|
+
| `symbols` | `boolean` | same as `strict` | Compare own enumerable symbol keys. |
|
|
258
|
+
| `ignoreUndefined` | `boolean` | `false` | A property set to `undefined` counts as missing: `{ a: undefined }` equals `{}`. Array elements are not affected. |
|
|
259
|
+
| `ignoreKeys` | `PropertyKey[] \| ((key, object) => boolean)` | none | Object keys to skip at any depth. A function returns `true` for keys to skip. Not used for Map keys or array indexes. |
|
|
260
|
+
| `customizer` | `(a, b) => boolean \| undefined` | none | Runs before the built-in rules. Return a boolean to decide, or `undefined` to continue. |
|
|
261
|
+
|
|
262
|
+
The `customizer` is called for every pair of values that is compared: the root values, properties, array elements, Map values, and Set members or Map keys that are not found by identity. It is not called for typed array elements.
|
|
263
|
+
|
|
264
|
+
A `TypeError` is thrown if `options` is not an object, `ignoreKeys` is not a list or function, or `customizer` is not a function.
|
|
265
|
+
|
|
266
|
+
### createEqual(options?)
|
|
267
|
+
|
|
268
|
+
```ts
|
|
269
|
+
function createEqual(options?: EqualOptions): Comparator
|
|
270
|
+
type Comparator = (a: unknown, b: unknown) => boolean
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
Returns a comparator with the options built in. It is faster than passing the same `customizer` or `ignoreKeys` on every call. Takes the same options as `equal`.
|
|
274
|
+
|
|
275
|
+
### diff(a, b, options?)
|
|
276
|
+
|
|
277
|
+
```ts
|
|
278
|
+
function diff(a: unknown, b: unknown, options?: EqualOptions): Difference[]
|
|
279
|
+
|
|
280
|
+
type Difference =
|
|
281
|
+
| { kind: 'changed'; path: Path; a: unknown; b: unknown }
|
|
282
|
+
| { kind: 'removed'; path: Path; a: unknown } // only in a
|
|
283
|
+
| { kind: 'added'; path: Path; b: unknown } // only in b
|
|
284
|
+
|
|
285
|
+
type Path = unknown[] // object keys, array indexes (numbers), Map keys; [] is the root
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
Lists the differences between `a` and `b`. Returns `[]` exactly when `equal(a, b, options)` is `true`. Takes the same options as `equal`.
|
|
289
|
+
|
|
290
|
+
- Objects, arrays and Maps are walked key by key. Map keys appear in the path as they are.
|
|
291
|
+
- Anything else that differs (a primitive, `Date`, `Set`, `Error` and so on) is one `'changed'` entry at its path.
|
|
292
|
+
- Arrays are compared index by index. An inserted item is not detected as a move.
|
|
293
|
+
- In strict mode, empty array slots show up as `'added'` or `'removed'`.
|
|
294
|
+
|
|
295
|
+
### assertEqual(actual, expected, options?)
|
|
296
|
+
|
|
297
|
+
```ts
|
|
298
|
+
function assertEqual<T>(actual: unknown, expected: T, options?: AssertOptions): asserts actual is T
|
|
299
|
+
|
|
300
|
+
interface AssertOptions extends EqualOptions {
|
|
301
|
+
message?: string
|
|
302
|
+
}
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
Throws a `NotEqualError` unless `equal(actual, expected, options)` is `true`. The message lists up to 20 differences. In TypeScript, `actual` has the type of `expected` after the call.
|
|
306
|
+
|
|
307
|
+
| Option | Type | Default | Description |
|
|
308
|
+
|---|---|---|---|
|
|
309
|
+
| `message` | `string` | generated | Use this message instead of the generated list of differences. |
|
|
310
|
+
| (all `equal` options) | | | See [equal()](#equala-b-options-and-isequala-b-options). |
|
|
311
|
+
|
|
312
|
+
### formatPath(path)
|
|
313
|
+
|
|
314
|
+
```ts
|
|
315
|
+
function formatPath(path: readonly unknown[]): string
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
Turns a path into a readable string.
|
|
319
|
+
|
|
11
320
|
```js
|
|
12
|
-
|
|
13
|
-
|
|
321
|
+
import { formatPath } from 'dittoly'
|
|
322
|
+
|
|
323
|
+
formatPath(['user', 'tags', 1]) // => 'user.tags[1]'
|
|
324
|
+
formatPath(['a-b']) // => '["a-b"]'
|
|
325
|
+
formatPath([]) // => ''
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
### NotEqualError
|
|
329
|
+
|
|
330
|
+
```ts
|
|
331
|
+
class NotEqualError extends Error {
|
|
332
|
+
name: string // 'NotEqualError'
|
|
333
|
+
readonly diffs: Difference[]
|
|
334
|
+
readonly actual: unknown
|
|
335
|
+
readonly expected: unknown
|
|
336
|
+
constructor(actual: unknown, expected: unknown, diffs: Difference[], message?: string)
|
|
337
|
+
}
|
|
14
338
|
```
|
|
15
339
|
|
|
340
|
+
Thrown by `assertEqual()`. `diffs` holds the differences as returned by `diff(actual, expected)`.
|
|
341
|
+
|
|
342
|
+
### Types
|
|
343
|
+
|
|
344
|
+
These TypeScript types are exported: `EqualOptions`, `AssertOptions`, `Customizer`, `IgnoreKeys`, `Comparator`, `Difference`, `Path`, `Dittoly`.
|
|
345
|
+
|
|
346
|
+
## How values are compared
|
|
347
|
+
|
|
348
|
+
- **Primitives**: `===`, plus `NaN` equals `NaN`. Functions and symbols are compared by identity (same reference).
|
|
349
|
+
- **Objects**: own enumerable string keys, in any order. Symbol keys too with `symbols` or `strict`. Getters on prototypes are never called, and non-enumerable properties are ignored.
|
|
350
|
+
- **Arrays**: same length, same elements in the same order.
|
|
351
|
+
- **Map**: same size, same keys and values in any order. Object keys are matched by content.
|
|
352
|
+
- **Set**: same size, same members in any order. Object members are matched by content.
|
|
353
|
+
- **Date**: same time. Two invalid dates are equal.
|
|
354
|
+
- **RegExp**: same source and flags.
|
|
355
|
+
- **Typed arrays**: same type, length and elements.
|
|
356
|
+
- **ArrayBuffer, SharedArrayBuffer, DataView**: same bytes.
|
|
357
|
+
- **Error**: same `name`, `message`, `cause`, `errors` (for `AggregateError`) and own enumerable properties. The stack is ignored.
|
|
358
|
+
- **Boxed primitives** (`new Number(1)`, `Object(1n)`): same type and value.
|
|
359
|
+
- **URL**: same `href`. **URLSearchParams**: same string. **Temporal** objects: same string form.
|
|
360
|
+
- **Promise, WeakMap, WeakSet, WeakRef, Blob, streams**: equal only if they are the same object, because their contents cannot be read synchronously.
|
|
361
|
+
- **Circular references**: two structures are equal if they unfold into the same (endless) tree. So an object that points to itself equals a two-step loop with the same data.
|
|
362
|
+
|
|
363
|
+
## Comparison
|
|
364
|
+
|
|
365
|
+
These are the results each package actually returns (fast-deep-equal 3.1.3, dequal 2.0.3, lodash.isequal 4.5.0, Node 20.14).
|
|
366
|
+
|
|
367
|
+
| | `fast-deep-equal` | `fast-deep-equal/es6` | `dequal` | `lodash.isequal` | `util.isDeepStrictEqual` | `dittoly` |
|
|
368
|
+
|---|---|---|---|---|---|---|
|
|
369
|
+
| Circular references | No (stack overflow) | No (stack overflow) | No (stack overflow) | Yes | Yes | Yes |
|
|
370
|
+
| `Map` / `Set` | No (any two Maps are equal) | Yes | Yes | Yes | Yes | Yes |
|
|
371
|
+
| Object keys in a `Map` / objects in a `Set` | No | No (by reference only) | Yes | Yes | Yes | Yes |
|
|
372
|
+
| `ArrayBuffer` / `DataView` | No (always equal) | No (always equal; hangs on `DataView`) | Yes | Yes | Yes | Yes |
|
|
373
|
+
| Tells `new Error('a')` and `new Error('b')` apart | Yes | Yes | No | Yes | Yes | Yes |
|
|
374
|
+
| Tells `new Number(1)` and `new Number(2)` apart | Yes | Yes | No | Yes | Yes | Yes |
|
|
375
|
+
| Tells two different `URL`s apart | Yes | Yes | Yes | Yes | No | Yes |
|
|
376
|
+
| Tells two different `Promise`s / `WeakMap`s apart | No | No | No | Yes | No | Yes |
|
|
377
|
+
| Tells `{ valueOf: () => 1, a: 1 }` and `{ valueOf: () => 1, a: 2 }` apart | No (trusts `valueOf`) | No | Yes | Yes | Yes | Yes |
|
|
378
|
+
| `+0` vs `-0` | Equal | Equal | Equal | Equal | Different | Your choice (`strict` / `signedZero`) |
|
|
379
|
+
| Sparse `[, 1]` vs `[undefined, 1]` | Equal | Equal | Equal | Equal | Different | Your choice (`strict`) |
|
|
380
|
+
| Symbol keys | Ignored | Ignored | Ignored | Compared | Compared | Your choice (`strict` / `symbols`) |
|
|
381
|
+
| Ignore keys / customizer | No | No | No | Customizer only (`isEqualWith`) | No | Yes, both |
|
|
382
|
+
| Diff with paths | No | No | No | No | No | Yes (`diff()`) |
|
|
383
|
+
| Runs in browsers | Yes | Yes | Yes | Yes | No (Node only) | Yes |
|
|
384
|
+
| TypeScript types | Yes | Yes | Yes | Through `@types/lodash.isequal` | Yes | Yes |
|
|
385
|
+
|
|
386
|
+
`lodash.isequal` is deprecated on npm. `deep-equal` has 18 runtime dependencies; `dittoly` has none.
|
|
387
|
+
|
|
388
|
+
## Performance
|
|
389
|
+
|
|
390
|
+
Operations per second on Node 20.14 (higher is better). These numbers come from one laptop, so treat them as a rough guide. The benchmark script is not part of the package.
|
|
391
|
+
|
|
392
|
+
| | `dittoly` | `dittoly` `strict` | `util.isDeepStrictEqual` | `fast-deep-equal` | `dequal` | `lodash.isequal` |
|
|
393
|
+
|---|---|---|---|---|---|---|
|
|
394
|
+
| Small nested object, equal | 1.9M | 620K | 390K | 1.6M | 1.7M | 460K |
|
|
395
|
+
| Small nested object, differs deep inside | 1.8M | 740K | 420K | 5.5M | 2.0M | 460K |
|
|
396
|
+
| Array of 1000 small objects | 14.6K | 5.9K | 4.3K | 10.6K | 13.1K | 2.4K |
|
|
397
|
+
| Object with a Map, a Set and a Date | 3.3M | 1.2M | 690K | (no Map support) | 4.1M | 210K |
|
|
398
|
+
|
|
399
|
+
In strict mode, `dittoly` does the same work as `util.isDeepStrictEqual` and is about 1.5x faster.
|
|
400
|
+
|
|
401
|
+
Circular reference checks cost nothing on normal data. `dittoly` only starts tracking visited objects when a comparison goes more than 100 levels deep.
|
|
402
|
+
|
|
403
|
+
## Migrating from other packages
|
|
404
|
+
|
|
405
|
+
### From fast-deep-equal or dequal
|
|
406
|
+
|
|
407
|
+
```js
|
|
408
|
+
// Before
|
|
409
|
+
import equal from 'fast-deep-equal' // or: import { dequal as equal } from 'dequal'
|
|
410
|
+
|
|
411
|
+
// After
|
|
412
|
+
import { equal } from 'dittoly'
|
|
413
|
+
|
|
414
|
+
equal(a, b)
|
|
415
|
+
```
|
|
416
|
+
|
|
417
|
+
You get the same answers for JSON data. This was checked on 200,000 random values against both packages and `lodash.isequal`. `dittoly` also compares Maps, Sets, buffers, Errors and circular references correctly.
|
|
418
|
+
|
|
419
|
+
Like both packages, `dittoly` compares prototypes, so `new Point(1, 2)` does not equal `{ x: 1, y: 2 }`. Pass `{ prototype: false }` to allow it.
|
|
420
|
+
|
|
421
|
+
### From lodash.isequal
|
|
422
|
+
|
|
423
|
+
```js
|
|
424
|
+
// Before
|
|
425
|
+
import { isEqual, isEqualWith } from 'lodash'
|
|
426
|
+
isEqual(a, b)
|
|
427
|
+
isEqualWith(a, b, fn)
|
|
428
|
+
|
|
429
|
+
// After
|
|
430
|
+
import { isEqual } from 'dittoly'
|
|
431
|
+
isEqual(a, b)
|
|
432
|
+
isEqual(a, b, { customizer: (x, y) => fn(x, y) })
|
|
433
|
+
```
|
|
434
|
+
|
|
435
|
+
The `dittoly` customizer gets the two values only. lodash compares symbol keys, so add `{ symbols: true }` to do the same.
|
|
436
|
+
|
|
437
|
+
### From util.isDeepStrictEqual
|
|
438
|
+
|
|
439
|
+
```js
|
|
440
|
+
// Before
|
|
441
|
+
import { isDeepStrictEqual } from 'node:util'
|
|
442
|
+
isDeepStrictEqual(a, b)
|
|
443
|
+
|
|
444
|
+
// After
|
|
445
|
+
import { equal } from 'dittoly'
|
|
446
|
+
equal(a, b, { strict: true })
|
|
447
|
+
```
|
|
448
|
+
|
|
449
|
+
The test suite checks that the two agree on thousands of random values. One difference: `dittoly` says two different URLs, Promises or WeakMaps are not equal.
|
|
450
|
+
|
|
451
|
+
### From Jest or Vitest `toEqual`
|
|
452
|
+
|
|
453
|
+
To compare like `toEqual` (ignore class types and `undefined` properties), use these options:
|
|
454
|
+
|
|
455
|
+
```js
|
|
456
|
+
// Before
|
|
457
|
+
expect(actual).toEqual(expected)
|
|
458
|
+
|
|
459
|
+
// After
|
|
460
|
+
import { assertEqual } from 'dittoly'
|
|
461
|
+
assertEqual(actual, expected, { prototype: false, ignoreUndefined: true })
|
|
462
|
+
```
|
|
463
|
+
|
|
464
|
+
## Notes and limitations
|
|
465
|
+
|
|
466
|
+
- Private class fields (`#x`) and closures cannot be read, so instances that differ only there compare as equal. Use a `customizer` for such classes.
|
|
467
|
+
- Very deep nesting (thousands of levels) can overflow the call stack, as with the other libraries.
|
|
468
|
+
- `customizer` and `ignoreKeys` functions may run more than once for the same values on very deep or circular data.
|
|
469
|
+
- `diff()` compares arrays index by index, so an inserted item shows up as several changes, not as a move.
|
|
470
|
+
- `assertEqual()` shows up to 20 differences in its message. The full list is in `error.diffs`.
|
|
471
|
+
|
|
472
|
+
## Requirements
|
|
473
|
+
|
|
474
|
+
- Node.js 20 or later. Also works in Bun, Deno and browsers.
|
|
475
|
+
- ESM and CommonJS.
|
|
476
|
+
- TypeScript types included.
|
|
477
|
+
- Zero dependencies.
|
|
478
|
+
|
|
16
479
|
## License
|
|
17
480
|
|
|
18
481
|
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
"use strict";var R=Object.defineProperty;var pe=Object.getOwnPropertyDescriptor;var ye=Object.getOwnPropertyNames;var de=Object.prototype.hasOwnProperty;var u=(n,o)=>R(n,"name",{value:o,configurable:!0});var ge=(n,o)=>{for(var r in o)R(n,r,{get:o[r],enumerable:!0})},me=(n,o,r,c)=>{if(o&&typeof o=="object"||typeof o=="function")for(let p of ye(o))!de.call(n,p)&&p!==r&&R(n,p,{get:()=>o[p],enumerable:!(c=pe(o,p))||c.enumerable});return n};var ke=n=>me(R({},"__esModule",{value:!0}),n);var Ke={};ge(Ke,{NotEqualError:()=>U,assertEqual:()=>_,createEqual:()=>ae,default:()=>$e,diff:()=>V,equal:()=>Q,formatPath:()=>J,isEqual:()=>ce});module.exports=ke(Ke);function M(n){if(n!==void 0&&(n===null||typeof n!="object"))throw new TypeError(`dittoly: options must be an object, got ${n===null?"null":typeof n}`);let o=n??{},r=o.strict===!0,{ignoreKeys:c,customizer:p}=o,g;if(typeof c=="function")g=c;else if(c!=null){if(typeof c[Symbol.iterator]!="function")throw new TypeError("dittoly: ignoreKeys must be an array of keys or a function");let S=new Set;for(let j of c)S.add(typeof j=="symbol"?j:String(j));S.size&&(g=u(j=>S.has(j),"skipKey"))}if(p!=null&&typeof p!="function")throw new TypeError("dittoly: customizer must be a function");return{strict:r,proto:o.prototype===!1?0:r?2:1,signedZero:o.signedZero??r,nanEqual:o.nanEqual??!0,symbols:o.symbols??r,ignoreUndefined:o.ignoreUndefined??!1,skipKey:g,customizer:p??void 0}}u(M,"resolve");var W=Object.prototype,he=Array.prototype,we=Map.prototype,je=Set.prototype,C=Object.getPrototypeOf,I=u(n=>W.toString.call(n),"toTag"),w=Object.keys,X=u((n,o)=>W.propertyIsEnumerable.call(n,o),"isEnum"),$=Array.isArray,be=Object.getOwnPropertySymbols,K=u(n=>n===null||n===W||C(n)===null,"isPlainProto"),re=u((n,o,r)=>n===o||r===0||r===1&&(K(n)&&K(o)||$(n)&&$(o)),"protoMatch"),Z=u(n=>{let o=be(n);return o.length?o.filter(r=>X(n,r)):o},"enumSymbols");function ie(n,o,r){let c=C(n),p=C(o);if(!re(c,p,r.proto))return null;let g=$(n);if(g!==$(o))return null;if(g)return"array";if(K(c)&&K(p))return"object";let S=I(n);return S!==I(o)?null:S==="[object Object]"?"object":S}u(ie,"classify");var Se=new Set(["[object Promise]","[object WeakMap]","[object WeakSet]","[object WeakRef]","[object FinalizationRegistry]","[object Blob]","[object File]","[object Generator]","[object AsyncGenerator]","[object ReadableStream]","[object WritableStream]"]),Ee=new Set(["[object Float16Array]","[object Float32Array]","[object Float64Array]"]),xe={"[object Number]":u(n=>Number.prototype.valueOf.call(n),"[object Number]"),"[object String]":u(n=>String.prototype.valueOf.call(n),"[object String]"),"[object Boolean]":u(n=>Boolean.prototype.valueOf.call(n),"[object Boolean]"),"[object BigInt]":u(n=>BigInt.prototype.valueOf.call(n),"[object BigInt]"),"[object Symbol]":u(n=>Symbol.prototype.valueOf.call(n),"[object Symbol]")},ee=Date.prototype.getTime,ne=u((n,o)=>{let r=n.length;if(r!==o.length)return!1;for(let c=0;c<r;c++)if(n[c]!==o[c])return!1;return!0},"bytesEqual"),Oe=100;function te(n){let{strict:o,proto:r,signedZero:c,nanEqual:p,symbols:g,ignoreUndefined:S,skipKey:j,customizer:A}=n,T=S||j!==void 0,F=A===void 0,D=u((e,t)=>e===t?!c||e!==0||1/e===1/t:p&&e!==e&&t!==t,"prim"),q=!1;function E(e,t,i){if(A!==void 0){let y=A(e,t);if(y!==void 0)return!!y}if(e===t)return!c||e!==0||1/e===1/t;if(typeof e!="object"||typeof t!="object"||e===null||t===null)return p&&e!==e&&t!==t;if(typeof i=="number")return i<Oe?N(e,t,i+1):(q=!0,!1);let s=i.get(e);if(s===void 0)i.set(e,s=[t]);else{if(s.includes(t))return!0;s.push(t)}let a=N(e,t,i);return s.length===1?i.delete(e):s.pop(),a}u(E,"eq");function N(e,t,i){let s=C(e),a=C(t);if(s===a){if(s===W||s===null)return x(e,t,w(e),w(t),0,i);if(s===he&&$(e)&&$(t))return L(e,t,i);if(s===we)return b(e,t,i)&&(!o||x(e,t,w(e),w(t),0,i));if(s===je)return k(e,t,i)&&(!o||x(e,t,w(e),w(t),0,i))}else if(!re(s,a,r))return!1;let y=$(e);if(y!==$(t))return!1;if(y)return L(e,t,i);if(K(s)&&K(a))return x(e,t,w(e),w(t),0,i);let h=I(e);return h!==I(t)?!1:f(e,t,h,i)}u(N,"deep");function L(e,t,i){let s=e.length;if(s!==t.length)return!1;if(!o){for(let d=0;d<s;d++)if(!E(e[d],t[d],i))return!1;return!0}let a=w(e),y=w(t);if(!T&&a.length!==y.length)return!1;let h=0;for(let d=0;d<s;d++)if(d in e){if(!(d in t)||!E(e[d],t[d],i))return!1;h++}else if(d in t)return!1;return x(e,t,a,y,h,i)}u(L,"arrays");function x(e,t,i,s,a,y){return l(e,t,i,s,a,y)?!g||l(e,t,Z(e),Z(t),0,y):!1}u(x,"props");function l(e,t,i,s,a,y){if(!T){if(i.length!==s.length)return!1;for(let d=a;d<i.length;d++){let P=i[d];if(P!==s[d]&&!X(t,P)||!E(e[P],t[P],y))return!1}return!0}let h=0;for(let d=a;d<i.length;d++){let P=i[d];if(j!==void 0&&j(P,e))continue;let Y=e[P];if(S&&Y===void 0)continue;if(h++,!X(t,P)||j!==void 0&&j(P,t))return!1;let v=t[P];if(S&&v===void 0||!E(Y,v,y))return!1}for(let d=a;d<s.length;d++){let P=s[d];j!==void 0&&j(P,t)||S&&t[P]===void 0||h--}return h===0}u(l,"keyList");function f(e,t,i,s){switch(i){case"[object Object]":return x(e,t,w(e),w(t),0,s);case"[object Date]":{let a=ee.call(e),y=ee.call(t);if(a!==y&&!(a!==a&&y!==y))return!1;break}case"[object RegExp]":if(e.source!==t.source||e.flags!==t.flags||o&&e.lastIndex!==t.lastIndex)return!1;break;case"[object Map]":if(!b(e,t,s))return!1;break;case"[object Set]":if(!k(e,t,s))return!1;break;case"[object Error]":return O(e,t,s);case"[object ArrayBuffer]":case"[object SharedArrayBuffer]":return ne(new Uint8Array(e),new Uint8Array(t));case"[object DataView]":return ne(new Uint8Array(e.buffer,e.byteOffset,e.byteLength),new Uint8Array(t.buffer,t.byteOffset,t.byteLength));case"[object URL]":if(e.href!==t.href)return!1;break;case"[object URLSearchParams]":if(String(e)!==String(t))return!1;break;default:{let a=xe[i];if(a!==void 0){if(!D(a(e),a(t)))return!1;break}if(ArrayBuffer.isView(e))return m(e,t,Ee.has(i));if(Se.has(i))return!1;if(i.startsWith("[object Temporal.")){if(String(e)!==String(t))return!1;break}return e instanceof Error?O(e,t,s):x(e,t,w(e),w(t),0,s)}}return!o||x(e,t,w(e),w(t),0,s)}u(f,"special");function m(e,t,i){let s=e.length;if(s!==t.length)return!1;if(i){for(let a=0;a<s;a++)if(!D(e[a],t[a]))return!1}else for(let a=0;a<s;a++)if(e[a]!==t[a])return!1;return!0}u(m,"typed");function O(e,t,i){if(e.name!==t.name||e.message!==t.message)return!1;let s="cause"in e;return s!=="cause"in t||s&&!E(e.cause,t.cause,i)||("errors"in e||"errors"in t)&&!E(e.errors,t.errors,i)?!1:x(e,t,w(e),w(t),0,i)}u(O,"errors");function b(e,t,i){if(e.size!==t.size)return!1;let s;for(let y of e){let h=y[0];if(t.has(h)){if(!E(y[1],t.get(h),i))return!1}else{if(F&&(typeof h!="object"||h===null))return!1;(s??(s=[])).push(h)}}if(s===void 0)return!0;let a=[];for(let y of t.keys())e.has(y)||a.push(y);e:for(let y of s){for(let h=0;h<a.length;h++){let d=a[h];if(E(y,d,i)&&E(e.get(y),t.get(d),i)){a.splice(h,1);continue e}}return!1}return!0}u(b,"maps");function k(e,t,i){if(e.size!==t.size)return!1;let s;for(let y of e)if(!t.has(y)){if(F&&(typeof y!="object"||y===null))return!1;(s??(s=[])).push(y)}if(s===void 0)return!0;let a=[];for(let y of t)e.has(y)||a.push(y);e:for(let y of s){for(let h=0;h<a.length;h++)if(E(y,a[h],i)){a.splice(h,1);continue e}return!1}return!0}return u(k,"sets"),(e,t)=>{if(A===void 0&&(e===t||typeof e!="object"||typeof t!="object"||e===null||t===null))return E(e,t,0);let i=q;q=!1;let s=E(e,t,0);return q&&(s=E(e,t,new Map)),q=i,s}}u(te,"compile");var oe=new Map;function z(n){if(n.customizer!==void 0||n.skipKey!==void 0)return te(n);let o=+n.strict|+n.signedZero<<1|+n.nanEqual<<2|+n.symbols<<3|+n.ignoreUndefined<<4|n.proto<<5,r=oe.get(o);return r===void 0&&oe.set(o,r=te(n)),r}u(z,"comparatorFor");var se=u(n=>typeof n=="object"&&n!==null,"isObject"),Pe=u(n=>String(+n>>>0)===n&&n!=="4294967295","isIndex");function V(n,o,r){let c=M(r);return ue(n,o,c,z(c))}u(V,"diff");function ue(n,o,r,c){let p=[],g=[],{strict:S,customizer:j,skipKey:A,ignoreUndefined:T,symbols:F}=r,D=u((l,f,m)=>p.push({kind:"changed",path:l,a:f,b:m}),"changed"),q=u((l,f)=>(F&&(f=f.concat(Z(l))),A===void 0&&!T?f:f.filter(m=>!(A!==void 0&&A(m,l))&&!(T&&l[m]===void 0))),"ownKeys"),E=u((l,f,m,O,b)=>{let k=new Set(O);for(let e of m)k.has(e)?(k.delete(e),x(l[e],f[e],[...b,e])):p.push({kind:"removed",path:[...b,e],a:l[e]});for(let e of k)p.push({kind:"added",path:[...b,e],b:f[e]})},"walkKeys"),N=u((l,f,m)=>{let O=p.length,b=Math.max(l.length,f.length);for(let k=0;k<b;k++){let e=S?k in l:k<l.length,t=S?k in f:k<f.length;e&&t?x(l[k],f[k],[...m,k]):e?p.push({kind:"removed",path:[...m,k],a:l[k]}):t&&p.push({kind:"added",path:[...m,k],b:f[k]})}if(S){l.length!==f.length&&p.length===O&&D([...m,"length"],l.length,f.length);let k=u(e=>q(e,w(e).filter(t=>!Pe(t))),"named");E(l,f,k(l),k(f),m)}},"walkArray"),L=u((l,f,m)=>{let O=[];for(let[e,t]of l)f.has(e)?x(t,f.get(e),[...m,e]):O.push(e);let b=[];for(let e of f.keys())l.has(e)||b.push(e);let k=[];for(let e of O){let t=b.findIndex(i=>c(e,i)&&c(l.get(e),f.get(i)));t===-1?k.push(e):b.splice(t,1)}for(let e of k){let t=b.findIndex(i=>c(e,i));t===-1?p.push({kind:"removed",path:[...m,e],a:l.get(e)}):x(l.get(e),f.get(b.splice(t,1)[0]),[...m,e])}for(let e of b)p.push({kind:"added",path:[...m,e],b:f.get(e)})},"walkMap");function x(l,f,m){if(se(l)&&se(f)&&l!==f){let O=ie(l,f,r);if(O==="array"||O==="object"||O==="[object Map]"){if(j!==void 0){let b=j(l,f);if(b!==void 0){b||D(m,l,f);return}}for(let b=g.length-2;b>=0;b-=2)if(g[b]===l&&g[b+1]===f)return;g.push(l,f),O==="array"?N(l,f,m):O==="object"?E(l,f,q(l,w(l)),q(f,w(f)),m):(L(l,f,m),S&&E(l,f,q(l,w(l)),q(f,w(f)),m)),g.length-=2;return}}c(l,f)||D(m,l,f)}return u(x,"walk"),x(n,o,[]),p}u(ue,"walkAll");var fe=/^[A-Za-z_$][\w$]*$/;function J(n){let o="";for(let r of n)typeof r=="number"?o+=`[${r}]`:typeof r=="string"?o+=fe.test(r)?(o?".":"")+r:`[${JSON.stringify(r)}]`:o+=`[${B(r)}]`;return o}u(J,"formatPath");function B(n,o=!1){try{switch(typeof n){case"string":return JSON.stringify(n.length>60?n.slice(0,57)+"...":n);case"number":return Object.is(n,-0)?"-0":String(n);case"bigint":return`${n}n`;case"symbol":return n.toString();case"function":return n.name?`[Function: ${n.name}]`:"[Function]";case"object":break;default:return String(n)}if(n===null)return"null";if(n instanceof Date)return isNaN(n.getTime())?"Invalid Date":n.toISOString();if(n instanceof RegExp)return String(n);if(n instanceof Error)return`${n.name}: ${n.message}`;if(n instanceof Map)return`Map(${n.size})`;if(n instanceof Set)return`Set(${n.size})`;if(Array.isArray(n)){if(o)return n.length?"[...]":"[]";let j=n.slice(0,5).map(A=>B(A,!0));return n.length>5&&j.push(`... ${n.length-5} more`),`[${j.join(", ")}]`}let r=C(n),c=K(r)?"":r.constructor?.name||I(n).slice(8,-1),p=c?c+" ":"",g=w(n);if(o||g.length===0)return p+(g.length?"{...}":"{}");let S=g.slice(0,5).map(j=>`${fe.test(j)?j:JSON.stringify(j)}: ${B(n[j],!0)}`);return g.length>5&&S.push(`... ${g.length-5} more`),`${p}{ ${S.join(", ")} }`}catch{return"[unprintable]"}}u(B,"show");var G=20;function qe(n){let o=n.slice(0,G).map(c=>{let p=J(c.path)||"(root)",g=c.kind==="added"?"(missing)":B(c.a),S=c.kind==="removed"?"(missing)":B(c.b);return` ${p}: got ${g}, expected ${S}`});return n.length>G&&o.push(` ... and ${n.length-G} more`),`Expected values to be deeply equal (${n.length===1?"1 difference":`${n.length} differences`}):
|
|
2
|
+
${o.join(`
|
|
3
|
+
`)}`}u(qe,"describe");var H=class H extends Error{constructor(r,c,p,g){super(g??qe(p));this.name="NotEqualError";this.diffs=p,this.actual=r,this.expected=c}};u(H,"NotEqualError");var U=H;function _(n,o,r){let c=M(r),p=z(c);if(p(n,o))return;let g=ue(n,o,c,p);throw new U(n,o,g,r?.message)}u(_,"assertEqual");var le=z(M(void 0));function Q(n,o,r){return r===void 0?n===o||le(n,o):z(M(r))(n,o)}u(Q,"equal");var ce=Q;function ae(n){return z(M(n))}u(ae,"createEqual");var Ae=Object.assign(u(function(o,r,c){return c===void 0?o===r||le(o,r):z(M(c))(o,r)},"dittoly"),{equal:Q,isEqual:ce,createEqual:ae,diff:V,assertEqual:_,formatPath:J,NotEqualError:U}),$e=Ae;0&&(module.exports={NotEqualError,assertEqual,createEqual,diff,equal,formatPath,isEqual});
|
|
4
|
+
var __d=module.exports.default;for(var __k in module.exports)if(!(__k in __d))__d[__k]=module.exports[__k];__d.default=__d;module.exports=__d;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The comparison engine. `compile(config)` builds a comparator with the options baked in,
|
|
3
|
+
* so the hot path never re-reads option objects.
|
|
4
|
+
*/
|
|
5
|
+
/** Decide a comparison yourself: return `true` or `false`, or `undefined` to fall back to the built-in rules. */
|
|
6
|
+
type Customizer = (a: unknown, b: unknown) => boolean | undefined | void;
|
|
7
|
+
/** Keys to skip on objects: a list, or a function that returns `true` for keys to ignore. */
|
|
8
|
+
type IgnoreKeys = readonly PropertyKey[] | ((key: string | symbol, object: object) => boolean);
|
|
9
|
+
interface EqualOptions {
|
|
10
|
+
/**
|
|
11
|
+
* Strict mode, matching Node's `util.isDeepStrictEqual`: exact prototypes (a plain object is not equal
|
|
12
|
+
* to a null-prototype object), `+0` and `-0` differ, symbol keys are compared, array holes differ from
|
|
13
|
+
* `undefined`, and extra properties on arrays, Dates, Maps, Sets etc. are compared. Default `false`.
|
|
14
|
+
* The options below override the defaults it picks.
|
|
15
|
+
*/
|
|
16
|
+
strict?: boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Compare prototypes, so `new Point(1, 2)` is not equal to `{ x: 1, y: 2 }`. Default `true`.
|
|
19
|
+
* Outside strict mode plain objects, null-prototype objects and objects from other realms count as the same.
|
|
20
|
+
* Set `false` to compare class instances by their properties only.
|
|
21
|
+
*/
|
|
22
|
+
prototype?: boolean;
|
|
23
|
+
/** `+0` and `-0` are different. Default: same as `strict`. */
|
|
24
|
+
signedZero?: boolean;
|
|
25
|
+
/** `NaN` equals `NaN`. Default `true`. */
|
|
26
|
+
nanEqual?: boolean;
|
|
27
|
+
/** Compare own enumerable symbol keys. Default: same as `strict`. */
|
|
28
|
+
symbols?: boolean;
|
|
29
|
+
/** Treat a property whose value is `undefined` like a missing property: `{ a: undefined }` equals `{}`. Default `false`. */
|
|
30
|
+
ignoreUndefined?: boolean;
|
|
31
|
+
/** Object keys to skip at any depth: `['updatedAt']`, or `(key, object) => key.startsWith('_')`. */
|
|
32
|
+
ignoreKeys?: IgnoreKeys;
|
|
33
|
+
/** Called for every pair of values before the built-in rules. Return a boolean to decide, `undefined` to continue. */
|
|
34
|
+
customizer?: Customizer;
|
|
35
|
+
}
|
|
36
|
+
type Comparator = (a: unknown, b: unknown) => boolean;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Where a difference is: object keys (strings or symbols), array indices (numbers) and Map keys (any value).
|
|
40
|
+
* `[]` is the root value.
|
|
41
|
+
*/
|
|
42
|
+
type Path = unknown[];
|
|
43
|
+
/** One difference between `a` and `b`. */
|
|
44
|
+
type Difference =
|
|
45
|
+
/** The value at `path` is different. */
|
|
46
|
+
{
|
|
47
|
+
kind: 'changed';
|
|
48
|
+
path: Path;
|
|
49
|
+
a: unknown;
|
|
50
|
+
b: unknown;
|
|
51
|
+
}
|
|
52
|
+
/** `a` has a value at `path` that `b` doesn't. */
|
|
53
|
+
| {
|
|
54
|
+
kind: 'removed';
|
|
55
|
+
path: Path;
|
|
56
|
+
a: unknown;
|
|
57
|
+
}
|
|
58
|
+
/** `b` has a value at `path` that `a` doesn't. */
|
|
59
|
+
| {
|
|
60
|
+
kind: 'added';
|
|
61
|
+
path: Path;
|
|
62
|
+
b: unknown;
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* List the differences between `a` and `b`, with paths. Returns `[]` exactly when `equal(a, b, options)` is `true`.
|
|
66
|
+
*
|
|
67
|
+
* Objects, arrays and Maps are walked key by key; anything else that differs (primitives, Dates, Sets,
|
|
68
|
+
* typed arrays, Errors, ...) is reported as one `'changed'` entry at its path.
|
|
69
|
+
*/
|
|
70
|
+
declare function diff(a: unknown, b: unknown, options?: EqualOptions): Difference[];
|
|
71
|
+
/**
|
|
72
|
+
* Format a path for humans: `['user', 'tags', 1]` => `'user.tags[1]'`. The root path `[]` is `''`.
|
|
73
|
+
*/
|
|
74
|
+
declare function formatPath(path: readonly unknown[]): string;
|
|
75
|
+
/** Thrown by `assertEqual`. The message lists the differences; `diffs` has them as data. */
|
|
76
|
+
declare class NotEqualError extends Error {
|
|
77
|
+
name: string;
|
|
78
|
+
/** The differences, as returned by `diff(actual, expected)`. */
|
|
79
|
+
readonly diffs: Difference[];
|
|
80
|
+
readonly actual: unknown;
|
|
81
|
+
readonly expected: unknown;
|
|
82
|
+
constructor(actual: unknown, expected: unknown, diffs: Difference[], message?: string);
|
|
83
|
+
}
|
|
84
|
+
interface AssertOptions extends EqualOptions {
|
|
85
|
+
/** Use this message instead of the generated list of differences. */
|
|
86
|
+
message?: string;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Throw a `NotEqualError` listing the differences unless `actual` deeply equals `expected`.
|
|
90
|
+
* In TypeScript, `actual` has the type of `expected` afterwards.
|
|
91
|
+
*/
|
|
92
|
+
declare function assertEqual<T>(actual: unknown, expected: T, options?: AssertOptions): asserts actual is T;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* dittoly — fast deep equality for objects, arrays, Maps, Sets, Dates and more.
|
|
96
|
+
*
|
|
97
|
+
* dittoly({ a: [1, 2] }, { a: [1, 2] }) // true
|
|
98
|
+
* dittoly(new Map([[1, {}]]), new Map([[1, {}]])) // true
|
|
99
|
+
* diff({ a: 1 }, { a: 2 }) // [{ kind: 'changed', path: ['a'], a: 1, b: 2 }]
|
|
100
|
+
*/
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* `true` if `a` and `b` are deeply equal.
|
|
104
|
+
*
|
|
105
|
+
* Compares primitives (`NaN` equals `NaN`), plain objects, class instances, arrays, Maps, Sets, Dates,
|
|
106
|
+
* RegExps, typed arrays, ArrayBuffers, DataViews, Errors, boxed primitives and URLs. Handles circular references.
|
|
107
|
+
*/
|
|
108
|
+
declare function equal(a: unknown, b: unknown, options?: EqualOptions): boolean;
|
|
109
|
+
/** Alias of `equal`, for code coming from lodash. */
|
|
110
|
+
declare const isEqual: typeof equal;
|
|
111
|
+
/**
|
|
112
|
+
* Build a comparator with the options fixed. Faster than passing options on every call when you
|
|
113
|
+
* use `customizer` or `ignoreKeys`.
|
|
114
|
+
*
|
|
115
|
+
* const sameUser = createEqual({ ignoreKeys: ['updatedAt'] })
|
|
116
|
+
* sameUser(a, b)
|
|
117
|
+
*/
|
|
118
|
+
declare function createEqual(options?: EqualOptions): Comparator;
|
|
119
|
+
interface Dittoly {
|
|
120
|
+
/** `true` if `a` and `b` are deeply equal. */
|
|
121
|
+
(a: unknown, b: unknown, options?: EqualOptions): boolean;
|
|
122
|
+
equal: typeof equal;
|
|
123
|
+
isEqual: typeof equal;
|
|
124
|
+
createEqual: typeof createEqual;
|
|
125
|
+
diff: typeof diff;
|
|
126
|
+
assertEqual: typeof assertEqual;
|
|
127
|
+
formatPath: typeof formatPath;
|
|
128
|
+
NotEqualError: typeof NotEqualError;
|
|
129
|
+
}
|
|
130
|
+
declare const dittoly: Dittoly;
|
|
131
|
+
|
|
132
|
+
export { type AssertOptions, type Comparator, type Customizer, type Difference, type Dittoly, type EqualOptions, type IgnoreKeys, NotEqualError, type Path, assertEqual, createEqual, dittoly as default, diff, equal, formatPath, isEqual };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The comparison engine. `compile(config)` builds a comparator with the options baked in,
|
|
3
|
+
* so the hot path never re-reads option objects.
|
|
4
|
+
*/
|
|
5
|
+
/** Decide a comparison yourself: return `true` or `false`, or `undefined` to fall back to the built-in rules. */
|
|
6
|
+
type Customizer = (a: unknown, b: unknown) => boolean | undefined | void;
|
|
7
|
+
/** Keys to skip on objects: a list, or a function that returns `true` for keys to ignore. */
|
|
8
|
+
type IgnoreKeys = readonly PropertyKey[] | ((key: string | symbol, object: object) => boolean);
|
|
9
|
+
interface EqualOptions {
|
|
10
|
+
/**
|
|
11
|
+
* Strict mode, matching Node's `util.isDeepStrictEqual`: exact prototypes (a plain object is not equal
|
|
12
|
+
* to a null-prototype object), `+0` and `-0` differ, symbol keys are compared, array holes differ from
|
|
13
|
+
* `undefined`, and extra properties on arrays, Dates, Maps, Sets etc. are compared. Default `false`.
|
|
14
|
+
* The options below override the defaults it picks.
|
|
15
|
+
*/
|
|
16
|
+
strict?: boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Compare prototypes, so `new Point(1, 2)` is not equal to `{ x: 1, y: 2 }`. Default `true`.
|
|
19
|
+
* Outside strict mode plain objects, null-prototype objects and objects from other realms count as the same.
|
|
20
|
+
* Set `false` to compare class instances by their properties only.
|
|
21
|
+
*/
|
|
22
|
+
prototype?: boolean;
|
|
23
|
+
/** `+0` and `-0` are different. Default: same as `strict`. */
|
|
24
|
+
signedZero?: boolean;
|
|
25
|
+
/** `NaN` equals `NaN`. Default `true`. */
|
|
26
|
+
nanEqual?: boolean;
|
|
27
|
+
/** Compare own enumerable symbol keys. Default: same as `strict`. */
|
|
28
|
+
symbols?: boolean;
|
|
29
|
+
/** Treat a property whose value is `undefined` like a missing property: `{ a: undefined }` equals `{}`. Default `false`. */
|
|
30
|
+
ignoreUndefined?: boolean;
|
|
31
|
+
/** Object keys to skip at any depth: `['updatedAt']`, or `(key, object) => key.startsWith('_')`. */
|
|
32
|
+
ignoreKeys?: IgnoreKeys;
|
|
33
|
+
/** Called for every pair of values before the built-in rules. Return a boolean to decide, `undefined` to continue. */
|
|
34
|
+
customizer?: Customizer;
|
|
35
|
+
}
|
|
36
|
+
type Comparator = (a: unknown, b: unknown) => boolean;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Where a difference is: object keys (strings or symbols), array indices (numbers) and Map keys (any value).
|
|
40
|
+
* `[]` is the root value.
|
|
41
|
+
*/
|
|
42
|
+
type Path = unknown[];
|
|
43
|
+
/** One difference between `a` and `b`. */
|
|
44
|
+
type Difference =
|
|
45
|
+
/** The value at `path` is different. */
|
|
46
|
+
{
|
|
47
|
+
kind: 'changed';
|
|
48
|
+
path: Path;
|
|
49
|
+
a: unknown;
|
|
50
|
+
b: unknown;
|
|
51
|
+
}
|
|
52
|
+
/** `a` has a value at `path` that `b` doesn't. */
|
|
53
|
+
| {
|
|
54
|
+
kind: 'removed';
|
|
55
|
+
path: Path;
|
|
56
|
+
a: unknown;
|
|
57
|
+
}
|
|
58
|
+
/** `b` has a value at `path` that `a` doesn't. */
|
|
59
|
+
| {
|
|
60
|
+
kind: 'added';
|
|
61
|
+
path: Path;
|
|
62
|
+
b: unknown;
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* List the differences between `a` and `b`, with paths. Returns `[]` exactly when `equal(a, b, options)` is `true`.
|
|
66
|
+
*
|
|
67
|
+
* Objects, arrays and Maps are walked key by key; anything else that differs (primitives, Dates, Sets,
|
|
68
|
+
* typed arrays, Errors, ...) is reported as one `'changed'` entry at its path.
|
|
69
|
+
*/
|
|
70
|
+
declare function diff(a: unknown, b: unknown, options?: EqualOptions): Difference[];
|
|
71
|
+
/**
|
|
72
|
+
* Format a path for humans: `['user', 'tags', 1]` => `'user.tags[1]'`. The root path `[]` is `''`.
|
|
73
|
+
*/
|
|
74
|
+
declare function formatPath(path: readonly unknown[]): string;
|
|
75
|
+
/** Thrown by `assertEqual`. The message lists the differences; `diffs` has them as data. */
|
|
76
|
+
declare class NotEqualError extends Error {
|
|
77
|
+
name: string;
|
|
78
|
+
/** The differences, as returned by `diff(actual, expected)`. */
|
|
79
|
+
readonly diffs: Difference[];
|
|
80
|
+
readonly actual: unknown;
|
|
81
|
+
readonly expected: unknown;
|
|
82
|
+
constructor(actual: unknown, expected: unknown, diffs: Difference[], message?: string);
|
|
83
|
+
}
|
|
84
|
+
interface AssertOptions extends EqualOptions {
|
|
85
|
+
/** Use this message instead of the generated list of differences. */
|
|
86
|
+
message?: string;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Throw a `NotEqualError` listing the differences unless `actual` deeply equals `expected`.
|
|
90
|
+
* In TypeScript, `actual` has the type of `expected` afterwards.
|
|
91
|
+
*/
|
|
92
|
+
declare function assertEqual<T>(actual: unknown, expected: T, options?: AssertOptions): asserts actual is T;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* dittoly — fast deep equality for objects, arrays, Maps, Sets, Dates and more.
|
|
96
|
+
*
|
|
97
|
+
* dittoly({ a: [1, 2] }, { a: [1, 2] }) // true
|
|
98
|
+
* dittoly(new Map([[1, {}]]), new Map([[1, {}]])) // true
|
|
99
|
+
* diff({ a: 1 }, { a: 2 }) // [{ kind: 'changed', path: ['a'], a: 1, b: 2 }]
|
|
100
|
+
*/
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* `true` if `a` and `b` are deeply equal.
|
|
104
|
+
*
|
|
105
|
+
* Compares primitives (`NaN` equals `NaN`), plain objects, class instances, arrays, Maps, Sets, Dates,
|
|
106
|
+
* RegExps, typed arrays, ArrayBuffers, DataViews, Errors, boxed primitives and URLs. Handles circular references.
|
|
107
|
+
*/
|
|
108
|
+
declare function equal(a: unknown, b: unknown, options?: EqualOptions): boolean;
|
|
109
|
+
/** Alias of `equal`, for code coming from lodash. */
|
|
110
|
+
declare const isEqual: typeof equal;
|
|
111
|
+
/**
|
|
112
|
+
* Build a comparator with the options fixed. Faster than passing options on every call when you
|
|
113
|
+
* use `customizer` or `ignoreKeys`.
|
|
114
|
+
*
|
|
115
|
+
* const sameUser = createEqual({ ignoreKeys: ['updatedAt'] })
|
|
116
|
+
* sameUser(a, b)
|
|
117
|
+
*/
|
|
118
|
+
declare function createEqual(options?: EqualOptions): Comparator;
|
|
119
|
+
interface Dittoly {
|
|
120
|
+
/** `true` if `a` and `b` are deeply equal. */
|
|
121
|
+
(a: unknown, b: unknown, options?: EqualOptions): boolean;
|
|
122
|
+
equal: typeof equal;
|
|
123
|
+
isEqual: typeof equal;
|
|
124
|
+
createEqual: typeof createEqual;
|
|
125
|
+
diff: typeof diff;
|
|
126
|
+
assertEqual: typeof assertEqual;
|
|
127
|
+
formatPath: typeof formatPath;
|
|
128
|
+
NotEqualError: typeof NotEqualError;
|
|
129
|
+
}
|
|
130
|
+
declare const dittoly: Dittoly;
|
|
131
|
+
|
|
132
|
+
export { type AssertOptions, type Comparator, type Customizer, type Difference, type Dittoly, type EqualOptions, type IgnoreKeys, NotEqualError, type Path, assertEqual, createEqual, dittoly as default, diff, equal, formatPath, isEqual };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
var le=Object.defineProperty;var u=(t,o)=>le(t,"name",{value:o,configurable:!0});function M(t){if(t!==void 0&&(t===null||typeof t!="object"))throw new TypeError(`dittoly: options must be an object, got ${t===null?"null":typeof t}`);let o=t??{},s=o.strict===!0,{ignoreKeys:a,customizer:y}=o,g;if(typeof a=="function")g=a;else if(a!=null){if(typeof a[Symbol.iterator]!="function")throw new TypeError("dittoly: ignoreKeys must be an array of keys or a function");let S=new Set;for(let j of a)S.add(typeof j=="symbol"?j:String(j));S.size&&(g=u(j=>S.has(j),"skipKey"))}if(y!=null&&typeof y!="function")throw new TypeError("dittoly: customizer must be a function");return{strict:s,proto:o.prototype===!1?0:s?2:1,signedZero:o.signedZero??s,nanEqual:o.nanEqual??!0,symbols:o.symbols??s,ignoreUndefined:o.ignoreUndefined??!1,skipKey:g,customizer:y??void 0}}u(M,"resolve");var Z=Object.prototype,ce=Array.prototype,ae=Map.prototype,pe=Set.prototype,C=Object.getPrototypeOf,I=u(t=>Z.toString.call(t),"toTag"),w=Object.keys,W=u((t,o)=>Z.propertyIsEnumerable.call(t,o),"isEnum"),$=Array.isArray,ye=Object.getOwnPropertySymbols,K=u(t=>t===null||t===Z||C(t)===null,"isPlainProto"),ee=u((t,o,s)=>t===o||s===0||s===1&&(K(t)&&K(o)||$(t)&&$(o)),"protoMatch"),R=u(t=>{let o=ye(t);return o.length?o.filter(s=>W(t,s)):o},"enumSymbols");function ne(t,o,s){let a=C(t),y=C(o);if(!ee(a,y,s.proto))return null;let g=$(t);if(g!==$(o))return null;if(g)return"array";if(K(a)&&K(y))return"object";let S=I(t);return S!==I(o)?null:S==="[object Object]"?"object":S}u(ne,"classify");var de=new Set(["[object Promise]","[object WeakMap]","[object WeakSet]","[object WeakRef]","[object FinalizationRegistry]","[object Blob]","[object File]","[object Generator]","[object AsyncGenerator]","[object ReadableStream]","[object WritableStream]"]),ge=new Set(["[object Float16Array]","[object Float32Array]","[object Float64Array]"]),me={"[object Number]":u(t=>Number.prototype.valueOf.call(t),"[object Number]"),"[object String]":u(t=>String.prototype.valueOf.call(t),"[object String]"),"[object Boolean]":u(t=>Boolean.prototype.valueOf.call(t),"[object Boolean]"),"[object BigInt]":u(t=>BigInt.prototype.valueOf.call(t),"[object BigInt]"),"[object Symbol]":u(t=>Symbol.prototype.valueOf.call(t),"[object Symbol]")},H=Date.prototype.getTime,Q=u((t,o)=>{let s=t.length;if(s!==o.length)return!1;for(let a=0;a<s;a++)if(t[a]!==o[a])return!1;return!0},"bytesEqual"),ke=100;function Y(t){let{strict:o,proto:s,signedZero:a,nanEqual:y,symbols:g,ignoreUndefined:S,skipKey:j,customizer:A}=t,U=S||j!==void 0,F=A===void 0,D=u((e,n)=>e===n?!a||e!==0||1/e===1/n:y&&e!==e&&n!==n,"prim"),q=!1;function E(e,n,r){if(A!==void 0){let p=A(e,n);if(p!==void 0)return!!p}if(e===n)return!a||e!==0||1/e===1/n;if(typeof e!="object"||typeof n!="object"||e===null||n===null)return y&&e!==e&&n!==n;if(typeof r=="number")return r<ke?N(e,n,r+1):(q=!0,!1);let i=r.get(e);if(i===void 0)r.set(e,i=[n]);else{if(i.includes(n))return!0;i.push(n)}let c=N(e,n,r);return i.length===1?r.delete(e):i.pop(),c}u(E,"eq");function N(e,n,r){let i=C(e),c=C(n);if(i===c){if(i===Z||i===null)return x(e,n,w(e),w(n),0,r);if(i===ce&&$(e)&&$(n))return L(e,n,r);if(i===ae)return b(e,n,r)&&(!o||x(e,n,w(e),w(n),0,r));if(i===pe)return k(e,n,r)&&(!o||x(e,n,w(e),w(n),0,r))}else if(!ee(i,c,s))return!1;let p=$(e);if(p!==$(n))return!1;if(p)return L(e,n,r);if(K(i)&&K(c))return x(e,n,w(e),w(n),0,r);let h=I(e);return h!==I(n)?!1:f(e,n,h,r)}u(N,"deep");function L(e,n,r){let i=e.length;if(i!==n.length)return!1;if(!o){for(let d=0;d<i;d++)if(!E(e[d],n[d],r))return!1;return!0}let c=w(e),p=w(n);if(!U&&c.length!==p.length)return!1;let h=0;for(let d=0;d<i;d++)if(d in e){if(!(d in n)||!E(e[d],n[d],r))return!1;h++}else if(d in n)return!1;return x(e,n,c,p,h,r)}u(L,"arrays");function x(e,n,r,i,c,p){return l(e,n,r,i,c,p)?!g||l(e,n,R(e),R(n),0,p):!1}u(x,"props");function l(e,n,r,i,c,p){if(!U){if(r.length!==i.length)return!1;for(let d=c;d<r.length;d++){let P=r[d];if(P!==i[d]&&!W(n,P)||!E(e[P],n[P],p))return!1}return!0}let h=0;for(let d=c;d<r.length;d++){let P=r[d];if(j!==void 0&&j(P,e))continue;let V=e[P];if(S&&V===void 0)continue;if(h++,!W(n,P)||j!==void 0&&j(P,n))return!1;let _=n[P];if(S&&_===void 0||!E(V,_,p))return!1}for(let d=c;d<i.length;d++){let P=i[d];j!==void 0&&j(P,n)||S&&n[P]===void 0||h--}return h===0}u(l,"keyList");function f(e,n,r,i){switch(r){case"[object Object]":return x(e,n,w(e),w(n),0,i);case"[object Date]":{let c=H.call(e),p=H.call(n);if(c!==p&&!(c!==c&&p!==p))return!1;break}case"[object RegExp]":if(e.source!==n.source||e.flags!==n.flags||o&&e.lastIndex!==n.lastIndex)return!1;break;case"[object Map]":if(!b(e,n,i))return!1;break;case"[object Set]":if(!k(e,n,i))return!1;break;case"[object Error]":return O(e,n,i);case"[object ArrayBuffer]":case"[object SharedArrayBuffer]":return Q(new Uint8Array(e),new Uint8Array(n));case"[object DataView]":return Q(new Uint8Array(e.buffer,e.byteOffset,e.byteLength),new Uint8Array(n.buffer,n.byteOffset,n.byteLength));case"[object URL]":if(e.href!==n.href)return!1;break;case"[object URLSearchParams]":if(String(e)!==String(n))return!1;break;default:{let c=me[r];if(c!==void 0){if(!D(c(e),c(n)))return!1;break}if(ArrayBuffer.isView(e))return m(e,n,ge.has(r));if(de.has(r))return!1;if(r.startsWith("[object Temporal.")){if(String(e)!==String(n))return!1;break}return e instanceof Error?O(e,n,i):x(e,n,w(e),w(n),0,i)}}return!o||x(e,n,w(e),w(n),0,i)}u(f,"special");function m(e,n,r){let i=e.length;if(i!==n.length)return!1;if(r){for(let c=0;c<i;c++)if(!D(e[c],n[c]))return!1}else for(let c=0;c<i;c++)if(e[c]!==n[c])return!1;return!0}u(m,"typed");function O(e,n,r){if(e.name!==n.name||e.message!==n.message)return!1;let i="cause"in e;return i!=="cause"in n||i&&!E(e.cause,n.cause,r)||("errors"in e||"errors"in n)&&!E(e.errors,n.errors,r)?!1:x(e,n,w(e),w(n),0,r)}u(O,"errors");function b(e,n,r){if(e.size!==n.size)return!1;let i;for(let p of e){let h=p[0];if(n.has(h)){if(!E(p[1],n.get(h),r))return!1}else{if(F&&(typeof h!="object"||h===null))return!1;(i??(i=[])).push(h)}}if(i===void 0)return!0;let c=[];for(let p of n.keys())e.has(p)||c.push(p);e:for(let p of i){for(let h=0;h<c.length;h++){let d=c[h];if(E(p,d,r)&&E(e.get(p),n.get(d),r)){c.splice(h,1);continue e}}return!1}return!0}u(b,"maps");function k(e,n,r){if(e.size!==n.size)return!1;let i;for(let p of e)if(!n.has(p)){if(F&&(typeof p!="object"||p===null))return!1;(i??(i=[])).push(p)}if(i===void 0)return!0;let c=[];for(let p of n)e.has(p)||c.push(p);e:for(let p of i){for(let h=0;h<c.length;h++)if(E(p,c[h],r)){c.splice(h,1);continue e}return!1}return!0}return u(k,"sets"),(e,n)=>{if(A===void 0&&(e===n||typeof e!="object"||typeof n!="object"||e===null||n===null))return E(e,n,0);let r=q;q=!1;let i=E(e,n,0);return q&&(i=E(e,n,new Map)),q=r,i}}u(Y,"compile");var v=new Map;function z(t){if(t.customizer!==void 0||t.skipKey!==void 0)return Y(t);let o=+t.strict|+t.signedZero<<1|+t.nanEqual<<2|+t.symbols<<3|+t.ignoreUndefined<<4|t.proto<<5,s=v.get(o);return s===void 0&&v.set(o,s=Y(t)),s}u(z,"comparatorFor");var te=u(t=>typeof t=="object"&&t!==null,"isObject"),he=u(t=>String(+t>>>0)===t&&t!=="4294967295","isIndex");function oe(t,o,s){let a=M(s);return re(t,o,a,z(a))}u(oe,"diff");function re(t,o,s,a){let y=[],g=[],{strict:S,customizer:j,skipKey:A,ignoreUndefined:U,symbols:F}=s,D=u((l,f,m)=>y.push({kind:"changed",path:l,a:f,b:m}),"changed"),q=u((l,f)=>(F&&(f=f.concat(R(l))),A===void 0&&!U?f:f.filter(m=>!(A!==void 0&&A(m,l))&&!(U&&l[m]===void 0))),"ownKeys"),E=u((l,f,m,O,b)=>{let k=new Set(O);for(let e of m)k.has(e)?(k.delete(e),x(l[e],f[e],[...b,e])):y.push({kind:"removed",path:[...b,e],a:l[e]});for(let e of k)y.push({kind:"added",path:[...b,e],b:f[e]})},"walkKeys"),N=u((l,f,m)=>{let O=y.length,b=Math.max(l.length,f.length);for(let k=0;k<b;k++){let e=S?k in l:k<l.length,n=S?k in f:k<f.length;e&&n?x(l[k],f[k],[...m,k]):e?y.push({kind:"removed",path:[...m,k],a:l[k]}):n&&y.push({kind:"added",path:[...m,k],b:f[k]})}if(S){l.length!==f.length&&y.length===O&&D([...m,"length"],l.length,f.length);let k=u(e=>q(e,w(e).filter(n=>!he(n))),"named");E(l,f,k(l),k(f),m)}},"walkArray"),L=u((l,f,m)=>{let O=[];for(let[e,n]of l)f.has(e)?x(n,f.get(e),[...m,e]):O.push(e);let b=[];for(let e of f.keys())l.has(e)||b.push(e);let k=[];for(let e of O){let n=b.findIndex(r=>a(e,r)&&a(l.get(e),f.get(r)));n===-1?k.push(e):b.splice(n,1)}for(let e of k){let n=b.findIndex(r=>a(e,r));n===-1?y.push({kind:"removed",path:[...m,e],a:l.get(e)}):x(l.get(e),f.get(b.splice(n,1)[0]),[...m,e])}for(let e of b)y.push({kind:"added",path:[...m,e],b:f.get(e)})},"walkMap");function x(l,f,m){if(te(l)&&te(f)&&l!==f){let O=ne(l,f,s);if(O==="array"||O==="object"||O==="[object Map]"){if(j!==void 0){let b=j(l,f);if(b!==void 0){b||D(m,l,f);return}}for(let b=g.length-2;b>=0;b-=2)if(g[b]===l&&g[b+1]===f)return;g.push(l,f),O==="array"?N(l,f,m):O==="object"?E(l,f,q(l,w(l)),q(f,w(f)),m):(L(l,f,m),S&&E(l,f,q(l,w(l)),q(f,w(f)),m)),g.length-=2;return}}a(l,f)||D(m,l,f)}return u(x,"walk"),x(t,o,[]),y}u(re,"walkAll");var ie=/^[A-Za-z_$][\w$]*$/;function X(t){let o="";for(let s of t)typeof s=="number"?o+=`[${s}]`:typeof s=="string"?o+=ie.test(s)?(o?".":"")+s:`[${JSON.stringify(s)}]`:o+=`[${T(s)}]`;return o}u(X,"formatPath");function T(t,o=!1){try{switch(typeof t){case"string":return JSON.stringify(t.length>60?t.slice(0,57)+"...":t);case"number":return Object.is(t,-0)?"-0":String(t);case"bigint":return`${t}n`;case"symbol":return t.toString();case"function":return t.name?`[Function: ${t.name}]`:"[Function]";case"object":break;default:return String(t)}if(t===null)return"null";if(t instanceof Date)return isNaN(t.getTime())?"Invalid Date":t.toISOString();if(t instanceof RegExp)return String(t);if(t instanceof Error)return`${t.name}: ${t.message}`;if(t instanceof Map)return`Map(${t.size})`;if(t instanceof Set)return`Set(${t.size})`;if(Array.isArray(t)){if(o)return t.length?"[...]":"[]";let j=t.slice(0,5).map(A=>T(A,!0));return t.length>5&&j.push(`... ${t.length-5} more`),`[${j.join(", ")}]`}let s=C(t),a=K(s)?"":s.constructor?.name||I(t).slice(8,-1),y=a?a+" ":"",g=w(t);if(o||g.length===0)return y+(g.length?"{...}":"{}");let S=g.slice(0,5).map(j=>`${ie.test(j)?j:JSON.stringify(j)}: ${T(t[j],!0)}`);return g.length>5&&S.push(`... ${g.length-5} more`),`${y}{ ${S.join(", ")} }`}catch{return"[unprintable]"}}u(T,"show");var J=20;function we(t){let o=t.slice(0,J).map(a=>{let y=X(a.path)||"(root)",g=a.kind==="added"?"(missing)":T(a.a),S=a.kind==="removed"?"(missing)":T(a.b);return` ${y}: got ${g}, expected ${S}`});return t.length>J&&o.push(` ... and ${t.length-J} more`),`Expected values to be deeply equal (${t.length===1?"1 difference":`${t.length} differences`}):
|
|
2
|
+
${o.join(`
|
|
3
|
+
`)}`}u(we,"describe");var G=class G extends Error{constructor(s,a,y,g){super(g??we(y));this.name="NotEqualError";this.diffs=y,this.actual=s,this.expected=a}};u(G,"NotEqualError");var B=G;function se(t,o,s){let a=M(s),y=z(a);if(y(t,o))return;let g=re(t,o,a,y);throw new B(t,o,g,s?.message)}u(se,"assertEqual");var ue=z(M(void 0));function fe(t,o,s){return s===void 0?t===o||ue(t,o):z(M(s))(t,o)}u(fe,"equal");var je=fe;function be(t){return z(M(t))}u(be,"createEqual");var Se=Object.assign(u(function(o,s,a){return a===void 0?o===s||ue(o,s):z(M(a))(o,s)},"dittoly"),{equal:fe,isEqual:je,createEqual:be,diff:oe,assertEqual:se,formatPath:X,NotEqualError:B}),Me=Se;export{B as NotEqualError,se as assertEqual,be as createEqual,Me as default,oe as diff,fe as equal,X as formatPath,je as isEqual};
|
package/package.json
CHANGED
|
@@ -1,10 +1,61 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dittoly",
|
|
3
|
-
"version": "1.0.0
|
|
4
|
-
"description": "Fast deep equality
|
|
5
|
-
"
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Fast deep equality for objects, arrays, Maps, Sets, Dates, typed arrays, class instances and circular references, plus diff() with paths for readable test failures. Strict and loose modes, ignoreKeys, customizer. TypeScript, zero dependencies.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"deep-equal",
|
|
7
|
+
"deep-equality",
|
|
8
|
+
"deepequal",
|
|
9
|
+
"equal",
|
|
10
|
+
"equality",
|
|
11
|
+
"isequal",
|
|
12
|
+
"is-equal",
|
|
13
|
+
"compare",
|
|
14
|
+
"comparison",
|
|
15
|
+
"deep-compare",
|
|
16
|
+
"diff",
|
|
17
|
+
"object-diff",
|
|
18
|
+
"deep-diff",
|
|
19
|
+
"fast-deep-equal",
|
|
20
|
+
"dequal",
|
|
21
|
+
"lodash.isequal",
|
|
22
|
+
"isDeepStrictEqual",
|
|
23
|
+
"assert",
|
|
24
|
+
"map",
|
|
25
|
+
"set",
|
|
26
|
+
"circular",
|
|
27
|
+
"typescript"
|
|
28
|
+
],
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"type": "module",
|
|
31
|
+
"sideEffects": false,
|
|
32
|
+
"main": "./dist/index.cjs",
|
|
33
|
+
"module": "./dist/index.js",
|
|
34
|
+
"types": "./dist/index.d.ts",
|
|
35
|
+
"exports": {
|
|
36
|
+
".": {
|
|
37
|
+
"import": {
|
|
38
|
+
"types": "./dist/index.d.ts",
|
|
39
|
+
"default": "./dist/index.js"
|
|
40
|
+
},
|
|
41
|
+
"require": {
|
|
42
|
+
"types": "./dist/index.d.cts",
|
|
43
|
+
"default": "./dist/index.cjs"
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
"./package.json": "./package.json"
|
|
47
|
+
},
|
|
48
|
+
"files": [
|
|
49
|
+
"dist",
|
|
50
|
+
"CHANGELOG.md"
|
|
51
|
+
],
|
|
52
|
+
"engines": {
|
|
53
|
+
"node": ">=20"
|
|
54
|
+
},
|
|
55
|
+
"scripts": {
|
|
56
|
+
"build": "tsup",
|
|
57
|
+
"test": "vitest run",
|
|
58
|
+
"typecheck": "tsc -p .",
|
|
59
|
+
"prepublishOnly": "npm run typecheck && npm test && npm run build"
|
|
60
|
+
}
|
|
10
61
|
}
|
package/index.d.ts
DELETED