xjs-common 7.0.0 → 8.0.1

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/README.md CHANGED
@@ -4,7 +4,7 @@ Available to `npm i xjs-common`.
4
4
  **NOTE**: The versions <= `v6.2.0` was unpublished. If you has been used these versions, please update to the version >= `v7.0.0`.
5
5
 
6
6
  # Overview
7
- Library modules for nodejs that bundled general-purpose implementations.
7
+ Library modules for nodejs + typescript that bundled general-purpose implementations.
8
8
  This module is very simple, therefore it has no dependencies.
9
9
 
10
10
  # Code example (only part)
@@ -181,28 +181,28 @@ class Cls_B {
181
181
  console.log(!!cropped.id && !cropped.p && !!cropped.objA.aryB && !cropped.objA.q) // true;
182
182
 
183
183
  // validation. below are valid cases.
184
- console.log(UType.validate(valid1)); // true
184
+ console.log(UType.validate(valid1)); // []
185
185
 
186
186
  const valid2 = { id: 0 };
187
- console.log(UType.validate(Object.assign(new Cls_A(), valid2))); // true
187
+ console.log(UType.validate(Object.assign(new Cls_A(), valid2))); // []
188
188
 
189
189
  // validation. below are invalid cases.
190
190
  const invalid1 = {};
191
- console.log(UType.validate(Object.assign(new Cls_A(), invalid1))); // false
191
+ console.log(UType.validate(Object.assign(new Cls_A(), invalid1))); // [ 'id' ]
192
192
 
193
193
  const invalid3 = { id: 0, strA: [], objA: valid_b1 };
194
- console.log(UType.validate(Object.assign(new Cls_A(), invalid3))); // false
194
+ console.log(UType.validate(Object.assign(new Cls_A(), invalid3))); // [ 'strA' ]
195
195
 
196
196
  const invalid4 = { id: "0", strA: "a", objA: valid_b1 };
197
- console.log(UType.validate(Object.assign(new Cls_A(), invalid4))); // false
197
+ console.log(UType.validate(Object.assign(new Cls_A(), invalid4))); // [ 'id' ]
198
198
 
199
199
  const invalid_b1 = Object.assign(new Cls_B(), { aryB: [1, 2, 3], boolB: 1 });
200
200
  const invalid5 = { id: 0, strA: "a", objA: invalid_b1 };
201
- console.log(UType.validate(Object.assign(new Cls_A(), invalid5))); // false
201
+ console.log(UType.validate(Object.assign(new Cls_A(), invalid5))); // [ 'objA.boolB' ]
202
202
 
203
203
  const invalid_b2 = Object.assign(new Cls_B(), { aryB: ["1"], boolB: true });
204
204
  const invalid6 = { id: 0, strA: "a", objA: invalid_b2 };
205
- console.log(UType.validate(Object.assign(new Cls_A(), invalid6))); // false
205
+ console.log(UType.validate(Object.assign(new Cls_A(), invalid6))); // [ 'objA.aryB' ]
206
206
  })();
207
207
  ```
208
208
  ## Error definition
@@ -1,5 +1,6 @@
1
1
  export type IndexSignature = string | number | symbol;
2
2
  export type NormalRecord = Record<IndexSignature, any>;
3
+ export type MaybeArray<T> = T | T[];
3
4
  export declare enum Type {
4
5
  string = "string",
5
6
  number = "number",
@@ -47,7 +47,7 @@ var DType;
47
47
  }
48
48
  DType.keep = keep;
49
49
  function setDesc(target, propKey, setter) {
50
- const map = target[exports.smbl_tm] ?? {};
50
+ const map = target[exports.smbl_tm] ? Object.assign({}, target[exports.smbl_tm]) : {};
51
51
  map[propKey] ??= { t: null, req: false, rec: false, ary: null };
52
52
  const td = map[propKey];
53
53
  setter(td);
@@ -66,6 +66,6 @@ var DType;
66
66
  }
67
67
  if (ex1 && ex2)
68
68
  throw new xjs_err_1.XjsErr(s_errCode, `decorator to express ${ex1} and ${ex2} are exclusive.`);
69
- Object.defineProperty(target, exports.smbl_tm, { value: map });
69
+ Object.defineProperty(target, exports.smbl_tm, { value: map, configurable: true });
70
70
  }
71
71
  })(DType = exports.DType || (exports.DType = {}));
@@ -1,10 +1,11 @@
1
1
  /// <reference types="node" />
2
+ import { MaybeArray } from "../const/types";
2
3
  export declare namespace UFile {
3
- function mkdir(p: string | string[]): boolean;
4
- function write(p: string | string[], c: string): void;
4
+ function mkdir(p: MaybeArray<string>): boolean;
5
+ function write(p: MaybeArray<string>, c: string): void;
5
6
  function exists(...p: string[]): boolean;
6
- function read(p: string | string[]): Buffer;
7
- function read(p: string | string[], encoding: BufferEncoding): string;
8
- function cp(from: string | string[], to: string | string[]): void;
9
- function mv(from: string | string[], to: string | string[]): void;
7
+ function read(p: MaybeArray<string>): Buffer;
8
+ function read(p: MaybeArray<string>, encoding: BufferEncoding): string;
9
+ function cp(from: MaybeArray<string>, to: MaybeArray<string>): void;
10
+ function mv(from: MaybeArray<string>, to: MaybeArray<string>): void;
10
11
  }
@@ -1,4 +1,4 @@
1
- import { Type } from "../const/types";
1
+ import { MaybeArray, Type } from "../const/types";
2
2
  export declare namespace UType {
3
3
  function isDefined(v: any): boolean;
4
4
  function isEmpty(v: any): boolean;
@@ -15,7 +15,11 @@ export declare namespace UType {
15
15
  function isArray(v: any, t: Type.symbol): v is symbol[];
16
16
  function isArray(v: any, t: Type.object): v is object[];
17
17
  function isArray(v: any): v is any[];
18
- /** validate properties which attached decorators in {@link DType} */
19
- function validate(o: any): boolean;
20
- function takeAsArray<T>(v: T | T[]): T[];
18
+ /**
19
+ * validate properties which attached decorators in {@link DType}.
20
+ * @param o object to be validated.
21
+ * @returns invalid property keys. returns an empty array if `o` is valid.
22
+ */
23
+ function validate(o: any): string[];
24
+ function takeAsArray<T>(v: MaybeArray<T>): T[];
21
25
  }
@@ -29,22 +29,27 @@ var UType;
29
29
  return Array.isArray(v) && (!t || v.every(e => typeof e === t));
30
30
  }
31
31
  UType.isArray = isArray;
32
- /** validate properties which attached decorators in {@link DType} */
32
+ /**
33
+ * validate properties which attached decorators in {@link DType}.
34
+ * @param o object to be validated.
35
+ * @returns invalid property keys. returns an empty array if `o` is valid.
36
+ */
33
37
  function validate(o) {
34
- return !o[d_type_1.smbl_tm] || Object.entries(o[d_type_1.smbl_tm])
35
- .every(e => validateProp(o[e[0]], e[1]));
38
+ if (!o[d_type_1.smbl_tm])
39
+ return [];
40
+ return Object.entries(o[d_type_1.smbl_tm]).flatMap(e => validateProp(e[0], o[e[0]], e[1]));
36
41
  }
37
42
  UType.validate = validate;
38
- function validateProp(prop, td) {
43
+ function validateProp(k, prop, td) {
39
44
  if (isEmpty(prop))
40
- return !td.req;
45
+ return td.req ? [k] : [];
41
46
  if (td.t && typeof prop !== td.t)
42
- return false;
47
+ return [k];
43
48
  if (td.ary)
44
- return Array.isArray(prop) && (!td.ary || prop.every(e => validateProp(e, td.ary)));
49
+ return Array.isArray(prop) ? prop.flatMap(e => validateProp(k, e, td.ary)) : [k];
45
50
  if (td.rec)
46
- return validate(prop);
47
- return true;
51
+ return validate(prop).flatMap(k2 => `${k}.${k2}`);
52
+ return [];
48
53
  }
49
54
  function takeAsArray(v) {
50
55
  return Array.isArray(v) ? v : [v];
package/package.json CHANGED
@@ -1,13 +1,14 @@
1
1
  {
2
2
  "name": "xjs-common",
3
- "version": "7.0.0",
4
- "description": "library modules for nodejs that bundled general-purpose implementations.",
3
+ "version": "8.0.1",
4
+ "description": "library modules for nodejs + typescript that bundled general-purpose implementations.",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "https://github.com/begyyal/xjs_commons"
8
8
  },
9
9
  "keywords": [
10
10
  "node",
11
+ "typescript",
11
12
  "utility"
12
13
  ],
13
14
  "scripts": {