@fluixi/utils 1.0.0-alpha.83 → 1.0.0-alpha.85
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.cjs +2858 -1
- package/dist/index.mjs +2838 -1
- package/dist/lib/array/advanced.cjs +2227 -1
- package/dist/lib/array/advanced.mjs +2204 -1
- package/dist/lib/array/array.cjs +57 -1
- package/dist/lib/array/array.mjs +36 -1
- package/dist/lib/array/index.cjs +2344 -1
- package/dist/lib/array/index.mjs +2322 -1
- package/dist/lib/array/utils.cjs +103 -1
- package/dist/lib/array/utils.mjs +82 -1
- package/dist/lib/color/color.cjs +2190 -1
- package/dist/lib/color/color.mjs +2167 -1
- package/dist/lib/color/constants.cjs +380 -1
- package/dist/lib/color/constants.mjs +359 -1
- package/dist/lib/color/conversions.cjs +629 -1
- package/dist/lib/color/conversions.mjs +608 -1
- package/dist/lib/color/helpers.cjs +2534 -1
- package/dist/lib/color/helpers.mjs +2511 -1
- package/dist/lib/color/index.cjs +2641 -1
- package/dist/lib/color/index.mjs +2619 -1
- package/dist/lib/color/types.cjs +18 -1
- package/dist/lib/color/utils.cjs +962 -1
- package/dist/lib/color/utils.mjs +939 -1
- package/dist/lib/compare/comparators.cjs +171 -1
- package/dist/lib/compare/comparators.mjs +150 -1
- package/dist/lib/compare/index.cjs +172 -1
- package/dist/lib/compare/index.mjs +152 -1
- package/dist/lib/crypto/hash.cjs +83 -1
- package/dist/lib/crypto/hash.mjs +62 -1
- package/dist/lib/crypto/index.cjs +85 -1
- package/dist/lib/crypto/index.mjs +62 -1
- package/dist/lib/dom/events.cjs +40 -1
- package/dist/lib/dom/events.mjs +19 -1
- package/dist/lib/dom/index.cjs +78 -1
- package/dist/lib/dom/index.mjs +56 -1
- package/dist/lib/dom/utils.cjs +57 -1
- package/dist/lib/dom/utils.mjs +36 -1
- package/dist/lib/functions/functions.cjs +366 -1
- package/dist/lib/functions/functions.mjs +345 -1
- package/dist/lib/functions/index.cjs +369 -1
- package/dist/lib/functions/index.mjs +347 -1
- package/dist/lib/functions/types.cjs +18 -1
- package/dist/lib/helpers.cjs +78 -1
- package/dist/lib/helpers.mjs +59 -1
- package/dist/lib/index.cjs +2857 -1
- package/dist/lib/index.mjs +2836 -1
- package/dist/lib/maths/constants.cjs +70 -1
- package/dist/lib/maths/constants.mjs +50 -1
- package/dist/lib/maths/fn/advanced.cjs +231 -1
- package/dist/lib/maths/fn/advanced.mjs +208 -1
- package/dist/lib/maths/fn/basic.cjs +212 -1
- package/dist/lib/maths/fn/basic.mjs +191 -1
- package/dist/lib/maths/fn/complex.cjs +268 -1
- package/dist/lib/maths/fn/complex.mjs +247 -1
- package/dist/lib/maths/fn/index.cjs +877 -1
- package/dist/lib/maths/fn/index.mjs +855 -1
- package/dist/lib/maths/fn/trigo.cjs +179 -1
- package/dist/lib/maths/fn/trigo.mjs +156 -1
- package/dist/lib/maths/fn/vector.cjs +728 -1
- package/dist/lib/maths/fn/vector.mjs +707 -1
- package/dist/lib/maths/formatter.cjs +61 -1
- package/dist/lib/maths/formatter.mjs +40 -1
- package/dist/lib/maths/guards.cjs +164 -1
- package/dist/lib/maths/guards.mjs +144 -1
- package/dist/lib/maths/index.cjs +1152 -1
- package/dist/lib/maths/index.mjs +1130 -1
- package/dist/lib/maths/parser.cjs +75 -1
- package/dist/lib/maths/parser.mjs +54 -1
- package/dist/lib/maths/random.cjs +49 -1
- package/dist/lib/maths/random.mjs +28 -1
- package/dist/lib/maths/types/advanced.cjs +18 -1
- package/dist/lib/maths/types/basic.cjs +18 -1
- package/dist/lib/maths/types/index.cjs +19 -1
- package/dist/lib/maths/types/index.mjs +1 -0
- package/dist/lib/maths/types/negative.cjs +18 -1
- package/dist/lib/maths/types/operations.cjs +18 -1
- package/dist/lib/object/clone.cjs +88 -1
- package/dist/lib/object/clone.mjs +65 -1
- package/dist/lib/object/diff.cjs +573 -1
- package/dist/lib/object/diff.mjs +550 -1
- package/dist/lib/object/getter-setter.cjs +2351 -1
- package/dist/lib/object/getter-setter.mjs +2330 -1
- package/dist/lib/object/index.cjs +3956 -6
- package/dist/lib/object/index.mjs +3936 -6
- package/dist/lib/object/merge.cjs +1029 -1
- package/dist/lib/object/merge.mjs +1006 -1
- package/dist/lib/object/object.cjs +280 -1
- package/dist/lib/object/object.mjs +257 -1
- package/dist/lib/object/omit.cjs +2022 -1
- package/dist/lib/object/omit.mjs +1999 -1
- package/dist/lib/object/reconcile.cjs +126 -1
- package/dist/lib/object/reconcile.js +1 -1
- package/dist/lib/object/reconcile.mjs +105 -1
- package/dist/lib/object/selector.cjs +303 -1
- package/dist/lib/object/selector.mjs +282 -1
- package/dist/lib/object/sort.cjs +209 -1
- package/dist/lib/object/sort.d.ts +1 -1
- package/dist/lib/object/sort.js +3 -3
- package/dist/lib/object/sort.mjs +186 -1
- package/dist/lib/object/transform.cjs +2211 -1
- package/dist/lib/object/transform.mjs +2190 -1
- package/dist/lib/object/types.cjs +18 -1
- package/dist/lib/object/utils.cjs +326 -6
- package/dist/lib/object/utils.mjs +305 -6
- package/dist/lib/primitive/boolean/boolean.cjs +407 -1
- package/dist/lib/primitive/boolean/boolean.mjs +386 -1
- package/dist/lib/primitive/boolean/index.cjs +382 -1
- package/dist/lib/primitive/boolean/index.mjs +359 -1
- package/dist/lib/primitive/boolean/types.cjs +18 -1
- package/dist/lib/primitive/date/date.cjs +1126 -1
- package/dist/lib/primitive/date/date.mjs +1105 -1
- package/dist/lib/primitive/date/index.cjs +1128 -1
- package/dist/lib/primitive/date/index.mjs +1105 -1
- package/dist/lib/primitive/date/types.cjs +18 -1
- package/dist/lib/primitive/index.cjs +2338 -5
- package/dist/lib/primitive/index.mjs +2316 -5
- package/dist/lib/primitive/string/index.cjs +875 -5
- package/dist/lib/primitive/string/index.mjs +852 -5
- package/dist/lib/primitive/string/parser-2.cjs +543 -8
- package/dist/lib/primitive/string/parser-2.mjs +522 -8
- package/dist/lib/primitive/string/parser.cjs +236 -3
- package/dist/lib/primitive/string/parser.mjs +215 -3
- package/dist/lib/primitive/string/string.cjs +926 -5
- package/dist/lib/primitive/string/string.mjs +903 -5
- package/dist/lib/primitive/string/types.cjs +18 -1
- package/dist/lib/primitive/types.cjs +18 -1
- package/dist/lib/url/index.cjs +327 -1
- package/dist/lib/url/index.mjs +304 -1
- package/dist/lib/url/parser.cjs +325 -1
- package/dist/lib/url/parser.mjs +304 -1
- package/dist/lib/url/types.cjs +18 -1
- package/dist/tsconfig.lib.tsbuildinfo +1 -1
- package/package.json +5 -5
package/dist/lib/object/sort.mjs
CHANGED
|
@@ -1 +1,186 @@
|
|
|
1
|
-
|
|
1
|
+
// src/lib/object/utils.ts
|
|
2
|
+
function isPlainObject(v) {
|
|
3
|
+
if (typeof v !== "object" || v === null) return false;
|
|
4
|
+
const proto = Object.getPrototypeOf(v);
|
|
5
|
+
return proto === Object.prototype || proto === null;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
// src/lib/object/sort.ts
|
|
9
|
+
function getAtPath(obj, path) {
|
|
10
|
+
if (obj == null) return void 0;
|
|
11
|
+
let cur = obj;
|
|
12
|
+
for (const segment of path.split(".")) {
|
|
13
|
+
if (cur == null) return void 0;
|
|
14
|
+
cur = cur[segment];
|
|
15
|
+
}
|
|
16
|
+
return cur;
|
|
17
|
+
}
|
|
18
|
+
function compareValues(a, b, locale, localeOptions) {
|
|
19
|
+
const aN = a == null;
|
|
20
|
+
const bN = b == null;
|
|
21
|
+
if (aN && bN) return 0;
|
|
22
|
+
if (aN) return 1;
|
|
23
|
+
if (bN) return -1;
|
|
24
|
+
const aNaN = typeof a === "number" && Number.isNaN(a);
|
|
25
|
+
const bNaN = typeof b === "number" && Number.isNaN(b);
|
|
26
|
+
if (aNaN && bNaN) return 0;
|
|
27
|
+
if (aNaN) return 1;
|
|
28
|
+
if (bNaN) return -1;
|
|
29
|
+
if (a instanceof Date && b instanceof Date) {
|
|
30
|
+
return a.getTime() - b.getTime();
|
|
31
|
+
}
|
|
32
|
+
if (typeof a === "boolean" && typeof b === "boolean") {
|
|
33
|
+
return a === b ? 0 : a ? 1 : -1;
|
|
34
|
+
}
|
|
35
|
+
if (typeof a === "number" && typeof b === "number") {
|
|
36
|
+
return a - b;
|
|
37
|
+
}
|
|
38
|
+
if (typeof a === "string" && typeof b === "string") {
|
|
39
|
+
return a.localeCompare(b, locale, localeOptions);
|
|
40
|
+
}
|
|
41
|
+
return String(a).localeCompare(String(b), locale, localeOptions);
|
|
42
|
+
}
|
|
43
|
+
function dirFor(order, i) {
|
|
44
|
+
const o = Array.isArray(order) ? order[i] ?? "asc" : order ?? "asc";
|
|
45
|
+
return o === "desc" ? -1 : 1;
|
|
46
|
+
}
|
|
47
|
+
function sortPrimitiveArray(arr, options) {
|
|
48
|
+
const { order, locale, localeOptions, nullsLast = true } = options;
|
|
49
|
+
const dir = dirFor(order, 0);
|
|
50
|
+
return arr.slice().sort((a, b) => {
|
|
51
|
+
const aN = a == null;
|
|
52
|
+
const bN = b == null;
|
|
53
|
+
if (aN && bN) return 0;
|
|
54
|
+
if (aN) return nullsLast ? 1 : -1;
|
|
55
|
+
if (bN) return nullsLast ? -1 : 1;
|
|
56
|
+
return compareValues(a, b, locale, localeOptions) * dir;
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
function sortObjectArray(arr, keys, options) {
|
|
60
|
+
const { order, locale, localeOptions, nullsLast = true, comparator } = options;
|
|
61
|
+
if (comparator) {
|
|
62
|
+
return arr.slice().sort(comparator);
|
|
63
|
+
}
|
|
64
|
+
return arr.slice().sort((a, b) => {
|
|
65
|
+
for (let i = 0; i < keys.length; i++) {
|
|
66
|
+
const aVal = getAtPath(a, keys[i]);
|
|
67
|
+
const bVal = getAtPath(b, keys[i]);
|
|
68
|
+
const aN = aVal == null;
|
|
69
|
+
const bN = bVal == null;
|
|
70
|
+
if (aN && bN) continue;
|
|
71
|
+
if (aN) return nullsLast ? 1 : -1;
|
|
72
|
+
if (bN) return nullsLast ? -1 : 1;
|
|
73
|
+
const cmp = compareValues(aVal, bVal, locale, localeOptions);
|
|
74
|
+
if (cmp !== 0) return cmp * dirFor(order, i);
|
|
75
|
+
}
|
|
76
|
+
return 0;
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
function sortRecord(record, options) {
|
|
80
|
+
const { order, priorityKeys = [], locale, localeOptions, deep } = options;
|
|
81
|
+
const dir = dirFor(order, 0);
|
|
82
|
+
const prioritySet = new Set(priorityKeys);
|
|
83
|
+
const rest = Object.keys(record).filter((k) => !prioritySet.has(k));
|
|
84
|
+
rest.sort(
|
|
85
|
+
(a, b) => a.localeCompare(b, locale, localeOptions) * dir
|
|
86
|
+
);
|
|
87
|
+
const ordered = [...priorityKeys.filter((k) => k in record), ...rest];
|
|
88
|
+
const result = {};
|
|
89
|
+
for (const key of ordered) {
|
|
90
|
+
result[key] = deep ? sortDeep(record[key], options) : record[key];
|
|
91
|
+
}
|
|
92
|
+
return result;
|
|
93
|
+
}
|
|
94
|
+
function sortDeep(value, options) {
|
|
95
|
+
if (Array.isArray(value)) {
|
|
96
|
+
const deepSorted = value.map((item) => sortDeep(item, options));
|
|
97
|
+
return sortArrayValue(deepSorted, options);
|
|
98
|
+
}
|
|
99
|
+
if (isPlainObject(value)) {
|
|
100
|
+
return sortRecord(value, options);
|
|
101
|
+
}
|
|
102
|
+
return value;
|
|
103
|
+
}
|
|
104
|
+
function applyIndices(arr, indices) {
|
|
105
|
+
const len = arr.length;
|
|
106
|
+
const used = /* @__PURE__ */ new Set();
|
|
107
|
+
const result = [];
|
|
108
|
+
for (const idx of indices) {
|
|
109
|
+
if (idx >= 0 && idx < len && !used.has(idx)) {
|
|
110
|
+
result.push(arr[idx]);
|
|
111
|
+
used.add(idx);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
for (let i = 0; i < len; i++) {
|
|
115
|
+
if (!used.has(i)) result.push(arr[i]);
|
|
116
|
+
}
|
|
117
|
+
return result;
|
|
118
|
+
}
|
|
119
|
+
function sortArrayValue(arr, options) {
|
|
120
|
+
const { by, comparator, indices, order } = options;
|
|
121
|
+
if (indices) {
|
|
122
|
+
const permuted = applyIndices(arr, indices);
|
|
123
|
+
return dirFor(order, 0) === -1 ? permuted.reverse() : permuted;
|
|
124
|
+
}
|
|
125
|
+
if (comparator) {
|
|
126
|
+
return arr.slice().sort(comparator);
|
|
127
|
+
}
|
|
128
|
+
if (by) {
|
|
129
|
+
const keys = Array.isArray(by) ? by : [by];
|
|
130
|
+
return sortObjectArray(arr, keys, options);
|
|
131
|
+
}
|
|
132
|
+
const firstReal = arr.find((x) => x != null);
|
|
133
|
+
if (firstReal != null && isPlainObject(firstReal)) {
|
|
134
|
+
return dirFor(order, 0) === -1 ? arr.slice().reverse() : arr.slice();
|
|
135
|
+
}
|
|
136
|
+
return sortPrimitiveArray(arr, options);
|
|
137
|
+
}
|
|
138
|
+
function sort(input, options = {}) {
|
|
139
|
+
const { deep } = options;
|
|
140
|
+
if (Array.isArray(input)) {
|
|
141
|
+
const items = deep ? input.map((item) => sortDeep(item, options)) : input;
|
|
142
|
+
return sortArrayValue(items, options);
|
|
143
|
+
}
|
|
144
|
+
if (isPlainObject(input)) {
|
|
145
|
+
return sortRecord(input, options);
|
|
146
|
+
}
|
|
147
|
+
return input;
|
|
148
|
+
}
|
|
149
|
+
function sortBy(arr, by, order = "asc", options = {}) {
|
|
150
|
+
return sort(arr, { ...options, by, order });
|
|
151
|
+
}
|
|
152
|
+
function sortKeys(record, options = {}) {
|
|
153
|
+
return sort(record, options);
|
|
154
|
+
}
|
|
155
|
+
function move(arr, from, to) {
|
|
156
|
+
const len = arr.length;
|
|
157
|
+
if (len === 0) return arr.slice();
|
|
158
|
+
const f = Math.max(0, Math.min(len - 1, from));
|
|
159
|
+
const t = Math.max(0, Math.min(len - 1, to));
|
|
160
|
+
if (f === t) return arr.slice();
|
|
161
|
+
const result = arr.slice();
|
|
162
|
+
const [item] = result.splice(f, 1);
|
|
163
|
+
result.splice(t, 0, item);
|
|
164
|
+
return result;
|
|
165
|
+
}
|
|
166
|
+
function swap(arr, i, j) {
|
|
167
|
+
const len = arr.length;
|
|
168
|
+
if (i < 0 || i >= len) throw new RangeError(`swap: index ${i} out of range`);
|
|
169
|
+
if (j < 0 || j >= len) throw new RangeError(`swap: index ${j} out of range`);
|
|
170
|
+
if (i === j) return arr.slice();
|
|
171
|
+
const result = arr.slice();
|
|
172
|
+
result[i] = arr[j];
|
|
173
|
+
result[j] = arr[i];
|
|
174
|
+
return result;
|
|
175
|
+
}
|
|
176
|
+
function reorder(arr, indices, order = "asc") {
|
|
177
|
+
return sort(arr, { indices, order });
|
|
178
|
+
}
|
|
179
|
+
export {
|
|
180
|
+
move,
|
|
181
|
+
reorder,
|
|
182
|
+
sort,
|
|
183
|
+
sortBy,
|
|
184
|
+
sortKeys,
|
|
185
|
+
swap
|
|
186
|
+
};
|