@idlebox/common 1.3.14 → 1.3.16
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/lib/array/arrayDiff.cjs +2 -2
- package/lib/array/arrayDiff.cjs.map +1 -1
- package/lib/array/arrayDiff.js +2 -2
- package/lib/array/arrayDiff.js.map +1 -1
- package/lib/array/arraySame.cjs.map +1 -1
- package/lib/array/arraySame.js.map +1 -1
- package/lib/array/arrayUnique.cjs.map +1 -1
- package/lib/array/arrayUnique.js.map +1 -1
- package/{dist → lib}/common-alpha.d.ts +57 -13
- package/{dist → lib}/common-beta.d.ts +57 -13
- package/{dist → lib}/common-public.d.ts +57 -13
- package/{dist → lib}/common.d.ts +57 -13
- package/lib/function/callbackList.cjs +4 -0
- package/lib/function/callbackList.cjs.map +1 -1
- package/lib/function/callbackList.js +4 -0
- package/lib/function/callbackList.js.map +1 -1
- package/lib/string/concatType.generated.cjs +8 -0
- package/lib/string/concatType.generated.cjs.map +1 -1
- package/lib/string/concatType.generated.js +8 -0
- package/lib/string/concatType.generated.js.map +1 -1
- package/{dist → lib}/tsdoc-metadata.json +1 -1
- package/lib/{typingHeler/deep.cjs → typingHelper/deep.partial.cjs} +1 -2
- package/lib/typingHelper/deep.partial.cjs.map +1 -0
- package/lib/typingHelper/deep.partial.js +2 -0
- package/lib/typingHelper/deep.partial.js.map +1 -0
- package/lib/typingHelper/deep.readonly.cjs +3 -0
- package/lib/typingHelper/deep.readonly.cjs.map +1 -0
- package/lib/typingHelper/deep.readonly.js +2 -0
- package/lib/typingHelper/deep.readonly.js.map +1 -0
- package/lib/typingHelper/deep.required.cjs +3 -0
- package/lib/typingHelper/deep.required.cjs.map +1 -0
- package/lib/typingHelper/deep.required.js +2 -0
- package/lib/typingHelper/deep.required.js.map +1 -0
- package/lib/typingHelper/deep.writable.cjs +3 -0
- package/lib/typingHelper/deep.writable.cjs.map +1 -0
- package/lib/typingHelper/deep.writable.js +2 -0
- package/lib/typingHelper/deep.writable.js.map +1 -0
- package/lib/typingHelper/literal.cjs +3 -0
- package/lib/typingHelper/literal.cjs.map +1 -0
- package/lib/typingHelper/literal.js +2 -0
- package/lib/typingHelper/literal.js.map +1 -0
- package/package.json +12 -12
- package/src/array/arrayDiff.ts +31 -0
- package/src/array/arraySame.ts +15 -0
- package/src/array/arrayUnique.ts +50 -0
- package/src/array/normalizeArray.ts +13 -0
- package/src/array/sortAlpha.ts +15 -0
- package/src/date/consts.ts +5 -0
- package/src/date/isInvalid.ts +6 -0
- package/src/date/sibling.ts +28 -0
- package/src/date/timeString.ts +150 -0
- package/src/date/unix.ts +13 -0
- package/src/debugging/serializable.ts +146 -0
- package/src/debugging/tryInspect.ts +37 -0
- package/src/error/convertUnknown.ts +10 -0
- package/src/error/getFrame.ts +13 -0
- package/src/function/asyncCallbackList.ts +75 -0
- package/src/function/callbackList.ts +88 -0
- package/src/function/delayCallbackList.ts +45 -0
- package/src/function/functionName.ts +39 -0
- package/src/index.generated.ts +289 -0
- package/src/lifecycle/dispose/bridges/rxjs.ts +6 -0
- package/src/lifecycle/dispose/disposableEvent.ts +117 -0
- package/src/lifecycle/dispose/disposedError.ts +16 -0
- package/src/lifecycle/dispose/lifecycle.async.ts +61 -0
- package/src/lifecycle/dispose/lifecycle.global.ts +61 -0
- package/src/lifecycle/dispose/lifecycle.sync.ts +79 -0
- package/src/lifecycle/dispose/lifecycle.ts +28 -0
- package/src/lifecycle/event/event.ts +81 -0
- package/src/lifecycle/event/memorized.ts +39 -0
- package/src/lifecycle/promise/cancel.ts +16 -0
- package/src/lifecycle/promise/cancellationToken/driver.browser.ts +55 -0
- package/src/lifecycle/promise/cancellationToken/driver.common.ts +43 -0
- package/src/lifecycle/promise/cancellationToken/source.ts +48 -0
- package/src/lifecycle/promise/deferredPromise.ts +104 -0
- package/src/lifecycle/timeout/timeout.ts +48 -0
- package/src/lifecycle/timeout/timeoutError.ts +16 -0
- package/src/log/logger.ts +148 -0
- package/src/mapSet/customSet.ts +91 -0
- package/src/mapSet/extendMap.ts +40 -0
- package/src/misc/assertNotNull.ts +21 -0
- package/src/object/definePublicConstant.ts +10 -0
- package/src/object/initOnRead.ts +27 -0
- package/src/object/objectPath.ts +10 -0
- package/src/object/objectSame.ts +52 -0
- package/src/path/isAbsolute.ts +11 -0
- package/src/path/normalizePath.ts +8 -0
- package/src/path/pathArray.ts +42 -0
- package/src/platform/globalObject.ts +22 -0
- package/src/platform/globalSingleton.ts +82 -0
- package/src/platform/globalSymbol.ts +36 -0
- package/src/platform/os.ts +46 -0
- package/src/promise/awaitIterator.ts +19 -0
- package/src/promise/finishAllPromise.ts +50 -0
- package/src/promise/promiseBool.ts +10 -0
- package/src/promise/promisePool.ts +40 -0
- package/src/promise/timeoutPromisePool.ts +22 -0
- package/src/reflection/classes/hookClass.ts +47 -0
- package/src/reflection/classes/singleton.ts +33 -0
- package/src/reflection/methods/bind.ts +30 -0
- package/src/reflection/methods/initOnRead.ts +11 -0
- package/src/reflection/methods/memorize.ts +33 -0
- package/src/string/castCase.ts +44 -0
- package/src/string/concatType.generated.ts +265 -0
- package/src/string/concatType.generator.ts +31 -0
- package/src/string/escapeRegexp.ts +4 -0
- package/src/string/pad2.ts +11 -0
- package/src/string/sizeString.ts +52 -0
- package/src/tsconfig.json +14 -0
- package/src/typingHelper/deep.partial.ts +16 -0
- package/src/typingHelper/deep.readonly.ts +16 -0
- package/src/typingHelper/deep.required.ts +16 -0
- package/src/typingHelper/deep.writable.ts +16 -0
- package/src/typingHelper/literal.ts +1 -0
- package/docs/common.api.json +0 -21737
- package/docs/common.api.md +0 -1139
- package/docs/package-public.d.ts +0 -952
- package/docs/tsdoc-metadata.json +0 -11
- package/lib/typingHeler/deep.cjs.map +0 -1
- package/lib/typingHeler/deep.js +0 -3
- package/lib/typingHeler/deep.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"callbackList.js","sourceRoot":"","sources":["../../src/function/callbackList.ts"],"names":[],"mappings":"AA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AC;IACb,CAAC;IAED;;;OAGG;IACH,GAAG,CAAC,GAAG,QAAkB;QACxB,IAAI,IAAI,CAAC,OAAO,EAAE;YACjB,MAAM,IAAI,KAAK,CAAC,4CAA4C,CAAC,CAAC;SAC9D;QACD,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;QAElB,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;QACpB,KAAK,MAAM,EAAE,IAAI,IAAI,CAAC,IAAI,EAAE;YAC3B,IAAI,IAAI,CAAC,IAAI;gBAAE,MAAM;YACrB,EAAE,CAAC,GAAG,QAAQ,CAAC,CAAC;SAChB;QACD,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC;QAErB,MAAM,GAAG,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC;QACvB,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;QAClB,OAAO,GAAG,CAAC;IACZ,CAAC;IAED,OAAO;QACN,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IAClB,CAAC;CACD"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"concatType.generated.js","sourceRoot":"","sources":["../../src/string/concatType.generated.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"concatType.generated.js","sourceRoot":"","sources":["../../src/string/concatType.generated.ts"],"names":[],"mappings":";AAAA,oBAAoB;AACpB,aAAa;AACb;;;;;GAKG;;;AA8PH,UAAU;AACV,SAAgB,gBAAgB,CAAC,CAAW;IAC3C,OAAO,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AACnB,CAAC;AAFD,4CAEC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"concatType.generated.js","sourceRoot":"","sources":["../../src/string/concatType.generated.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"concatType.generated.js","sourceRoot":"","sources":["../../src/string/concatType.generated.ts"],"names":[],"mappings":"AAAA,oBAAoB;AACpB,aAAa;AACb;;;;;GAKG;AA8PH,UAAU;AACV,MAAM,UAAU,gBAAgB,CAAC,CAAW;IAC3C,OAAO,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AACnB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deep.partial.js","sourceRoot":"","sources":["../../src/typingHelper/deep.partial.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deep.partial.js","sourceRoot":"","sources":["../../src/typingHelper/deep.partial.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deep.readonly.js","sourceRoot":"","sources":["../../src/typingHelper/deep.readonly.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deep.readonly.js","sourceRoot":"","sources":["../../src/typingHelper/deep.readonly.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deep.required.js","sourceRoot":"","sources":["../../src/typingHelper/deep.required.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deep.required.js","sourceRoot":"","sources":["../../src/typingHelper/deep.required.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deep.writable.js","sourceRoot":"","sources":["../../src/typingHelper/deep.writable.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deep.writable.js","sourceRoot":"","sources":["../../src/typingHelper/deep.writable.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"literal.js","sourceRoot":"","sources":["../../src/typingHelper/literal.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"literal.js","sourceRoot":"","sources":["../../src/typingHelper/literal.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@idlebox/common",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.3.
|
|
4
|
+
"version": "1.3.16",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "./lib/index.generated.cjs",
|
|
7
7
|
"module": "./lib/index.generated.js",
|
|
@@ -12,21 +12,20 @@
|
|
|
12
12
|
},
|
|
13
13
|
"./package.json": "./package.json"
|
|
14
14
|
},
|
|
15
|
-
"types": "./
|
|
15
|
+
"types": "./lib/common-public.d.ts",
|
|
16
16
|
"repository": "https://github.com/GongT/baobao",
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"tslib": "^2.4.
|
|
18
|
+
"tslib": "^2.4.1"
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
|
21
|
-
"@
|
|
22
|
-
"@build-script/heft-duel-stack": "^1.0.4",
|
|
23
|
-
"@build-script/single-dog-asset": "^1.0.22",
|
|
24
|
-
"@build-script/typescript-transformer-dual-package": "^2.0.2",
|
|
25
|
-
"@idlebox/itypes": "^1.0.3",
|
|
26
|
-
"@rushstack/heft": "^0.47.2",
|
|
27
|
-
"local-rig": "^1.0.0",
|
|
21
|
+
"@rushstack/heft": "^0.48.8",
|
|
28
22
|
"source-map-support": "^0.5.21",
|
|
29
|
-
"typescript": "^4.
|
|
23
|
+
"typescript": "^4.8.4",
|
|
24
|
+
"@build-script/heft-duel-stack": "1.0.7",
|
|
25
|
+
"@build-script/single-dog-asset": "1.0.23",
|
|
26
|
+
"@build-script/codegen": "0.0.6",
|
|
27
|
+
"local-rig": "1.0.1",
|
|
28
|
+
"@idlebox/itypes": "1.0.4"
|
|
30
29
|
},
|
|
31
30
|
"monorepo": "rush",
|
|
32
31
|
"jspm": {
|
|
@@ -36,7 +35,8 @@
|
|
|
36
35
|
"scripts": {
|
|
37
36
|
"test": "heft test",
|
|
38
37
|
"lint": "heft build",
|
|
39
|
-
"build": "heft
|
|
38
|
+
"build": "heft build --production",
|
|
39
|
+
"build:watch": "heft build --lite",
|
|
40
40
|
"watch": "heft build --watch",
|
|
41
41
|
"clean": "heft clean"
|
|
42
42
|
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @public
|
|
3
|
+
*/
|
|
4
|
+
export interface IArrayUpdate<T> {
|
|
5
|
+
add: T[];
|
|
6
|
+
del: T[];
|
|
7
|
+
same: T[];
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Compare two array, returns the difference from `before` to `after`
|
|
12
|
+
* @public
|
|
13
|
+
*/
|
|
14
|
+
export function arrayDiff<T>(_before: readonly T[], after: readonly T[]) {
|
|
15
|
+
const before = _before.slice().sort();
|
|
16
|
+
const add: T[] = after.slice().sort();
|
|
17
|
+
const del: T[] = [];
|
|
18
|
+
const same: T[] = [];
|
|
19
|
+
next: while (before.length) {
|
|
20
|
+
const item = before.pop()!;
|
|
21
|
+
for (let j = add.length - 1; j >= 0; j--) {
|
|
22
|
+
if (item === add[j]) {
|
|
23
|
+
same.push(item);
|
|
24
|
+
add.splice(j, 1);
|
|
25
|
+
continue next;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
del.push(item);
|
|
29
|
+
}
|
|
30
|
+
return { add, del, same };
|
|
31
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* is the two array EXACTLY same
|
|
3
|
+
* @public
|
|
4
|
+
*/
|
|
5
|
+
export function isArraySame<T>(a1: readonly T[], a2: readonly T[]): boolean {
|
|
6
|
+
if (a1.length !== a2.length) {
|
|
7
|
+
return false;
|
|
8
|
+
}
|
|
9
|
+
for (let i = a1.length - 1; i >= 0; i--) {
|
|
10
|
+
if (a1[i] !== a2[i]) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a new array without duplicate values
|
|
3
|
+
* @public
|
|
4
|
+
*/
|
|
5
|
+
export function arrayUnique<T>(arr: readonly T[]): T[] {
|
|
6
|
+
return arr.filter((item, index) => {
|
|
7
|
+
return arr.lastIndexOf(item) === index;
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Removes duplicate values from an array
|
|
13
|
+
* @public
|
|
14
|
+
*/
|
|
15
|
+
export function arrayUniqueReference(arr: any[]): void {
|
|
16
|
+
for (let index = arr.length - 1; index >= 0; index--) {
|
|
17
|
+
if (arr.lastIndexOf(arr[index]) !== index) {
|
|
18
|
+
arr.splice(index, 1);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface IUniqueIdFactory<T> {
|
|
24
|
+
(item: T): string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function defaultFactory(t: any) {
|
|
28
|
+
return t as string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Returns a function to be used in <arr>.filter()
|
|
33
|
+
*
|
|
34
|
+
* the returned function can use multiple times, it will remember all values inter multiple arrays
|
|
35
|
+
*
|
|
36
|
+
* @param {IUniqueIdFactory} idFactory function takes an array element, return it's id to be compare with each other
|
|
37
|
+
* @public
|
|
38
|
+
*/
|
|
39
|
+
export function uniqueFilter<T>(idFactory: IUniqueIdFactory<T> = defaultFactory) {
|
|
40
|
+
const seen: { [id: string]: true } = {};
|
|
41
|
+
return function uniqueFilterInner(item: T): boolean {
|
|
42
|
+
const id = idFactory(item);
|
|
43
|
+
if (seen[id]) {
|
|
44
|
+
return false;
|
|
45
|
+
} else {
|
|
46
|
+
seen[id] = true;
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ensure a value is an array
|
|
3
|
+
* @public
|
|
4
|
+
*/
|
|
5
|
+
export function normalizeArray<T>(input: T | T[]): T[] {
|
|
6
|
+
if (input && Array.isArray(input)) {
|
|
7
|
+
return input;
|
|
8
|
+
} else if (typeof input !== undefined) {
|
|
9
|
+
return [input];
|
|
10
|
+
} else {
|
|
11
|
+
return [];
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sort string array alphabet order
|
|
3
|
+
*
|
|
4
|
+
* to be used in <arr>.sort()
|
|
5
|
+
* @public
|
|
6
|
+
*/
|
|
7
|
+
export function sortByString(a: string, b: string): number {
|
|
8
|
+
if (a === b) {
|
|
9
|
+
return 0;
|
|
10
|
+
} else if (a > b) {
|
|
11
|
+
return 1;
|
|
12
|
+
} else {
|
|
13
|
+
return -1;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export function nextSecond(d: Date, n = 1) {
|
|
2
|
+
d.setSeconds(d.getSeconds() + n);
|
|
3
|
+
return d;
|
|
4
|
+
}
|
|
5
|
+
export function nextMinute(d: Date, n = 1) {
|
|
6
|
+
d.setMinutes(d.getMinutes() + n);
|
|
7
|
+
return d;
|
|
8
|
+
}
|
|
9
|
+
export function nextHour(d: Date, n = 1) {
|
|
10
|
+
d.setHours(d.getHours() + n);
|
|
11
|
+
return d;
|
|
12
|
+
}
|
|
13
|
+
export function nextDay(d: Date, n = 1) {
|
|
14
|
+
d.setDate(d.getDate() + n);
|
|
15
|
+
return d;
|
|
16
|
+
}
|
|
17
|
+
export function nextWeek(d: Date, n = 1) {
|
|
18
|
+
d.setDate(d.getDate() + n * 7);
|
|
19
|
+
return d;
|
|
20
|
+
}
|
|
21
|
+
export function nextMonth(d: Date, n = 1) {
|
|
22
|
+
d.setMonth(d.getMonth() + n);
|
|
23
|
+
return d;
|
|
24
|
+
}
|
|
25
|
+
export function nextYear(d: Date, n = 1) {
|
|
26
|
+
d.setFullYear(d.getFullYear() + n);
|
|
27
|
+
return d;
|
|
28
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { pad2 } from '../string/pad2';
|
|
2
|
+
|
|
3
|
+
export namespace humanDate {
|
|
4
|
+
/**
|
|
5
|
+
* Format: HH:mm:ss
|
|
6
|
+
*/
|
|
7
|
+
export function time(date: Date | string | number) {
|
|
8
|
+
if (typeof date === 'string') {
|
|
9
|
+
date = parseInt(date);
|
|
10
|
+
}
|
|
11
|
+
date = new Date(date as number);
|
|
12
|
+
return `${pad2(date.getHours())}:${pad2(date.getMinutes())}:${pad2(date.getSeconds())}`;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Format: YYYY-MM-dd
|
|
17
|
+
*
|
|
18
|
+
* separator can change
|
|
19
|
+
*/
|
|
20
|
+
export function date(date: Date | string | number, sp = '-') {
|
|
21
|
+
if (typeof date === 'string') {
|
|
22
|
+
date = parseInt(date);
|
|
23
|
+
}
|
|
24
|
+
date = new Date(date as number);
|
|
25
|
+
return `${date.getFullYear()}${sp}${pad2(date.getMonth() + 1)}${sp}${pad2(date.getDate())}`;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Format: YYYY-MM-dd HH:mm:ss
|
|
30
|
+
*/
|
|
31
|
+
export function datetime(date: Date | string | number) {
|
|
32
|
+
if (typeof date === 'string') {
|
|
33
|
+
date = parseInt(date);
|
|
34
|
+
}
|
|
35
|
+
date = new Date(date as number);
|
|
36
|
+
return (
|
|
37
|
+
`${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}` +
|
|
38
|
+
' ' +
|
|
39
|
+
`${pad2(date.getHours())}:${pad2(date.getMinutes())}:${pad2(date.getSeconds())}`
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface ITimeFormatter {
|
|
44
|
+
(s: number): string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface IFormatters {
|
|
48
|
+
s: ITimeFormatter;
|
|
49
|
+
m: ITimeFormatter;
|
|
50
|
+
h: ITimeFormatter;
|
|
51
|
+
d: ITimeFormatter;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const formatters: IFormatters = {
|
|
55
|
+
s(v: number) {
|
|
56
|
+
return v + 's';
|
|
57
|
+
},
|
|
58
|
+
m(v: number) {
|
|
59
|
+
return v + 'm';
|
|
60
|
+
},
|
|
61
|
+
h(v: number) {
|
|
62
|
+
return v + 'h';
|
|
63
|
+
},
|
|
64
|
+
d(v: number) {
|
|
65
|
+
return v + 'd';
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* set format for time delta
|
|
71
|
+
*/
|
|
72
|
+
export function setLocaleFormatter(formatter: Partial<IFormatters>) {
|
|
73
|
+
Object.assign(formatters, formatter);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* format time delta (in ms) to string, like: '1d'
|
|
78
|
+
* when ms<=0, returns '0s'
|
|
79
|
+
*
|
|
80
|
+
* format can set by `setLocaleFormatter`
|
|
81
|
+
* day is the largest unit
|
|
82
|
+
*/
|
|
83
|
+
export function deltaTiny(ms: number) {
|
|
84
|
+
if (ms <= 0) {
|
|
85
|
+
return '0s';
|
|
86
|
+
}
|
|
87
|
+
if (ms > 86400000) {
|
|
88
|
+
return formatters.d(Math.floor(ms / 86400000));
|
|
89
|
+
}
|
|
90
|
+
if (ms > 3600000) {
|
|
91
|
+
return formatters.h(Math.floor(ms / 3600000));
|
|
92
|
+
}
|
|
93
|
+
if (ms > 60000) {
|
|
94
|
+
return formatters.m(Math.floor(ms / 60000));
|
|
95
|
+
}
|
|
96
|
+
return formatters.s(Math.floor(ms / 1000));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* format time delta (in ms) to string, like: '1d10m42s'
|
|
101
|
+
* when ms<=0, returns '0s'
|
|
102
|
+
*
|
|
103
|
+
* format can set by `setLocaleFormatter`
|
|
104
|
+
* day is the largest unit
|
|
105
|
+
*/
|
|
106
|
+
export function delta(ms: number) {
|
|
107
|
+
let ret = '';
|
|
108
|
+
let val = Math.ceil(ms / 1000);
|
|
109
|
+
|
|
110
|
+
if (val <= 0) {
|
|
111
|
+
return '0s';
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// sec
|
|
115
|
+
const s = val % 60;
|
|
116
|
+
val = Math.floor(val / 60);
|
|
117
|
+
if (s > 0) {
|
|
118
|
+
ret = formatters.s(s);
|
|
119
|
+
}
|
|
120
|
+
if (val === 0) {
|
|
121
|
+
return ret;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// min
|
|
125
|
+
const m = val % 60;
|
|
126
|
+
val = Math.floor(val / 60);
|
|
127
|
+
if (m > 0) {
|
|
128
|
+
ret = formatters.m(m) + ret;
|
|
129
|
+
}
|
|
130
|
+
if (val === 0) {
|
|
131
|
+
return ret;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// hour
|
|
135
|
+
const h = val % 24;
|
|
136
|
+
val = Math.floor(val / 24);
|
|
137
|
+
if (h > 0) {
|
|
138
|
+
ret = formatters.h(h) + ret;
|
|
139
|
+
}
|
|
140
|
+
if (val === 0) {
|
|
141
|
+
return ret;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// day
|
|
145
|
+
if (val > 0) {
|
|
146
|
+
ret = formatters.d(h) + ret;
|
|
147
|
+
}
|
|
148
|
+
return ret;
|
|
149
|
+
}
|
|
150
|
+
}
|
package/src/date/unix.ts
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
declare const window: any;
|
|
2
|
+
declare const EventTarget: any;
|
|
3
|
+
declare const Element: any;
|
|
4
|
+
|
|
5
|
+
type ScalarTypes =
|
|
6
|
+
| bigint
|
|
7
|
+
| number
|
|
8
|
+
| Number
|
|
9
|
+
| boolean
|
|
10
|
+
| Boolean
|
|
11
|
+
| string
|
|
12
|
+
| String
|
|
13
|
+
| symbol
|
|
14
|
+
| undefined
|
|
15
|
+
| null
|
|
16
|
+
| Date
|
|
17
|
+
| RegExp
|
|
18
|
+
| Function;
|
|
19
|
+
|
|
20
|
+
export function isScalar(value: any): value is ScalarTypes {
|
|
21
|
+
switch (typeof value) {
|
|
22
|
+
case 'bigint':
|
|
23
|
+
case 'number':
|
|
24
|
+
case 'boolean':
|
|
25
|
+
case 'string':
|
|
26
|
+
case 'symbol':
|
|
27
|
+
case 'undefined':
|
|
28
|
+
case 'function':
|
|
29
|
+
return true;
|
|
30
|
+
case 'object':
|
|
31
|
+
return (
|
|
32
|
+
value === null ||
|
|
33
|
+
value instanceof Date ||
|
|
34
|
+
value instanceof Boolean ||
|
|
35
|
+
value instanceof String ||
|
|
36
|
+
value instanceof Number ||
|
|
37
|
+
value instanceof RegExp
|
|
38
|
+
);
|
|
39
|
+
default:
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export enum SerializableKind {
|
|
45
|
+
Invalid = 0,
|
|
46
|
+
Primitive,
|
|
47
|
+
Manual,
|
|
48
|
+
Other,
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function isSerializable(value: any): SerializableKind {
|
|
52
|
+
const t = typeof value;
|
|
53
|
+
switch (t) {
|
|
54
|
+
case 'bigint':
|
|
55
|
+
// @ts-expect-error
|
|
56
|
+
case 'number':
|
|
57
|
+
if (isNaN(value) || Infinity === value || -Infinity === value) {
|
|
58
|
+
return SerializableKind.Invalid;
|
|
59
|
+
}
|
|
60
|
+
case 'boolean':
|
|
61
|
+
case 'string':
|
|
62
|
+
case 'undefined':
|
|
63
|
+
return SerializableKind.Primitive;
|
|
64
|
+
case 'object':
|
|
65
|
+
if (!value) {
|
|
66
|
+
return SerializableKind.Invalid;
|
|
67
|
+
}
|
|
68
|
+
if ('toJSON' in value || Symbol.toPrimitive in value) {
|
|
69
|
+
return SerializableKind.Manual;
|
|
70
|
+
}
|
|
71
|
+
if (
|
|
72
|
+
value instanceof Map ||
|
|
73
|
+
value instanceof WeakMap ||
|
|
74
|
+
value instanceof Set ||
|
|
75
|
+
value instanceof WeakSet ||
|
|
76
|
+
value instanceof RegExp ||
|
|
77
|
+
value instanceof Promise ||
|
|
78
|
+
value === Math
|
|
79
|
+
) {
|
|
80
|
+
return SerializableKind.Invalid;
|
|
81
|
+
}
|
|
82
|
+
if (typeof window === 'object') {
|
|
83
|
+
if (value instanceof EventTarget) {
|
|
84
|
+
return SerializableKind.Invalid;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if ('then' in value && typeof value['then'] === 'function') {
|
|
88
|
+
return SerializableKind.Invalid;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return SerializableKind.Other;
|
|
92
|
+
}
|
|
93
|
+
return SerializableKind.Invalid;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function getTypeOf(value: any) {
|
|
97
|
+
const t = typeof value;
|
|
98
|
+
switch (t) {
|
|
99
|
+
case 'bigint':
|
|
100
|
+
case 'number':
|
|
101
|
+
case 'boolean':
|
|
102
|
+
case 'string':
|
|
103
|
+
case 'symbol':
|
|
104
|
+
case 'undefined':
|
|
105
|
+
case 'function':
|
|
106
|
+
return t;
|
|
107
|
+
}
|
|
108
|
+
if (value === null) return 'null';
|
|
109
|
+
if (value instanceof Promise) return 'Promise';
|
|
110
|
+
if (value instanceof Error) return 'Error';
|
|
111
|
+
|
|
112
|
+
if (typeof window === 'object') {
|
|
113
|
+
if (value instanceof Element) return 'DOM';
|
|
114
|
+
if (value instanceof EventTarget) return 'EventTarget';
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (value instanceof String) return 'string';
|
|
118
|
+
if (value instanceof Number) return 'number';
|
|
119
|
+
if (value instanceof Boolean) return 'boolean';
|
|
120
|
+
if (value instanceof Date) return 'datetime';
|
|
121
|
+
if (value instanceof RegExp) return 'regexp';
|
|
122
|
+
|
|
123
|
+
return 'unknown';
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function assertSerializable(value: any) {
|
|
127
|
+
if (!value) return;
|
|
128
|
+
|
|
129
|
+
assertSerializableWalker(value, '$');
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function assertSerializableWalker(data: any, path: string) {
|
|
133
|
+
const type = isSerializable(data);
|
|
134
|
+
if (type === SerializableKind.Invalid) {
|
|
135
|
+
console.error('在对象中发现不可序列化的数据: 路径为 %s, 可能的类型为: %s\n%j', path, getTypeOf(data), data);
|
|
136
|
+
throw new TypeError(`对象路径 ${path} 无法序列化`);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (type === SerializableKind.Manual || type === SerializableKind.Primitive) {
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
for (const [key, value] of Object.entries(data)) {
|
|
144
|
+
assertSerializableWalker(value, `${path}/${key}`);
|
|
145
|
+
}
|
|
146
|
+
}
|