@aitianyu.cn/types 0.0.1 → 0.0.2

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/dist/lib/index.js CHANGED
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  /**@format */
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.hash = exports.guid = exports.Performance = exports.Log = exports.parseAreaString = exports.parseAreaCode = exports.ArgumentNullOrEmptyException = exports.EncryptOption = exports.LogLevel = exports.Exception = exports.AreaCode = void 0;
4
+ exports.hash = exports.guid = exports.ObjectHelper = exports.Performance = exports.Log = exports.parseAreaString = exports.parseAreaCode = exports.ObjectCloneFunctionNotSupportException = exports.ArgumentNullOrEmptyException = exports.PathProcessor = exports.PathDirectoryValidationFailException = exports.PathProcessorSourceLostException = exports.EncryptOption = exports.LogLevel = exports.Exception = exports.AreaCode = void 0;
5
+ // src/types
5
6
  var AreaCode_1 = require("./types/AreaCode");
6
7
  Object.defineProperty(exports, "AreaCode", { enumerable: true, get: function () { return AreaCode_1.AreaCode; } });
7
8
  var Exception_1 = require("./types/Exception");
@@ -10,14 +11,26 @@ var Logs_1 = require("./types/Logs");
10
11
  Object.defineProperty(exports, "LogLevel", { enumerable: true, get: function () { return Logs_1.LogLevel; } });
11
12
  var Security_1 = require("./types/Security");
12
13
  Object.defineProperty(exports, "EncryptOption", { enumerable: true, get: function () { return Security_1.EncryptOption; } });
14
+ // utilities
15
+ // coding
16
+ var Error_1 = require("./utilities/coding/Error");
17
+ Object.defineProperty(exports, "PathProcessorSourceLostException", { enumerable: true, get: function () { return Error_1.PathProcessorSourceLostException; } });
18
+ Object.defineProperty(exports, "PathDirectoryValidationFailException", { enumerable: true, get: function () { return Error_1.PathDirectoryValidationFailException; } });
19
+ var PathProcessor_1 = require("./utilities/coding/PathProcessor");
20
+ Object.defineProperty(exports, "PathProcessor", { enumerable: true, get: function () { return PathProcessor_1.PathProcessor; } });
21
+ // core
13
22
  var Errors_1 = require("./utilities/core/Errors");
14
23
  Object.defineProperty(exports, "ArgumentNullOrEmptyException", { enumerable: true, get: function () { return Errors_1.ArgumentNullOrEmptyException; } });
24
+ Object.defineProperty(exports, "ObjectCloneFunctionNotSupportException", { enumerable: true, get: function () { return Errors_1.ObjectCloneFunctionNotSupportException; } });
15
25
  var Language_1 = require("./utilities/core/Language");
16
26
  Object.defineProperty(exports, "parseAreaCode", { enumerable: true, get: function () { return Language_1.parseAreaCode; } });
17
27
  Object.defineProperty(exports, "parseAreaString", { enumerable: true, get: function () { return Language_1.parseAreaString; } });
18
28
  var Log_1 = require("./utilities/core/Log");
19
29
  Object.defineProperty(exports, "Log", { enumerable: true, get: function () { return Log_1.Log; } });
20
30
  Object.defineProperty(exports, "Performance", { enumerable: true, get: function () { return Log_1.Performance; } });
31
+ var ObjectHelper_1 = require("./utilities/core/ObjectHelper");
32
+ Object.defineProperty(exports, "ObjectHelper", { enumerable: true, get: function () { return ObjectHelper_1.ObjectHelper; } });
33
+ // security
21
34
  var Guid_1 = require("./utilities/security/Guid");
22
35
  Object.defineProperty(exports, "guid", { enumerable: true, get: function () { return Guid_1.guid; } });
23
36
  var Hash_1 = require("./utilities/security/Hash");
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ /**@format */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ /**@format */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.PathDirectoryValidationFailException = exports.PathProcessorSourceLostException = void 0;
5
+ const Exception_1 = require("../../types/Exception");
6
+ class PathProcessorSourceLostException extends Exception_1.Exception {
7
+ constructor(msg) {
8
+ super(msg);
9
+ }
10
+ toString() {
11
+ return `路径处理器 - 指定的参数 ( path.file ) 为空`;
12
+ }
13
+ }
14
+ exports.PathProcessorSourceLostException = PathProcessorSourceLostException;
15
+ class PathDirectoryValidationFailException extends Exception_1.Exception {
16
+ constructor(msg) {
17
+ super(msg);
18
+ }
19
+ toString() {
20
+ return `路径处理器 - 指定的路径目录 ( ${this.message} ) 无效`;
21
+ }
22
+ }
23
+ exports.PathDirectoryValidationFailException = PathDirectoryValidationFailException;
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+ /**@format */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.PathProcessor = void 0;
5
+ const Error_1 = require("./Error");
6
+ const __charInDirValidation = {
7
+ "/": { file: false, dir: false },
8
+ ".": { file: true, dir: false },
9
+ };
10
+ function _dirNameChecker(dir) {
11
+ if (!dir)
12
+ return "drop";
13
+ for (const ch of dir) {
14
+ const validation = __charInDirValidation[ch];
15
+ if (validation && !validation.dir) {
16
+ return "invalid";
17
+ }
18
+ }
19
+ return "valid";
20
+ }
21
+ function _dirFileTypeChecker(src) {
22
+ if (!src)
23
+ return "drop";
24
+ let dirType = false;
25
+ let fileType = false;
26
+ for (const char of src) {
27
+ const validation = __charInDirValidation[char];
28
+ if (dirType) {
29
+ if (validation && !validation.dir)
30
+ return "invalid";
31
+ }
32
+ else if (fileType) {
33
+ if (validation && !validation.file)
34
+ return "invalid";
35
+ }
36
+ else {
37
+ if (validation) {
38
+ if (validation.dir && !validation.file)
39
+ dirType = true;
40
+ else if (!validation.dir && validation.file)
41
+ fileType = true;
42
+ else if (!validation.dir && !validation.file)
43
+ return "invalid";
44
+ }
45
+ }
46
+ }
47
+ if ((!dirType && !fileType) || dirType)
48
+ return "directory";
49
+ else
50
+ return "file";
51
+ }
52
+ class PathProcessor {
53
+ static parsePathFromString(src) {
54
+ const path = { dirs: [], type: "directory" };
55
+ if (!src) {
56
+ return path;
57
+ }
58
+ const srcSplit = src.split("/");
59
+ if (srcSplit.length === 0) {
60
+ return path;
61
+ }
62
+ for (let i = 0; i < srcSplit.length - 1; ++i) {
63
+ const dir = srcSplit[i];
64
+ const validation = _dirNameChecker(dir);
65
+ if (validation === "invalid") {
66
+ throw new Error_1.PathDirectoryValidationFailException(dir);
67
+ }
68
+ if (validation === "valid") {
69
+ path.dirs.push(dir);
70
+ }
71
+ }
72
+ const lastDir = srcSplit[srcSplit.length - 1];
73
+ const dirType = _dirFileTypeChecker(lastDir);
74
+ if (dirType === "directory") {
75
+ path.dirs.push(lastDir);
76
+ }
77
+ else if (dirType === "file") {
78
+ path.file = lastDir;
79
+ path.type = "file";
80
+ }
81
+ return path;
82
+ }
83
+ static formatPathToString(path) {
84
+ if (path.type === "file" && !!!path.file) {
85
+ throw new Error_1.PathProcessorSourceLostException();
86
+ }
87
+ if (path.dirs.length === 0 && !!!path.file) {
88
+ return "";
89
+ }
90
+ const dirs = [];
91
+ for (const dirItem of path.dirs) {
92
+ const validation = _dirNameChecker(dirItem);
93
+ if (validation === "invalid") {
94
+ throw new Error_1.PathDirectoryValidationFailException(dirItem);
95
+ }
96
+ if (validation === "valid") {
97
+ dirs.push(dirItem);
98
+ }
99
+ }
100
+ if (!!path.file)
101
+ dirs.push(path.file);
102
+ if (dirs.length === 0) {
103
+ return "";
104
+ }
105
+ return dirs.join("/");
106
+ }
107
+ }
108
+ exports.PathProcessor = PathProcessor;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  /**@format */
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.ArgumentNullOrEmptyException = void 0;
4
+ exports.ObjectCloneFunctionNotSupportException = exports.ArgumentNullOrEmptyException = void 0;
5
5
  const Exception_1 = require("../../types/Exception");
6
6
  class ArgumentNullOrEmptyException extends Exception_1.Exception {
7
7
  constructor(msg) {
@@ -12,3 +12,12 @@ class ArgumentNullOrEmptyException extends Exception_1.Exception {
12
12
  }
13
13
  }
14
14
  exports.ArgumentNullOrEmptyException = ArgumentNullOrEmptyException;
15
+ class ObjectCloneFunctionNotSupportException extends Exception_1.Exception {
16
+ constructor() {
17
+ super();
18
+ }
19
+ toString() {
20
+ return `不支持的类型 - 正在克隆为Function类型不支持`;
21
+ }
22
+ }
23
+ exports.ObjectCloneFunctionNotSupportException = ObjectCloneFunctionNotSupportException;
@@ -0,0 +1,201 @@
1
+ "use strict";
2
+ /**@format */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.ObjectHelper = void 0;
5
+ const Errors_1 = require("./Errors");
6
+ /** Object Helper of Tianyu to provide data type checking, objects comparing, validation, data clone and other functions */
7
+ class ObjectHelper {
8
+ /**
9
+ * check whether the specified value does extend from nodejs native type
10
+ *
11
+ * @param {any} value supported value type of tianyu store
12
+ * @returns {boolean} return true if the value is based on type number, string, boolean, function, otherwise false
13
+ */
14
+ static isSimpleDataType(value) {
15
+ if (!!!value)
16
+ return true;
17
+ const typeofValue = typeof value;
18
+ return typeofValue === "boolean" || typeofValue === "string" || typeofValue === "number" || typeofValue === "symbol";
19
+ }
20
+ /**
21
+ * get a deep copy from source
22
+ *
23
+ * @param {any} sources the specified source value
24
+ * @returns {any} return a new value that is independent from sources
25
+ */
26
+ static clone(source) {
27
+ return ObjectHelper._clone(source);
28
+ }
29
+ static _clone(source) {
30
+ if (typeof source === "function") {
31
+ throw new Errors_1.ObjectCloneFunctionNotSupportException();
32
+ }
33
+ // check if source is simple data type, return directly
34
+ if (ObjectHelper.isSimpleDataType(source)) {
35
+ return source;
36
+ }
37
+ if (Array.isArray(source)) {
38
+ // if is array type
39
+ return ObjectHelper._cloneArray(source);
40
+ }
41
+ // otherwise, source value is a record type
42
+ return ObjectHelper._cloneRecordType(source);
43
+ }
44
+ static _cloneArray(source) {
45
+ // create a new array to save result
46
+ const result = [];
47
+ if (0 === source.length) {
48
+ // if source value is empty array, return directly
49
+ return result;
50
+ }
51
+ // loop all the item in the source value and to clone the item
52
+ for (const item of source) {
53
+ result.push(ObjectHelper._clone(item));
54
+ }
55
+ return result;
56
+ }
57
+ static _cloneRecordType(source) {
58
+ // get all the record key from source object
59
+ const keys = Object.keys(source);
60
+ if (0 === keys.length) {
61
+ // if the object is empty, return an empty object
62
+ return {};
63
+ }
64
+ const result = {};
65
+ // loop all the items in the source object by the key name
66
+ for (const keyName of keys) {
67
+ const sourceItem = source[keyName];
68
+ result[keyName] = ObjectHelper._clone(sourceItem);
69
+ }
70
+ return result;
71
+ }
72
+ /**
73
+ * Check the object whether can be stringified
74
+ *
75
+ * @param obj be checked object
76
+ * @returns return true is valid, otherwise false
77
+ */
78
+ static validateSerializable(obj) {
79
+ // if type is function, is not invaliable to be string
80
+ if (typeof obj === "function") {
81
+ return false;
82
+ }
83
+ // if is simple data type, is valiable
84
+ if (ObjectHelper.isSimpleDataType(obj)) {
85
+ return true;
86
+ }
87
+ // if is array, check member
88
+ if (Array.isArray(obj)) {
89
+ for (const item of obj) {
90
+ // contains invalid member, return false
91
+ if (!ObjectHelper.validateSerializable(item)) {
92
+ return false;
93
+ }
94
+ }
95
+ return true;
96
+ }
97
+ try {
98
+ // if is object, check member
99
+ const keys = Object.keys(obj);
100
+ for (const keyName of keys) {
101
+ const item = obj[keyName];
102
+ // contains invalid member, return false
103
+ if (!ObjectHelper.validateSerializable(item)) {
104
+ return false;
105
+ }
106
+ }
107
+ return true;
108
+ }
109
+ catch {
110
+ // if the object is not an object type, return false
111
+ return false;
112
+ }
113
+ }
114
+ /**
115
+ * Compare two or more objects are the same or different
116
+ *
117
+ * @param objs the objects which need to be compared
118
+ * @returns same - if all the objects are same, different - if at least one object is not same to other objects
119
+ * @description if there are only one parameter or no parameter is provided, same result will be returned
120
+ */
121
+ static compareObjects(...objs) {
122
+ // if objs less than 2, is not comparable
123
+ if (objs.length < 2) {
124
+ return "same";
125
+ }
126
+ // if the first obj is simple data type, return different if other objects is not simple obj
127
+ // or the type is not totally same.
128
+ if (ObjectHelper.isSimpleDataType(objs[0])) {
129
+ return ObjectHelper._compareSimpleType(...objs);
130
+ }
131
+ // if the first obj is array type, check all the objects are array and to do the detail checking
132
+ if (Array.isArray(objs[0])) {
133
+ return ObjectHelper._compareArrayType(...objs);
134
+ }
135
+ return ObjectHelper._compareObjectType(...objs);
136
+ }
137
+ static _compareSimpleType(...objs) {
138
+ const firstObjType = typeof objs[0];
139
+ for (let i = 1; i < objs.length; ++i) {
140
+ if (firstObjType !== typeof objs[i] || objs[0] !== objs[i]) {
141
+ return "different";
142
+ }
143
+ }
144
+ return "same";
145
+ }
146
+ static _compareArrayType(...objs) {
147
+ for (let i = 1; i < objs.length; ++i) {
148
+ // check the type and array length if the obj is array type
149
+ if (!Array.isArray(objs[i]) || objs[i].length !== objs[0].length) {
150
+ return "different";
151
+ }
152
+ }
153
+ // loop each item of the array to have a detail checking
154
+ for (let j = 0; j < objs[0].length; ++j) {
155
+ // create a checking array
156
+ const items = [objs[0][j]];
157
+ for (let i = 1; i < objs.length; ++i) {
158
+ // add all the items of the each object which have the same index
159
+ items.push(objs[i][j]);
160
+ }
161
+ const cmpRes = ObjectHelper.compareObjects(...items);
162
+ if (cmpRes !== "same") {
163
+ return cmpRes;
164
+ }
165
+ }
166
+ return "same";
167
+ }
168
+ static _compareObjectType(...objs) {
169
+ try {
170
+ // other cases, the first obj is not simple obj and array, take checking as object
171
+ // start add all the objects keys
172
+ const keysOfObjs = [];
173
+ keysOfObjs.push(Object.keys(objs[0]));
174
+ // firstly, check the types of other objects
175
+ for (let i = 1; i < objs.length; ++i) {
176
+ keysOfObjs.push(Object.keys(objs[i]));
177
+ }
178
+ // check the keys are same or different
179
+ if (ObjectHelper.compareObjects(...keysOfObjs) !== "same") {
180
+ return "different";
181
+ }
182
+ // compare all the items
183
+ for (const key of keysOfObjs[0]) {
184
+ const items = [];
185
+ for (const obj of objs) {
186
+ items.push(obj[key]);
187
+ }
188
+ // get the items compare result
189
+ const cmpRes = ObjectHelper.compareObjects(...items);
190
+ if (cmpRes !== "same") {
191
+ return cmpRes;
192
+ }
193
+ }
194
+ return "same";
195
+ }
196
+ catch {
197
+ return "different";
198
+ }
199
+ }
200
+ }
201
+ exports.ObjectHelper = ObjectHelper;
@@ -1,11 +1,15 @@
1
1
  /**@format */
2
2
  export { AreaCode } from "./types/AreaCode";
3
+ export { type PathTargetType, type IPath } from "./types/Coding";
3
4
  export { Exception } from "./types/Exception";
4
5
  export { LogLevel, type ILog, type IPerfRecorder } from "./types/Logs";
5
6
  export { EncryptOption, type ICipher } from "./types/Security";
6
7
  export { type MapOfBoolean, type MapOfStrings, type MapOfString, type MapOfType, type CallbackAction, type CallbackActionT, } from "./types/Types";
7
- export { ArgumentNullOrEmptyException } from "./utilities/core/Errors";
8
+ export { PathProcessorSourceLostException, PathDirectoryValidationFailException } from "./utilities/coding/Error";
9
+ export { PathProcessor } from "./utilities/coding/PathProcessor";
10
+ export { ArgumentNullOrEmptyException, ObjectCloneFunctionNotSupportException } from "./utilities/core/Errors";
8
11
  export { parseAreaCode, parseAreaString } from "./utilities/core/Language";
9
12
  export { Log, Performance } from "./utilities/core/Log";
13
+ export { ObjectHelper } from "./utilities/core/ObjectHelper";
10
14
  export { guid } from "./utilities/security/Guid";
11
15
  export { hash } from "./utilities/security/Hash";
@@ -0,0 +1,7 @@
1
+ /**@format */
2
+ export type PathTargetType = "directory" | "file";
3
+ export interface IPath {
4
+ type: PathTargetType;
5
+ dirs: string[];
6
+ file?: string;
7
+ }
@@ -0,0 +1,10 @@
1
+ /**@format */
2
+ import { Exception } from "../../types/Exception";
3
+ export declare class PathProcessorSourceLostException extends Exception {
4
+ constructor(msg?: string);
5
+ toString(): string;
6
+ }
7
+ export declare class PathDirectoryValidationFailException extends Exception {
8
+ constructor(msg?: string);
9
+ toString(): string;
10
+ }
@@ -0,0 +1,6 @@
1
+ /**@format */
2
+ import { IPath } from "../../types/Coding";
3
+ export declare class PathProcessor {
4
+ static parsePathFromString(src: string): IPath;
5
+ static formatPathToString(path: IPath): string;
6
+ }
@@ -4,3 +4,7 @@ export declare class ArgumentNullOrEmptyException extends Exception {
4
4
  constructor(msg: string);
5
5
  toString(): string;
6
6
  }
7
+ export declare class ObjectCloneFunctionNotSupportException extends Exception {
8
+ constructor();
9
+ toString(): string;
10
+ }
@@ -0,0 +1,39 @@
1
+ /**@format */
2
+ /** Object Helper of Tianyu to provide data type checking, objects comparing, validation, data clone and other functions */
3
+ export declare class ObjectHelper {
4
+ /**
5
+ * check whether the specified value does extend from nodejs native type
6
+ *
7
+ * @param {any} value supported value type of tianyu store
8
+ * @returns {boolean} return true if the value is based on type number, string, boolean, function, otherwise false
9
+ */
10
+ static isSimpleDataType(value: any): boolean;
11
+ /**
12
+ * get a deep copy from source
13
+ *
14
+ * @param {any} sources the specified source value
15
+ * @returns {any} return a new value that is independent from sources
16
+ */
17
+ static clone(source: any): any;
18
+ private static _clone;
19
+ private static _cloneArray;
20
+ private static _cloneRecordType;
21
+ /**
22
+ * Check the object whether can be stringified
23
+ *
24
+ * @param obj be checked object
25
+ * @returns return true is valid, otherwise false
26
+ */
27
+ static validateSerializable(obj: any): boolean;
28
+ /**
29
+ * Compare two or more objects are the same or different
30
+ *
31
+ * @param objs the objects which need to be compared
32
+ * @returns same - if all the objects are same, different - if at least one object is not same to other objects
33
+ * @description if there are only one parameter or no parameter is provided, same result will be returned
34
+ */
35
+ static compareObjects(...objs: any): "same" | "different";
36
+ private static _compareSimpleType;
37
+ private static _compareArrayType;
38
+ private static _compareObjectType;
39
+ }
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@aitianyu.cn/types",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "private": false,
5
- "description": "Basic components for aitianyu nodejs server",
5
+ "description": "Common modules (types, functions, classes) for aitianyu",
6
6
  "main": "./dist/lib/index.js",
7
7
  "types": "./dist/types/index.d.ts",
8
8
  "exports": {
@@ -12,6 +12,11 @@
12
12
  "dist"
13
13
  ],
14
14
  "type": "commonjs",
15
+ "keywords": [
16
+ "aitianyu.cn",
17
+ "base type",
18
+ "common lib"
19
+ ],
15
20
  "scripts": {
16
21
  "start": "ts-node packages/index.ts",
17
22
  "build": "npm run before-build && npm run compiler && npm run copy-lib && npm run copy-types && npm run after-build",