@bb-labs/deep-sort-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 +68 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +19 -0
- package/package.json +31 -0
package/README.md
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
## Introduction
|
|
2
|
+
|
|
3
|
+
A tiny utility that **deeply normalizes** JavaScript values by:xs
|
|
4
|
+
|
|
5
|
+
- ✅ Recursively sorting **object keys**
|
|
6
|
+
- ✅ Recursively sorting **arrays** using **SuperJSON** for deterministic comparison
|
|
7
|
+
- ✅ Preserving non-plain objects (`Date`, `Map`, `Set`, `BigInt`, class instances, etc.)
|
|
8
|
+
- ⚡ Perfect for generating cache keys, hashing structured data, or stabilizing query arguments
|
|
9
|
+
|
|
10
|
+
The goal:
|
|
11
|
+
**Identical structures always produce identical normalized output — even when key order or array order differs.**
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install @bigbang-sdk/deep-sort
|
|
19
|
+
# or
|
|
20
|
+
yarn add @bigbang-sdk/deep-sort
|
|
21
|
+
# or
|
|
22
|
+
bun add @bigbang-sdk/deep-sort
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
## API
|
|
28
|
+
|
|
29
|
+
### `deepSort(value: unknown): unknown`
|
|
30
|
+
|
|
31
|
+
Returns a **fully normalized, deeply sorted copy** of any JSON-compatible data structure.
|
|
32
|
+
|
|
33
|
+
Behavior:
|
|
34
|
+
csa
|
|
35
|
+
|
|
36
|
+
- **Object keys** are sorted at every level
|
|
37
|
+
- **Arrays** are deeply normalized, then **sorted** using SuperJSON’s structural encoding
|
|
38
|
+
- Non-plain objects (Dates, Maps, Sets, etc.) are **preserved**, and naturally handled by SuperJSON
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import { deepSort } from "@bigbang-sdk/deep-sort";
|
|
42
|
+
|
|
43
|
+
deepSort([3, 1, 2]);
|
|
44
|
+
// → [1, 2, 3]
|
|
45
|
+
|
|
46
|
+
deepSort([{ b: 2, a: 1 }, { a: 3 }]);
|
|
47
|
+
// → [ { a: 1, b: 2 }, { a: 3 } ]
|
|
48
|
+
|
|
49
|
+
deepSort([
|
|
50
|
+
{ id: 2, tags: ["b", "a"] },
|
|
51
|
+
{ id: 1, tags: ["c", "a"] },
|
|
52
|
+
]);
|
|
53
|
+
// → normalized deeply, sorted deterministically
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
## Caveats
|
|
59
|
+
|
|
60
|
+
- Sorting arrays by SuperJSON string is deterministic but may be expensive for very large/nested data.
|
|
61
|
+
- Circular references are **not supported** (SuperJSON cannot serialize them).
|
|
62
|
+
- This is intended for **cache key normalization**, **equality pre-normalization**, and **stable hashing** — not for mutating live data structuress.
|
|
63
|
+
|
|
64
|
+
---
|
|
65
|
+
|
|
66
|
+
## License
|
|
67
|
+
|
|
68
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const deepSort: (value: unknown) => unknown;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { sort as sortObjectKeys } from "@tamtamchik/json-deep-sort";
|
|
2
|
+
import superjson from "superjson";
|
|
3
|
+
export const deepSort = (value) => {
|
|
4
|
+
if (Array.isArray(value)) {
|
|
5
|
+
// Normalize each item deeply
|
|
6
|
+
const normalizedItems = value.map(deepSort);
|
|
7
|
+
// Deterministic array sorting using SuperJSON
|
|
8
|
+
return normalizedItems.sort((a, b) => {
|
|
9
|
+
const sa = superjson.stringify(a);
|
|
10
|
+
const sb = superjson.stringify(b);
|
|
11
|
+
return sa < sb ? -1 : sa > sb ? 1 : 0;
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
if (value !== null && typeof value === "object") {
|
|
15
|
+
// Sort object keys recursively
|
|
16
|
+
return sortObjectKeys(value);
|
|
17
|
+
}
|
|
18
|
+
return value; // primitives unchanged
|
|
19
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bb-labs/deep-sort-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.15",
|
|
27
|
+
"@bb-labs/tsconfigs": "^0.0.3",
|
|
28
|
+
"@tamtamchik/json-deep-sort": "^1.4.1",
|
|
29
|
+
"superjson": "^2.2.6"
|
|
30
|
+
}
|
|
31
|
+
}
|