@fluixi/utils 1.0.0-alpha.83 → 1.0.0-alpha.84
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.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.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/package.json +5 -5
|
@@ -1 +1,126 @@
|
|
|
1
|
-
"use strict";
|
|
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/lib/object/reconcile.ts
|
|
21
|
+
var reconcile_exports = {};
|
|
22
|
+
__export(reconcile_exports, {
|
|
23
|
+
reconcile: () => reconcile
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(reconcile_exports);
|
|
26
|
+
function isPlain(v) {
|
|
27
|
+
if (v == null || typeof v !== "object") return false;
|
|
28
|
+
const p = Object.getPrototypeOf(v);
|
|
29
|
+
return p === Object.prototype || p === null;
|
|
30
|
+
}
|
|
31
|
+
function reconcileValue(next, current, opts) {
|
|
32
|
+
if (next === current) return current;
|
|
33
|
+
if (Array.isArray(next)) return reconcileArr(next, Array.isArray(current) ? current : [], opts);
|
|
34
|
+
if (isPlain(next)) return reconcileObj(next, isPlain(current) ? current : {}, opts);
|
|
35
|
+
return next;
|
|
36
|
+
}
|
|
37
|
+
function reconcileArr(next, current, opts) {
|
|
38
|
+
const { key, keepRef } = opts;
|
|
39
|
+
const curLen = current.length;
|
|
40
|
+
const nextLen = next.length;
|
|
41
|
+
const curItems = new Array(curLen);
|
|
42
|
+
for (let i = 0; i < curLen; i++) curItems[i] = current[i];
|
|
43
|
+
if (key === false) {
|
|
44
|
+
if (keepRef) {
|
|
45
|
+
for (let i = 0; i < nextLen; i++) {
|
|
46
|
+
const rv = reconcileValue(next[i], curItems[i], opts);
|
|
47
|
+
if (rv !== curItems[i]) current[i] = rv;
|
|
48
|
+
}
|
|
49
|
+
if (curLen > nextLen) current.splice(nextLen);
|
|
50
|
+
return current;
|
|
51
|
+
}
|
|
52
|
+
let changed2 = nextLen !== curLen;
|
|
53
|
+
const out = new Array(nextLen);
|
|
54
|
+
for (let i = 0; i < nextLen; i++) {
|
|
55
|
+
out[i] = reconcileValue(next[i], curItems[i], opts);
|
|
56
|
+
if (out[i] !== curItems[i]) changed2 = true;
|
|
57
|
+
}
|
|
58
|
+
return changed2 ? out : current;
|
|
59
|
+
}
|
|
60
|
+
const currentMap = /* @__PURE__ */ new Map();
|
|
61
|
+
for (let i = 0; i < curLen; i++) {
|
|
62
|
+
const item = curItems[i];
|
|
63
|
+
if (item != null && typeof item === "object" && key in item) {
|
|
64
|
+
currentMap.set(item[key], item);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const resolved = new Array(nextLen);
|
|
68
|
+
for (let i = 0; i < nextLen; i++) {
|
|
69
|
+
const n = next[i];
|
|
70
|
+
if (n != null && typeof n === "object" && key in n) {
|
|
71
|
+
resolved[i] = reconcileValue(n, currentMap.get(n[key]), opts);
|
|
72
|
+
} else {
|
|
73
|
+
resolved[i] = reconcileValue(n, curItems[i], opts);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (keepRef) {
|
|
77
|
+
for (let i = 0; i < nextLen; i++) {
|
|
78
|
+
if (resolved[i] !== curItems[i]) current[i] = resolved[i];
|
|
79
|
+
}
|
|
80
|
+
if (curLen > nextLen) current.splice(nextLen);
|
|
81
|
+
return current;
|
|
82
|
+
}
|
|
83
|
+
let changed = nextLen !== curLen;
|
|
84
|
+
for (let i = 0; !changed && i < nextLen; i++) {
|
|
85
|
+
if (resolved[i] !== curItems[i]) changed = true;
|
|
86
|
+
}
|
|
87
|
+
return changed ? resolved : current;
|
|
88
|
+
}
|
|
89
|
+
function reconcileObj(next, current, opts) {
|
|
90
|
+
const { merge, keepRef } = opts;
|
|
91
|
+
const nextKeys = Object.keys(next);
|
|
92
|
+
const currentKeys = Object.keys(current);
|
|
93
|
+
const curValues = /* @__PURE__ */ Object.create(null);
|
|
94
|
+
for (let i = 0; i < currentKeys.length; i++) {
|
|
95
|
+
curValues[currentKeys[i]] = current[currentKeys[i]];
|
|
96
|
+
}
|
|
97
|
+
let out = null;
|
|
98
|
+
const getOut = () => {
|
|
99
|
+
if (!out) out = keepRef ? current : { ...curValues };
|
|
100
|
+
return out;
|
|
101
|
+
};
|
|
102
|
+
if (!merge) {
|
|
103
|
+
for (let i = 0; i < currentKeys.length; i++) {
|
|
104
|
+
const k = currentKeys[i];
|
|
105
|
+
if (!(k in next)) delete getOut()[k];
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
for (let i = 0; i < nextKeys.length; i++) {
|
|
109
|
+
const k = nextKeys[i];
|
|
110
|
+
const cv = curValues[k];
|
|
111
|
+
const rv = reconcileValue(next[k], cv, opts);
|
|
112
|
+
if (rv !== cv || !(k in curValues)) getOut()[k] = rv;
|
|
113
|
+
}
|
|
114
|
+
return out ?? current;
|
|
115
|
+
}
|
|
116
|
+
function reconcile(next, options) {
|
|
117
|
+
const opts = {
|
|
118
|
+
key: options?.key !== void 0 ? options.key : "id",
|
|
119
|
+
merge: options?.merge ?? false,
|
|
120
|
+
keepRef: options?.keepRef ?? false
|
|
121
|
+
};
|
|
122
|
+
return (current) => {
|
|
123
|
+
if (current === void 0 || current === null) return next;
|
|
124
|
+
return reconcileValue(next, current, opts);
|
|
125
|
+
};
|
|
126
|
+
}
|
|
@@ -1 +1,105 @@
|
|
|
1
|
-
|
|
1
|
+
// src/lib/object/reconcile.ts
|
|
2
|
+
function isPlain(v) {
|
|
3
|
+
if (v == null || typeof v !== "object") return false;
|
|
4
|
+
const p = Object.getPrototypeOf(v);
|
|
5
|
+
return p === Object.prototype || p === null;
|
|
6
|
+
}
|
|
7
|
+
function reconcileValue(next, current, opts) {
|
|
8
|
+
if (next === current) return current;
|
|
9
|
+
if (Array.isArray(next)) return reconcileArr(next, Array.isArray(current) ? current : [], opts);
|
|
10
|
+
if (isPlain(next)) return reconcileObj(next, isPlain(current) ? current : {}, opts);
|
|
11
|
+
return next;
|
|
12
|
+
}
|
|
13
|
+
function reconcileArr(next, current, opts) {
|
|
14
|
+
const { key, keepRef } = opts;
|
|
15
|
+
const curLen = current.length;
|
|
16
|
+
const nextLen = next.length;
|
|
17
|
+
const curItems = new Array(curLen);
|
|
18
|
+
for (let i = 0; i < curLen; i++) curItems[i] = current[i];
|
|
19
|
+
if (key === false) {
|
|
20
|
+
if (keepRef) {
|
|
21
|
+
for (let i = 0; i < nextLen; i++) {
|
|
22
|
+
const rv = reconcileValue(next[i], curItems[i], opts);
|
|
23
|
+
if (rv !== curItems[i]) current[i] = rv;
|
|
24
|
+
}
|
|
25
|
+
if (curLen > nextLen) current.splice(nextLen);
|
|
26
|
+
return current;
|
|
27
|
+
}
|
|
28
|
+
let changed2 = nextLen !== curLen;
|
|
29
|
+
const out = new Array(nextLen);
|
|
30
|
+
for (let i = 0; i < nextLen; i++) {
|
|
31
|
+
out[i] = reconcileValue(next[i], curItems[i], opts);
|
|
32
|
+
if (out[i] !== curItems[i]) changed2 = true;
|
|
33
|
+
}
|
|
34
|
+
return changed2 ? out : current;
|
|
35
|
+
}
|
|
36
|
+
const currentMap = /* @__PURE__ */ new Map();
|
|
37
|
+
for (let i = 0; i < curLen; i++) {
|
|
38
|
+
const item = curItems[i];
|
|
39
|
+
if (item != null && typeof item === "object" && key in item) {
|
|
40
|
+
currentMap.set(item[key], item);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const resolved = new Array(nextLen);
|
|
44
|
+
for (let i = 0; i < nextLen; i++) {
|
|
45
|
+
const n = next[i];
|
|
46
|
+
if (n != null && typeof n === "object" && key in n) {
|
|
47
|
+
resolved[i] = reconcileValue(n, currentMap.get(n[key]), opts);
|
|
48
|
+
} else {
|
|
49
|
+
resolved[i] = reconcileValue(n, curItems[i], opts);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
if (keepRef) {
|
|
53
|
+
for (let i = 0; i < nextLen; i++) {
|
|
54
|
+
if (resolved[i] !== curItems[i]) current[i] = resolved[i];
|
|
55
|
+
}
|
|
56
|
+
if (curLen > nextLen) current.splice(nextLen);
|
|
57
|
+
return current;
|
|
58
|
+
}
|
|
59
|
+
let changed = nextLen !== curLen;
|
|
60
|
+
for (let i = 0; !changed && i < nextLen; i++) {
|
|
61
|
+
if (resolved[i] !== curItems[i]) changed = true;
|
|
62
|
+
}
|
|
63
|
+
return changed ? resolved : current;
|
|
64
|
+
}
|
|
65
|
+
function reconcileObj(next, current, opts) {
|
|
66
|
+
const { merge, keepRef } = opts;
|
|
67
|
+
const nextKeys = Object.keys(next);
|
|
68
|
+
const currentKeys = Object.keys(current);
|
|
69
|
+
const curValues = /* @__PURE__ */ Object.create(null);
|
|
70
|
+
for (let i = 0; i < currentKeys.length; i++) {
|
|
71
|
+
curValues[currentKeys[i]] = current[currentKeys[i]];
|
|
72
|
+
}
|
|
73
|
+
let out = null;
|
|
74
|
+
const getOut = () => {
|
|
75
|
+
if (!out) out = keepRef ? current : { ...curValues };
|
|
76
|
+
return out;
|
|
77
|
+
};
|
|
78
|
+
if (!merge) {
|
|
79
|
+
for (let i = 0; i < currentKeys.length; i++) {
|
|
80
|
+
const k = currentKeys[i];
|
|
81
|
+
if (!(k in next)) delete getOut()[k];
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
for (let i = 0; i < nextKeys.length; i++) {
|
|
85
|
+
const k = nextKeys[i];
|
|
86
|
+
const cv = curValues[k];
|
|
87
|
+
const rv = reconcileValue(next[k], cv, opts);
|
|
88
|
+
if (rv !== cv || !(k in curValues)) getOut()[k] = rv;
|
|
89
|
+
}
|
|
90
|
+
return out ?? current;
|
|
91
|
+
}
|
|
92
|
+
function reconcile(next, options) {
|
|
93
|
+
const opts = {
|
|
94
|
+
key: options?.key !== void 0 ? options.key : "id",
|
|
95
|
+
merge: options?.merge ?? false,
|
|
96
|
+
keepRef: options?.keepRef ?? false
|
|
97
|
+
};
|
|
98
|
+
return (current) => {
|
|
99
|
+
if (current === void 0 || current === null) return next;
|
|
100
|
+
return reconcileValue(next, current, opts);
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
export {
|
|
104
|
+
reconcile
|
|
105
|
+
};
|
|
@@ -1 +1,303 @@
|
|
|
1
|
-
"use strict";
|
|
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/lib/object/selector.ts
|
|
21
|
+
var selector_exports = {};
|
|
22
|
+
__export(selector_exports, {
|
|
23
|
+
createFilterFunction: () => createFilterFunction,
|
|
24
|
+
createSelector: () => createSelector,
|
|
25
|
+
detectSelectorType: () => detectSelectorType,
|
|
26
|
+
parseSelector: () => parseSelector,
|
|
27
|
+
stringifySelector: () => stringifySelector,
|
|
28
|
+
validateSelector: () => validateSelector
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(selector_exports);
|
|
31
|
+
var parseSelector = (selector, options = {}) => {
|
|
32
|
+
const { type = "auto", throwOnError = false } = options;
|
|
33
|
+
if (!selector || typeof selector !== "string") {
|
|
34
|
+
return [];
|
|
35
|
+
}
|
|
36
|
+
const segments = [];
|
|
37
|
+
let current = "";
|
|
38
|
+
let inBrackets = false;
|
|
39
|
+
let inQuotes = false;
|
|
40
|
+
let quoteChar = "";
|
|
41
|
+
let bracketContent = "";
|
|
42
|
+
let bracketDepth = 0;
|
|
43
|
+
const detectType = (str) => {
|
|
44
|
+
if (str.includes("..")) return "descendant";
|
|
45
|
+
if (str.includes("[?(")) return "filter";
|
|
46
|
+
if (str.includes("[:") || str.includes("[0:")) return "slice";
|
|
47
|
+
if (str.includes("[,") || str.includes("[0,1]")) return "union";
|
|
48
|
+
if (str.includes("[*]") || str.includes(".*.")) return "wildcard";
|
|
49
|
+
if (str.includes("[]")) return "array-wildcard";
|
|
50
|
+
if (str.includes("[") && str.includes("]")) return "bracket";
|
|
51
|
+
return "dot";
|
|
52
|
+
};
|
|
53
|
+
const actualType = type === "auto" ? detectType(selector) : type;
|
|
54
|
+
const parseBracketContent = (content) => {
|
|
55
|
+
const trimmed = content.trim();
|
|
56
|
+
if (trimmed === "*" || trimmed === "") {
|
|
57
|
+
return {
|
|
58
|
+
type: "wildcard",
|
|
59
|
+
value: trimmed === "" ? "[]" : "*",
|
|
60
|
+
raw: trimmed === "" ? "[]" : content
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
if (trimmed === "[]") {
|
|
64
|
+
return { type: "wildcard", value: "[]", raw: content };
|
|
65
|
+
}
|
|
66
|
+
if (/^-?\d+$/.test(trimmed)) {
|
|
67
|
+
return { type: "index", value: parseInt(trimmed, 10), raw: content };
|
|
68
|
+
}
|
|
69
|
+
if (trimmed.includes(":")) {
|
|
70
|
+
const parts = trimmed.split(":").map((p) => p.trim());
|
|
71
|
+
const start = parts[0] === "" ? 0 : parseInt(parts[0], 10);
|
|
72
|
+
const end = parts[1] === "" ? Infinity : parseInt(parts[1], 10);
|
|
73
|
+
if (isNaN(start) || isNaN(end)) {
|
|
74
|
+
if (throwOnError) throw new Error(`Invalid slice notation: ${content}`);
|
|
75
|
+
return { type: "wildcard", value: "*", raw: content };
|
|
76
|
+
}
|
|
77
|
+
return { type: "slice", value: [start, end], raw: content };
|
|
78
|
+
}
|
|
79
|
+
if (trimmed.includes(",")) {
|
|
80
|
+
const indices = trimmed.split(",").map((p) => {
|
|
81
|
+
const num = parseInt(p.trim(), 10);
|
|
82
|
+
return isNaN(num) ? -1 : num;
|
|
83
|
+
}).filter((n) => n >= 0);
|
|
84
|
+
if (indices.length === 0) {
|
|
85
|
+
if (throwOnError) throw new Error(`Invalid union notation: ${content}`);
|
|
86
|
+
return { type: "wildcard", value: "*", raw: content };
|
|
87
|
+
}
|
|
88
|
+
return { type: "union", value: indices, raw: content };
|
|
89
|
+
}
|
|
90
|
+
if (trimmed.startsWith("?(") && trimmed.endsWith(")")) {
|
|
91
|
+
const expr = trimmed.slice(2, -1);
|
|
92
|
+
try {
|
|
93
|
+
const filterFn = createFilterFunction(expr);
|
|
94
|
+
return { type: "filter", value: filterFn, raw: content };
|
|
95
|
+
} catch (error) {
|
|
96
|
+
if (throwOnError) throw error;
|
|
97
|
+
return { type: "wildcard", value: "*", raw: content };
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
if (trimmed.startsWith('"') && trimmed.endsWith('"') || trimmed.startsWith("'") && trimmed.endsWith("'")) {
|
|
101
|
+
return { type: "property", value: trimmed.slice(1, -1), raw: content };
|
|
102
|
+
}
|
|
103
|
+
return { type: "property", value: trimmed, raw: content };
|
|
104
|
+
};
|
|
105
|
+
for (let i = 0; i < selector.length; i++) {
|
|
106
|
+
const char = selector[i];
|
|
107
|
+
const nextChar = selector[i + 1];
|
|
108
|
+
if ((char === '"' || char === "'") && !inQuotes) {
|
|
109
|
+
inQuotes = true;
|
|
110
|
+
quoteChar = char;
|
|
111
|
+
continue;
|
|
112
|
+
} else if (char === quoteChar && inQuotes) {
|
|
113
|
+
inQuotes = false;
|
|
114
|
+
quoteChar = "";
|
|
115
|
+
if (!(char === '"' || char === "'")) {
|
|
116
|
+
if (inBrackets) {
|
|
117
|
+
bracketContent += char;
|
|
118
|
+
} else {
|
|
119
|
+
current += char;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (inQuotes) {
|
|
125
|
+
if (inBrackets) {
|
|
126
|
+
bracketContent += char;
|
|
127
|
+
} else {
|
|
128
|
+
current += char;
|
|
129
|
+
}
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
if (char === "[" && !inBrackets) {
|
|
133
|
+
if (current) {
|
|
134
|
+
segments.push({ type: "property", value: current, raw: current });
|
|
135
|
+
current = "";
|
|
136
|
+
}
|
|
137
|
+
inBrackets = true;
|
|
138
|
+
bracketContent = "";
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
if (char === "]" && inBrackets && !inQuotes) {
|
|
142
|
+
inBrackets = false;
|
|
143
|
+
const segment = parseBracketContent(bracketContent);
|
|
144
|
+
segments.push(segment);
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
let lastDot = segments.length > 0 ? segments[segments.length - 1] : null;
|
|
148
|
+
if (char === "." && !inBrackets && !inQuotes) {
|
|
149
|
+
if (nextChar === ".") {
|
|
150
|
+
const l = selector.split("..")[0];
|
|
151
|
+
segments.push({ type: "property", value: l, raw: l });
|
|
152
|
+
segments.push({ type: "descendant", value: "..", raw: ".." });
|
|
153
|
+
i++;
|
|
154
|
+
} else if (current) {
|
|
155
|
+
if (current === "*") {
|
|
156
|
+
segments.push({ type: "wildcard", value: current, raw: current });
|
|
157
|
+
} else {
|
|
158
|
+
segments.push({ type: "property", value: current, raw: current });
|
|
159
|
+
}
|
|
160
|
+
current = "";
|
|
161
|
+
}
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
if (inBrackets) {
|
|
165
|
+
bracketContent += char;
|
|
166
|
+
} else {
|
|
167
|
+
if (lastDot && lastDot.type !== "descendant") {
|
|
168
|
+
current += char;
|
|
169
|
+
} else {
|
|
170
|
+
const l = selector.split(lastDot?.raw)[1];
|
|
171
|
+
if (l !== void 0) {
|
|
172
|
+
current = l;
|
|
173
|
+
} else {
|
|
174
|
+
current += char;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
if (current) {
|
|
180
|
+
segments.push({ type: "property", value: current, raw: current });
|
|
181
|
+
}
|
|
182
|
+
return segments.map((segment) => {
|
|
183
|
+
if (segment.type === "property" && typeof segment.value === "string") {
|
|
184
|
+
if (/^-?\d+$/.test(segment.value)) {
|
|
185
|
+
const num = parseInt(segment.value, 10);
|
|
186
|
+
if (!isNaN(num)) {
|
|
187
|
+
return { ...segment, type: "index", value: num };
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return segment;
|
|
192
|
+
});
|
|
193
|
+
};
|
|
194
|
+
var createFilterFunction = (expr) => {
|
|
195
|
+
const safeExpr = expr.replace(/@\.([a-zA-Z_$][\w$]*)/g, (_, prop) => `item.${prop}`).replace(/@/g, "item");
|
|
196
|
+
try {
|
|
197
|
+
return new Function("item", `return ${safeExpr}`);
|
|
198
|
+
} catch (error) {
|
|
199
|
+
throw new Error(`Invalid filter expression: ${expr}`);
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
var stringifySelector = (segments) => {
|
|
203
|
+
const parts = [];
|
|
204
|
+
for (let i = 0; i < segments.length; i++) {
|
|
205
|
+
const segment = segments[i];
|
|
206
|
+
const prevSegment = i > 0 ? segments[i - 1] : null;
|
|
207
|
+
switch (segment.type) {
|
|
208
|
+
case "property":
|
|
209
|
+
const needsQuotes = /[\.\[\]\s]/.test(String(segment.value));
|
|
210
|
+
if (needsQuotes) {
|
|
211
|
+
parts.push(`["${segment.value}"]`);
|
|
212
|
+
} else {
|
|
213
|
+
if (prevSegment && prevSegment.type !== "descendant") {
|
|
214
|
+
parts.push(".");
|
|
215
|
+
}
|
|
216
|
+
parts.push(String(segment.value));
|
|
217
|
+
}
|
|
218
|
+
break;
|
|
219
|
+
case "index":
|
|
220
|
+
parts.push(`[${segment.value}]`);
|
|
221
|
+
break;
|
|
222
|
+
case "wildcard":
|
|
223
|
+
if (segment.value === "[]") {
|
|
224
|
+
parts.push("[]");
|
|
225
|
+
} else {
|
|
226
|
+
parts.push("[*]");
|
|
227
|
+
}
|
|
228
|
+
break;
|
|
229
|
+
case "slice":
|
|
230
|
+
const [start, end] = segment.value;
|
|
231
|
+
parts.push(`[${start}:${end === Infinity ? "" : end}]`);
|
|
232
|
+
break;
|
|
233
|
+
case "union":
|
|
234
|
+
const indices = segment.value;
|
|
235
|
+
parts.push(`[${indices.join(",")}]`);
|
|
236
|
+
break;
|
|
237
|
+
case "filter":
|
|
238
|
+
parts.push(`[?(${segment.raw?.slice(2, -1) || ""})]`);
|
|
239
|
+
break;
|
|
240
|
+
case "descendant":
|
|
241
|
+
parts.push("..");
|
|
242
|
+
break;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return parts.join("").replace(/\.\[/g, "[");
|
|
246
|
+
};
|
|
247
|
+
var validateSelector = (selector, options = {}) => {
|
|
248
|
+
if (Array.isArray(selector)) {
|
|
249
|
+
return selector.map(
|
|
250
|
+
(sel) => validateSelector(sel, options)
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
if (!/^[a-zA-Z]/.test(selector)) {
|
|
254
|
+
return {
|
|
255
|
+
valid: false,
|
|
256
|
+
error: `Selector starts with '.'`,
|
|
257
|
+
type: detectSelectorType(selector)
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
if (!/[a-zA-Z]$/.test(selector)) {
|
|
261
|
+
return {
|
|
262
|
+
valid: false,
|
|
263
|
+
error: `Selector end with '.'`,
|
|
264
|
+
type: detectSelectorType(selector)
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
try {
|
|
268
|
+
const segments = parseSelector(selector, { throwOnError: true });
|
|
269
|
+
const type = detectSelectorType(selector);
|
|
270
|
+
if (options.allowedTypes) {
|
|
271
|
+
if (!options.allowedTypes.includes(type)) {
|
|
272
|
+
return {
|
|
273
|
+
valid: false,
|
|
274
|
+
error: `Selector type ${type} not allowed. Allowed: ${options.allowedTypes.join(
|
|
275
|
+
", "
|
|
276
|
+
)}`,
|
|
277
|
+
type
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return { valid: true, type: detectSelectorType(selector) };
|
|
282
|
+
} catch (error) {
|
|
283
|
+
return { valid: false, error: error.message };
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
var detectSelectorType = (selector) => {
|
|
287
|
+
if (selector.includes("..")) return "descendant";
|
|
288
|
+
if (selector.includes("[?(")) return "filter";
|
|
289
|
+
if (selector.includes("[:") || selector.includes("[0:")) return "slice";
|
|
290
|
+
if (/\[[^\]]*,[^\]]*\]/.test(selector)) return "union";
|
|
291
|
+
if (selector.includes("[*]") || selector.includes(".*.")) return "wildcard";
|
|
292
|
+
if (selector.includes("[]")) return "array-wildcard";
|
|
293
|
+
if (selector.includes("[") && selector.includes("]")) return "bracket";
|
|
294
|
+
return "dot";
|
|
295
|
+
};
|
|
296
|
+
var createSelector = (template, replacements) => {
|
|
297
|
+
return template.replace(/\{(\w+)\}/g, (_, key) => {
|
|
298
|
+
if (key in replacements) {
|
|
299
|
+
return String(replacements[key]);
|
|
300
|
+
}
|
|
301
|
+
return `{${key}}`;
|
|
302
|
+
});
|
|
303
|
+
};
|