@ls-stack/utils 2.8.0 → 2.10.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/dist/getObjStableKey.cjs +83 -0
- package/dist/getObjStableKey.d.cts +3 -0
- package/dist/getObjStableKey.d.ts +3 -0
- package/dist/getObjStableKey.js +44 -0
- package/dist/main.d.ts +1 -0
- package/package.json +9 -1
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/getObjStableKey.ts
|
|
21
|
+
var getObjStableKey_exports = {};
|
|
22
|
+
__export(getObjStableKey_exports, {
|
|
23
|
+
getObjStableKey: () => getObjStableKey
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(getObjStableKey_exports);
|
|
26
|
+
|
|
27
|
+
// src/arrayUtils.ts
|
|
28
|
+
function filterAndMap(array, mapFilter) {
|
|
29
|
+
const result = [];
|
|
30
|
+
let i = -1;
|
|
31
|
+
for (const item of array) {
|
|
32
|
+
i += 1;
|
|
33
|
+
const filterResult = mapFilter(item, i);
|
|
34
|
+
if (filterResult !== false) {
|
|
35
|
+
result.push(filterResult);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// src/assertions.ts
|
|
42
|
+
function isObject(value) {
|
|
43
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// src/getObjStableKey.ts
|
|
47
|
+
function getObjStableKey(input, maxDepth = 3) {
|
|
48
|
+
if (typeof input === "string") return String(input);
|
|
49
|
+
if (!input || typeof input !== "object") return `#$${input}$#`;
|
|
50
|
+
return JSON.stringify(sortValues(input, maxDepth, 0));
|
|
51
|
+
}
|
|
52
|
+
function sortValues(input, maxDepth, depth) {
|
|
53
|
+
if (depth >= maxDepth) return input;
|
|
54
|
+
if (Array.isArray(input)) {
|
|
55
|
+
return input.map((v) => sortValues(v, maxDepth, depth + 1));
|
|
56
|
+
}
|
|
57
|
+
if (isObject(input)) {
|
|
58
|
+
return orderedProps(input, (v) => sortValues(v, maxDepth, depth + 1));
|
|
59
|
+
}
|
|
60
|
+
return input;
|
|
61
|
+
}
|
|
62
|
+
var emptyObject = {};
|
|
63
|
+
function orderedProps(obj, mapValue) {
|
|
64
|
+
const keys = Object.keys(obj);
|
|
65
|
+
if (keys.length === 0) return emptyObject;
|
|
66
|
+
if (keys.length === 1) {
|
|
67
|
+
const value = obj[keys[0]];
|
|
68
|
+
if (value === void 0) return emptyObject;
|
|
69
|
+
return { [keys[0]]: mapValue(value) };
|
|
70
|
+
}
|
|
71
|
+
const mappedValues = filterAndMap(keys.sort(), (k) => {
|
|
72
|
+
const value = obj[k];
|
|
73
|
+
if (value === void 0) return false;
|
|
74
|
+
return { [k]: mapValue(value) };
|
|
75
|
+
});
|
|
76
|
+
if (mappedValues.length === 0) return emptyObject;
|
|
77
|
+
if (mappedValues.length === 1) return mappedValues[0];
|
|
78
|
+
return mappedValues;
|
|
79
|
+
}
|
|
80
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
81
|
+
0 && (module.exports = {
|
|
82
|
+
getObjStableKey
|
|
83
|
+
});
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import {
|
|
2
|
+
filterAndMap
|
|
3
|
+
} from "./chunk-QMFZE2VO.js";
|
|
4
|
+
import {
|
|
5
|
+
isObject
|
|
6
|
+
} from "./chunk-4UGSP3L3.js";
|
|
7
|
+
|
|
8
|
+
// src/getObjStableKey.ts
|
|
9
|
+
function getObjStableKey(input, maxDepth = 3) {
|
|
10
|
+
if (typeof input === "string") return String(input);
|
|
11
|
+
if (!input || typeof input !== "object") return `#$${input}$#`;
|
|
12
|
+
return JSON.stringify(sortValues(input, maxDepth, 0));
|
|
13
|
+
}
|
|
14
|
+
function sortValues(input, maxDepth, depth) {
|
|
15
|
+
if (depth >= maxDepth) return input;
|
|
16
|
+
if (Array.isArray(input)) {
|
|
17
|
+
return input.map((v) => sortValues(v, maxDepth, depth + 1));
|
|
18
|
+
}
|
|
19
|
+
if (isObject(input)) {
|
|
20
|
+
return orderedProps(input, (v) => sortValues(v, maxDepth, depth + 1));
|
|
21
|
+
}
|
|
22
|
+
return input;
|
|
23
|
+
}
|
|
24
|
+
var emptyObject = {};
|
|
25
|
+
function orderedProps(obj, mapValue) {
|
|
26
|
+
const keys = Object.keys(obj);
|
|
27
|
+
if (keys.length === 0) return emptyObject;
|
|
28
|
+
if (keys.length === 1) {
|
|
29
|
+
const value = obj[keys[0]];
|
|
30
|
+
if (value === void 0) return emptyObject;
|
|
31
|
+
return { [keys[0]]: mapValue(value) };
|
|
32
|
+
}
|
|
33
|
+
const mappedValues = filterAndMap(keys.sort(), (k) => {
|
|
34
|
+
const value = obj[k];
|
|
35
|
+
if (value === void 0) return false;
|
|
36
|
+
return { [k]: mapValue(value) };
|
|
37
|
+
});
|
|
38
|
+
if (mappedValues.length === 0) return emptyObject;
|
|
39
|
+
if (mappedValues.length === 1) return mappedValues[0];
|
|
40
|
+
return mappedValues;
|
|
41
|
+
}
|
|
42
|
+
export {
|
|
43
|
+
getObjStableKey
|
|
44
|
+
};
|
package/dist/main.d.ts
CHANGED
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
///<reference path="deepEqual.d.ts" />
|
|
11
11
|
///<reference path="enhancedMap.d.ts" />
|
|
12
12
|
///<reference path="exhaustiveMatch.d.ts" />
|
|
13
|
+
///<reference path="getObjStableKey.d.ts" />
|
|
13
14
|
///<reference path="internalUtils.d.ts" />
|
|
14
15
|
///<reference path="interpolate.d.ts" />
|
|
15
16
|
///<reference path="levenshtein.d.ts" />
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ls-stack/utils",
|
|
3
3
|
"description": "Typescript utils",
|
|
4
|
-
"version": "2.
|
|
4
|
+
"version": "2.10.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist"
|
|
@@ -79,6 +79,11 @@
|
|
|
79
79
|
"types": "./dist/exhaustiveMatch.d.ts",
|
|
80
80
|
"require": "./dist/exhaustiveMatch.cjs"
|
|
81
81
|
},
|
|
82
|
+
"./getObjStableKey": {
|
|
83
|
+
"import": "./dist/getObjStableKey.js",
|
|
84
|
+
"types": "./dist/getObjStableKey.d.ts",
|
|
85
|
+
"require": "./dist/getObjStableKey.cjs"
|
|
86
|
+
},
|
|
82
87
|
"./internalUtils": {
|
|
83
88
|
"import": "./dist/internalUtils.js",
|
|
84
89
|
"types": "./dist/internalUtils.d.ts",
|
|
@@ -248,6 +253,9 @@
|
|
|
248
253
|
"exhaustiveMatch": [
|
|
249
254
|
"./dist/exhaustiveMatch.d.ts"
|
|
250
255
|
],
|
|
256
|
+
"getObjStableKey": [
|
|
257
|
+
"./dist/getObjStableKey.d.ts"
|
|
258
|
+
],
|
|
251
259
|
"internalUtils": [
|
|
252
260
|
"./dist/internalUtils.d.ts"
|
|
253
261
|
],
|