@bb-labs/deep-equal-test 0.0.1
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/README.md +62 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +17 -0
- package/package.json +32 -0
package/README.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
## Introduction
|
|
2
|
+
|
|
3
|
+
A tiny utility for **deep structural equality** that is:sa
|
|
4
|
+
|
|
5
|
+
- ✅ Aware of rich JavaScript types via **[SuperJSON](https://github.com/blitz-js/superjson)**
|
|
6
|
+
- ✅ Uses **[fast-deep-equal](https://github.com/epoberezkin/fast-deep-equal)** under the hood
|
|
7
|
+
- ✅ Treats **arrays as equal even when items are in a different order**
|
|
8
|
+
- ✅ Stable & deterministic (objects and arrays are normalized before comparison)
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install @bigbang-sdk/local-db
|
|
16
|
+
# or
|
|
17
|
+
yarn add @bigbang-sdk/local-db
|
|
18
|
+
# or
|
|
19
|
+
bun add @bigbang-sdk/local-db
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## Why?
|
|
25
|
+
|
|
26
|
+
Plain deep equality libraries usually assume:
|
|
27
|
+
|
|
28
|
+
- Arrays must be in the **same order**
|
|
29
|
+
- Only handle plain JSON-ish structures welcasl
|
|
30
|
+
|
|
31
|
+
This helper does two important things:
|
|
32
|
+
|
|
33
|
+
1. Runs values through **SuperJSON** so things like `Date`, `BigInt`, `Map`, `Set`, etc. end up in a consistent, JSON-safe shape.
|
|
34
|
+
2. **Recursively normalizes** that shape:
|
|
35
|
+
- Arrays are **sorted** by their JSON representation → order no longer matters.
|
|
36
|
+
- Object keys are **sorted** so key order never affects equality.
|
|
37
|
+
|
|
38
|
+
Then it compares the normalized values with `fast-deep-equal`.
|
|
39
|
+
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
## API
|
|
43
|
+
|
|
44
|
+
### `isDeepEqual(a: unknown, b: unknown): boolean`
|
|
45
|
+
|
|
46
|
+
Deep equality check with:
|
|
47
|
+
|
|
48
|
+
- SuperJSON-aware serialization
|
|
49
|
+
- Order-insensitive arrays
|
|
50
|
+
- Stable object key orderingg
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
import { isDeepEqual } from "@bigbang-sdk/local-db";
|
|
54
|
+
|
|
55
|
+
isDeepEqual([1, 2, 3], [3, 2, 1]); // true
|
|
56
|
+
|
|
57
|
+
isDeepEqual([{ x: 1, y: 2 }, { a: 5 }], [{ a: 5 }, { y: 2, x: 1 }]); // true
|
|
58
|
+
|
|
59
|
+
isDeepEqual(new Date("2020-01-01"), new Date("2020-01-01")); // true
|
|
60
|
+
|
|
61
|
+
isDeepEqual(new Set([3, 2, 1]), new Set([1, 2, 3])); // true (via SuperJSON serialization)
|
|
62
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const isDeepEqual: (a: unknown, b: unknown) => boolean;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import deepEqual from "fast-deep-equal";
|
|
2
|
+
import SuperJSON from "superjson";
|
|
3
|
+
import { deepSort } from "@bb-labs/deep-sort-test";
|
|
4
|
+
/**
|
|
5
|
+
* Normalize structures so deep-equal works:
|
|
6
|
+
* - Encode using SuperJSON (handles Dates, BigInts, Maps/Sets via metadata)
|
|
7
|
+
* - Deep-sort objects and arrays so order does not matter
|
|
8
|
+
*/
|
|
9
|
+
const normalize = (value) => {
|
|
10
|
+
// First serialize using SuperJSON for consistency / rich types
|
|
11
|
+
const { json } = SuperJSON.serialize(value);
|
|
12
|
+
// Then deep-sort the JSON-safe structure
|
|
13
|
+
return deepSort(json);
|
|
14
|
+
};
|
|
15
|
+
export const isDeepEqual = (a, b) => {
|
|
16
|
+
return deepEqual(normalize(a), normalize(b));
|
|
17
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bb-labs/deep-equal-test",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"npm": true,
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/beepbop-labs/libraries.git"
|
|
8
|
+
},
|
|
9
|
+
"main": "dist/index.js",
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "bldr",
|
|
15
|
+
"dev": "bldr -w",
|
|
16
|
+
"clean": "rm -rf node_modules bun.lock dist"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@types/bun": "latest",
|
|
20
|
+
"tsc-alias": "^1.8.16"
|
|
21
|
+
},
|
|
22
|
+
"peerDependencies": {
|
|
23
|
+
"typescript": "^5"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@bb-labs/bldr": "^0.0.16",
|
|
27
|
+
"@bb-labs/deep-sort-test": "0.0.0",
|
|
28
|
+
"@bb-labs/tsconfigs": "^0.0.4",
|
|
29
|
+
"fast-deep-equal": "^3.1.3",
|
|
30
|
+
"superjson": "^2.2.6"
|
|
31
|
+
}
|
|
32
|
+
}
|