@signaldb/core 2.0.0-beta.6 → 2.0.0-beta.8
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/.vite/manifest.json +118 -84
- package/dist/AsyncDataAdapter.d.ts +4 -6
- package/dist/AutoFetchDataAdapter.d.ts +4 -6
- package/dist/Collection/Cursor.d.ts +3 -3
- package/dist/Collection/index.d.ts +8 -9
- package/dist/Collection/types.d.ts +2 -2
- package/dist/DataAdapter.d.ts +3 -4
- package/dist/DefaultDataAdapter.d.ts +3 -5
- package/dist/WorkerDataAdapter.d.ts +2 -4
- package/dist/WorkerDataAdapterHost.d.ts +5 -5
- package/dist/createIndex.d.ts +2 -2
- package/dist/createIndexProvider.d.ts +2 -2
- package/dist/createReactivityAdapter.d.ts +2 -2
- package/dist/createStorageAdapter.d.ts +1 -1
- package/dist/getIndexInfo.d.ts +3 -4
- package/dist/index.cjs.js +33 -34
- package/dist/index.mjs +17 -35
- package/dist/index10.cjs.js +23 -0
- package/dist/index10.mjs +21 -17
- package/dist/index11.cjs.js +45 -0
- package/dist/index11.mjs +44 -15
- package/dist/index12.cjs.js +22 -0
- package/dist/index12.mjs +21 -47
- package/dist/index13.cjs.js +45 -0
- package/dist/index13.mjs +44 -365
- package/dist/index14.cjs.js +86 -0
- package/dist/index14.mjs +85 -416
- package/dist/index15.cjs.js +15 -0
- package/dist/index15.mjs +14 -206
- package/dist/index16.cjs.js +136 -0
- package/dist/index16.mjs +135 -388
- package/dist/index17.cjs.js +47 -0
- package/dist/index17.mjs +45 -578
- package/dist/index18.cjs.js +15 -0
- package/dist/index18.mjs +14 -157
- package/dist/index19.cjs.js +23 -0
- package/dist/index19.mjs +22 -26
- package/dist/index2.cjs.js +33 -0
- package/dist/index2.mjs +32 -258
- package/dist/index20.cjs.js +37 -0
- package/dist/index20.mjs +36 -41
- package/dist/index21.cjs.js +35 -0
- package/dist/index21.mjs +34 -16
- package/dist/index22.cjs.js +25 -0
- package/dist/index22.mjs +24 -104
- package/dist/index23.cjs.js +19 -0
- package/dist/index23.mjs +18 -128
- package/dist/index24.cjs.js +342 -0
- package/dist/index24.mjs +340 -6
- package/dist/index25.cjs.js +519 -0
- package/dist/index25.mjs +518 -27
- package/dist/index26.cjs.js +11 -0
- package/dist/index26.mjs +10 -10
- package/dist/index27.cjs.js +11 -0
- package/dist/index27.mjs +10 -29
- package/dist/index28.cjs.js +90 -0
- package/dist/index28.mjs +88 -46
- package/dist/index29.cjs.js +370 -0
- package/dist/index29.mjs +369 -8
- package/dist/index3.cjs.js +19 -0
- package/dist/index3.mjs +18 -539
- package/dist/index30.cjs.js +72 -0
- package/dist/index30.mjs +71 -9
- package/dist/index31.cjs.js +191 -0
- package/dist/index31.mjs +190 -7
- package/dist/index32.cjs.js +21 -0
- package/dist/index32.mjs +20 -42
- package/dist/index33.cjs.js +336 -0
- package/dist/index33.mjs +335 -29
- package/dist/index34.cjs.js +539 -0
- package/dist/index34.mjs +537 -4
- package/dist/index4.cjs.js +150 -0
- package/dist/index4.mjs +149 -5
- package/dist/index5.cjs.js +254 -0
- package/dist/index5.mjs +252 -4
- package/dist/index6.cjs.js +125 -0
- package/dist/index6.mjs +124 -29
- package/dist/index7.cjs.js +33 -0
- package/dist/index7.mjs +32 -12
- package/dist/index8.cjs.js +12 -0
- package/dist/index8.mjs +10 -4
- package/dist/index9.cjs.js +11 -0
- package/dist/index9.mjs +10 -133
- package/dist/types/IndexProvider.d.ts +2 -2
- package/dist/types/Modifier.d.ts +1 -1
- package/dist/types/ReactivityAdapter.d.ts +1 -1
- package/dist/utils/createSignal.d.ts +2 -2
- package/dist/utils/getMatchingKeys.d.ts +2 -2
- package/dist/utils/isFieldExpression.d.ts +1 -1
- package/dist/utils/match.d.ts +1 -1
- package/dist/utils/modify.d.ts +1 -1
- package/dist/utils/queryId.d.ts +2 -2
- package/package.json +1 -1
- package/dist/index.cjs10.js +0 -18
- package/dist/index.cjs11.js +0 -15
- package/dist/index.cjs12.js +0 -48
- package/dist/index.cjs13.js +0 -365
- package/dist/index.cjs14.js +0 -416
- package/dist/index.cjs15.js +0 -206
- package/dist/index.cjs16.js +0 -388
- package/dist/index.cjs17.js +0 -579
- package/dist/index.cjs18.js +0 -157
- package/dist/index.cjs19.js +0 -26
- package/dist/index.cjs2.js +0 -259
- package/dist/index.cjs20.js +0 -42
- package/dist/index.cjs21.js +0 -16
- package/dist/index.cjs22.js +0 -104
- package/dist/index.cjs23.js +0 -129
- package/dist/index.cjs24.js +0 -7
- package/dist/index.cjs25.js +0 -27
- package/dist/index.cjs26.js +0 -10
- package/dist/index.cjs27.js +0 -29
- package/dist/index.cjs28.js +0 -46
- package/dist/index.cjs29.js +0 -8
- package/dist/index.cjs3.js +0 -540
- package/dist/index.cjs30.js +0 -9
- package/dist/index.cjs31.js +0 -7
- package/dist/index.cjs32.js +0 -42
- package/dist/index.cjs33.js +0 -29
- package/dist/index.cjs34.js +0 -5
- package/dist/index.cjs4.js +0 -5
- package/dist/index.cjs5.js +0 -5
- package/dist/index.cjs6.js +0 -29
- package/dist/index.cjs7.js +0 -12
- package/dist/index.cjs8.js +0 -5
- package/dist/index.cjs9.js +0 -133
package/dist/index20.mjs
CHANGED
|
@@ -1,42 +1,37 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
1
|
+
//#region src/utils/set.ts
|
|
2
|
+
/**
|
|
3
|
+
* Sets a value at a specified path within an object. Creates nested structures
|
|
4
|
+
* (arrays or objects) as needed to set the value at the correct location. Supports
|
|
5
|
+
* deleting the key if the value is `undefined` and the `deleteIfUndefined` flag is set to `true`.
|
|
6
|
+
* @template T - The type of the object to modify.
|
|
7
|
+
* @template K - The type of the value to set.
|
|
8
|
+
* @param object - The object to modify. The object is mutated directly.
|
|
9
|
+
* @param path - The path (dot or bracket notation) where the value should be set.
|
|
10
|
+
* @param value - The value to set at the specified path.
|
|
11
|
+
* @param deleteIfUndefined - A boolean indicating whether to delete the key if the value is `undefined` (default: `false`).
|
|
12
|
+
* @returns The modified object.
|
|
13
|
+
*/
|
|
14
|
+
function set(object, path, value, deleteIfUndefined = false) {
|
|
15
|
+
if (object == null) return object;
|
|
16
|
+
const segments = path.split(/[.[\]]/g);
|
|
17
|
+
if (segments[0] === "") segments.shift();
|
|
18
|
+
if (segments.at(-1) === "") segments.pop();
|
|
19
|
+
const apply = (node) => {
|
|
20
|
+
if (segments.length > 1) {
|
|
21
|
+
const key = segments.shift();
|
|
22
|
+
const nextIsNumber = !Number.isNaN(Number.parseInt(segments[0], 10));
|
|
23
|
+
if (node[key] === void 0) node[key] = nextIsNumber ? [] : {};
|
|
24
|
+
apply(node[key]);
|
|
25
|
+
} else {
|
|
26
|
+
if (deleteIfUndefined && value === void 0) {
|
|
27
|
+
delete node[segments[0]];
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
node[segments[0]] = value;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
apply(object);
|
|
34
|
+
return object;
|
|
33
35
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
return structuredClone(object);
|
|
37
|
-
return clone(object);
|
|
38
|
-
}
|
|
39
|
-
export {
|
|
40
|
-
clone,
|
|
41
|
-
deepClone as default
|
|
42
|
-
};
|
|
36
|
+
//#endregion
|
|
37
|
+
export { set as default };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
const require_get = require("./index10.cjs.js");
|
|
2
|
+
const require_set = require("./index20.cjs.js");
|
|
3
|
+
//#region src/utils/project.ts
|
|
4
|
+
/**
|
|
5
|
+
* Projects the fields of an object based on a specified fields configuration.
|
|
6
|
+
* Supports inclusion (`1`) and exclusion (`0`) of specific fields. Creates a new object
|
|
7
|
+
* with the desired fields included or excluded, based on the configuration.
|
|
8
|
+
* @template T - The type of the object being projected.
|
|
9
|
+
* @param item - The original object to project fields from.
|
|
10
|
+
* @param fields - An object defining the fields to include (`1`) or exclude (`0`).
|
|
11
|
+
* - Keys are the field names, and values are either `1` (include) or `0` (exclude).
|
|
12
|
+
* @returns A new object with the specified fields included or excluded.
|
|
13
|
+
* - If all fields are set to `0`, the excluded fields are removed from the result.
|
|
14
|
+
* - If fields are set to `1`, only the included fields are retained.
|
|
15
|
+
*/
|
|
16
|
+
function project(item, fields) {
|
|
17
|
+
if (Object.values(fields).every((value) => value === 0)) {
|
|
18
|
+
const result = { ...item };
|
|
19
|
+
Object.keys(fields).forEach((key) => {
|
|
20
|
+
if (require_get.default(item, key) === void 0) return;
|
|
21
|
+
require_set.default(result, key, void 0, true);
|
|
22
|
+
});
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
25
|
+
const result = {};
|
|
26
|
+
Object.entries(fields).forEach(([key, value]) => {
|
|
27
|
+
const fieldValue = require_get.default(item, key);
|
|
28
|
+
if (fieldValue === void 0) return;
|
|
29
|
+
if (fieldValue == null && value !== 1) return;
|
|
30
|
+
require_set.default(result, key, value === 1 ? fieldValue : void 0);
|
|
31
|
+
});
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
//#endregion
|
|
35
|
+
exports.default = project;
|
package/dist/index21.mjs
CHANGED
|
@@ -1,17 +1,35 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
import get from "./index10.mjs";
|
|
2
|
+
import set from "./index20.mjs";
|
|
3
|
+
//#region src/utils/project.ts
|
|
4
|
+
/**
|
|
5
|
+
* Projects the fields of an object based on a specified fields configuration.
|
|
6
|
+
* Supports inclusion (`1`) and exclusion (`0`) of specific fields. Creates a new object
|
|
7
|
+
* with the desired fields included or excluded, based on the configuration.
|
|
8
|
+
* @template T - The type of the object being projected.
|
|
9
|
+
* @param item - The original object to project fields from.
|
|
10
|
+
* @param fields - An object defining the fields to include (`1`) or exclude (`0`).
|
|
11
|
+
* - Keys are the field names, and values are either `1` (include) or `0` (exclude).
|
|
12
|
+
* @returns A new object with the specified fields included or excluded.
|
|
13
|
+
* - If all fields are set to `0`, the excluded fields are removed from the result.
|
|
14
|
+
* - If fields are set to `1`, only the included fields are retained.
|
|
15
|
+
*/
|
|
16
|
+
function project(item, fields) {
|
|
17
|
+
if (Object.values(fields).every((value) => value === 0)) {
|
|
18
|
+
const result = { ...item };
|
|
19
|
+
Object.keys(fields).forEach((key) => {
|
|
20
|
+
if (get(item, key) === void 0) return;
|
|
21
|
+
set(result, key, void 0, true);
|
|
22
|
+
});
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
25
|
+
const result = {};
|
|
26
|
+
Object.entries(fields).forEach(([key, value]) => {
|
|
27
|
+
const fieldValue = get(item, key);
|
|
28
|
+
if (fieldValue === void 0) return;
|
|
29
|
+
if (fieldValue == null && value !== 1) return;
|
|
30
|
+
set(result, key, value === 1 ? fieldValue : void 0);
|
|
31
|
+
});
|
|
32
|
+
return result;
|
|
9
33
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const optionsId = isEmptyOptions(options) ? -1 : JSON.stringify(options);
|
|
13
|
-
return `${selectorId}:${optionsId}`;
|
|
14
|
-
}
|
|
15
|
-
export {
|
|
16
|
-
queryId as default
|
|
17
|
-
};
|
|
34
|
+
//#endregion
|
|
35
|
+
export { project as default };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
//#region src/utils/queryId.ts
|
|
2
|
+
/**
|
|
3
|
+
* Returns true when `options` is effectively "empty" for the purpose of queryId generation.
|
|
4
|
+
* Treats `undefined`/`null` as empty, and only considers plain objects with zero own enumerable keys as empty.
|
|
5
|
+
* Arrays and non-object values are not considered empty.
|
|
6
|
+
* @param options - Query options to test.
|
|
7
|
+
* @returns `true` if `options` is `null`/`undefined` or a plain object with no own keys; otherwise `false`.
|
|
8
|
+
*/
|
|
9
|
+
function isEmptyOptions(options) {
|
|
10
|
+
if (options == null) return true;
|
|
11
|
+
if (typeof options !== "object") return false;
|
|
12
|
+
if (Array.isArray(options)) return false;
|
|
13
|
+
return Object.keys(options).length === 0;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Generates a unique identifier for a query based on its selector and options.
|
|
17
|
+
* @param selector - The selector object.
|
|
18
|
+
* @param options - The query options object (optional).
|
|
19
|
+
* @returns A unique identifier string for the query.
|
|
20
|
+
*/
|
|
21
|
+
function queryId(selector, options) {
|
|
22
|
+
return `${JSON.stringify(selector)}:${isEmptyOptions(options) ? -1 : JSON.stringify(options)}`;
|
|
23
|
+
}
|
|
24
|
+
//#endregion
|
|
25
|
+
exports.default = queryId;
|
package/dist/index22.mjs
CHANGED
|
@@ -1,105 +1,25 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
};
|
|
15
|
-
return createIndexProvider({
|
|
16
|
-
query(selector) {
|
|
17
|
-
if (!Object.hasOwnProperty.call(selector, field)) {
|
|
18
|
-
return { matched: false };
|
|
19
|
-
}
|
|
20
|
-
const fieldSelector = selector[field];
|
|
21
|
-
const filteresForNull = fieldSelector == null || fieldSelector.$exists === false;
|
|
22
|
-
const keys = filteresForNull ? { include: null, exclude: [...index.keys()].filter((key) => key != null) } : getMatchingKeys(field, selector);
|
|
23
|
-
if (keys.include == null && keys.exclude == null)
|
|
24
|
-
return { matched: false };
|
|
25
|
-
let includedIds = [];
|
|
26
|
-
if (keys.include == null) {
|
|
27
|
-
for (const set of index.values()) {
|
|
28
|
-
for (const pos of set) {
|
|
29
|
-
includedIds.push(pos);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
} else {
|
|
33
|
-
for (const key of keys.include) {
|
|
34
|
-
const posSet = index.get(key);
|
|
35
|
-
if (posSet) {
|
|
36
|
-
for (const pos of posSet) {
|
|
37
|
-
includedIds.push(pos);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
if (keys.exclude != null) {
|
|
43
|
-
const excludeIds = /* @__PURE__ */ new Set();
|
|
44
|
-
for (const key of keys.exclude) {
|
|
45
|
-
const posSet = index.get(key);
|
|
46
|
-
if (posSet) {
|
|
47
|
-
for (const pos of posSet) {
|
|
48
|
-
excludeIds.add(pos);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
includedIds = includedIds.filter((pos) => !excludeIds.has(pos));
|
|
53
|
-
}
|
|
54
|
-
return {
|
|
55
|
-
matched: true,
|
|
56
|
-
ids: includedIds,
|
|
57
|
-
fields: [field],
|
|
58
|
-
keepSelector: filteresForNull
|
|
59
|
-
};
|
|
60
|
-
},
|
|
61
|
-
rebuild(items) {
|
|
62
|
-
index.clear();
|
|
63
|
-
items.forEach((item) => {
|
|
64
|
-
const value = serializeValue(get(item, field));
|
|
65
|
-
ensureSet(value).add(item.id);
|
|
66
|
-
});
|
|
67
|
-
},
|
|
68
|
-
// NEW: delta methods
|
|
69
|
-
insert(items) {
|
|
70
|
-
for (const item of items) {
|
|
71
|
-
const value = serializeValue(get(item, field));
|
|
72
|
-
ensureSet(value).add(item.id);
|
|
73
|
-
}
|
|
74
|
-
},
|
|
75
|
-
remove(items) {
|
|
76
|
-
for (const item of items) {
|
|
77
|
-
const value = serializeValue(get(item, field));
|
|
78
|
-
const set = index.get(value);
|
|
79
|
-
if (!set)
|
|
80
|
-
continue;
|
|
81
|
-
set.delete(item.id);
|
|
82
|
-
if (set.size === 0)
|
|
83
|
-
index.delete(value);
|
|
84
|
-
}
|
|
85
|
-
},
|
|
86
|
-
update(pairs) {
|
|
87
|
-
for (const { oldItem, newItem } of pairs) {
|
|
88
|
-
const oldValue = serializeValue(get(oldItem, field));
|
|
89
|
-
const newValue = serializeValue(get(newItem, field));
|
|
90
|
-
if (oldValue === newValue)
|
|
91
|
-
continue;
|
|
92
|
-
const oldSet = index.get(oldValue);
|
|
93
|
-
if (oldSet) {
|
|
94
|
-
oldSet.delete(oldItem.id);
|
|
95
|
-
if (oldSet.size === 0)
|
|
96
|
-
index.delete(oldValue);
|
|
97
|
-
}
|
|
98
|
-
ensureSet(newValue).add(newItem.id);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
});
|
|
1
|
+
//#region src/utils/queryId.ts
|
|
2
|
+
/**
|
|
3
|
+
* Returns true when `options` is effectively "empty" for the purpose of queryId generation.
|
|
4
|
+
* Treats `undefined`/`null` as empty, and only considers plain objects with zero own enumerable keys as empty.
|
|
5
|
+
* Arrays and non-object values are not considered empty.
|
|
6
|
+
* @param options - Query options to test.
|
|
7
|
+
* @returns `true` if `options` is `null`/`undefined` or a plain object with no own keys; otherwise `false`.
|
|
8
|
+
*/
|
|
9
|
+
function isEmptyOptions(options) {
|
|
10
|
+
if (options == null) return true;
|
|
11
|
+
if (typeof options !== "object") return false;
|
|
12
|
+
if (Array.isArray(options)) return false;
|
|
13
|
+
return Object.keys(options).length === 0;
|
|
102
14
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
15
|
+
/**
|
|
16
|
+
* Generates a unique identifier for a query based on its selector and options.
|
|
17
|
+
* @param selector - The selector object.
|
|
18
|
+
* @param options - The query options object (optional).
|
|
19
|
+
* @returns A unique identifier string for the query.
|
|
20
|
+
*/
|
|
21
|
+
function queryId(selector, options) {
|
|
22
|
+
return `${JSON.stringify(selector)}:${isEmptyOptions(options) ? -1 : JSON.stringify(options)}`;
|
|
23
|
+
}
|
|
24
|
+
//#endregion
|
|
25
|
+
export { queryId as default };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const require_get = require("./index10.cjs.js");
|
|
2
|
+
let fast_sort = require("fast-sort");
|
|
3
|
+
//#region src/utils/sortItems.ts
|
|
4
|
+
/**
|
|
5
|
+
* Sorts an array of items based on multiple fields and their specified sort order.
|
|
6
|
+
* Uses the `fast-sort` library for efficient sorting.
|
|
7
|
+
* @template T - The type of the items in the array.
|
|
8
|
+
* @param items - The array of items to be sorted.
|
|
9
|
+
* @param sortFields - An object defining the sort order for each field.
|
|
10
|
+
* - Keys are the field names, and values are either `1` (ascending) or `-1` (descending).
|
|
11
|
+
* @returns A new array of items sorted based on the specified fields and their order.
|
|
12
|
+
*/
|
|
13
|
+
function sortItems(items, sortFields) {
|
|
14
|
+
return (0, fast_sort.sort)(items).by(Object.entries(sortFields).map(([key, value]) => {
|
|
15
|
+
return { [value === 1 ? "asc" : "desc"]: (i) => require_get.default(i, key) };
|
|
16
|
+
}));
|
|
17
|
+
}
|
|
18
|
+
//#endregion
|
|
19
|
+
exports.default = sortItems;
|
package/dist/index23.mjs
CHANGED
|
@@ -1,129 +1,19 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const memo2 = memoOrPromise instanceof Promise ? await memoOrPromise : memoOrPromise;
|
|
18
|
-
return processResult(memo2, result);
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
const memo = memoOrPromise;
|
|
22
|
-
if (memo instanceof Promise)
|
|
23
|
-
throw new Error("Mixing async and sync index providers is not supported");
|
|
24
|
-
return processResult(memo, resultOrPromise);
|
|
25
|
-
}, {
|
|
26
|
-
matched: false,
|
|
27
|
-
ids: [],
|
|
28
|
-
optimizedSelector: { ...selector }
|
|
29
|
-
});
|
|
1
|
+
import get from "./index10.mjs";
|
|
2
|
+
import { sort } from "fast-sort";
|
|
3
|
+
//#region src/utils/sortItems.ts
|
|
4
|
+
/**
|
|
5
|
+
* Sorts an array of items based on multiple fields and their specified sort order.
|
|
6
|
+
* Uses the `fast-sort` library for efficient sorting.
|
|
7
|
+
* @template T - The type of the items in the array.
|
|
8
|
+
* @param items - The array of items to be sorted.
|
|
9
|
+
* @param sortFields - An object defining the sort order for each field.
|
|
10
|
+
* - Keys are the field names, and values are either `1` (ascending) or `-1` (descending).
|
|
11
|
+
* @returns A new array of items sorted based on the specified fields and their order.
|
|
12
|
+
*/
|
|
13
|
+
function sortItems(items, sortFields) {
|
|
14
|
+
return sort(items).by(Object.entries(sortFields).map(([key, value]) => {
|
|
15
|
+
return { [value === 1 ? "asc" : "desc"]: (i) => get(i, key) };
|
|
16
|
+
}));
|
|
30
17
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const getSelector = (indexInfo) => {
|
|
34
|
-
const { matched: selMatched, ids: selIds, optimizedSelector: optimizedSelector2 } = indexInfo;
|
|
35
|
-
if (selMatched) {
|
|
36
|
-
idsCallback(true, selIds);
|
|
37
|
-
if (Object.keys(optimizedSelector2).length > 0) {
|
|
38
|
-
return optimizedSelector2;
|
|
39
|
-
}
|
|
40
|
-
} else {
|
|
41
|
-
idsCallback(false, []);
|
|
42
|
-
return sel;
|
|
43
|
-
}
|
|
44
|
-
};
|
|
45
|
-
const indexInfoOrPromise = getIndexInfo(queryFunctions, sel);
|
|
46
|
-
if (indexInfoOrPromise instanceof Promise) {
|
|
47
|
-
return indexInfoOrPromise.then(async (indexInfo) => {
|
|
48
|
-
const memo2 = memoOrPromise instanceof Promise ? await memoOrPromise : memoOrPromise;
|
|
49
|
-
const optimizedSelector2 = getSelector(indexInfo);
|
|
50
|
-
if (optimizedSelector2)
|
|
51
|
-
memo2.push(optimizedSelector2);
|
|
52
|
-
return memo2;
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
const memo = memoOrPromise;
|
|
56
|
-
if (memo instanceof Promise)
|
|
57
|
-
throw new Error("Mixing async and sync index providers is not supported");
|
|
58
|
-
const optimizedSelector = getSelector(indexInfoOrPromise);
|
|
59
|
-
if (optimizedSelector)
|
|
60
|
-
memo.push(optimizedSelector);
|
|
61
|
-
return memo;
|
|
62
|
-
}, []);
|
|
63
|
-
}
|
|
64
|
-
function getIndexInfo(queryFunctions, selector) {
|
|
65
|
-
if (selector == null || Object.keys(selector).length <= 0) {
|
|
66
|
-
return {
|
|
67
|
-
matched: false,
|
|
68
|
-
ids: [],
|
|
69
|
-
optimizedSelector: selector
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
const { $and, $or, ...rest } = selector;
|
|
73
|
-
const flatInfoOrPromise = getMergedIndexInfo(queryFunctions, rest);
|
|
74
|
-
const processFlatInfo = (flatInfo) => {
|
|
75
|
-
let { matched, ids } = flatInfo;
|
|
76
|
-
const newSelector = flatInfo.optimizedSelector;
|
|
77
|
-
const $andNewOrPromise = Array.isArray($and) ? optimizeLogicGate(queryFunctions, $and, (match, selIds) => {
|
|
78
|
-
if (!match)
|
|
79
|
-
return;
|
|
80
|
-
ids = matched ? intersection(ids, selIds) : selIds;
|
|
81
|
-
matched = true;
|
|
82
|
-
}) : void 0;
|
|
83
|
-
const process$and = ($andNew) => {
|
|
84
|
-
if ($andNew && $andNew.length > 0)
|
|
85
|
-
newSelector.$and = $andNew;
|
|
86
|
-
let hasNonIndexField = false;
|
|
87
|
-
const matchedBefore = matched;
|
|
88
|
-
const idsBefore = ids;
|
|
89
|
-
const process$or = ($orNew) => {
|
|
90
|
-
if ($orNew && $orNew.length > 0)
|
|
91
|
-
newSelector.$or = $orNew;
|
|
92
|
-
if (hasNonIndexField) {
|
|
93
|
-
newSelector.$or = $or;
|
|
94
|
-
matched = matchedBefore;
|
|
95
|
-
ids = idsBefore;
|
|
96
|
-
}
|
|
97
|
-
return {
|
|
98
|
-
matched,
|
|
99
|
-
ids: ids || [],
|
|
100
|
-
optimizedSelector: newSelector
|
|
101
|
-
};
|
|
102
|
-
};
|
|
103
|
-
const $orNewOrPromise = Array.isArray($or) ? optimizeLogicGate(queryFunctions, $or, (match, selIds) => {
|
|
104
|
-
if (match) {
|
|
105
|
-
ids = [.../* @__PURE__ */ new Set([...ids, ...selIds])];
|
|
106
|
-
matched = true;
|
|
107
|
-
} else {
|
|
108
|
-
hasNonIndexField = true;
|
|
109
|
-
}
|
|
110
|
-
}) : void 0;
|
|
111
|
-
if ($orNewOrPromise instanceof Promise) {
|
|
112
|
-
return $orNewOrPromise.then(($orNew) => process$or($orNew));
|
|
113
|
-
}
|
|
114
|
-
return process$or($orNewOrPromise);
|
|
115
|
-
};
|
|
116
|
-
if ($andNewOrPromise instanceof Promise) {
|
|
117
|
-
return $andNewOrPromise.then(($andNew) => process$and($andNew));
|
|
118
|
-
}
|
|
119
|
-
return process$and($andNewOrPromise);
|
|
120
|
-
};
|
|
121
|
-
if (flatInfoOrPromise instanceof Promise) {
|
|
122
|
-
return flatInfoOrPromise.then(processFlatInfo);
|
|
123
|
-
}
|
|
124
|
-
return processFlatInfo(flatInfoOrPromise);
|
|
125
|
-
}
|
|
126
|
-
export {
|
|
127
|
-
getIndexInfo as default,
|
|
128
|
-
getMergedIndexInfo
|
|
129
|
-
};
|
|
18
|
+
//#endregion
|
|
19
|
+
export { sortItems as default };
|