xjs-common 13.0.3 → 13.2.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/build/cjs/func/u-array.js +21 -0
- package/build/cjs/func/u-obj.js +1 -1
- package/build/cjs/func/u.js +11 -12
- package/build/esm/func/u-array.d.ts +13 -2
- package/build/esm/func/u-array.js +21 -0
- package/build/esm/func/u-obj.d.ts +1 -1
- package/build/esm/func/u-obj.js +1 -1
- package/build/esm/func/u.d.ts +12 -1
- package/build/esm/func/u.js +11 -11
- package/package.json +1 -1
|
@@ -39,6 +39,27 @@ var UArray;
|
|
|
39
39
|
return result;
|
|
40
40
|
}
|
|
41
41
|
UArray.distinct = distinct;
|
|
42
|
+
function duplicate(array, op) {
|
|
43
|
+
if (!array || array.length === 0)
|
|
44
|
+
return [];
|
|
45
|
+
if (op?.k)
|
|
46
|
+
return Array.from((0, u_1.array2map)(array, e => e[op.k]).values()).filter(a => a.length > 1).flatMap(a => a);
|
|
47
|
+
const a = [...array], result = [];
|
|
48
|
+
const p = op?.predicate ?? ((v1, v2) => v1 == v2);
|
|
49
|
+
while (a.length > 0) {
|
|
50
|
+
const e = a.pop();
|
|
51
|
+
let dup = [];
|
|
52
|
+
for (let i = a.length - 1, e2 = a[i]; i >= 0; i--, e2 = a[i])
|
|
53
|
+
if (p(e, e2)) {
|
|
54
|
+
a.splice(i, 1);
|
|
55
|
+
dup.push(e2);
|
|
56
|
+
}
|
|
57
|
+
if (dup.length > 0)
|
|
58
|
+
result.push(...dup, e);
|
|
59
|
+
}
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
62
|
+
UArray.duplicate = duplicate;
|
|
42
63
|
function chop(array, len) {
|
|
43
64
|
return [...Array(Math.ceil(array.length / len)).keys()]
|
|
44
65
|
.map(i => {
|
package/build/cjs/func/u-obj.js
CHANGED
|
@@ -72,7 +72,7 @@ var UObj;
|
|
|
72
72
|
}
|
|
73
73
|
UObj.manipulateProperties = manipulateProperties;
|
|
74
74
|
/**
|
|
75
|
-
* generate a record object which contains
|
|
75
|
+
* generate a record object which contains specified keys with values generated from value generator.
|
|
76
76
|
* @param keys keys contained in the object.
|
|
77
77
|
* @param vgen value generator.
|
|
78
78
|
*/
|
package/build/cjs/func/u.js
CHANGED
|
@@ -4,7 +4,6 @@ exports.getJSTDate = getJSTDate;
|
|
|
4
4
|
exports.delay = delay;
|
|
5
5
|
exports.int2array = int2array;
|
|
6
6
|
exports.array2map = array2map;
|
|
7
|
-
exports.bitor = bitor;
|
|
8
7
|
exports.retry = retry;
|
|
9
8
|
const xjs_err_1 = require("../obj/xjs-err");
|
|
10
9
|
const s_errCode = 10;
|
|
@@ -20,20 +19,18 @@ function int2array(size) {
|
|
|
20
19
|
throw new xjs_err_1.XjsErr(s_errCode, "size of the argument is not number.");
|
|
21
20
|
return Array.from(Array(s).keys());
|
|
22
21
|
}
|
|
23
|
-
function array2map(array, keyGen) {
|
|
22
|
+
function array2map(array, keyGen, op) {
|
|
23
|
+
const _acm = op?.accumulate ?? true;
|
|
24
24
|
const map = new Map();
|
|
25
25
|
for (const e of array) {
|
|
26
26
|
const k = keyGen(e);
|
|
27
27
|
if (map.has(k))
|
|
28
|
-
map.get(k).push(e);
|
|
28
|
+
_acm && map.get(k).push(e);
|
|
29
29
|
else
|
|
30
|
-
map.set(k, [e]);
|
|
30
|
+
map.set(k, _acm ? [e] : e);
|
|
31
31
|
}
|
|
32
32
|
return map;
|
|
33
33
|
}
|
|
34
|
-
function bitor(...bit) {
|
|
35
|
-
return bit.reduce((a, b) => a | b);
|
|
36
|
-
}
|
|
37
34
|
;
|
|
38
35
|
;
|
|
39
36
|
;
|
|
@@ -46,9 +43,11 @@ function retry(cb, op) {
|
|
|
46
43
|
l.warn(e);
|
|
47
44
|
return true;
|
|
48
45
|
};
|
|
49
|
-
const prcs = (c) => {
|
|
50
|
-
if (c < 0)
|
|
51
|
-
|
|
46
|
+
const prcs = (c, e) => {
|
|
47
|
+
if (c < 0) {
|
|
48
|
+
l.error("failure exceeds retryable count.");
|
|
49
|
+
throw e ?? new xjs_err_1.XjsErr(s_errCode, "failure exceeds retryable count.");
|
|
50
|
+
}
|
|
52
51
|
let ret = null;
|
|
53
52
|
const innerPrcs = () => {
|
|
54
53
|
try {
|
|
@@ -56,7 +55,7 @@ function retry(cb, op) {
|
|
|
56
55
|
}
|
|
57
56
|
catch (e) {
|
|
58
57
|
if (handleError(e))
|
|
59
|
-
ret = prcs(c - 1);
|
|
58
|
+
ret = prcs(c - 1, e);
|
|
60
59
|
else
|
|
61
60
|
throw e;
|
|
62
61
|
}
|
|
@@ -64,7 +63,7 @@ function retry(cb, op) {
|
|
|
64
63
|
return new Promise((resolve, reject) => ret.then(resolve).catch((e) => {
|
|
65
64
|
if (handleError(e))
|
|
66
65
|
try {
|
|
67
|
-
ret = resolve(prcs(c - 1));
|
|
66
|
+
ret = resolve(prcs(c - 1, e));
|
|
68
67
|
}
|
|
69
68
|
catch (e2) {
|
|
70
69
|
reject(e2);
|
|
@@ -14,7 +14,7 @@ export declare namespace UArray {
|
|
|
14
14
|
useStrictEqual?: boolean;
|
|
15
15
|
}): boolean;
|
|
16
16
|
/**
|
|
17
|
-
* returns array which is removed duplicate of elements.
|
|
17
|
+
* returns an array which is removed duplicate of elements.
|
|
18
18
|
* this doesn't mutate the param.
|
|
19
19
|
*/
|
|
20
20
|
function distinct<T>(array: T[]): T[];
|
|
@@ -27,7 +27,18 @@ export declare namespace UArray {
|
|
|
27
27
|
takeLast?: boolean;
|
|
28
28
|
}): T[];
|
|
29
29
|
/**
|
|
30
|
-
*
|
|
30
|
+
* returns an array which contains duplicate values of the original array.
|
|
31
|
+
* this doesn't mutate the param.
|
|
32
|
+
*/
|
|
33
|
+
function duplicate<T>(array: T[]): T[];
|
|
34
|
+
function duplicate<T>(array: T[], op?: {
|
|
35
|
+
k?: keyof T;
|
|
36
|
+
}): T[];
|
|
37
|
+
function duplicate<T>(array: T[], op?: {
|
|
38
|
+
predicate?: (v1: T, v2: T) => boolean;
|
|
39
|
+
}): T[];
|
|
40
|
+
/**
|
|
41
|
+
* chop an array to partial arrays which have specified length. the remainder is added to end of a result.
|
|
31
42
|
* this function has compatibility to {@link AlmostArray} like {@link Uint8Array} etc.
|
|
32
43
|
*/
|
|
33
44
|
function chop<E>(array: E[], len: number): E[][];
|
|
@@ -36,6 +36,27 @@ export var UArray;
|
|
|
36
36
|
return result;
|
|
37
37
|
}
|
|
38
38
|
UArray.distinct = distinct;
|
|
39
|
+
function duplicate(array, op) {
|
|
40
|
+
if (!array || array.length === 0)
|
|
41
|
+
return [];
|
|
42
|
+
if (op?.k)
|
|
43
|
+
return Array.from(array2map(array, e => e[op.k]).values()).filter(a => a.length > 1).flatMap(a => a);
|
|
44
|
+
const a = [...array], result = [];
|
|
45
|
+
const p = op?.predicate ?? ((v1, v2) => v1 == v2);
|
|
46
|
+
while (a.length > 0) {
|
|
47
|
+
const e = a.pop();
|
|
48
|
+
let dup = [];
|
|
49
|
+
for (let i = a.length - 1, e2 = a[i]; i >= 0; i--, e2 = a[i])
|
|
50
|
+
if (p(e, e2)) {
|
|
51
|
+
a.splice(i, 1);
|
|
52
|
+
dup.push(e2);
|
|
53
|
+
}
|
|
54
|
+
if (dup.length > 0)
|
|
55
|
+
result.push(...dup, e);
|
|
56
|
+
}
|
|
57
|
+
return result;
|
|
58
|
+
}
|
|
59
|
+
UArray.duplicate = duplicate;
|
|
39
60
|
function chop(array, len) {
|
|
40
61
|
return [...Array(Math.ceil(array.length / len)).keys()]
|
|
41
62
|
.map(i => {
|
|
@@ -37,7 +37,7 @@ export declare namespace UObj {
|
|
|
37
37
|
targetType?: MaybeArray<Exclude<Type, "object" | "null" | "undefined">>;
|
|
38
38
|
}): T;
|
|
39
39
|
/**
|
|
40
|
-
* generate a record object which contains
|
|
40
|
+
* generate a record object which contains specified keys with values generated from value generator.
|
|
41
41
|
* @param keys keys contained in the object.
|
|
42
42
|
* @param vgen value generator.
|
|
43
43
|
*/
|
package/build/esm/func/u-obj.js
CHANGED
|
@@ -69,7 +69,7 @@ export var UObj;
|
|
|
69
69
|
}
|
|
70
70
|
UObj.manipulateProperties = manipulateProperties;
|
|
71
71
|
/**
|
|
72
|
-
* generate a record object which contains
|
|
72
|
+
* generate a record object which contains specified keys with values generated from value generator.
|
|
73
73
|
* @param keys keys contained in the object.
|
|
74
74
|
* @param vgen value generator.
|
|
75
75
|
*/
|
package/build/esm/func/u.d.ts
CHANGED
|
@@ -2,8 +2,19 @@ import { Loggable, MaybePromise } from "../const/types";
|
|
|
2
2
|
export declare function getJSTDate(d?: Date): Date;
|
|
3
3
|
export declare function delay(sec: number): Promise<void>;
|
|
4
4
|
export declare function int2array(size: number): number[];
|
|
5
|
+
/**
|
|
6
|
+
* generate `Map` object from an array.
|
|
7
|
+
* @param array source array.
|
|
8
|
+
* @param keyGen predicate which generates map keys from a value of the array.
|
|
9
|
+
* @param op.accumulate flag whether values of the map are accumulated or not if key/value from the array conflict. default is true.
|
|
10
|
+
*/
|
|
5
11
|
export declare function array2map<K, T>(array: T[], keyGen: (e: T) => K): Map<K, T[]>;
|
|
6
|
-
export declare function
|
|
12
|
+
export declare function array2map<K, T>(array: T[], keyGen: (e: T) => K, op: {
|
|
13
|
+
accumulate: false;
|
|
14
|
+
}): Map<K, T>;
|
|
15
|
+
export declare function array2map<K, T>(array: T[], keyGen: (e: T) => K, op?: {
|
|
16
|
+
accumulate?: boolean;
|
|
17
|
+
}): Map<K, T[]>;
|
|
7
18
|
export interface RetryOption<T = MaybePromise> {
|
|
8
19
|
/**
|
|
9
20
|
* number of retries. default is 1.
|
package/build/esm/func/u.js
CHANGED
|
@@ -12,20 +12,18 @@ export function int2array(size) {
|
|
|
12
12
|
throw new XjsErr(s_errCode, "size of the argument is not number.");
|
|
13
13
|
return Array.from(Array(s).keys());
|
|
14
14
|
}
|
|
15
|
-
export function array2map(array, keyGen) {
|
|
15
|
+
export function array2map(array, keyGen, op) {
|
|
16
|
+
const _acm = op?.accumulate ?? true;
|
|
16
17
|
const map = new Map();
|
|
17
18
|
for (const e of array) {
|
|
18
19
|
const k = keyGen(e);
|
|
19
20
|
if (map.has(k))
|
|
20
|
-
map.get(k).push(e);
|
|
21
|
+
_acm && map.get(k).push(e);
|
|
21
22
|
else
|
|
22
|
-
map.set(k, [e]);
|
|
23
|
+
map.set(k, _acm ? [e] : e);
|
|
23
24
|
}
|
|
24
25
|
return map;
|
|
25
26
|
}
|
|
26
|
-
export function bitor(...bit) {
|
|
27
|
-
return bit.reduce((a, b) => a | b);
|
|
28
|
-
}
|
|
29
27
|
;
|
|
30
28
|
;
|
|
31
29
|
;
|
|
@@ -38,9 +36,11 @@ export function retry(cb, op) {
|
|
|
38
36
|
l.warn(e);
|
|
39
37
|
return true;
|
|
40
38
|
};
|
|
41
|
-
const prcs = (c) => {
|
|
42
|
-
if (c < 0)
|
|
43
|
-
|
|
39
|
+
const prcs = (c, e) => {
|
|
40
|
+
if (c < 0) {
|
|
41
|
+
l.error("failure exceeds retryable count.");
|
|
42
|
+
throw e ?? new XjsErr(s_errCode, "failure exceeds retryable count.");
|
|
43
|
+
}
|
|
44
44
|
let ret = null;
|
|
45
45
|
const innerPrcs = () => {
|
|
46
46
|
try {
|
|
@@ -48,7 +48,7 @@ export function retry(cb, op) {
|
|
|
48
48
|
}
|
|
49
49
|
catch (e) {
|
|
50
50
|
if (handleError(e))
|
|
51
|
-
ret = prcs(c - 1);
|
|
51
|
+
ret = prcs(c - 1, e);
|
|
52
52
|
else
|
|
53
53
|
throw e;
|
|
54
54
|
}
|
|
@@ -56,7 +56,7 @@ export function retry(cb, op) {
|
|
|
56
56
|
return new Promise((resolve, reject) => ret.then(resolve).catch((e) => {
|
|
57
57
|
if (handleError(e))
|
|
58
58
|
try {
|
|
59
|
-
ret = resolve(prcs(c - 1));
|
|
59
|
+
ret = resolve(prcs(c - 1, e));
|
|
60
60
|
}
|
|
61
61
|
catch (e2) {
|
|
62
62
|
reject(e2);
|