@medyll/idae-engine 1.0.0 → 1.1.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/README.md +121 -121
- package/dist/dataOp/dataOp.d.ts +37 -29
- package/dist/dataOp/dataOp.js +51 -25
- package/package.json +47 -46
package/README.md
CHANGED
|
@@ -1,122 +1,122 @@
|
|
|
1
|
-
# @medyll/engine
|
|
2
|
-
|
|
3
|
-
A powerful TypeScript library for data manipulation and operations across the idae-engine.
|
|
4
|
-
|
|
5
|
-
## Installation
|
|
6
|
-
|
|
7
|
-
Install the package using npm:
|
|
8
|
-
|
|
9
|
-
```bash
|
|
10
|
-
npm install @medyll/idae-engine
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
Or using yarn:
|
|
14
|
-
|
|
15
|
-
```bash
|
|
16
|
-
yarn add @medyll/idae-engine
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
## Usage
|
|
20
|
-
|
|
21
|
-
The `@medyll/idae-eengine` package contains several utility classes. This section focuses on the `dataOp` class, which provides various data manipulation methods.
|
|
22
|
-
|
|
23
|
-
### Importing
|
|
24
|
-
|
|
25
|
-
```typescript
|
|
26
|
-
import { dataOp } from '@medyll/idae-engine';
|
|
27
|
-
```
|
|
28
|
-
## dataOp
|
|
29
|
-
### Methods
|
|
30
|
-
|
|
31
|
-
#### do
|
|
32
|
-
|
|
33
|
-
Performs a combination of sort, find, and group operations on data.
|
|
34
|
-
|
|
35
|
-
```typescript
|
|
36
|
-
const result = dataOp.do({
|
|
37
|
-
sort: { arr: myArray, by: 'fieldName', sort: 'asc' },
|
|
38
|
-
find: { arr: myArray, kw: 'searchTerm', field: 'fieldName' },
|
|
39
|
-
group: { dataList: myArray, groupBy: 'fieldName' }
|
|
40
|
-
});
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
#### sortBy
|
|
44
|
-
|
|
45
|
-
Sorts an array of objects based on specified fields.
|
|
46
|
-
|
|
47
|
-
```typescript
|
|
48
|
-
const sortedArray = dataOp.sortBy({
|
|
49
|
-
arr: myArray,
|
|
50
|
-
by: 'fieldName',
|
|
51
|
-
sort: 'asc'
|
|
52
|
-
});
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
#### find
|
|
56
|
-
|
|
57
|
-
Searches for objects in an array based on specified criteria.
|
|
58
|
-
|
|
59
|
-
```typescript
|
|
60
|
-
const foundItems = dataOp.find({
|
|
61
|
-
arr: myArray,
|
|
62
|
-
kw: 'searchTerm',
|
|
63
|
-
field: 'fieldName'
|
|
64
|
-
});
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
#### findOne
|
|
68
|
-
|
|
69
|
-
Searches for the first object in an array that matches the specified criteria.
|
|
70
|
-
|
|
71
|
-
```typescript
|
|
72
|
-
const foundItem = dataOp.findOne({
|
|
73
|
-
arr: myArray,
|
|
74
|
-
kw: 'searchTerm',
|
|
75
|
-
field: 'fieldName'
|
|
76
|
-
});
|
|
77
|
-
```
|
|
78
|
-
|
|
79
|
-
#### groupBy
|
|
80
|
-
|
|
81
|
-
Groups objects in an array based on specified fields or a custom grouping function.
|
|
82
|
-
|
|
83
|
-
```typescript
|
|
84
|
-
const groupedData = dataOp.groupBy({
|
|
85
|
-
dataList: myArray,
|
|
86
|
-
groupBy: 'fieldName'
|
|
87
|
-
});
|
|
88
|
-
```
|
|
89
|
-
|
|
90
|
-
#### findByIndex
|
|
91
|
-
|
|
92
|
-
Finds the index of an object in an array based on a specified key-value pair.
|
|
93
|
-
|
|
94
|
-
```typescript
|
|
95
|
-
const index = dataOp.findByIndex(myArray, 'value', 'keyName');
|
|
96
|
-
```
|
|
97
|
-
|
|
98
|
-
#### resolveDotPath
|
|
99
|
-
|
|
100
|
-
Resolves a dot-notated path in an object.
|
|
101
|
-
|
|
102
|
-
```typescript
|
|
103
|
-
const value = dataOp.resolveDotPath(myObject, 'path.to.property');
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
### Testing
|
|
107
|
-
|
|
108
|
-
The `dataOp` class comes with a comprehensive test suite. To run the tests:
|
|
109
|
-
|
|
110
|
-
1. Clone the repository
|
|
111
|
-
2. Install dependencies: `npm install` or `yarn install`
|
|
112
|
-
3. Run tests: `npm test` or `yarn test`
|
|
113
|
-
|
|
114
|
-
The tests cover various scenarios for each method, ensuring the reliability and correctness of the `dataOp` class.
|
|
115
|
-
|
|
116
|
-
## Contributing
|
|
117
|
-
|
|
118
|
-
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
119
|
-
|
|
120
|
-
## License
|
|
121
|
-
|
|
1
|
+
# @medyll/engine
|
|
2
|
+
|
|
3
|
+
A powerful TypeScript library for data manipulation and operations across the idae-engine.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Install the package using npm:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @medyll/idae-engine
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Or using yarn:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
yarn add @medyll/idae-engine
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
The `@medyll/idae-eengine` package contains several utility classes. This section focuses on the `dataOp` class, which provides various data manipulation methods.
|
|
22
|
+
|
|
23
|
+
### Importing
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import { dataOp } from '@medyll/idae-engine';
|
|
27
|
+
```
|
|
28
|
+
## dataOp
|
|
29
|
+
### Methods
|
|
30
|
+
|
|
31
|
+
#### do
|
|
32
|
+
|
|
33
|
+
Performs a combination of sort, find, and group operations on data.
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
const result = dataOp.do({
|
|
37
|
+
sort: { arr: myArray, by: 'fieldName', sort: 'asc' },
|
|
38
|
+
find: { arr: myArray, kw: 'searchTerm', field: 'fieldName' },
|
|
39
|
+
group: { dataList: myArray, groupBy: 'fieldName' }
|
|
40
|
+
});
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
#### sortBy
|
|
44
|
+
|
|
45
|
+
Sorts an array of objects based on specified fields.
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
const sortedArray = dataOp.sortBy({
|
|
49
|
+
arr: myArray,
|
|
50
|
+
by: 'fieldName',
|
|
51
|
+
sort: 'asc'
|
|
52
|
+
});
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
#### find
|
|
56
|
+
|
|
57
|
+
Searches for objects in an array based on specified criteria.
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
const foundItems = dataOp.find({
|
|
61
|
+
arr: myArray,
|
|
62
|
+
kw: 'searchTerm',
|
|
63
|
+
field: 'fieldName'
|
|
64
|
+
});
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
#### findOne
|
|
68
|
+
|
|
69
|
+
Searches for the first object in an array that matches the specified criteria.
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
const foundItem = dataOp.findOne({
|
|
73
|
+
arr: myArray,
|
|
74
|
+
kw: 'searchTerm',
|
|
75
|
+
field: 'fieldName'
|
|
76
|
+
});
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
#### groupBy
|
|
80
|
+
|
|
81
|
+
Groups objects in an array based on specified fields or a custom grouping function.
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
const groupedData = dataOp.groupBy({
|
|
85
|
+
dataList: myArray,
|
|
86
|
+
groupBy: 'fieldName'
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
#### findByIndex
|
|
91
|
+
|
|
92
|
+
Finds the index of an object in an array based on a specified key-value pair.
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
const index = dataOp.findByIndex(myArray, 'value', 'keyName');
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
#### resolveDotPath
|
|
99
|
+
|
|
100
|
+
Resolves a dot-notated path in an object.
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
const value = dataOp.resolveDotPath(myObject, 'path.to.property');
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Testing
|
|
107
|
+
|
|
108
|
+
The `dataOp` class comes with a comprehensive test suite. To run the tests:
|
|
109
|
+
|
|
110
|
+
1. Clone the repository
|
|
111
|
+
2. Install dependencies: `npm install` or `yarn install`
|
|
112
|
+
3. Run tests: `npm test` or `yarn test`
|
|
113
|
+
|
|
114
|
+
The tests cover various scenarios for each method, ensuring the reliability and correctness of the `dataOp` class.
|
|
115
|
+
|
|
116
|
+
## Contributing
|
|
117
|
+
|
|
118
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
119
|
+
|
|
120
|
+
## License
|
|
121
|
+
|
|
122
122
|
This project is licensed under the MIT License.
|
package/dist/dataOp/dataOp.d.ts
CHANGED
|
@@ -1,66 +1,75 @@
|
|
|
1
|
-
import { type ResolverPathType } from
|
|
2
|
-
type
|
|
3
|
-
export type DataGroupResult<T> = {
|
|
1
|
+
import { type ResolverPathType } from './types.js';
|
|
2
|
+
export type DataOpGroupResult<T> = {
|
|
4
3
|
[key: string]: {
|
|
5
4
|
title: string;
|
|
6
5
|
code?: string;
|
|
7
6
|
data: T[];
|
|
8
7
|
};
|
|
9
8
|
};
|
|
10
|
-
type
|
|
9
|
+
export type DataOpSortBy<T> = {
|
|
11
10
|
arr: T[];
|
|
12
|
-
by: Partial<Record<ResolverPathType<T>,
|
|
11
|
+
by: Partial<Record<ResolverPathType<T>, 'asc' | 'desc' | 1 | -1 | undefined>>;
|
|
13
12
|
options?: {
|
|
14
13
|
keepRef: true;
|
|
15
14
|
};
|
|
16
15
|
} | {
|
|
17
16
|
arr: T[];
|
|
18
17
|
by: ResolverPathType<T> | ResolverPathType<T>[];
|
|
19
|
-
sort?:
|
|
18
|
+
sort?: 'asc' | 'desc' | 1 | -1 | undefined;
|
|
20
19
|
options?: {
|
|
21
20
|
keepRef: true;
|
|
22
21
|
};
|
|
23
22
|
};
|
|
24
|
-
type
|
|
23
|
+
export type DataOpFind<T> = {
|
|
25
24
|
kw: number | string;
|
|
26
25
|
arr: T[];
|
|
27
|
-
field?: ResolverPathType<T> |
|
|
26
|
+
field?: ResolverPathType<T> | '*';
|
|
28
27
|
caseSensitive?: boolean;
|
|
29
28
|
strict?: false;
|
|
30
29
|
};
|
|
31
|
-
type
|
|
30
|
+
export type DataOpGroupBy<T> = {
|
|
32
31
|
dataList: T[];
|
|
33
|
-
groupBy:
|
|
34
|
-
title: string;
|
|
35
|
-
code: string;
|
|
36
|
-
}) | ResolverPathType<T> | ResolverPathType<T>[];
|
|
32
|
+
groupBy: DataOpGroupByOptions<T>;
|
|
37
33
|
keepUngroupedData?: boolean;
|
|
38
34
|
};
|
|
35
|
+
export type DataOpGroupByOptions<T> = ((item: T) => {
|
|
36
|
+
title: string;
|
|
37
|
+
code: string;
|
|
38
|
+
}) | ResolverPathType<T> | ResolverPathType<T>[];
|
|
39
39
|
/** data manipulation class */
|
|
40
40
|
export declare class dataOp {
|
|
41
|
+
/**
|
|
42
|
+
* Get all keys of the objects in an array of objects.
|
|
43
|
+
* Keys are delimited by a dot.
|
|
44
|
+
*
|
|
45
|
+
* @template T - The type of objects in the array.
|
|
46
|
+
* @param {T[]} arr - The array of objects.
|
|
47
|
+
* @returns {string[]} An array of all keys.
|
|
48
|
+
*/
|
|
49
|
+
static getDataKeys(arr: T[]): string[];
|
|
41
50
|
/**
|
|
42
51
|
* Performs a combination of sort, find, and group operations on data.
|
|
43
52
|
*
|
|
44
53
|
* @template T - The type of objects in the array, extends Data.
|
|
45
54
|
* @param {Object} options - The options for data operations.
|
|
46
|
-
* @param {
|
|
47
|
-
* @param {
|
|
48
|
-
* @param {
|
|
55
|
+
* @param {DataOpSortBy<T>} [options.sort] - Sorting options.
|
|
56
|
+
* @param {DataOpFind<T>} [options.find] - Finding options.
|
|
57
|
+
* @param {DataOpGroupBy<T>} [options.group] - Grouping options.
|
|
49
58
|
* @returns {T[] | GroupResult<T>} The resulting array or grouped data.
|
|
50
59
|
*/
|
|
51
|
-
static do<T extends
|
|
52
|
-
sort?:
|
|
53
|
-
find?:
|
|
54
|
-
group?:
|
|
55
|
-
}): T[] |
|
|
60
|
+
static do<T extends object>(options: {
|
|
61
|
+
sort?: DataOpSortBy<T>;
|
|
62
|
+
find?: DataOpFind<T>;
|
|
63
|
+
group?: DataOpGroupBy<T>;
|
|
64
|
+
}): T[] | DataOpGroupResult<T>;
|
|
56
65
|
/**
|
|
57
66
|
* Sorts an array of objects based on one or more specified fields with individual sort directions.
|
|
58
67
|
*
|
|
59
68
|
* @template T - The type of objects in the array, extends Data.
|
|
60
|
-
* @param {
|
|
69
|
+
* @param {DataOpSortBy<T>} sortOptions - The sorting options.
|
|
61
70
|
* @returns {T[]} The sorted array.
|
|
62
71
|
*/
|
|
63
|
-
static sortBy<T extends
|
|
72
|
+
static sortBy<T extends object>(sortOptions: DataOpSortBy<T>): T[];
|
|
64
73
|
/**
|
|
65
74
|
* Searches for objects in an array based on specified criteria.
|
|
66
75
|
*
|
|
@@ -72,7 +81,7 @@ export declare class dataOp {
|
|
|
72
81
|
* @param {boolean} [options.caseSensitive=false] - Whether the search should be case-sensitive.
|
|
73
82
|
* @returns {T[]} An array of objects that match the search criteria.
|
|
74
83
|
*/
|
|
75
|
-
static find<T extends object = Record<string, unknown>>(options:
|
|
84
|
+
static find<T extends object = Record<string, unknown>>(options: DataOpFind<T>): T[];
|
|
76
85
|
/**
|
|
77
86
|
* Searches for the first object in an array that matches the specified criteria.
|
|
78
87
|
*
|
|
@@ -84,7 +93,7 @@ export declare class dataOp {
|
|
|
84
93
|
* @param {boolean} [options.caseSensitive=false] - Whether the search should be case-sensitive.
|
|
85
94
|
* @returns {T | undefined} The first object that matches the search criteria, or undefined if no match is found.
|
|
86
95
|
*/
|
|
87
|
-
static findOne<T extends
|
|
96
|
+
static findOne<T extends object>(options: DataOpFind<T>): T | undefined;
|
|
88
97
|
/**
|
|
89
98
|
* Groups objects in an array based on specified fields or a custom grouping function.
|
|
90
99
|
*
|
|
@@ -93,9 +102,9 @@ export declare class dataOp {
|
|
|
93
102
|
* @param {T[]} options.dataList - The array to group.
|
|
94
103
|
* @param {((item: T) => { title: string; code: string }) | ResolverPathType<T> | ResolverPathType<T>[]} options.groupBy - The field(s) to group by or a function to determine the group.
|
|
95
104
|
* @param {boolean} [options.keepUngroupedData=false] - Whether to keep ungrouped data.
|
|
96
|
-
* @returns {
|
|
105
|
+
* @returns {DataOpGroupResult<T>} An object where keys are group codes and values are objects containing title, code, and data array.
|
|
97
106
|
*/
|
|
98
|
-
static groupBy<T
|
|
107
|
+
static groupBy<T extends object>(options: DataOpGroupBy<T>): DataOpGroupResult<T>;
|
|
99
108
|
/**
|
|
100
109
|
*
|
|
101
110
|
* @param arr array to find in
|
|
@@ -104,7 +113,7 @@ export declare class dataOp {
|
|
|
104
113
|
* @returns number
|
|
105
114
|
*/
|
|
106
115
|
static findByIndex<T = Record<string, any>>(arr: T[], value: any, key?: keyof T | ResolverPathType<T>): number;
|
|
107
|
-
static resolveDotPath<T
|
|
116
|
+
static resolveDotPath<T>(object: T, path: ResolverPathType<T>, defaultValue?: any): any;
|
|
108
117
|
/**
|
|
109
118
|
* Compares two values for sorting.
|
|
110
119
|
*
|
|
@@ -119,4 +128,3 @@ export declare class dataOp {
|
|
|
119
128
|
private static compareValues;
|
|
120
129
|
private static compareSearchValues;
|
|
121
130
|
}
|
|
122
|
-
export {};
|
package/dist/dataOp/dataOp.js
CHANGED
|
@@ -1,14 +1,41 @@
|
|
|
1
|
-
import {} from
|
|
1
|
+
import {} from './types.js';
|
|
2
2
|
/** data manipulation class */
|
|
3
3
|
export class dataOp {
|
|
4
|
+
/**
|
|
5
|
+
* Get all keys of the objects in an array of objects.
|
|
6
|
+
* Keys are delimited by a dot.
|
|
7
|
+
*
|
|
8
|
+
* @template T - The type of objects in the array.
|
|
9
|
+
* @param {T[]} arr - The array of objects.
|
|
10
|
+
* @returns {string[]} An array of all keys.
|
|
11
|
+
*/
|
|
12
|
+
static getDataKeys(arr) {
|
|
13
|
+
const keys = new Set();
|
|
14
|
+
function addKeys(obj, prefix = '') {
|
|
15
|
+
for (const key in obj) {
|
|
16
|
+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
17
|
+
const fullKey = prefix ? `${prefix}.${key}` : key;
|
|
18
|
+
keys.add(fullKey);
|
|
19
|
+
if (typeof obj[key] === 'object' && obj[key] !== null) {
|
|
20
|
+
addKeys(obj[key], fullKey);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const limit = Math.min(arr.length, 100);
|
|
26
|
+
for (let i = 0; i < limit; i++) {
|
|
27
|
+
addKeys(arr[i]);
|
|
28
|
+
}
|
|
29
|
+
return Array.from(keys);
|
|
30
|
+
}
|
|
4
31
|
/**
|
|
5
32
|
* Performs a combination of sort, find, and group operations on data.
|
|
6
33
|
*
|
|
7
34
|
* @template T - The type of objects in the array, extends Data.
|
|
8
35
|
* @param {Object} options - The options for data operations.
|
|
9
|
-
* @param {
|
|
10
|
-
* @param {
|
|
11
|
-
* @param {
|
|
36
|
+
* @param {DataOpSortBy<T>} [options.sort] - Sorting options.
|
|
37
|
+
* @param {DataOpFind<T>} [options.find] - Finding options.
|
|
38
|
+
* @param {DataOpGroupBy<T>} [options.group] - Grouping options.
|
|
12
39
|
* @returns {T[] | GroupResult<T>} The resulting array or grouped data.
|
|
13
40
|
*/
|
|
14
41
|
static do(options) {
|
|
@@ -28,20 +55,19 @@ export class dataOp {
|
|
|
28
55
|
* Sorts an array of objects based on one or more specified fields with individual sort directions.
|
|
29
56
|
*
|
|
30
57
|
* @template T - The type of objects in the array, extends Data.
|
|
31
|
-
* @param {
|
|
58
|
+
* @param {DataOpSortBy<T>} sortOptions - The sorting options.
|
|
32
59
|
* @returns {T[]} The sorted array.
|
|
33
60
|
*/
|
|
34
61
|
static sortBy(sortOptions) {
|
|
35
62
|
const { arr, options } = sortOptions;
|
|
36
63
|
const sortArray = options?.keepRef ? arr : [...arr];
|
|
37
|
-
if (!sortOptions.by ||
|
|
38
|
-
(Array.isArray(sortOptions.by) && sortOptions.by.length === 0)) {
|
|
64
|
+
if (!sortOptions.by || (Array.isArray(sortOptions.by) && sortOptions.by.length === 0)) {
|
|
39
65
|
return sortArray;
|
|
40
66
|
}
|
|
41
67
|
return sortArray.sort((a, b) => {
|
|
42
|
-
if (
|
|
43
|
-
const { by, sort =
|
|
44
|
-
const sortD = sort ===
|
|
68
|
+
if ('sort' in sortOptions) {
|
|
69
|
+
const { by, sort = 'asc' } = sortOptions;
|
|
70
|
+
const sortD = sort === 'asc' || sort === 1 ? 1 : -1;
|
|
45
71
|
const fields = Array.isArray(by) ? by : [by];
|
|
46
72
|
for (const field of fields) {
|
|
47
73
|
const comparison = this.compareValues(a, b, field, sortD);
|
|
@@ -51,7 +77,7 @@ export class dataOp {
|
|
|
51
77
|
}
|
|
52
78
|
else {
|
|
53
79
|
for (const [field, direction] of Object.entries(sortOptions.by)) {
|
|
54
|
-
const sortD = direction ===
|
|
80
|
+
const sortD = direction === 'asc' || Number(direction) === 1 ? 1 : -1;
|
|
55
81
|
const comparison = this.compareValues(a, b, field, sortD);
|
|
56
82
|
if (comparison !== 0)
|
|
57
83
|
return comparison;
|
|
@@ -72,9 +98,9 @@ export class dataOp {
|
|
|
72
98
|
* @returns {T[]} An array of objects that match the search criteria.
|
|
73
99
|
*/
|
|
74
100
|
static find(options) {
|
|
75
|
-
const { arr, kw, field =
|
|
101
|
+
const { arr, kw, field = '*', caseSensitive = false, strict = false } = options;
|
|
76
102
|
return arr.filter((item) => {
|
|
77
|
-
if (field !==
|
|
103
|
+
if (field !== '*') {
|
|
78
104
|
const itemValue = this.resolveDotPath(item, field);
|
|
79
105
|
return this.compareSearchValues(itemValue, kw, caseSensitive, strict);
|
|
80
106
|
}
|
|
@@ -93,9 +119,9 @@ export class dataOp {
|
|
|
93
119
|
* @returns {T | undefined} The first object that matches the search criteria, or undefined if no match is found.
|
|
94
120
|
*/
|
|
95
121
|
static findOne(options) {
|
|
96
|
-
const { arr, kw, field =
|
|
122
|
+
const { arr, kw, field = '*', caseSensitive = false, strict = false } = options;
|
|
97
123
|
return arr.find((item) => {
|
|
98
|
-
if (field !==
|
|
124
|
+
if (field !== '*') {
|
|
99
125
|
const itemValue = this.resolveDotPath(item, field);
|
|
100
126
|
return this.compareSearchValues(itemValue, kw, caseSensitive, strict);
|
|
101
127
|
}
|
|
@@ -110,13 +136,13 @@ export class dataOp {
|
|
|
110
136
|
* @param {T[]} options.dataList - The array to group.
|
|
111
137
|
* @param {((item: T) => { title: string; code: string }) | ResolverPathType<T> | ResolverPathType<T>[]} options.groupBy - The field(s) to group by or a function to determine the group.
|
|
112
138
|
* @param {boolean} [options.keepUngroupedData=false] - Whether to keep ungrouped data.
|
|
113
|
-
* @returns {
|
|
139
|
+
* @returns {DataOpGroupResult<T>} An object where keys are group codes and values are objects containing title, code, and data array.
|
|
114
140
|
*/
|
|
115
141
|
static groupBy(options) {
|
|
116
|
-
const { dataList, groupBy: groupField, keepUngroupedData = false
|
|
142
|
+
const { dataList, groupBy: groupField, keepUngroupedData = false } = options;
|
|
117
143
|
return dataList.reduce((result, currentItem) => {
|
|
118
144
|
let groupInfo;
|
|
119
|
-
if (typeof groupField ===
|
|
145
|
+
if (typeof groupField === 'function') {
|
|
120
146
|
groupInfo = groupField(currentItem);
|
|
121
147
|
}
|
|
122
148
|
else {
|
|
@@ -124,7 +150,7 @@ export class dataOp {
|
|
|
124
150
|
const groupValue = fields
|
|
125
151
|
.map((field) => this.resolveDotPath(currentItem, field))
|
|
126
152
|
.filter((value) => value !== undefined)
|
|
127
|
-
.join(
|
|
153
|
+
.join('.');
|
|
128
154
|
if (groupValue) {
|
|
129
155
|
groupInfo = { title: groupValue, code: groupValue };
|
|
130
156
|
}
|
|
@@ -132,14 +158,14 @@ export class dataOp {
|
|
|
132
158
|
if (!groupInfo) {
|
|
133
159
|
if (!keepUngroupedData)
|
|
134
160
|
return result;
|
|
135
|
-
groupInfo = { title:
|
|
161
|
+
groupInfo = { title: '- Ungrouped', code: '- ungrouped' };
|
|
136
162
|
}
|
|
137
163
|
const groupKey = groupInfo.code;
|
|
138
164
|
if (!result[groupKey]) {
|
|
139
165
|
result[groupKey] = {
|
|
140
166
|
title: groupInfo.title,
|
|
141
167
|
code: groupInfo.code,
|
|
142
|
-
data: []
|
|
168
|
+
data: []
|
|
143
169
|
};
|
|
144
170
|
}
|
|
145
171
|
result[groupKey].data.push(currentItem);
|
|
@@ -153,13 +179,13 @@ export class dataOp {
|
|
|
153
179
|
* @param key object key to match with
|
|
154
180
|
* @returns number
|
|
155
181
|
*/
|
|
156
|
-
static findByIndex(arr, value, key =
|
|
182
|
+
static findByIndex(arr, value, key = 'id') {
|
|
157
183
|
return arr.findIndex((obj) => this.resolveDotPath(obj, key) === value);
|
|
158
184
|
}
|
|
159
185
|
static resolveDotPath(object, path, defaultValue) {
|
|
160
186
|
return path
|
|
161
|
-
.split(
|
|
162
|
-
.reduce((r, s) => (r && typeof r ===
|
|
187
|
+
.split('.')
|
|
188
|
+
.reduce((r, s) => (r && typeof r === 'object' && s in r ? r[s] : defaultValue), object);
|
|
163
189
|
}
|
|
164
190
|
/**
|
|
165
191
|
* Compares two values for sorting.
|
|
@@ -175,7 +201,7 @@ export class dataOp {
|
|
|
175
201
|
static compareValues(a, b, field, sortD) {
|
|
176
202
|
const aValue = this.resolveDotPath(a, field);
|
|
177
203
|
const bValue = this.resolveDotPath(b, field);
|
|
178
|
-
if (typeof aValue ===
|
|
204
|
+
if (typeof aValue === 'string' && typeof bValue === 'string') {
|
|
179
205
|
return sortD * aValue.localeCompare(bValue);
|
|
180
206
|
}
|
|
181
207
|
return sortD * (aValue > bValue ? 1 : aValue < bValue ? -1 : 0);
|
package/package.json
CHANGED
|
@@ -1,46 +1,47 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@medyll/idae-engine",
|
|
3
|
-
"scope": "@medyll",
|
|
4
|
-
"version": "1.
|
|
5
|
-
"scripts": {
|
|
6
|
-
"dev": "vite dev",
|
|
7
|
-
"build": "vite build && npm run package",
|
|
8
|
-
"preview": "vite preview",
|
|
9
|
-
"package": "svelte-kit sync && svelte-package && publint",
|
|
10
|
-
"package:watch": "svelte-kit sync && svelte-package --watch",
|
|
11
|
-
"prepublishOnly": "npm run package",
|
|
12
|
-
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
|
|
13
|
-
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
|
|
14
|
-
"test": "vitest"
|
|
15
|
-
},
|
|
16
|
-
"exports": {
|
|
17
|
-
".": {
|
|
18
|
-
"types": "./dist/index.d.ts",
|
|
19
|
-
"svelte": "./dist/index.js"
|
|
20
|
-
}
|
|
21
|
-
},
|
|
22
|
-
"files": [
|
|
23
|
-
"dist",
|
|
24
|
-
"!dist/**/*.test.*",
|
|
25
|
-
"!dist/**/*.spec.*"
|
|
26
|
-
],
|
|
27
|
-
"peerDependencies": {
|
|
28
|
-
"svelte": "^
|
|
29
|
-
},
|
|
30
|
-
"devDependencies": {
|
|
31
|
-
"@
|
|
32
|
-
"@sveltejs/
|
|
33
|
-
"@sveltejs/
|
|
34
|
-
"@sveltejs/
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
"svelte
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@medyll/idae-engine",
|
|
3
|
+
"scope": "@medyll",
|
|
4
|
+
"version": "1.1.0",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"dev": "vite dev",
|
|
7
|
+
"build": "vite build && npm run package",
|
|
8
|
+
"preview": "vite preview",
|
|
9
|
+
"package": "svelte-kit sync && svelte-package && publint",
|
|
10
|
+
"package:watch": "svelte-kit sync && svelte-package --watch",
|
|
11
|
+
"prepublishOnly": "npm run package",
|
|
12
|
+
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
|
|
13
|
+
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
|
|
14
|
+
"test": "vitest"
|
|
15
|
+
},
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"svelte": "./dist/index.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist",
|
|
24
|
+
"!dist/**/*.test.*",
|
|
25
|
+
"!dist/**/*.spec.*"
|
|
26
|
+
],
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"svelte": "^5.0.0-next.1"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@medyll/idae-prettier-config": "^1.0.0",
|
|
32
|
+
"@sveltejs/adapter-auto": "^3.0.0",
|
|
33
|
+
"@sveltejs/kit": "^2.0.0",
|
|
34
|
+
"@sveltejs/package": "^2.0.0",
|
|
35
|
+
"@sveltejs/vite-plugin-svelte": "^3.0.0",
|
|
36
|
+
"publint": "^0.1.9",
|
|
37
|
+
"svelte": "^5.0.0-next.183",
|
|
38
|
+
"svelte-check": "^3.6.0",
|
|
39
|
+
"tslib": "^2.4.1",
|
|
40
|
+
"typescript": "^5.0.0",
|
|
41
|
+
"vite": "^5.0.11",
|
|
42
|
+
"vitest": "^1.2.0"
|
|
43
|
+
},
|
|
44
|
+
"svelte": "./dist/index.js",
|
|
45
|
+
"types": "./dist/index.d.ts",
|
|
46
|
+
"type": "module"
|
|
47
|
+
}
|