@etsoo/shared 1.1.67 → 1.1.69
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 +2 -0
- package/__tests__/Utils.ts +12 -0
- package/lib/cjs/Utils.d.ts +21 -1
- package/lib/cjs/Utils.js +46 -5
- package/lib/mjs/Utils.d.ts +21 -1
- package/lib/mjs/Utils.js +46 -5
- package/package.json +1 -1
- package/src/Utils.ts +42 -6
package/README.md
CHANGED
|
@@ -218,6 +218,8 @@ String and other related utilities
|
|
|
218
218
|
|charsToNumber|Base64 chars to number|
|
|
219
219
|
|correctTypes|Correct object's property value type|
|
|
220
220
|
|equals|Two values equal|
|
|
221
|
+
|exclude|Exclude specific items|
|
|
222
|
+
|excludeAsync|Async exclude specific items|
|
|
221
223
|
|formatInitial|Format inital character to lower case or upper case|
|
|
222
224
|
|formatString|Format string with parameters|
|
|
223
225
|
|getDataChanges|Get data changed fields with input data updated|
|
package/__tests__/Utils.ts
CHANGED
|
@@ -9,6 +9,8 @@ test('Tests for addBlankItem', () => {
|
|
|
9
9
|
expect(options.length).toBe(3);
|
|
10
10
|
expect(options[0].id).toBe('');
|
|
11
11
|
expect(options[0].name).toBe('---');
|
|
12
|
+
Utils.addBlankItem(options, 'id', 'name');
|
|
13
|
+
expect(options.length).toBe(3);
|
|
12
14
|
});
|
|
13
15
|
|
|
14
16
|
test('Tests for correctTypes', () => {
|
|
@@ -65,6 +67,16 @@ test('Tests for getDataChanges', () => {
|
|
|
65
67
|
expect(input.amount).toBeUndefined();
|
|
66
68
|
});
|
|
67
69
|
|
|
70
|
+
test('Tests for exclude', () => {
|
|
71
|
+
const options = [
|
|
72
|
+
{ id1: 1, name: 'a' },
|
|
73
|
+
{ id1: 2, name: 'b' }
|
|
74
|
+
];
|
|
75
|
+
const result = Utils.exclude(options, 'id1', 1);
|
|
76
|
+
expect(result.length).toBe(1);
|
|
77
|
+
expect(result[0].id1).toBe(2);
|
|
78
|
+
});
|
|
79
|
+
|
|
68
80
|
test('Tests for formatInitial', () => {
|
|
69
81
|
expect(Utils.formatInitial('HelloWorld')).toBe('helloWorld');
|
|
70
82
|
expect('HelloWorld'.formatInitial(false)).toBe('helloWorld');
|
package/lib/cjs/Utils.d.ts
CHANGED
|
@@ -53,7 +53,7 @@ export declare namespace Utils {
|
|
|
53
53
|
* @param labelField Label field, default is label
|
|
54
54
|
* @param blankLabel Blank label, default is ---
|
|
55
55
|
*/
|
|
56
|
-
function addBlankItem<T extends object>(options: T[], idField?: string | keyof T, labelField?: unknown, blankLabel?: string):
|
|
56
|
+
function addBlankItem<T extends object>(options: T[], idField?: string | keyof T, labelField?: unknown, blankLabel?: string): T[];
|
|
57
57
|
/**
|
|
58
58
|
* Base64 chars to number
|
|
59
59
|
* @param base64Chars Base64 chars
|
|
@@ -75,6 +75,26 @@ export declare namespace Utils {
|
|
|
75
75
|
* @param strict Strict level, 0 with ==, 1 === but null equal undefined, 2 ===
|
|
76
76
|
*/
|
|
77
77
|
function equals(v1: unknown, v2: unknown, strict?: number): boolean;
|
|
78
|
+
/**
|
|
79
|
+
* Exclude specific items
|
|
80
|
+
* @param items Items
|
|
81
|
+
* @param field Filter field
|
|
82
|
+
* @param excludedValues Excluded values
|
|
83
|
+
* @returns Result
|
|
84
|
+
*/
|
|
85
|
+
function exclude<T extends {
|
|
86
|
+
[P in D]: DataTypes.IdType;
|
|
87
|
+
}, D extends string = 'id'>(items: T[], field: D, ...excludedValues: T[D][]): T[];
|
|
88
|
+
/**
|
|
89
|
+
* Async exclude specific items
|
|
90
|
+
* @param items Items
|
|
91
|
+
* @param field Filter field
|
|
92
|
+
* @param excludedValues Excluded values
|
|
93
|
+
* @returns Result
|
|
94
|
+
*/
|
|
95
|
+
function excludeAsync<T extends {
|
|
96
|
+
[P in D]: DataTypes.IdType;
|
|
97
|
+
}, D extends string = 'id'>(items: Promise<T[] | undefined>, field: D, ...excludedValues: T[D][]): Promise<T[] | undefined>;
|
|
78
98
|
/**
|
|
79
99
|
* Format inital character to lower case or upper case
|
|
80
100
|
* @param input Input string
|
package/lib/cjs/Utils.js
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
2
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
12
|
exports.Utils = void 0;
|
|
4
13
|
const DataTypes_1 = require("./DataTypes");
|
|
@@ -60,11 +69,16 @@ var Utils;
|
|
|
60
69
|
* @param blankLabel Blank label, default is ---
|
|
61
70
|
*/
|
|
62
71
|
function addBlankItem(options, idField, labelField, blankLabel) {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
72
|
+
// Avoid duplicate blank items
|
|
73
|
+
idField !== null && idField !== void 0 ? idField : (idField = 'id');
|
|
74
|
+
if (options.length === 0 || Reflect.get(options[0], idField) !== '') {
|
|
75
|
+
const blankItem = {
|
|
76
|
+
[idField]: '',
|
|
77
|
+
[typeof labelField === 'string' ? labelField : 'label']: blankLabel !== null && blankLabel !== void 0 ? blankLabel : '---'
|
|
78
|
+
};
|
|
79
|
+
options.unshift(blankItem);
|
|
80
|
+
}
|
|
81
|
+
return options;
|
|
68
82
|
}
|
|
69
83
|
Utils.addBlankItem = addBlankItem;
|
|
70
84
|
/**
|
|
@@ -122,6 +136,33 @@ var Utils;
|
|
|
122
136
|
return v1 === v2;
|
|
123
137
|
}
|
|
124
138
|
Utils.equals = equals;
|
|
139
|
+
/**
|
|
140
|
+
* Exclude specific items
|
|
141
|
+
* @param items Items
|
|
142
|
+
* @param field Filter field
|
|
143
|
+
* @param excludedValues Excluded values
|
|
144
|
+
* @returns Result
|
|
145
|
+
*/
|
|
146
|
+
function exclude(items, field, ...excludedValues) {
|
|
147
|
+
return items.filter((item) => !excludedValues.includes(item[field]));
|
|
148
|
+
}
|
|
149
|
+
Utils.exclude = exclude;
|
|
150
|
+
/**
|
|
151
|
+
* Async exclude specific items
|
|
152
|
+
* @param items Items
|
|
153
|
+
* @param field Filter field
|
|
154
|
+
* @param excludedValues Excluded values
|
|
155
|
+
* @returns Result
|
|
156
|
+
*/
|
|
157
|
+
function excludeAsync(items, field, ...excludedValues) {
|
|
158
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
159
|
+
const result = yield items;
|
|
160
|
+
if (result == null)
|
|
161
|
+
return result;
|
|
162
|
+
return exclude(result, field, ...excludedValues);
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
Utils.excludeAsync = excludeAsync;
|
|
125
166
|
/**
|
|
126
167
|
* Format inital character to lower case or upper case
|
|
127
168
|
* @param input Input string
|
package/lib/mjs/Utils.d.ts
CHANGED
|
@@ -53,7 +53,7 @@ export declare namespace Utils {
|
|
|
53
53
|
* @param labelField Label field, default is label
|
|
54
54
|
* @param blankLabel Blank label, default is ---
|
|
55
55
|
*/
|
|
56
|
-
function addBlankItem<T extends object>(options: T[], idField?: string | keyof T, labelField?: unknown, blankLabel?: string):
|
|
56
|
+
function addBlankItem<T extends object>(options: T[], idField?: string | keyof T, labelField?: unknown, blankLabel?: string): T[];
|
|
57
57
|
/**
|
|
58
58
|
* Base64 chars to number
|
|
59
59
|
* @param base64Chars Base64 chars
|
|
@@ -75,6 +75,26 @@ export declare namespace Utils {
|
|
|
75
75
|
* @param strict Strict level, 0 with ==, 1 === but null equal undefined, 2 ===
|
|
76
76
|
*/
|
|
77
77
|
function equals(v1: unknown, v2: unknown, strict?: number): boolean;
|
|
78
|
+
/**
|
|
79
|
+
* Exclude specific items
|
|
80
|
+
* @param items Items
|
|
81
|
+
* @param field Filter field
|
|
82
|
+
* @param excludedValues Excluded values
|
|
83
|
+
* @returns Result
|
|
84
|
+
*/
|
|
85
|
+
function exclude<T extends {
|
|
86
|
+
[P in D]: DataTypes.IdType;
|
|
87
|
+
}, D extends string = 'id'>(items: T[], field: D, ...excludedValues: T[D][]): T[];
|
|
88
|
+
/**
|
|
89
|
+
* Async exclude specific items
|
|
90
|
+
* @param items Items
|
|
91
|
+
* @param field Filter field
|
|
92
|
+
* @param excludedValues Excluded values
|
|
93
|
+
* @returns Result
|
|
94
|
+
*/
|
|
95
|
+
function excludeAsync<T extends {
|
|
96
|
+
[P in D]: DataTypes.IdType;
|
|
97
|
+
}, D extends string = 'id'>(items: Promise<T[] | undefined>, field: D, ...excludedValues: T[D][]): Promise<T[] | undefined>;
|
|
78
98
|
/**
|
|
79
99
|
* Format inital character to lower case or upper case
|
|
80
100
|
* @param input Input string
|
package/lib/mjs/Utils.js
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
1
10
|
import { DataTypes } from './DataTypes';
|
|
2
11
|
String.prototype.format = function (...parameters) {
|
|
3
12
|
let template = this;
|
|
@@ -57,11 +66,16 @@ export var Utils;
|
|
|
57
66
|
* @param blankLabel Blank label, default is ---
|
|
58
67
|
*/
|
|
59
68
|
function addBlankItem(options, idField, labelField, blankLabel) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
69
|
+
// Avoid duplicate blank items
|
|
70
|
+
idField !== null && idField !== void 0 ? idField : (idField = 'id');
|
|
71
|
+
if (options.length === 0 || Reflect.get(options[0], idField) !== '') {
|
|
72
|
+
const blankItem = {
|
|
73
|
+
[idField]: '',
|
|
74
|
+
[typeof labelField === 'string' ? labelField : 'label']: blankLabel !== null && blankLabel !== void 0 ? blankLabel : '---'
|
|
75
|
+
};
|
|
76
|
+
options.unshift(blankItem);
|
|
77
|
+
}
|
|
78
|
+
return options;
|
|
65
79
|
}
|
|
66
80
|
Utils.addBlankItem = addBlankItem;
|
|
67
81
|
/**
|
|
@@ -119,6 +133,33 @@ export var Utils;
|
|
|
119
133
|
return v1 === v2;
|
|
120
134
|
}
|
|
121
135
|
Utils.equals = equals;
|
|
136
|
+
/**
|
|
137
|
+
* Exclude specific items
|
|
138
|
+
* @param items Items
|
|
139
|
+
* @param field Filter field
|
|
140
|
+
* @param excludedValues Excluded values
|
|
141
|
+
* @returns Result
|
|
142
|
+
*/
|
|
143
|
+
function exclude(items, field, ...excludedValues) {
|
|
144
|
+
return items.filter((item) => !excludedValues.includes(item[field]));
|
|
145
|
+
}
|
|
146
|
+
Utils.exclude = exclude;
|
|
147
|
+
/**
|
|
148
|
+
* Async exclude specific items
|
|
149
|
+
* @param items Items
|
|
150
|
+
* @param field Filter field
|
|
151
|
+
* @param excludedValues Excluded values
|
|
152
|
+
* @returns Result
|
|
153
|
+
*/
|
|
154
|
+
function excludeAsync(items, field, ...excludedValues) {
|
|
155
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
156
|
+
const result = yield items;
|
|
157
|
+
if (result == null)
|
|
158
|
+
return result;
|
|
159
|
+
return exclude(result, field, ...excludedValues);
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
Utils.excludeAsync = excludeAsync;
|
|
122
163
|
/**
|
|
123
164
|
* Format inital character to lower case or upper case
|
|
124
165
|
* @param input Input string
|
package/package.json
CHANGED
package/src/Utils.ts
CHANGED
|
@@ -125,12 +125,18 @@ export namespace Utils {
|
|
|
125
125
|
labelField?: unknown,
|
|
126
126
|
blankLabel?: string
|
|
127
127
|
) {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
128
|
+
// Avoid duplicate blank items
|
|
129
|
+
idField ??= 'id';
|
|
130
|
+
if (options.length === 0 || Reflect.get(options[0], idField) !== '') {
|
|
131
|
+
const blankItem: any = {
|
|
132
|
+
[idField]: '',
|
|
133
|
+
[typeof labelField === 'string' ? labelField : 'label']:
|
|
134
|
+
blankLabel ?? '---'
|
|
135
|
+
};
|
|
136
|
+
options.unshift(blankItem);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return options;
|
|
134
140
|
}
|
|
135
141
|
|
|
136
142
|
/**
|
|
@@ -193,6 +199,36 @@ export namespace Utils {
|
|
|
193
199
|
return v1 === v2;
|
|
194
200
|
}
|
|
195
201
|
|
|
202
|
+
/**
|
|
203
|
+
* Exclude specific items
|
|
204
|
+
* @param items Items
|
|
205
|
+
* @param field Filter field
|
|
206
|
+
* @param excludedValues Excluded values
|
|
207
|
+
* @returns Result
|
|
208
|
+
*/
|
|
209
|
+
export function exclude<
|
|
210
|
+
T extends { [P in D]: DataTypes.IdType },
|
|
211
|
+
D extends string = 'id'
|
|
212
|
+
>(items: T[], field: D, ...excludedValues: T[D][]) {
|
|
213
|
+
return items.filter((item) => !excludedValues.includes(item[field]));
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Async exclude specific items
|
|
218
|
+
* @param items Items
|
|
219
|
+
* @param field Filter field
|
|
220
|
+
* @param excludedValues Excluded values
|
|
221
|
+
* @returns Result
|
|
222
|
+
*/
|
|
223
|
+
export async function excludeAsync<
|
|
224
|
+
T extends { [P in D]: DataTypes.IdType },
|
|
225
|
+
D extends string = 'id'
|
|
226
|
+
>(items: Promise<T[] | undefined>, field: D, ...excludedValues: T[D][]) {
|
|
227
|
+
const result = await items;
|
|
228
|
+
if (result == null) return result;
|
|
229
|
+
return exclude(result, field, ...excludedValues);
|
|
230
|
+
}
|
|
231
|
+
|
|
196
232
|
/**
|
|
197
233
|
* Format inital character to lower case or upper case
|
|
198
234
|
* @param input Input string
|