xjs-common 13.4.0 → 13.5.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.
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Array2 = void 0;
4
+ const u_type_1 = require("./u-type");
5
+ var Array2;
6
+ (function (Array2) {
7
+ function map(array, keyGen, op) {
8
+ const _acm = op?.accumulate ?? true;
9
+ const map = new Map();
10
+ for (const e of array) {
11
+ const k = keyGen(e);
12
+ if (map.has(k))
13
+ _acm && map.get(k).push(e);
14
+ else
15
+ map.set(k, _acm ? [e] : e);
16
+ }
17
+ return map;
18
+ }
19
+ Array2.map = map;
20
+ function record(keyOrValues, op) {
21
+ return keyOrValues.reduce(!!op.kgen
22
+ ? (o, korv) => { o[op.kgen(korv)] = korv; return o; }
23
+ : (o, korv) => { o[korv] = op.vgen(korv); return o; }, {});
24
+ }
25
+ Array2.record = record;
26
+ function sum(array) {
27
+ return array.map(e => u_type_1.UType.isNumber(e) ? e : Number(e)).reduce((a, b) => a + b, 0);
28
+ }
29
+ Array2.sum = sum;
30
+ })(Array2 || (exports.Array2 = Array2 = {}));
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.UArray = void 0;
4
+ const array2_1 = require("./array2");
4
5
  const u_1 = require("./u");
5
6
  var UArray;
6
7
  (function (UArray) {
@@ -31,7 +32,7 @@ var UArray;
31
32
  if (!array || array.length === 0)
32
33
  return [];
33
34
  if (op?.k)
34
- return Array.from((0, u_1.array2map)(array, e => e[op.k]).values()).map(a => op?.takeLast ? a.pop() : a.shift());
35
+ return Array.from(array2_1.Array2.map(array, e => e[op.k]).values()).map(a => op?.takeLast ? a.pop() : a.shift());
35
36
  const a = op?.takeLast ? [...array].reverse() : [...array];
36
37
  const p = op?.predicate ?? ((v1, v2) => v1 == v2);
37
38
  const result = [a.shift()];
@@ -43,7 +44,7 @@ var UArray;
43
44
  if (!array || array.length === 0)
44
45
  return [];
45
46
  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
+ return Array.from(array2_1.Array2.map(array, e => e[op.k]).values()).filter(a => a.length > 1).flatMap(a => a);
47
48
  const a = [...array], result = [];
48
49
  const p = op?.predicate ?? ((v1, v2) => v1 == v2);
49
50
  while (a.length > 0) {
@@ -75,10 +75,4 @@ var UObj;
75
75
  return o;
76
76
  }
77
77
  UObj.manipulateProperties = manipulateProperties;
78
- function array2record(keyOrValues, op) {
79
- return keyOrValues.reduce(!!op.kgen
80
- ? (o, korv) => { o[op.kgen(korv)] = korv; return o; }
81
- : (o, korv) => { o[korv] = op.vgen(korv); return o; }, {});
82
- }
83
- UObj.array2record = array2record;
84
78
  })(UObj || (exports.UObj = UObj = {}));
@@ -19,8 +19,8 @@ var UString;
19
19
  /**
20
20
  * generate date time number as fixed length (depends on `unit`) string without separator charactor.
21
21
  * For example, `2025-06-08T10:15:06.366Z` is to be `20250608101506366`.
22
- * @param op.date Date object refered by this. default is `new Date()`.
23
- * @param op.unit time unit. default is secound.
22
+ * @param op.date Date object referred by this. default is `new Date()`.
23
+ * @param op.unit time unit. default is second.
24
24
  */
25
25
  function simpleTime(op) {
26
26
  const t = (op?.date ?? new Date()).toISOString().split(".")[0].replace(/[-T:]/g, "");
@@ -3,12 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getJSTDate = getJSTDate;
4
4
  exports.delay = delay;
5
5
  exports.int2array = int2array;
6
- exports.array2map = array2map;
7
6
  exports.retry = retry;
8
7
  const xjs_err_1 = require("../obj/xjs-err");
8
+ const u_type_1 = require("./u-type");
9
9
  const s_errCode = 10;
10
10
  function getJSTDate(d) {
11
- return new Date((d ? d.getTime() : Date.now()) + 9 * 60 * 60 * 1000);
11
+ const adjuster = 9 * 60 * 60 * 1000;
12
+ return u_type_1.UType.isNumber(d) ? new Date(d + adjuster) : new Date((d ? d.getTime() : Date.now()) + adjuster);
12
13
  }
13
14
  function delay(sec) {
14
15
  return new Promise(resolve => setTimeout(resolve, 1000 * sec));
@@ -19,18 +20,6 @@ function int2array(size) {
19
20
  throw new xjs_err_1.XjsErr(s_errCode, "size of the argument is not number.");
20
21
  return Array.from(Array(s).keys());
21
22
  }
22
- function array2map(array, keyGen, op) {
23
- const _acm = op?.accumulate ?? true;
24
- const map = new Map();
25
- for (const e of array) {
26
- const k = keyGen(e);
27
- if (map.has(k))
28
- _acm && map.get(k).push(e);
29
- else
30
- map.set(k, _acm ? [e] : e);
31
- }
32
- return map;
33
- }
34
23
  ;
35
24
  ;
36
25
  ;
@@ -0,0 +1,29 @@
1
+ import { IndexSignature } from "../const/types";
2
+ export declare namespace Array2 {
3
+ /**
4
+ * generate `Map` object from an array.
5
+ * @param array source array.
6
+ * @param keyGen predicate which generates map keys from a value of the array.
7
+ * @param op.accumulate flag whether values of the map are accumulated or not if key/value from the array conflict. default is true.
8
+ */
9
+ function map<K, T>(array: T[], keyGen: (e: T) => K): Map<K, T[]>;
10
+ function map<K, T>(array: T[], keyGen: (e: T) => K, op: {
11
+ accumulate: false;
12
+ }): Map<K, T>;
13
+ function map<K, T>(array: T[], keyGen: (e: T) => K, op?: {
14
+ accumulate?: boolean;
15
+ }): Map<K, T[]>;
16
+ /**
17
+ * generate a record object which is mapped by specified keys or values with pair of the entry generated from generator.
18
+ * @param keyOrValues keys or values to be contained in the object.
19
+ * @param op.kgen key generator. if pass this, `keyOrValues` is treated as values.
20
+ * @param op.vgen value generator. if pass this, `keyOrValues` is treated as keys.
21
+ */
22
+ function record<K extends IndexSignature, V>(keyOrValues: K[], op: {
23
+ vgen: (k: K) => V;
24
+ }): Record<K, V>;
25
+ function record<K extends IndexSignature, V>(keyOrValues: V[], op: {
26
+ kgen: (v: V) => K;
27
+ }): Record<K, V>;
28
+ function sum(array: (number | `${number}`)[]): number;
29
+ }
@@ -0,0 +1,27 @@
1
+ import { UType } from "./u-type";
2
+ export var Array2;
3
+ (function (Array2) {
4
+ function map(array, keyGen, op) {
5
+ const _acm = op?.accumulate ?? true;
6
+ const map = new Map();
7
+ for (const e of array) {
8
+ const k = keyGen(e);
9
+ if (map.has(k))
10
+ _acm && map.get(k).push(e);
11
+ else
12
+ map.set(k, _acm ? [e] : e);
13
+ }
14
+ return map;
15
+ }
16
+ Array2.map = map;
17
+ function record(keyOrValues, op) {
18
+ return keyOrValues.reduce(!!op.kgen
19
+ ? (o, korv) => { o[op.kgen(korv)] = korv; return o; }
20
+ : (o, korv) => { o[korv] = op.vgen(korv); return o; }, {});
21
+ }
22
+ Array2.record = record;
23
+ function sum(array) {
24
+ return array.map(e => UType.isNumber(e) ? e : Number(e)).reduce((a, b) => a + b, 0);
25
+ }
26
+ Array2.sum = sum;
27
+ })(Array2 || (Array2 = {}));
@@ -1,4 +1,5 @@
1
- import { array2map, int2array } from "./u";
1
+ import { Array2 } from "./array2";
2
+ import { int2array } from "./u";
2
3
  export var UArray;
3
4
  (function (UArray) {
4
5
  /**
@@ -28,7 +29,7 @@ export var UArray;
28
29
  if (!array || array.length === 0)
29
30
  return [];
30
31
  if (op?.k)
31
- return Array.from(array2map(array, e => e[op.k]).values()).map(a => op?.takeLast ? a.pop() : a.shift());
32
+ return Array.from(Array2.map(array, e => e[op.k]).values()).map(a => op?.takeLast ? a.pop() : a.shift());
32
33
  const a = op?.takeLast ? [...array].reverse() : [...array];
33
34
  const p = op?.predicate ?? ((v1, v2) => v1 == v2);
34
35
  const result = [a.shift()];
@@ -40,7 +41,7 @@ export var UArray;
40
41
  if (!array || array.length === 0)
41
42
  return [];
42
43
  if (op?.k)
43
- return Array.from(array2map(array, e => e[op.k]).values()).filter(a => a.length > 1).flatMap(a => a);
44
+ return Array.from(Array2.map(array, e => e[op.k]).values()).filter(a => a.length > 1).flatMap(a => a);
44
45
  const a = [...array], result = [];
45
46
  const p = op?.predicate ?? ((v1, v2) => v1 == v2);
46
47
  while (a.length > 0) {
@@ -44,16 +44,4 @@ export declare namespace UObj {
44
44
  recursive?: boolean;
45
45
  targetType?: MaybeArray<Exclude<Type, "object" | "null" | "undefined">>;
46
46
  }): T;
47
- /**
48
- * generate a record object which is mapped by specified keys or values with pair of the entry generated from generator.
49
- * @param keyOrValues keys or values to be contained in the object.
50
- * @param op.kgen key generator. if pass this, `keyOrValues` is treated as values.
51
- * @param op.vgen value generator. if pass this, `keyOrValues` is treated as keys.
52
- */
53
- function array2record<K extends IndexSignature, V>(keyOrValues: K[], op: {
54
- vgen: (k: K) => V;
55
- }): Record<K, V>;
56
- function array2record<K extends IndexSignature, V>(keyOrValues: V[], op: {
57
- kgen: (v: V) => K;
58
- }): Record<K, V>;
59
47
  }
@@ -72,10 +72,4 @@ export var UObj;
72
72
  return o;
73
73
  }
74
74
  UObj.manipulateProperties = manipulateProperties;
75
- function array2record(keyOrValues, op) {
76
- return keyOrValues.reduce(!!op.kgen
77
- ? (o, korv) => { o[op.kgen(korv)] = korv; return o; }
78
- : (o, korv) => { o[korv] = op.vgen(korv); return o; }, {});
79
- }
80
- UObj.array2record = array2record;
81
75
  })(UObj || (UObj = {}));
@@ -5,8 +5,8 @@ export declare namespace UString {
5
5
  /**
6
6
  * generate date time number as fixed length (depends on `unit`) string without separator charactor.
7
7
  * For example, `2025-06-08T10:15:06.366Z` is to be `20250608101506366`.
8
- * @param op.date Date object refered by this. default is `new Date()`.
9
- * @param op.unit time unit. default is secound.
8
+ * @param op.date Date object referred by this. default is `new Date()`.
9
+ * @param op.unit time unit. default is second.
10
10
  */
11
11
  function simpleTime(op?: {
12
12
  date?: Date;
@@ -16,8 +16,8 @@ export var UString;
16
16
  /**
17
17
  * generate date time number as fixed length (depends on `unit`) string without separator charactor.
18
18
  * For example, `2025-06-08T10:15:06.366Z` is to be `20250608101506366`.
19
- * @param op.date Date object refered by this. default is `new Date()`.
20
- * @param op.unit time unit. default is secound.
19
+ * @param op.date Date object referred by this. default is `new Date()`.
20
+ * @param op.unit time unit. default is second.
21
21
  */
22
22
  function simpleTime(op) {
23
23
  const t = (op?.date ?? new Date()).toISOString().split(".")[0].replace(/[-T:]/g, "");
@@ -1,20 +1,7 @@
1
1
  import { Loggable, MaybePromise } from "../const/types";
2
- export declare function getJSTDate(d?: Date): Date;
2
+ export declare function getJSTDate(d?: Date | number): 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
- */
11
- export declare function array2map<K, T>(array: T[], keyGen: (e: T) => K): Map<K, T[]>;
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[]>;
18
5
  export interface RetryOption<T = MaybePromise> {
19
6
  /**
20
7
  * number of retries. default is 1.
@@ -37,7 +24,7 @@ export interface SyncRetryOption extends RetryOption<void> {
37
24
  }
38
25
  export interface AsyncRetryOption extends RetryOption {
39
26
  /**
40
- * secounds to wait between callbacks. this wait occurs after `intervalPredicate`.
27
+ * seconds to wait between callbacks. this wait occurs after `intervalPredicate`.
41
28
  */
42
29
  intervalSec?: number;
43
30
  }
@@ -1,7 +1,9 @@
1
1
  import { XjsErr } from "../obj/xjs-err";
2
+ import { UType } from "./u-type";
2
3
  const s_errCode = 10;
3
4
  export function getJSTDate(d) {
4
- return new Date((d ? d.getTime() : Date.now()) + 9 * 60 * 60 * 1000);
5
+ const adjuster = 9 * 60 * 60 * 1000;
6
+ return UType.isNumber(d) ? new Date(d + adjuster) : new Date((d ? d.getTime() : Date.now()) + adjuster);
5
7
  }
6
8
  export function delay(sec) {
7
9
  return new Promise(resolve => setTimeout(resolve, 1000 * sec));
@@ -12,18 +14,6 @@ export function int2array(size) {
12
14
  throw new XjsErr(s_errCode, "size of the argument is not number.");
13
15
  return Array.from(Array(s).keys());
14
16
  }
15
- export function array2map(array, keyGen, op) {
16
- const _acm = op?.accumulate ?? true;
17
- const map = new Map();
18
- for (const e of array) {
19
- const k = keyGen(e);
20
- if (map.has(k))
21
- _acm && map.get(k).push(e);
22
- else
23
- map.set(k, _acm ? [e] : e);
24
- }
25
- return map;
26
- }
27
17
  ;
28
18
  ;
29
19
  ;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xjs-common",
3
- "version": "13.4.0",
3
+ "version": "13.5.0",
4
4
  "description": "library modules for typescript that bundled general-purpose implementations.",
5
5
  "repository": {
6
6
  "type": "git",