@bb-labs/deep-sort 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 ADDED
@@ -0,0 +1,67 @@
1
+ ## Introduction
2
+
3
+ A tiny utility that **deeply normalizes** JavaScript values by:
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
+
35
+ - **Object keys** are sorted at every level
36
+ - **Arrays** are deeply normalized, then **sorted** using SuperJSON’s structural encoding
37
+ - Non-plain objects (Dates, Maps, Sets, etc.) are **preserved**, and naturally handled by SuperJSON
38
+
39
+ ```ts
40
+ import { deepSort } from "@bigbang-sdk/deep-sort";
41
+
42
+ deepSort([3, 1, 2]);
43
+ // → [1, 2, 3]
44
+
45
+ deepSort([{ b: 2, a: 1 }, { a: 3 }]);
46
+ // → [ { a: 1, b: 2 }, { a: 3 } ]
47
+
48
+ deepSort([
49
+ { id: 2, tags: ["b", "a"] },
50
+ { id: 1, tags: ["c", "a"] },
51
+ ]);
52
+ // → normalized deeply, sorted deterministically
53
+ ```
54
+
55
+ ---
56
+
57
+ ## Caveats
58
+
59
+ - Sorting arrays by SuperJSON string is deterministic but may be expensive for very large/nested data.
60
+ - Circular references are **not supported** (SuperJSON cannot serialize them).
61
+ - This is intended for **cache key normalization**, **equality pre-normalization**, and **stable hashing** — not for mutating live data structuress.
62
+
63
+ ---
64
+
65
+ ## License
66
+
67
+ MIT
@@ -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,36 @@
1
+ {
2
+ "name": "@bb-labs/deep-sort",
3
+ "description": "A library for deep sorting using",
4
+ "homepage": "https://github.com/beepbop-labs/deep-sort",
5
+ "keywords": [
6
+ "deep-sort",
7
+ "json-deep-sort"
8
+ ],
9
+ "author": "Beepbop",
10
+ "license": "MIT",
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "https://github.com/beepbop-labs/deep-sort.git"
14
+ },
15
+ "main": "dist/index.js",
16
+ "files": [
17
+ "dist",
18
+ "README.md"
19
+ ],
20
+ "scripts": {
21
+ "clean": "rm -rf dist",
22
+ "build": "npm run clean && tsc -p tsconfig.json",
23
+ "pack": "npm run build && npm pack --pack-destination ./archive/"
24
+ },
25
+ "devDependencies": {
26
+ "@types/bun": "latest"
27
+ },
28
+ "peerDependencies": {
29
+ "typescript": "^5"
30
+ },
31
+ "dependencies": {
32
+ "@tamtamchik/json-deep-sort": "^1.4.1",
33
+ "superjson": "^2.2.6"
34
+ },
35
+ "version": "0.0.1"
36
+ }