xjs-common 13.0.2 → 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.
@@ -41,7 +41,7 @@ var DType;
41
41
  DType.required = required;
42
42
  /**
43
43
  * express array.
44
- * @param elmDesc {@link TypeDesc} or class constructor type.
44
+ * @param elmDesc {@link TypeDesc} or {@link Ctor|class constructor type}.
45
45
  */
46
46
  function array(elmDesc = {}) {
47
47
  return (target, propKey) => setDesc(target, propKey, (td) => u_type_1.UType.isFunction(elmDesc) ? td.ary = { cls: elmDesc } : td.ary = elmDesc);
@@ -49,7 +49,7 @@ var DType;
49
49
  DType.array = array;
50
50
  /**
51
51
  * express record object. note that this may allow array type because array is essentialy object type has properties.
52
- * @param elmDesc {@link TypeDesc} or class constructor type.
52
+ * @param elmDesc {@link TypeDesc} or {@link Ctor|class constructor type}.
53
53
  */
54
54
  function record(elmDesc = {}) {
55
55
  return (target, propKey) => setDesc(target, propKey, (td) => u_type_1.UType.isFunction(elmDesc) ? td.rcd = { cls: elmDesc } : td.rcd = elmDesc);
@@ -57,7 +57,7 @@ var DType;
57
57
  DType.record = record;
58
58
  /**
59
59
  * express an object which has properties that specified class express with {@link DType}.
60
- * @param ctor class constructor type.
60
+ * @param ctor {@link Ctor|class constructor type}.
61
61
  */
62
62
  function object(ctor) {
63
63
  return (target, propKey) => setDesc(target, propKey, (td) => td.cls = ctor);
@@ -5,7 +5,7 @@ const xjs_err_1 = require("../../obj/xjs-err");
5
5
  const u_1 = require("../u");
6
6
  const s_errCode = 100;
7
7
  /**
8
- * applies transation to the method. note that the method must return a Promise.
8
+ * applies transation to the method. **note that the method must return a `Promise`**.
9
9
  * @param op.timeoutSec default is `30`.
10
10
  */
11
11
  function transaction(op) {
@@ -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 => {
@@ -71,4 +71,13 @@ var UObj;
71
71
  return o;
72
72
  }
73
73
  UObj.manipulateProperties = manipulateProperties;
74
+ /**
75
+ * generate a record object which contains specified keys with values generated from value generator.
76
+ * @param keys keys contained in the object.
77
+ * @param vgen value generator.
78
+ */
79
+ function generateRecord(keys, vgen) {
80
+ return keys.reduce((o, k) => { o[k] = vgen(k); return o; }, {});
81
+ }
82
+ UObj.generateRecord = generateRecord;
74
83
  })(UObj || (exports.UObj = UObj = {}));
@@ -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);
@@ -55,17 +55,17 @@ export declare namespace DType {
55
55
  function required(target: Object, propKey: string): void;
56
56
  /**
57
57
  * express array.
58
- * @param elmDesc {@link TypeDesc} or class constructor type.
58
+ * @param elmDesc {@link TypeDesc} or {@link Ctor|class constructor type}.
59
59
  */
60
60
  function array(elmDesc?: AnyTypeDesc | Ctor): (target: Object, propKey: string) => void;
61
61
  /**
62
62
  * express record object. note that this may allow array type because array is essentialy object type has properties.
63
- * @param elmDesc {@link TypeDesc} or class constructor type.
63
+ * @param elmDesc {@link TypeDesc} or {@link Ctor|class constructor type}.
64
64
  */
65
65
  function record(elmDesc?: AnyTypeDesc | Ctor): (target: Object, propKey: string) => void;
66
66
  /**
67
67
  * express an object which has properties that specified class express with {@link DType}.
68
- * @param ctor class constructor type.
68
+ * @param ctor {@link Ctor|class constructor type}.
69
69
  */
70
70
  function object(ctor: Ctor): (target: Object, propKey: string) => void;
71
71
  function keep(target: Object, propKey: string): void;
@@ -38,7 +38,7 @@ export var DType;
38
38
  DType.required = required;
39
39
  /**
40
40
  * express array.
41
- * @param elmDesc {@link TypeDesc} or class constructor type.
41
+ * @param elmDesc {@link TypeDesc} or {@link Ctor|class constructor type}.
42
42
  */
43
43
  function array(elmDesc = {}) {
44
44
  return (target, propKey) => setDesc(target, propKey, (td) => UType.isFunction(elmDesc) ? td.ary = { cls: elmDesc } : td.ary = elmDesc);
@@ -46,7 +46,7 @@ export var DType;
46
46
  DType.array = array;
47
47
  /**
48
48
  * express record object. note that this may allow array type because array is essentialy object type has properties.
49
- * @param elmDesc {@link TypeDesc} or class constructor type.
49
+ * @param elmDesc {@link TypeDesc} or {@link Ctor|class constructor type}.
50
50
  */
51
51
  function record(elmDesc = {}) {
52
52
  return (target, propKey) => setDesc(target, propKey, (td) => UType.isFunction(elmDesc) ? td.rcd = { cls: elmDesc } : td.rcd = elmDesc);
@@ -54,7 +54,7 @@ export var DType;
54
54
  DType.record = record;
55
55
  /**
56
56
  * express an object which has properties that specified class express with {@link DType}.
57
- * @param ctor class constructor type.
57
+ * @param ctor {@link Ctor|class constructor type}.
58
58
  */
59
59
  function object(ctor) {
60
60
  return (target, propKey) => setDesc(target, propKey, (td) => td.cls = ctor);
@@ -1,5 +1,5 @@
1
1
  /**
2
- * applies transation to the method. note that the method must return a Promise.
2
+ * applies transation to the method. **note that the method must return a `Promise`**.
3
3
  * @param op.timeoutSec default is `30`.
4
4
  */
5
5
  export declare function transaction(op?: {
@@ -2,7 +2,7 @@ import { XjsErr } from "../../obj/xjs-err";
2
2
  import { delay } from "../u";
3
3
  const s_errCode = 100;
4
4
  /**
5
- * applies transation to the method. note that the method must return a Promise.
5
+ * applies transation to the method. **note that the method must return a `Promise`**.
6
6
  * @param op.timeoutSec default is `30`.
7
7
  */
8
8
  export function transaction(op) {
@@ -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 => {
@@ -1,4 +1,4 @@
1
- import { Ctor, MaybeArray, NonObject, NormalRecord, Type } from "../const/types";
1
+ import { Ctor, IndexSignature, MaybeArray, NonObject, NormalRecord, Type } from "../const/types";
2
2
  export declare namespace UObj {
3
3
  /**
4
4
  * assign properties to the object with specified property keys.
@@ -36,4 +36,10 @@ export declare namespace UObj {
36
36
  recursive?: boolean;
37
37
  targetType?: MaybeArray<Exclude<Type, "object" | "null" | "undefined">>;
38
38
  }): T;
39
+ /**
40
+ * generate a record object which contains specified keys with values generated from value generator.
41
+ * @param keys keys contained in the object.
42
+ * @param vgen value generator.
43
+ */
44
+ function generateRecord<K extends IndexSignature, V>(keys: K[], vgen: (k: K) => V): Record<K, V>;
39
45
  }
@@ -68,4 +68,13 @@ export var UObj;
68
68
  return o;
69
69
  }
70
70
  UObj.manipulateProperties = manipulateProperties;
71
+ /**
72
+ * generate a record object which contains specified keys with values generated from value generator.
73
+ * @param keys keys contained in the object.
74
+ * @param vgen value generator.
75
+ */
76
+ function generateRecord(keys, vgen) {
77
+ return keys.reduce((o, k) => { o[k] = vgen(k); return o; }, {});
78
+ }
79
+ UObj.generateRecord = generateRecord;
71
80
  })(UObj || (UObj = {}));
@@ -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.2",
3
+ "version": "13.1.0",
4
4
  "description": "library modules for typescript that bundled general-purpose implementations.",
5
5
  "repository": {
6
6
  "type": "git",