xjs-common 13.0.3 → 13.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.
@@ -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 => {
@@ -72,7 +72,7 @@ var UObj;
72
72
  }
73
73
  UObj.manipulateProperties = manipulateProperties;
74
74
  /**
75
- * generate a record object which contains spedified keys with values generated from value generator.
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
  */
@@ -46,9 +46,11 @@ function retry(cb, op) {
46
46
  l.warn(e);
47
47
  return true;
48
48
  };
49
- const prcs = (c) => {
50
- if (c < 0)
51
- throw new xjs_err_1.XjsErr(s_errCode, "failure exceeds retryable count.");
49
+ const prcs = (c, e) => {
50
+ if (c < 0) {
51
+ l.error("failure exceeds retryable count.");
52
+ throw e ?? new xjs_err_1.XjsErr(s_errCode, "failure exceeds retryable count.");
53
+ }
52
54
  let ret = null;
53
55
  const innerPrcs = () => {
54
56
  try {
@@ -56,7 +58,7 @@ function retry(cb, op) {
56
58
  }
57
59
  catch (e) {
58
60
  if (handleError(e))
59
- ret = prcs(c - 1);
61
+ ret = prcs(c - 1, e);
60
62
  else
61
63
  throw e;
62
64
  }
@@ -64,7 +66,7 @@ function retry(cb, op) {
64
66
  return new Promise((resolve, reject) => ret.then(resolve).catch((e) => {
65
67
  if (handleError(e))
66
68
  try {
67
- ret = resolve(prcs(c - 1));
69
+ ret = resolve(prcs(c - 1, e));
68
70
  }
69
71
  catch (e2) {
70
72
  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
- * chop array to partial arrays which have specified length. the remainder is added to end of a result.
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 spedified keys with values generated from value generator.
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
  */
@@ -69,7 +69,7 @@ export var UObj;
69
69
  }
70
70
  UObj.manipulateProperties = manipulateProperties;
71
71
  /**
72
- * generate a record object which contains spedified keys with values generated from value generator.
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
  */
@@ -38,9 +38,11 @@ export function retry(cb, op) {
38
38
  l.warn(e);
39
39
  return true;
40
40
  };
41
- const prcs = (c) => {
42
- if (c < 0)
43
- throw new XjsErr(s_errCode, "failure exceeds retryable count.");
41
+ const prcs = (c, e) => {
42
+ if (c < 0) {
43
+ l.error("failure exceeds retryable count.");
44
+ throw e ?? new XjsErr(s_errCode, "failure exceeds retryable count.");
45
+ }
44
46
  let ret = null;
45
47
  const innerPrcs = () => {
46
48
  try {
@@ -48,7 +50,7 @@ export function retry(cb, op) {
48
50
  }
49
51
  catch (e) {
50
52
  if (handleError(e))
51
- ret = prcs(c - 1);
53
+ ret = prcs(c - 1, e);
52
54
  else
53
55
  throw e;
54
56
  }
@@ -56,7 +58,7 @@ export function retry(cb, op) {
56
58
  return new Promise((resolve, reject) => ret.then(resolve).catch((e) => {
57
59
  if (handleError(e))
58
60
  try {
59
- ret = resolve(prcs(c - 1));
61
+ ret = resolve(prcs(c - 1, e));
60
62
  }
61
63
  catch (e2) {
62
64
  reject(e2);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xjs-common",
3
- "version": "13.0.3",
3
+ "version": "13.1.0",
4
4
  "description": "library modules for typescript that bundled general-purpose implementations.",
5
5
  "repository": {
6
6
  "type": "git",