@medyll/idae-engine 1.65.0 → 1.66.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/dataOp/dataOp.d.ts +39 -6
- package/dist/dataOp/dataOp.js +25 -22
- package/package.json +11 -11
package/dist/dataOp/dataOp.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type ResolverPathType } from
|
|
1
|
+
import { type ResolverPathType } from "./types.js";
|
|
2
2
|
export type DataOpGroupResult<T> = {
|
|
3
3
|
[key: string]: {
|
|
4
4
|
title: string;
|
|
@@ -8,35 +8,68 @@ export type DataOpGroupResult<T> = {
|
|
|
8
8
|
};
|
|
9
9
|
export type DataOpSortBy<T> = {
|
|
10
10
|
arr: T[];
|
|
11
|
-
by: Partial<Record<ResolverPathType<T>,
|
|
11
|
+
by: Partial<Record<ResolverPathType<T>, "asc" | "desc" | 1 | -1 | undefined>>;
|
|
12
12
|
options?: {
|
|
13
13
|
keepRef: true;
|
|
14
14
|
};
|
|
15
15
|
} | {
|
|
16
16
|
arr: T[];
|
|
17
17
|
by: ResolverPathType<T> | ResolverPathType<T>[];
|
|
18
|
-
sort?:
|
|
18
|
+
sort?: "asc" | "desc" | 1 | -1 | undefined;
|
|
19
19
|
options?: {
|
|
20
20
|
keepRef: true;
|
|
21
21
|
};
|
|
22
22
|
};
|
|
23
|
+
/**
|
|
24
|
+
* Represents the parameters for a data operation find function.
|
|
25
|
+
*
|
|
26
|
+
* @template T - The type of elements in the array.
|
|
27
|
+
*
|
|
28
|
+
* @property {number | string} kw - The keyword to search for in the array.
|
|
29
|
+
* @property {T[]} arr - The array of elements to search within.
|
|
30
|
+
* @property {ResolverPathType<T> | "*"} [field] - The field or path to search within each element. If not provided, the entire element is searched.
|
|
31
|
+
* @property {boolean} [caseSensitive] - Whether the search should be case-sensitive. Defaults to false.
|
|
32
|
+
* @property {false} [strict] - Whether the search should be strict. Defaults to false.
|
|
33
|
+
*/
|
|
23
34
|
export type DataOpFind<T> = {
|
|
35
|
+
/** keyword */
|
|
24
36
|
kw: number | string;
|
|
25
37
|
arr: T[];
|
|
26
|
-
field?: ResolverPathType<T> |
|
|
38
|
+
field?: ResolverPathType<T> | "*";
|
|
27
39
|
caseSensitive?: boolean;
|
|
28
40
|
strict?: false;
|
|
29
41
|
};
|
|
42
|
+
/**
|
|
43
|
+
* Represents a data operation that groups a list of data items.
|
|
44
|
+
*
|
|
45
|
+
* @template T - The type of the data items in the list.
|
|
46
|
+
*
|
|
47
|
+
* @property {T[]} dataList - The list of data items to be grouped.
|
|
48
|
+
* @property {DataOpGroupByOptions<T>} groupBy - The options for grouping the data items.
|
|
49
|
+
* @property {boolean} [keepUngroupedData] - Optional flag indicating whether to keep ungrouped data items.
|
|
50
|
+
*/
|
|
30
51
|
export type DataOpGroupBy<T> = {
|
|
31
52
|
dataList: T[];
|
|
32
53
|
groupBy: DataOpGroupByOptions<T>;
|
|
33
54
|
keepUngroupedData?: boolean;
|
|
34
55
|
};
|
|
56
|
+
/**
|
|
57
|
+
* Represents the options for grouping data operations.
|
|
58
|
+
*
|
|
59
|
+
* @template T - The type of the items being grouped.
|
|
60
|
+
*
|
|
61
|
+
* This type can be one of the following:
|
|
62
|
+
* - A function that takes an item of type `T` and returns an object with `title` and `code` properties.
|
|
63
|
+
* - A `ResolverPathType` of type `T`.
|
|
64
|
+
* - An array of `ResolverPathType` of type `T`.
|
|
65
|
+
*/
|
|
35
66
|
export type DataOpGroupByOptions<T> = ((item: T) => {
|
|
36
67
|
title: string;
|
|
37
68
|
code: string;
|
|
38
69
|
}) | ResolverPathType<T> | ResolverPathType<T>[];
|
|
39
|
-
/**
|
|
70
|
+
/**
|
|
71
|
+
* A class that provides various data operations such as getting keys, sorting, finding, and grouping objects in an array.
|
|
72
|
+
*/
|
|
40
73
|
export declare class dataOp {
|
|
41
74
|
/**
|
|
42
75
|
* Get all keys of the objects in an array of objects.
|
|
@@ -104,7 +137,7 @@ export declare class dataOp {
|
|
|
104
137
|
* @param {boolean} [options.keepUngroupedData=false] - Whether to keep ungrouped data.
|
|
105
138
|
* @returns {DataOpGroupResult<T>} An object where keys are group codes and values are objects containing title, code, and data array.
|
|
106
139
|
*/
|
|
107
|
-
static groupBy<T
|
|
140
|
+
static groupBy<T>(options: DataOpGroupBy<T>): DataOpGroupResult<T>;
|
|
108
141
|
/**
|
|
109
142
|
*
|
|
110
143
|
* @param arr array to find in
|
package/dist/dataOp/dataOp.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import {} from
|
|
2
|
-
/**
|
|
1
|
+
import {} from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* A class that provides various data operations such as getting keys, sorting, finding, and grouping objects in an array.
|
|
4
|
+
*/
|
|
3
5
|
export class dataOp {
|
|
4
6
|
/**
|
|
5
7
|
* Get all keys of the objects in an array of objects.
|
|
@@ -11,12 +13,12 @@ export class dataOp {
|
|
|
11
13
|
*/
|
|
12
14
|
static getDataKeys(arr) {
|
|
13
15
|
const keys = new Set();
|
|
14
|
-
function addKeys(obj, prefix =
|
|
16
|
+
function addKeys(obj, prefix = "") {
|
|
15
17
|
for (const key in obj) {
|
|
16
18
|
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
17
19
|
const fullKey = prefix ? `${prefix}.${key}` : key;
|
|
18
20
|
keys.add(fullKey);
|
|
19
|
-
if (typeof obj[key] ===
|
|
21
|
+
if (typeof obj[key] === "object" && obj[key] !== null) {
|
|
20
22
|
addKeys(obj[key], fullKey);
|
|
21
23
|
}
|
|
22
24
|
}
|
|
@@ -61,13 +63,14 @@ export class dataOp {
|
|
|
61
63
|
static sortBy(sortOptions) {
|
|
62
64
|
const { arr, options } = sortOptions;
|
|
63
65
|
const sortArray = options?.keepRef ? arr : [...arr];
|
|
64
|
-
if (!sortOptions.by ||
|
|
66
|
+
if (!sortOptions.by ||
|
|
67
|
+
(Array.isArray(sortOptions.by) && sortOptions.by.length === 0)) {
|
|
65
68
|
return sortArray;
|
|
66
69
|
}
|
|
67
70
|
return sortArray.sort((a, b) => {
|
|
68
|
-
if (
|
|
69
|
-
const { by, sort =
|
|
70
|
-
const sortD = sort ===
|
|
71
|
+
if ("sort" in sortOptions) {
|
|
72
|
+
const { by, sort = "asc" } = sortOptions;
|
|
73
|
+
const sortD = sort === "asc" || sort === 1 ? 1 : -1;
|
|
71
74
|
const fields = Array.isArray(by) ? by : [by];
|
|
72
75
|
for (const field of fields) {
|
|
73
76
|
const comparison = this.compareValues(a, b, field, sortD);
|
|
@@ -77,7 +80,7 @@ export class dataOp {
|
|
|
77
80
|
}
|
|
78
81
|
else {
|
|
79
82
|
for (const [field, direction] of Object.entries(sortOptions.by)) {
|
|
80
|
-
const sortD = direction ===
|
|
83
|
+
const sortD = direction === "asc" || Number(direction) === 1 ? 1 : -1;
|
|
81
84
|
const comparison = this.compareValues(a, b, field, sortD);
|
|
82
85
|
if (comparison !== 0)
|
|
83
86
|
return comparison;
|
|
@@ -98,9 +101,9 @@ export class dataOp {
|
|
|
98
101
|
* @returns {T[]} An array of objects that match the search criteria.
|
|
99
102
|
*/
|
|
100
103
|
static find(options) {
|
|
101
|
-
const { arr, kw, field =
|
|
104
|
+
const { arr, kw, field = "*", caseSensitive = false, strict = false, } = options;
|
|
102
105
|
return arr.filter((item) => {
|
|
103
|
-
if (field !==
|
|
106
|
+
if (field !== "*") {
|
|
104
107
|
const itemValue = this.resolveDotPath(item, field);
|
|
105
108
|
return this.compareSearchValues(itemValue, kw, caseSensitive, strict);
|
|
106
109
|
}
|
|
@@ -119,9 +122,9 @@ export class dataOp {
|
|
|
119
122
|
* @returns {T | undefined} The first object that matches the search criteria, or undefined if no match is found.
|
|
120
123
|
*/
|
|
121
124
|
static findOne(options) {
|
|
122
|
-
const { arr, kw, field =
|
|
125
|
+
const { arr, kw, field = "*", caseSensitive = false, strict = false, } = options;
|
|
123
126
|
return arr.find((item) => {
|
|
124
|
-
if (field !==
|
|
127
|
+
if (field !== "*") {
|
|
125
128
|
const itemValue = this.resolveDotPath(item, field);
|
|
126
129
|
return this.compareSearchValues(itemValue, kw, caseSensitive, strict);
|
|
127
130
|
}
|
|
@@ -139,10 +142,10 @@ export class dataOp {
|
|
|
139
142
|
* @returns {DataOpGroupResult<T>} An object where keys are group codes and values are objects containing title, code, and data array.
|
|
140
143
|
*/
|
|
141
144
|
static groupBy(options) {
|
|
142
|
-
const { dataList, groupBy: groupField, keepUngroupedData = false } = options;
|
|
145
|
+
const { dataList, groupBy: groupField, keepUngroupedData = false, } = options;
|
|
143
146
|
return dataList.reduce((result, currentItem) => {
|
|
144
147
|
let groupInfo;
|
|
145
|
-
if (typeof groupField ===
|
|
148
|
+
if (typeof groupField === "function") {
|
|
146
149
|
groupInfo = groupField(currentItem);
|
|
147
150
|
}
|
|
148
151
|
else {
|
|
@@ -150,7 +153,7 @@ export class dataOp {
|
|
|
150
153
|
const groupValue = fields
|
|
151
154
|
.map((field) => this.resolveDotPath(currentItem, field))
|
|
152
155
|
.filter((value) => value !== undefined)
|
|
153
|
-
.join(
|
|
156
|
+
.join(".");
|
|
154
157
|
if (groupValue) {
|
|
155
158
|
groupInfo = { title: groupValue, code: groupValue };
|
|
156
159
|
}
|
|
@@ -158,14 +161,14 @@ export class dataOp {
|
|
|
158
161
|
if (!groupInfo) {
|
|
159
162
|
if (!keepUngroupedData)
|
|
160
163
|
return result;
|
|
161
|
-
groupInfo = { title:
|
|
164
|
+
groupInfo = { title: "- Ungrouped", code: "- ungrouped" };
|
|
162
165
|
}
|
|
163
166
|
const groupKey = groupInfo.code;
|
|
164
167
|
if (!result[groupKey]) {
|
|
165
168
|
result[groupKey] = {
|
|
166
169
|
title: groupInfo.title,
|
|
167
170
|
code: groupInfo.code,
|
|
168
|
-
data: []
|
|
171
|
+
data: [],
|
|
169
172
|
};
|
|
170
173
|
}
|
|
171
174
|
result[groupKey].data.push(currentItem);
|
|
@@ -179,13 +182,13 @@ export class dataOp {
|
|
|
179
182
|
* @param key object key to match with
|
|
180
183
|
* @returns number
|
|
181
184
|
*/
|
|
182
|
-
static findByIndex(arr, value, key =
|
|
185
|
+
static findByIndex(arr, value, key = "id") {
|
|
183
186
|
return arr.findIndex((obj) => this.resolveDotPath(obj, key) === value);
|
|
184
187
|
}
|
|
185
188
|
static resolveDotPath(object, path, defaultValue) {
|
|
186
189
|
return path
|
|
187
|
-
.split(
|
|
188
|
-
.reduce((r, s) => (r && typeof r ===
|
|
190
|
+
.split(".")
|
|
191
|
+
.reduce((r, s) => (r && typeof r === "object" && s in r ? r[s] : defaultValue), object);
|
|
189
192
|
}
|
|
190
193
|
/**
|
|
191
194
|
* Compares two values for sorting.
|
|
@@ -201,7 +204,7 @@ export class dataOp {
|
|
|
201
204
|
static compareValues(a, b, field, sortD) {
|
|
202
205
|
const aValue = this.resolveDotPath(a, field);
|
|
203
206
|
const bValue = this.resolveDotPath(b, field);
|
|
204
|
-
if (typeof aValue ===
|
|
207
|
+
if (typeof aValue === "string" && typeof bValue === "string") {
|
|
205
208
|
return sortD * aValue.localeCompare(bValue);
|
|
206
209
|
}
|
|
207
210
|
return sortD * (aValue > bValue ? 1 : aValue < bValue ? -1 : 0);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@medyll/idae-engine",
|
|
3
3
|
"scope": "@medyll",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.66.0",
|
|
5
5
|
"description": "A powerful TypeScript library for data manipulation and operations across the idae-engine. Provides utility classes for sorting, finding, grouping, and more.",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"dev": "vite dev",
|
|
@@ -31,18 +31,18 @@
|
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"@medyll/idae-prettier-config": "^1.1.0",
|
|
34
|
-
"@sveltejs/adapter-auto": "^
|
|
35
|
-
"@sveltejs/kit": "^2.
|
|
36
|
-
"@sveltejs/package": "^2.3.
|
|
37
|
-
"@sveltejs/vite-plugin-svelte": "^
|
|
38
|
-
"svelte": "^5.
|
|
39
|
-
"svelte-check": "^
|
|
40
|
-
"tslib": "^2.
|
|
34
|
+
"@sveltejs/adapter-auto": "^4.0.0",
|
|
35
|
+
"@sveltejs/kit": "^2.18.0",
|
|
36
|
+
"@sveltejs/package": "^2.3.10",
|
|
37
|
+
"@sveltejs/vite-plugin-svelte": "^5.0.3",
|
|
38
|
+
"svelte": "^5.22.4",
|
|
39
|
+
"svelte-check": "^4.1.4",
|
|
40
|
+
"tslib": "^2.8.1",
|
|
41
41
|
"typescript": "^5.8.2",
|
|
42
|
-
"vite": "^
|
|
43
|
-
"vitest": "^
|
|
42
|
+
"vite": "^6.2.0",
|
|
43
|
+
"vitest": "^3.0.7"
|
|
44
44
|
},
|
|
45
45
|
"svelte": "./dist/index.js",
|
|
46
46
|
"types": "./dist/index.d.ts",
|
|
47
47
|
"type": "module"
|
|
48
|
-
}
|
|
48
|
+
}
|