@equinor/cpl-utils 0.2.1 → 0.3.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/index.d.mts +33 -1
- package/dist/index.d.ts +33 -1
- package/dist/index.js +52 -1
- package/dist/index.mjs +53 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,3 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a sort function that orders items by a numeric property
|
|
3
|
+
*
|
|
4
|
+
* @param key - The property key to sort by (must be a number type)
|
|
5
|
+
* @param order - Sort order: 'asc' for ascending, 'desc' for descending
|
|
6
|
+
* @returns A comparator function for use with Array.sort()
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const users = [{ age: 30 }, { age: 25 }, { age: 35 }]
|
|
10
|
+
* users.sort(buildObjectNumberCompareFn('age', 'asc')) // 25, 30, 35
|
|
11
|
+
* users.sort(buildObjectNumberCompareFn('age', 'desc')) // 35, 30, 25
|
|
12
|
+
*/
|
|
13
|
+
declare function buildObjectNumberCompareFn<T>(key: {
|
|
14
|
+
[K in keyof T]: T[K] extends number | undefined | null ? K : never;
|
|
15
|
+
}[keyof T], order?: 'asc' | 'desc'): (a: T, b: T) => number;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Creates a sort function that orders items by a string property
|
|
19
|
+
*
|
|
20
|
+
* @param key - The property key to sort by (must be a string type)
|
|
21
|
+
* @param order - Sort order: 'asc' for ascending, 'desc' for descending
|
|
22
|
+
* @returns A comparator function for use with Array.sort()
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* const users = [{ name: 'John' }, { name: 'Alice' }]
|
|
26
|
+
* users.sort(buildObjectStringCompareFn('name', 'asc')) // Alice, John
|
|
27
|
+
* users.sort(buildObjectStringCompareFn('name', 'desc')) // John, Alice
|
|
28
|
+
*/
|
|
29
|
+
declare function buildObjectStringCompareFn<T>(key: {
|
|
30
|
+
[K in keyof T]: T[K] extends string | undefined | null ? K : never;
|
|
31
|
+
}[keyof T], order?: 'asc' | 'desc'): (a: T, b: T) => number;
|
|
32
|
+
|
|
1
33
|
type TreeNode<T> = T & {
|
|
2
34
|
children?: TreeNode<T>[];
|
|
3
35
|
};
|
|
@@ -205,4 +237,4 @@ declare function objectHasOwnNestedProperty<K extends readonly string[]>(object:
|
|
|
205
237
|
*/
|
|
206
238
|
declare function objectHasOwnProperty<Key extends PropertyKey>(object: object, key: Key): object is Record<Key, unknown>;
|
|
207
239
|
|
|
208
|
-
export { type TreeNode, buildTreeNodes, hasCircularDependency, itemIsDefined, objectHasOwnNestedProperty, objectHasOwnProperty };
|
|
240
|
+
export { type TreeNode, buildObjectNumberCompareFn, buildObjectStringCompareFn, buildTreeNodes, hasCircularDependency, itemIsDefined, objectHasOwnNestedProperty, objectHasOwnProperty };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a sort function that orders items by a numeric property
|
|
3
|
+
*
|
|
4
|
+
* @param key - The property key to sort by (must be a number type)
|
|
5
|
+
* @param order - Sort order: 'asc' for ascending, 'desc' for descending
|
|
6
|
+
* @returns A comparator function for use with Array.sort()
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const users = [{ age: 30 }, { age: 25 }, { age: 35 }]
|
|
10
|
+
* users.sort(buildObjectNumberCompareFn('age', 'asc')) // 25, 30, 35
|
|
11
|
+
* users.sort(buildObjectNumberCompareFn('age', 'desc')) // 35, 30, 25
|
|
12
|
+
*/
|
|
13
|
+
declare function buildObjectNumberCompareFn<T>(key: {
|
|
14
|
+
[K in keyof T]: T[K] extends number | undefined | null ? K : never;
|
|
15
|
+
}[keyof T], order?: 'asc' | 'desc'): (a: T, b: T) => number;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Creates a sort function that orders items by a string property
|
|
19
|
+
*
|
|
20
|
+
* @param key - The property key to sort by (must be a string type)
|
|
21
|
+
* @param order - Sort order: 'asc' for ascending, 'desc' for descending
|
|
22
|
+
* @returns A comparator function for use with Array.sort()
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* const users = [{ name: 'John' }, { name: 'Alice' }]
|
|
26
|
+
* users.sort(buildObjectStringCompareFn('name', 'asc')) // Alice, John
|
|
27
|
+
* users.sort(buildObjectStringCompareFn('name', 'desc')) // John, Alice
|
|
28
|
+
*/
|
|
29
|
+
declare function buildObjectStringCompareFn<T>(key: {
|
|
30
|
+
[K in keyof T]: T[K] extends string | undefined | null ? K : never;
|
|
31
|
+
}[keyof T], order?: 'asc' | 'desc'): (a: T, b: T) => number;
|
|
32
|
+
|
|
1
33
|
type TreeNode<T> = T & {
|
|
2
34
|
children?: TreeNode<T>[];
|
|
3
35
|
};
|
|
@@ -205,4 +237,4 @@ declare function objectHasOwnNestedProperty<K extends readonly string[]>(object:
|
|
|
205
237
|
*/
|
|
206
238
|
declare function objectHasOwnProperty<Key extends PropertyKey>(object: object, key: Key): object is Record<Key, unknown>;
|
|
207
239
|
|
|
208
|
-
export { type TreeNode, buildTreeNodes, hasCircularDependency, itemIsDefined, objectHasOwnNestedProperty, objectHasOwnProperty };
|
|
240
|
+
export { type TreeNode, buildObjectNumberCompareFn, buildObjectStringCompareFn, buildTreeNodes, hasCircularDependency, itemIsDefined, objectHasOwnNestedProperty, objectHasOwnProperty };
|
package/dist/index.js
CHANGED
|
@@ -2,7 +2,21 @@
|
|
|
2
2
|
var __defProp = Object.defineProperty;
|
|
3
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
6
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
8
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
9
|
+
var __spreadValues = (a, b) => {
|
|
10
|
+
for (var prop in b || (b = {}))
|
|
11
|
+
if (__hasOwnProp.call(b, prop))
|
|
12
|
+
__defNormalProp(a, prop, b[prop]);
|
|
13
|
+
if (__getOwnPropSymbols)
|
|
14
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
15
|
+
if (__propIsEnum.call(b, prop))
|
|
16
|
+
__defNormalProp(a, prop, b[prop]);
|
|
17
|
+
}
|
|
18
|
+
return a;
|
|
19
|
+
};
|
|
6
20
|
var __export = (target, all) => {
|
|
7
21
|
for (var name in all)
|
|
8
22
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -20,6 +34,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
34
|
// src/index.ts
|
|
21
35
|
var index_exports = {};
|
|
22
36
|
__export(index_exports, {
|
|
37
|
+
buildObjectNumberCompareFn: () => buildObjectNumberCompareFn,
|
|
38
|
+
buildObjectStringCompareFn: () => buildObjectStringCompareFn,
|
|
23
39
|
buildTreeNodes: () => buildTreeNodes,
|
|
24
40
|
hasCircularDependency: () => hasCircularDependency,
|
|
25
41
|
itemIsDefined: () => itemIsDefined,
|
|
@@ -28,6 +44,38 @@ __export(index_exports, {
|
|
|
28
44
|
});
|
|
29
45
|
module.exports = __toCommonJS(index_exports);
|
|
30
46
|
|
|
47
|
+
// src/build-object-number-compare-fn.ts
|
|
48
|
+
function buildObjectNumberCompareFn(key, order = "asc") {
|
|
49
|
+
return (a, b) => {
|
|
50
|
+
const aValue = Number(a[key] || 0);
|
|
51
|
+
const bValue = Number(b[key] || 0);
|
|
52
|
+
const aNum = isNaN(aValue) ? 0 : aValue;
|
|
53
|
+
const bNum = isNaN(bValue) ? 0 : bValue;
|
|
54
|
+
if (aNum < bNum) {
|
|
55
|
+
return order === "asc" ? -1 : 1;
|
|
56
|
+
}
|
|
57
|
+
if (aNum > bNum) {
|
|
58
|
+
return order === "asc" ? 1 : -1;
|
|
59
|
+
}
|
|
60
|
+
return 0;
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// src/build-object-string-compare-fn.ts
|
|
65
|
+
function buildObjectStringCompareFn(key, order = "asc") {
|
|
66
|
+
return (a, b) => {
|
|
67
|
+
const aValue = String(a[key] || "").toLowerCase();
|
|
68
|
+
const bValue = String(b[key] || "").toLowerCase();
|
|
69
|
+
if (aValue < bValue) {
|
|
70
|
+
return order === "asc" ? -1 : 1;
|
|
71
|
+
}
|
|
72
|
+
if (aValue > bValue) {
|
|
73
|
+
return order === "asc" ? 1 : -1;
|
|
74
|
+
}
|
|
75
|
+
return 0;
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
31
79
|
// src/has-circular-dependency.ts
|
|
32
80
|
function hasCircularDependency(items, idKey, parentIdKey) {
|
|
33
81
|
const parentMap = /* @__PURE__ */ new Map();
|
|
@@ -73,7 +121,8 @@ function buildTreeNodes(items, idKey, parentIdKey, options = {}) {
|
|
|
73
121
|
if (!objectHasOwnProperty(item, idKey)) {
|
|
74
122
|
throw new Error(`Item is missing idKey named '${String(idKey)}'`);
|
|
75
123
|
}
|
|
76
|
-
|
|
124
|
+
const nodeCopy = __spreadValues({}, item);
|
|
125
|
+
lookupTable.set(item[idKey], nodeCopy);
|
|
77
126
|
}
|
|
78
127
|
const rootNodes = [];
|
|
79
128
|
for (const item of lookupTable.values()) {
|
|
@@ -125,6 +174,8 @@ function objectHasOwnNestedProperty(object, ...keys) {
|
|
|
125
174
|
}
|
|
126
175
|
// Annotate the CommonJS export names for ESM import in node:
|
|
127
176
|
0 && (module.exports = {
|
|
177
|
+
buildObjectNumberCompareFn,
|
|
178
|
+
buildObjectStringCompareFn,
|
|
128
179
|
buildTreeNodes,
|
|
129
180
|
hasCircularDependency,
|
|
130
181
|
itemIsDefined,
|
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,52 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
3
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
4
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __spreadValues = (a, b) => {
|
|
7
|
+
for (var prop in b || (b = {}))
|
|
8
|
+
if (__hasOwnProp.call(b, prop))
|
|
9
|
+
__defNormalProp(a, prop, b[prop]);
|
|
10
|
+
if (__getOwnPropSymbols)
|
|
11
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
12
|
+
if (__propIsEnum.call(b, prop))
|
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
|
14
|
+
}
|
|
15
|
+
return a;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// src/build-object-number-compare-fn.ts
|
|
19
|
+
function buildObjectNumberCompareFn(key, order = "asc") {
|
|
20
|
+
return (a, b) => {
|
|
21
|
+
const aValue = Number(a[key] || 0);
|
|
22
|
+
const bValue = Number(b[key] || 0);
|
|
23
|
+
const aNum = isNaN(aValue) ? 0 : aValue;
|
|
24
|
+
const bNum = isNaN(bValue) ? 0 : bValue;
|
|
25
|
+
if (aNum < bNum) {
|
|
26
|
+
return order === "asc" ? -1 : 1;
|
|
27
|
+
}
|
|
28
|
+
if (aNum > bNum) {
|
|
29
|
+
return order === "asc" ? 1 : -1;
|
|
30
|
+
}
|
|
31
|
+
return 0;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// src/build-object-string-compare-fn.ts
|
|
36
|
+
function buildObjectStringCompareFn(key, order = "asc") {
|
|
37
|
+
return (a, b) => {
|
|
38
|
+
const aValue = String(a[key] || "").toLowerCase();
|
|
39
|
+
const bValue = String(b[key] || "").toLowerCase();
|
|
40
|
+
if (aValue < bValue) {
|
|
41
|
+
return order === "asc" ? -1 : 1;
|
|
42
|
+
}
|
|
43
|
+
if (aValue > bValue) {
|
|
44
|
+
return order === "asc" ? 1 : -1;
|
|
45
|
+
}
|
|
46
|
+
return 0;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
1
50
|
// src/has-circular-dependency.ts
|
|
2
51
|
function hasCircularDependency(items, idKey, parentIdKey) {
|
|
3
52
|
const parentMap = /* @__PURE__ */ new Map();
|
|
@@ -43,7 +92,8 @@ function buildTreeNodes(items, idKey, parentIdKey, options = {}) {
|
|
|
43
92
|
if (!objectHasOwnProperty(item, idKey)) {
|
|
44
93
|
throw new Error(`Item is missing idKey named '${String(idKey)}'`);
|
|
45
94
|
}
|
|
46
|
-
|
|
95
|
+
const nodeCopy = __spreadValues({}, item);
|
|
96
|
+
lookupTable.set(item[idKey], nodeCopy);
|
|
47
97
|
}
|
|
48
98
|
const rootNodes = [];
|
|
49
99
|
for (const item of lookupTable.values()) {
|
|
@@ -94,6 +144,8 @@ function objectHasOwnNestedProperty(object, ...keys) {
|
|
|
94
144
|
return true;
|
|
95
145
|
}
|
|
96
146
|
export {
|
|
147
|
+
buildObjectNumberCompareFn,
|
|
148
|
+
buildObjectStringCompareFn,
|
|
97
149
|
buildTreeNodes,
|
|
98
150
|
hasCircularDependency,
|
|
99
151
|
itemIsDefined,
|