@vope/utils 1.6.3 → 1.6.5
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/cron.d.ts +74 -35
- package/dist/cron.js +65 -65
- package/dist/dic.d.ts +36 -0
- package/dist/dic.js +95 -0
- package/dist/index.cjs.js +170 -76
- package/dist/index.d.ts +2 -1
- package/dist/index.js +21 -19
- package/dist/parse/num.js +5 -4
- package/package.json +81 -81
package/dist/cron.d.ts
CHANGED
|
@@ -85,6 +85,9 @@ export declare const default_max_month = 12;
|
|
|
85
85
|
export declare const default_min_month = 1;
|
|
86
86
|
export declare const default_max_week = 6;
|
|
87
87
|
export declare const default_min_week = 0;
|
|
88
|
+
/**
|
|
89
|
+
* 生成Cron表达式正则
|
|
90
|
+
*/
|
|
88
91
|
export declare const createCronRegexText: (options?: Options) => {
|
|
89
92
|
regex: string;
|
|
90
93
|
enableSlot: boolean;
|
|
@@ -94,74 +97,110 @@ export declare const createCronRegexText: (options?: Options) => {
|
|
|
94
97
|
enableWeekLast: boolean;
|
|
95
98
|
enableInvalid: boolean;
|
|
96
99
|
};
|
|
97
|
-
interface
|
|
100
|
+
interface BasicUnit {
|
|
98
101
|
text(): string;
|
|
99
102
|
}
|
|
100
|
-
|
|
103
|
+
/**
|
|
104
|
+
* 占位符单元(即'?')
|
|
105
|
+
*/
|
|
106
|
+
export declare class InvalidUnit implements BasicUnit {
|
|
101
107
|
private isEnableInvalid;
|
|
102
108
|
constructor(isEnableInvalid: boolean);
|
|
103
109
|
text(): "?" | "*";
|
|
104
110
|
}
|
|
105
|
-
|
|
106
|
-
|
|
111
|
+
/**
|
|
112
|
+
* 判断一个值是否为占位符单元
|
|
113
|
+
*/
|
|
114
|
+
export declare const isInvalidUnit: (value: unknown) => value is InvalidUnit;
|
|
115
|
+
declare abstract class LastValue implements BasicUnit {
|
|
107
116
|
value: number;
|
|
108
117
|
constructor(value: number);
|
|
109
118
|
abstract text(): string;
|
|
110
119
|
}
|
|
111
|
-
|
|
120
|
+
/**
|
|
121
|
+
* 倒数(日)值单元
|
|
122
|
+
*/
|
|
123
|
+
export declare class DayLastUnit extends LastValue {
|
|
112
124
|
text(): string;
|
|
113
125
|
}
|
|
114
|
-
|
|
115
|
-
|
|
126
|
+
/**
|
|
127
|
+
* 判断一个值是否为倒数(日)值单元
|
|
128
|
+
*/
|
|
129
|
+
export declare const isDayLastUnit: (value: unknown) => value is DayLastUnit;
|
|
130
|
+
/**
|
|
131
|
+
* 倒数(周)值单元
|
|
132
|
+
*/
|
|
133
|
+
export declare class WeekLastUnit extends LastValue {
|
|
116
134
|
text(): string;
|
|
117
135
|
}
|
|
118
|
-
|
|
119
|
-
|
|
136
|
+
/**
|
|
137
|
+
* 判断一个值是否为倒数(周)值单元
|
|
138
|
+
*/
|
|
139
|
+
export declare const isWeekLastUnit: (value: unknown) => value is WeekLastUnit;
|
|
140
|
+
/**
|
|
141
|
+
* 通配值单元(即'*')
|
|
142
|
+
*/
|
|
143
|
+
export declare class WildcardUnit implements BasicUnit {
|
|
120
144
|
text(): string;
|
|
121
145
|
}
|
|
122
|
-
|
|
123
|
-
|
|
146
|
+
/**
|
|
147
|
+
* 判断一个值是否为通配值单元
|
|
148
|
+
*/
|
|
149
|
+
export declare const isWildcardUnit: (value: unknown) => value is WildcardUnit;
|
|
150
|
+
/**
|
|
151
|
+
* 范围值单元
|
|
152
|
+
*/
|
|
153
|
+
export declare class RangeUnit implements BasicUnit {
|
|
124
154
|
start: number;
|
|
125
155
|
stop: number;
|
|
126
156
|
constructor(start: number, stop: number);
|
|
127
157
|
text(): string;
|
|
128
158
|
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
export declare
|
|
159
|
+
/**
|
|
160
|
+
* 判断一个值是否为范围值单元
|
|
161
|
+
*/
|
|
162
|
+
export declare const isRangeUnit: (value: unknown) => value is RangeUnit;
|
|
163
|
+
type IGroup = number | DayLastUnit | WeekLastUnit | RangeUnit;
|
|
164
|
+
type IValues = WildcardUnit | IGroup[];
|
|
165
|
+
/**
|
|
166
|
+
* 聚合值单元
|
|
167
|
+
*/
|
|
168
|
+
export declare class AggUnit implements BasicUnit {
|
|
133
169
|
values: IValues;
|
|
134
170
|
increment?: number | undefined;
|
|
135
171
|
constructor(values: IValues, increment?: number | undefined);
|
|
136
172
|
text(): string;
|
|
137
173
|
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
type
|
|
174
|
+
/**
|
|
175
|
+
* 判断一个值是否为聚合值单元
|
|
176
|
+
*/
|
|
177
|
+
export declare const isAggUnit: (value: unknown) => value is AggUnit;
|
|
178
|
+
type IValue<T extends string = never> = '*' | number | T | (number | T | [number, number])[];
|
|
179
|
+
type BasicValue = IValue;
|
|
180
|
+
type DayValue = IValue<'L' | `L-${number}`>;
|
|
181
|
+
type WeekValue = IValue<`${number}L`>;
|
|
143
182
|
declare class Parse {
|
|
144
183
|
cron: Cron;
|
|
145
|
-
second?:
|
|
146
|
-
minute:
|
|
147
|
-
hour:
|
|
148
|
-
day:
|
|
149
|
-
month:
|
|
150
|
-
week:
|
|
151
|
-
year?:
|
|
184
|
+
second?: AggUnit;
|
|
185
|
+
minute: AggUnit;
|
|
186
|
+
hour: AggUnit;
|
|
187
|
+
day: InvalidUnit | AggUnit;
|
|
188
|
+
month: AggUnit;
|
|
189
|
+
week: InvalidUnit | AggUnit;
|
|
190
|
+
year?: AggUnit;
|
|
152
191
|
constructor(second: string | undefined, minute: string, hour: string, day: string, month: string, week: string, year: string | undefined, cron: Cron);
|
|
153
|
-
parse(text: string):
|
|
154
|
-
setSecond(value:
|
|
155
|
-
setMinute(value:
|
|
156
|
-
setHour(value:
|
|
192
|
+
parse(text: string): InvalidUnit | AggUnit;
|
|
193
|
+
setSecond(value: BasicValue, increment?: number): boolean;
|
|
194
|
+
setMinute(value: BasicValue, increment?: number): boolean;
|
|
195
|
+
setHour(value: BasicValue, increment?: number): boolean;
|
|
157
196
|
setDayInvalid(): void;
|
|
158
197
|
setDay(value: '?'): boolean;
|
|
159
|
-
setDay(value:
|
|
160
|
-
setMonth(value:
|
|
198
|
+
setDay(value: DayValue, increment?: number): boolean;
|
|
199
|
+
setMonth(value: BasicValue, increment?: number): boolean;
|
|
161
200
|
setWeekInvalid(): void;
|
|
162
201
|
setWeek(value: '?'): boolean;
|
|
163
|
-
setWeek(value:
|
|
164
|
-
setYear(value:
|
|
202
|
+
setWeek(value: WeekValue, increment?: number): boolean;
|
|
203
|
+
setYear(value: BasicValue, increment?: number): boolean;
|
|
165
204
|
text(): string;
|
|
166
205
|
}
|
|
167
206
|
export declare class Cron {
|
package/dist/cron.js
CHANGED
|
@@ -261,7 +261,7 @@ const createCronRegexText = (options = {}) => {
|
|
|
261
261
|
enableInvalid
|
|
262
262
|
};
|
|
263
263
|
};
|
|
264
|
-
class
|
|
264
|
+
class InvalidUnit {
|
|
265
265
|
constructor(isEnableInvalid) {
|
|
266
266
|
this.isEnableInvalid = isEnableInvalid;
|
|
267
267
|
}
|
|
@@ -269,32 +269,32 @@ class InvalidValue {
|
|
|
269
269
|
return this.isEnableInvalid ? "?" : "*";
|
|
270
270
|
}
|
|
271
271
|
}
|
|
272
|
-
const
|
|
272
|
+
const isInvalidUnit = (value) => value instanceof InvalidUnit;
|
|
273
273
|
class LastValue {
|
|
274
274
|
constructor(value) {
|
|
275
275
|
this.value = value;
|
|
276
276
|
}
|
|
277
277
|
}
|
|
278
|
-
class
|
|
278
|
+
class DayLastUnit extends LastValue {
|
|
279
279
|
text() {
|
|
280
280
|
const { value } = this;
|
|
281
281
|
return "L" + (value ? `-${value}` : "");
|
|
282
282
|
}
|
|
283
283
|
}
|
|
284
|
-
const
|
|
285
|
-
class
|
|
284
|
+
const isDayLastUnit = (value) => value instanceof DayLastUnit;
|
|
285
|
+
class WeekLastUnit extends LastValue {
|
|
286
286
|
text() {
|
|
287
287
|
return `${this.value}L`;
|
|
288
288
|
}
|
|
289
289
|
}
|
|
290
|
-
const
|
|
291
|
-
class
|
|
290
|
+
const isWeekLastUnit = (value) => value instanceof WeekLastUnit;
|
|
291
|
+
class WildcardUnit {
|
|
292
292
|
text() {
|
|
293
293
|
return "*";
|
|
294
294
|
}
|
|
295
295
|
}
|
|
296
|
-
const
|
|
297
|
-
class
|
|
296
|
+
const isWildcardUnit = (value) => value instanceof WildcardUnit;
|
|
297
|
+
class RangeUnit {
|
|
298
298
|
constructor(start, stop) {
|
|
299
299
|
this.start = start;
|
|
300
300
|
this.stop = stop;
|
|
@@ -303,8 +303,8 @@ class RangeValue {
|
|
|
303
303
|
return `${this.start}-${this.stop}`;
|
|
304
304
|
}
|
|
305
305
|
}
|
|
306
|
-
const
|
|
307
|
-
class
|
|
306
|
+
const isRangeUnit = (value) => value instanceof RangeUnit;
|
|
307
|
+
class AggUnit {
|
|
308
308
|
constructor(values, increment) {
|
|
309
309
|
this.values = values;
|
|
310
310
|
this.increment = increment;
|
|
@@ -328,7 +328,7 @@ class DigitsValue {
|
|
|
328
328
|
return increment ? `${tmp}/${increment}` : tmp;
|
|
329
329
|
}
|
|
330
330
|
}
|
|
331
|
-
const
|
|
331
|
+
const isAggUnit = (value) => value instanceof AggUnit;
|
|
332
332
|
const re_num = /\d/;
|
|
333
333
|
const parseDigit = (text) => {
|
|
334
334
|
if (re_num.test(text[0])) {
|
|
@@ -337,22 +337,22 @@ const parseDigit = (text) => {
|
|
|
337
337
|
return weekDic.item(text.toLocaleUpperCase());
|
|
338
338
|
}
|
|
339
339
|
};
|
|
340
|
-
const
|
|
340
|
+
const parseAgg = (text) => {
|
|
341
341
|
return text.split(",").map((text2) => {
|
|
342
342
|
const [a, b] = text2.split("-");
|
|
343
343
|
if (a === "L" && b === void 0) {
|
|
344
|
-
return new
|
|
344
|
+
return new DayLastUnit(0);
|
|
345
345
|
} else if (b === void 0) {
|
|
346
346
|
if (a.slice(-1) === "L") {
|
|
347
|
-
return new
|
|
347
|
+
return new WeekLastUnit(parseInt(a.slice(0, -1)));
|
|
348
348
|
} else {
|
|
349
349
|
return parseDigit(a);
|
|
350
350
|
}
|
|
351
351
|
} else {
|
|
352
352
|
if (a === "L") {
|
|
353
|
-
return new
|
|
353
|
+
return new DayLastUnit(parseInt(b));
|
|
354
354
|
} else {
|
|
355
|
-
return new
|
|
355
|
+
return new RangeUnit(parseDigit(a), parseDigit(b));
|
|
356
356
|
}
|
|
357
357
|
}
|
|
358
358
|
});
|
|
@@ -370,16 +370,16 @@ const validate = (value, min, max) => {
|
|
|
370
370
|
const validateIncrement = (value, min, max) => {
|
|
371
371
|
return isInteger(value) && value > 0 && value >= min && value <= max;
|
|
372
372
|
};
|
|
373
|
-
const
|
|
373
|
+
const formaterAgg = (value, min, max) => {
|
|
374
374
|
let values;
|
|
375
375
|
if (value === "*") {
|
|
376
|
-
values = new
|
|
376
|
+
values = new WildcardUnit();
|
|
377
377
|
} else if (Array.isArray(value)) {
|
|
378
378
|
const tmp = [];
|
|
379
379
|
value.some((data) => {
|
|
380
380
|
if (validate(data, min, max)) {
|
|
381
381
|
tmp.push(
|
|
382
|
-
isNumber(data) ? data : new
|
|
382
|
+
isNumber(data) ? data : new RangeUnit(data[0], data[1])
|
|
383
383
|
);
|
|
384
384
|
return false;
|
|
385
385
|
} else {
|
|
@@ -391,23 +391,23 @@ const formaterDigits = (value, min, max) => {
|
|
|
391
391
|
}
|
|
392
392
|
return values;
|
|
393
393
|
};
|
|
394
|
-
const
|
|
394
|
+
const formaterAggByLast = (value, min, max, prototype) => {
|
|
395
395
|
let values;
|
|
396
396
|
if (value === "*") {
|
|
397
|
-
values = new
|
|
397
|
+
values = new WildcardUnit();
|
|
398
398
|
} else {
|
|
399
399
|
const tmp = [];
|
|
400
400
|
const list = Array.isArray(value) ? value : [value];
|
|
401
401
|
list.some((data) => {
|
|
402
402
|
if (isString(data)) {
|
|
403
|
-
const result =
|
|
403
|
+
const result = parseAgg(data);
|
|
404
404
|
if (result.length === 1 && result[0] instanceof prototype && validate(result[0].value, min, max - 1)) {
|
|
405
405
|
tmp.push(result[0]);
|
|
406
406
|
return false;
|
|
407
407
|
}
|
|
408
408
|
} else if (validate(data, min, max)) {
|
|
409
409
|
tmp.push(
|
|
410
|
-
isNumber(data) ? data : new
|
|
410
|
+
isNumber(data) ? data : new RangeUnit(data[0], data[1])
|
|
411
411
|
);
|
|
412
412
|
return false;
|
|
413
413
|
}
|
|
@@ -439,18 +439,18 @@ class Parse {
|
|
|
439
439
|
week;
|
|
440
440
|
year;
|
|
441
441
|
parse(text) {
|
|
442
|
-
if (text === "?") return new
|
|
442
|
+
if (text === "?") return new InvalidUnit(this.cron.isEnableInvalid);
|
|
443
443
|
const [slices, increment] = text.split("/");
|
|
444
|
-
return new
|
|
445
|
-
slices === "*" ? new
|
|
444
|
+
return new AggUnit(
|
|
445
|
+
slices === "*" ? new WildcardUnit() : parseAgg(slices),
|
|
446
446
|
increment ? parseInt(increment) : void 0
|
|
447
447
|
);
|
|
448
448
|
}
|
|
449
449
|
setSecond(value, increment) {
|
|
450
450
|
if (!this.cron.isEnableSecond) return false;
|
|
451
|
-
const values =
|
|
451
|
+
const values = formaterAgg(value, default_min_second, default_max_second);
|
|
452
452
|
if (!values) return false;
|
|
453
|
-
const second = new
|
|
453
|
+
const second = new AggUnit(values);
|
|
454
454
|
if (validateIncrement(increment, default_min_second, default_max_second)) {
|
|
455
455
|
second.increment = increment;
|
|
456
456
|
}
|
|
@@ -458,9 +458,9 @@ class Parse {
|
|
|
458
458
|
return true;
|
|
459
459
|
}
|
|
460
460
|
setMinute(value, increment) {
|
|
461
|
-
const values =
|
|
461
|
+
const values = formaterAgg(value, default_min_minute, default_max_minute);
|
|
462
462
|
if (!values) return false;
|
|
463
|
-
const minute = new
|
|
463
|
+
const minute = new AggUnit(values);
|
|
464
464
|
if (validateIncrement(increment, default_min_minute, default_max_minute)) {
|
|
465
465
|
minute.increment = increment;
|
|
466
466
|
}
|
|
@@ -468,9 +468,9 @@ class Parse {
|
|
|
468
468
|
return true;
|
|
469
469
|
}
|
|
470
470
|
setHour(value, increment) {
|
|
471
|
-
const values =
|
|
471
|
+
const values = formaterAgg(value, default_min_hour, default_max_hour);
|
|
472
472
|
if (!values) return false;
|
|
473
|
-
const hour = new
|
|
473
|
+
const hour = new AggUnit(values);
|
|
474
474
|
if (validateIncrement(increment, default_min_hour, default_max_hour)) {
|
|
475
475
|
hour.increment = increment;
|
|
476
476
|
}
|
|
@@ -478,9 +478,9 @@ class Parse {
|
|
|
478
478
|
return true;
|
|
479
479
|
}
|
|
480
480
|
setDayInvalid() {
|
|
481
|
-
this.day = new
|
|
482
|
-
if (this.week instanceof
|
|
483
|
-
this.week = new
|
|
481
|
+
this.day = new InvalidUnit(this.cron.isEnableInvalid);
|
|
482
|
+
if (this.week instanceof InvalidUnit) {
|
|
483
|
+
this.week = new AggUnit(new WildcardUnit());
|
|
484
484
|
}
|
|
485
485
|
}
|
|
486
486
|
setDay(value, increment) {
|
|
@@ -489,30 +489,30 @@ class Parse {
|
|
|
489
489
|
this.setDayInvalid();
|
|
490
490
|
return true;
|
|
491
491
|
} else {
|
|
492
|
-
const tmp2 =
|
|
492
|
+
const tmp2 = formaterAggByLast(value, default_min_day, default_max_day, DayLastUnit);
|
|
493
493
|
if (!tmp2) return false;
|
|
494
494
|
values = tmp2;
|
|
495
495
|
}
|
|
496
|
-
const tmp = new
|
|
496
|
+
const tmp = new AggUnit(values);
|
|
497
497
|
if (validateIncrement(increment, default_min_day, default_max_day)) {
|
|
498
498
|
tmp.increment = increment;
|
|
499
499
|
}
|
|
500
500
|
this.day = tmp;
|
|
501
501
|
const { week } = this;
|
|
502
|
-
if (
|
|
502
|
+
if (isAggUnit(week) && (week.increment !== void 0 || !isWildcardUnit(week.values))) {
|
|
503
503
|
if (this.cron.isEnableInvalid) {
|
|
504
|
-
this.week = new
|
|
504
|
+
this.week = new InvalidUnit(true);
|
|
505
505
|
} else {
|
|
506
|
-
week.values = new
|
|
506
|
+
week.values = new WildcardUnit();
|
|
507
507
|
week.increment = void 0;
|
|
508
508
|
}
|
|
509
509
|
}
|
|
510
510
|
return true;
|
|
511
511
|
}
|
|
512
512
|
setMonth(value, increment) {
|
|
513
|
-
const values =
|
|
513
|
+
const values = formaterAgg(value, default_min_month, default_max_month);
|
|
514
514
|
if (!values) return false;
|
|
515
|
-
const month = new
|
|
515
|
+
const month = new AggUnit(values);
|
|
516
516
|
if (validateIncrement(increment, default_min_month, default_max_month)) {
|
|
517
517
|
month.increment = increment;
|
|
518
518
|
}
|
|
@@ -520,9 +520,9 @@ class Parse {
|
|
|
520
520
|
return true;
|
|
521
521
|
}
|
|
522
522
|
setWeekInvalid() {
|
|
523
|
-
this.week = new
|
|
524
|
-
if (this.day instanceof
|
|
525
|
-
this.day = new
|
|
523
|
+
this.week = new InvalidUnit(this.cron.isEnableInvalid);
|
|
524
|
+
if (this.day instanceof InvalidUnit) {
|
|
525
|
+
this.day = new AggUnit(new WildcardUnit());
|
|
526
526
|
}
|
|
527
527
|
}
|
|
528
528
|
setWeek(value, increment) {
|
|
@@ -531,21 +531,21 @@ class Parse {
|
|
|
531
531
|
this.setWeekInvalid();
|
|
532
532
|
return true;
|
|
533
533
|
} else {
|
|
534
|
-
const tmp2 =
|
|
534
|
+
const tmp2 = formaterAggByLast(value, default_min_week, default_max_week, WeekLastUnit);
|
|
535
535
|
if (!tmp2) return false;
|
|
536
536
|
values = tmp2;
|
|
537
537
|
}
|
|
538
|
-
const tmp = new
|
|
538
|
+
const tmp = new AggUnit(values);
|
|
539
539
|
if (validateIncrement(increment, default_min_week, default_max_week)) {
|
|
540
540
|
tmp.increment = increment;
|
|
541
541
|
}
|
|
542
542
|
this.week = tmp;
|
|
543
543
|
const { day } = this;
|
|
544
|
-
if (
|
|
544
|
+
if (isAggUnit(day) && (day.increment !== void 0 || !isWildcardUnit(day.values))) {
|
|
545
545
|
if (this.cron.isEnableInvalid) {
|
|
546
|
-
this.day = new
|
|
546
|
+
this.day = new InvalidUnit(true);
|
|
547
547
|
} else {
|
|
548
|
-
day.values = new
|
|
548
|
+
day.values = new WildcardUnit();
|
|
549
549
|
day.increment = void 0;
|
|
550
550
|
}
|
|
551
551
|
}
|
|
@@ -553,9 +553,9 @@ class Parse {
|
|
|
553
553
|
}
|
|
554
554
|
setYear(value, increment) {
|
|
555
555
|
if (!this.cron.isEnableYear) return false;
|
|
556
|
-
const values =
|
|
556
|
+
const values = formaterAgg(value, 0, MAX_SAFE_INTEGER);
|
|
557
557
|
if (!values) return false;
|
|
558
|
-
const year = new
|
|
558
|
+
const year = new AggUnit(values);
|
|
559
559
|
if (validateIncrement(increment, 0, MAX_SAFE_INTEGER)) {
|
|
560
560
|
year.increment = increment;
|
|
561
561
|
}
|
|
@@ -644,13 +644,13 @@ class Cron {
|
|
|
644
644
|
}
|
|
645
645
|
}
|
|
646
646
|
export {
|
|
647
|
+
AggUnit,
|
|
647
648
|
Cron,
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
WildcardValue,
|
|
649
|
+
DayLastUnit,
|
|
650
|
+
InvalidUnit,
|
|
651
|
+
RangeUnit,
|
|
652
|
+
WeekLastUnit,
|
|
653
|
+
WildcardUnit,
|
|
654
654
|
createCronRegexText,
|
|
655
655
|
default_max_day,
|
|
656
656
|
default_max_hour,
|
|
@@ -664,11 +664,11 @@ export {
|
|
|
664
664
|
default_min_month,
|
|
665
665
|
default_min_second,
|
|
666
666
|
default_min_week,
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
667
|
+
isAggUnit,
|
|
668
|
+
isDayLastUnit,
|
|
669
|
+
isInvalidUnit,
|
|
670
|
+
isRangeUnit,
|
|
671
|
+
isWeekLastUnit,
|
|
672
|
+
isWildcardUnit,
|
|
673
673
|
weekDic
|
|
674
674
|
};
|
package/dist/dic.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { IsNever, Revise } from '@vope/types';
|
|
2
|
+
type Key = string | number;
|
|
3
|
+
export interface Dic<V extends Key> {
|
|
4
|
+
label: string;
|
|
5
|
+
value: V;
|
|
6
|
+
}
|
|
7
|
+
type CustomLabel<T, M extends string> = ((data: T) => string) | Record<M, (data: T) => string>;
|
|
8
|
+
interface Options<K extends Key, T extends Dic<K>, M extends string> {
|
|
9
|
+
defaultValue?: K;
|
|
10
|
+
customLabel?: CustomLabel<T, M>;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* 字典操作
|
|
14
|
+
*/
|
|
15
|
+
declare class DicRollup<K extends Key, T extends Dic<K> = Dic<K>, Mode extends string = never> {
|
|
16
|
+
#private;
|
|
17
|
+
protected readonly memory: Record<K, T>;
|
|
18
|
+
constructor(list: T[], { defaultValue, customLabel, }?: Options<K, T, Mode>);
|
|
19
|
+
get size(): number;
|
|
20
|
+
get default(): K | undefined;
|
|
21
|
+
get list(): T[];
|
|
22
|
+
includes(key: Key): key is K;
|
|
23
|
+
label(key: K, mode?: Mode): string | undefined;
|
|
24
|
+
get<P extends keyof T>(key: K, property: P): Record<K, T>[K][P];
|
|
25
|
+
item(key: K): Readonly<T>;
|
|
26
|
+
map<V>(callback: (data: Readonly<T>, index: number) => V): V[];
|
|
27
|
+
reduce<V>(callback: (previousValue: V, currentValue: T, currentIndex: number) => V, initialValue: V): V;
|
|
28
|
+
copy<P extends K, M extends string = never>(keys: P[], options?: Options<P, Readonly<Revise<T, 'value', P>>, M>): DicRollup<P, Revise<T, "value", P>, IsNever<M> extends true ? Mode : M>;
|
|
29
|
+
copyAnti<P extends K, M extends string = never>(keys: P[], options?: Options<Exclude<K, P>, Readonly<Revise<T, 'value', Exclude<K, P>>>, M>): DicRollup<Exclude<K, P>, Revise<T, "value", Exclude<K, P>>, IsNever<M> extends true ? Mode : M>;
|
|
30
|
+
}
|
|
31
|
+
declare namespace DicRollup {
|
|
32
|
+
type Type<T extends DicRollup<any, any, string>> = T extends DicRollup<infer K, any, string> ? K : never;
|
|
33
|
+
type Dic<T extends DicRollup<any, any, string>> = T extends DicRollup<any, infer V, string> ? V : never;
|
|
34
|
+
type Mode<T extends DicRollup<any, any, string>> = T extends DicRollup<any, any, infer M> ? M : never;
|
|
35
|
+
}
|
|
36
|
+
export default DicRollup;
|
package/dist/dic.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { isFunction } from "lodash-es";
|
|
2
|
+
import { hash8json } from "./convert/json";
|
|
3
|
+
import { hash8strings } from "./convert/strings";
|
|
4
|
+
class DicRollup {
|
|
5
|
+
#list;
|
|
6
|
+
#default;
|
|
7
|
+
#customLabel;
|
|
8
|
+
memory;
|
|
9
|
+
constructor(list, {
|
|
10
|
+
defaultValue,
|
|
11
|
+
customLabel
|
|
12
|
+
} = {}) {
|
|
13
|
+
this.#list = list;
|
|
14
|
+
this.#default = defaultValue;
|
|
15
|
+
this.#customLabel = customLabel;
|
|
16
|
+
const getter = () => hash8json(list, "value");
|
|
17
|
+
Object.defineProperty(this, "memory", {
|
|
18
|
+
get() {
|
|
19
|
+
return getter();
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
get size() {
|
|
24
|
+
return this.#list.length;
|
|
25
|
+
}
|
|
26
|
+
get default() {
|
|
27
|
+
return this.#default;
|
|
28
|
+
}
|
|
29
|
+
get list() {
|
|
30
|
+
return this.#list;
|
|
31
|
+
}
|
|
32
|
+
includes(key) {
|
|
33
|
+
return key in this.memory;
|
|
34
|
+
}
|
|
35
|
+
label(key, mode) {
|
|
36
|
+
const data = this.memory[key];
|
|
37
|
+
if (!data) return;
|
|
38
|
+
const custom = this.#customLabel;
|
|
39
|
+
if (!custom) return data.label;
|
|
40
|
+
if (!mode && isFunction(custom)) {
|
|
41
|
+
return custom(data);
|
|
42
|
+
} else if (mode) {
|
|
43
|
+
const tmp = custom[mode];
|
|
44
|
+
return tmp ? tmp(data) : data.label;
|
|
45
|
+
} else {
|
|
46
|
+
return data.label;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
get(key, property) {
|
|
50
|
+
return this.memory[key]?.[property];
|
|
51
|
+
}
|
|
52
|
+
item(key) {
|
|
53
|
+
return this.memory[key];
|
|
54
|
+
}
|
|
55
|
+
map(callback) {
|
|
56
|
+
return this.#list.map(callback);
|
|
57
|
+
}
|
|
58
|
+
reduce(callback, initialValue) {
|
|
59
|
+
return this.#list.reduce(callback, initialValue);
|
|
60
|
+
}
|
|
61
|
+
copy(keys, options) {
|
|
62
|
+
const tmp = hash8strings(keys);
|
|
63
|
+
const opts = Object.assign({
|
|
64
|
+
defaultValue: this.#default,
|
|
65
|
+
customLabel: this.#customLabel
|
|
66
|
+
}, options);
|
|
67
|
+
const { defaultValue } = opts;
|
|
68
|
+
if (defaultValue !== void 0 && defaultValue in tmp === false) {
|
|
69
|
+
opts.defaultValue = void 0;
|
|
70
|
+
}
|
|
71
|
+
return new DicRollup(
|
|
72
|
+
this.#list.filter((data) => data.value in tmp),
|
|
73
|
+
opts
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
copyAnti(keys, options) {
|
|
77
|
+
const tmp = hash8strings(keys);
|
|
78
|
+
const opts = Object.assign({
|
|
79
|
+
defaultValue: this.#default,
|
|
80
|
+
customLabel: this.#customLabel
|
|
81
|
+
}, options);
|
|
82
|
+
const { defaultValue } = opts;
|
|
83
|
+
if (defaultValue !== void 0 && defaultValue in tmp) {
|
|
84
|
+
opts.defaultValue = void 0;
|
|
85
|
+
}
|
|
86
|
+
return new DicRollup(
|
|
87
|
+
this.#list.filter((data) => data.value in tmp === false),
|
|
88
|
+
opts
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
var dic_default = DicRollup;
|
|
93
|
+
export {
|
|
94
|
+
dic_default as default
|
|
95
|
+
};
|