@cclr/lang 0.0.3 → 0.0.6

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,143 @@
1
+ 'use strict';
2
+
3
+ const VAL_TYPE = {
4
+ boolean: "boolean",
5
+ undefined: "undefined",
6
+ number: "undefined",
7
+ string: "string",
8
+ null: "null",
9
+ "[object Object]": "object",
10
+ "[object Function]": "function",
11
+ "[object RegExp]": "regexp",
12
+ "[object Array]": "array",
13
+ "[object Date]": "date",
14
+ "[object Error]": "error",
15
+ "[object Blob]": "blob",
16
+ "[object File]": "file",
17
+ "[object ArrayBuffer]": "arrayBuffer",
18
+ };
19
+
20
+ /**
21
+ * 获取参数类型
22
+ * @param p 参数
23
+ * @returns
24
+ */
25
+ function getParamType(p) {
26
+ return (VAL_TYPE[typeof p] ||
27
+ VAL_TYPE[Object.prototype.toString.call(p)] ||
28
+ (p ? "object" : "null"));
29
+ }
30
+ function isType(params, type) {
31
+ return getParamType(params) === type;
32
+ }
33
+ function isString(s) {
34
+ return getParamType(s) === "string";
35
+ }
36
+ function isBoolean(b) {
37
+ return getParamType(b) === "boolean";
38
+ }
39
+ function isFunction(b) {
40
+ return getParamType(b) === "function";
41
+ }
42
+ function isPlainObject(b) {
43
+ return Object.prototype.toString.call(b) === "[object Object]";
44
+ }
45
+ function isObject(b) {
46
+ return getParamType(b) === "object";
47
+ }
48
+ function isArray(a) {
49
+ return Array.isArray(a);
50
+ }
51
+ function isNull(v) {
52
+ return v === null;
53
+ }
54
+ function isUndefined(v) {
55
+ return v === undefined;
56
+ }
57
+ function isUndefinedOrNull(v) {
58
+ return isNull(v) || isUndefined(v);
59
+ }
60
+
61
+ const enumToArray = (e) => {
62
+ if (isPlainObject(e)) {
63
+ return [Object.keys(e)];
64
+ }
65
+ return [];
66
+ };
67
+
68
+ /* eslint-disable @typescript-eslint/no-unsafe-return */
69
+ const loopObjectRead = (source, [head, ...tail]) => {
70
+ source = source[head];
71
+ return tail.length && source ? loopObjectRead(source, tail) : source;
72
+ };
73
+ /**
74
+ * Gets the value at path of object. TODO: typings.
75
+ * @param source The object to query.
76
+ * @param path The path of the property to get.
77
+ * @param [defaultValue] The value returned for undefined resolved values.
78
+ */
79
+ const get = (source, path, defaultValue) => {
80
+ const result = loopObjectRead(source || {}, path.split("."));
81
+ return isUndefined(result) || isNull(result) ? defaultValue : result;
82
+ };
83
+
84
+ const loopObjectSet = (source, [head, ...tail], value) => {
85
+ source = source[head] = tail.length ? source[head] || {} : value;
86
+ if (tail.length) {
87
+ if (isPlainObject(source) && !isArray(source)) {
88
+ loopObjectSet(source, tail, value);
89
+ }
90
+ else {
91
+ throw new Error(`path node ['.${head}'] must be plain object {}!`);
92
+ }
93
+ }
94
+ };
95
+ /**
96
+ * Sets the value at path of object. If a portion of path doesn't exist, it's created.
97
+ * @param source The object to modify.
98
+ * @param path The path of the property to set.
99
+ * @param value The value to set.
100
+ */
101
+ const set = (source, path, value) => {
102
+ source = source || {};
103
+ loopObjectSet(source, path.split("."), value);
104
+ return source;
105
+ };
106
+
107
+ /**
108
+ * A function that returns a universally unique identifier (uuid).
109
+ * Note: If used in long period data storage it is best to add a time stamp (e.g. logging)
110
+ * @example
111
+ * 1b83fd69-abe7-468c-bea1-306a8aa1c81d
112
+ * @returns `string` : 32 character uuid (see example)
113
+ */
114
+ const uuid = () => {
115
+ const hashTable = ['a', 'b', 'c', 'd', 'e', 'f', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
116
+ const uuid = [];
117
+ for (let i = 0; i < 36; i++) {
118
+ if (i === 8 || i === 13 || i === 18 || i === 23) {
119
+ uuid[i] = '-';
120
+ }
121
+ else {
122
+ uuid[i] = hashTable[Math.ceil(Math.random() * hashTable.length - 1)];
123
+ }
124
+ }
125
+ return uuid.join('');
126
+ };
127
+
128
+ exports.VAL_TYPE = VAL_TYPE;
129
+ exports.enumToArray = enumToArray;
130
+ exports.get = get;
131
+ exports.getParamType = getParamType;
132
+ exports.isArray = isArray;
133
+ exports.isBoolean = isBoolean;
134
+ exports.isFunction = isFunction;
135
+ exports.isNull = isNull;
136
+ exports.isObject = isObject;
137
+ exports.isPlainObject = isPlainObject;
138
+ exports.isString = isString;
139
+ exports.isType = isType;
140
+ exports.isUndefined = isUndefined;
141
+ exports.isUndefinedOrNull = isUndefinedOrNull;
142
+ exports.set = set;
143
+ exports.uuid = uuid;
@@ -0,0 +1,126 @@
1
+ const VAL_TYPE = {
2
+ boolean: "boolean",
3
+ undefined: "undefined",
4
+ number: "undefined",
5
+ string: "string",
6
+ null: "null",
7
+ "[object Object]": "object",
8
+ "[object Function]": "function",
9
+ "[object RegExp]": "regexp",
10
+ "[object Array]": "array",
11
+ "[object Date]": "date",
12
+ "[object Error]": "error",
13
+ "[object Blob]": "blob",
14
+ "[object File]": "file",
15
+ "[object ArrayBuffer]": "arrayBuffer",
16
+ };
17
+
18
+ /**
19
+ * 获取参数类型
20
+ * @param p 参数
21
+ * @returns
22
+ */
23
+ function getParamType(p) {
24
+ return (VAL_TYPE[typeof p] ||
25
+ VAL_TYPE[Object.prototype.toString.call(p)] ||
26
+ (p ? "object" : "null"));
27
+ }
28
+ function isType(params, type) {
29
+ return getParamType(params) === type;
30
+ }
31
+ function isString(s) {
32
+ return getParamType(s) === "string";
33
+ }
34
+ function isBoolean(b) {
35
+ return getParamType(b) === "boolean";
36
+ }
37
+ function isFunction(b) {
38
+ return getParamType(b) === "function";
39
+ }
40
+ function isPlainObject(b) {
41
+ return Object.prototype.toString.call(b) === "[object Object]";
42
+ }
43
+ function isObject(b) {
44
+ return getParamType(b) === "object";
45
+ }
46
+ function isArray(a) {
47
+ return Array.isArray(a);
48
+ }
49
+ function isNull(v) {
50
+ return v === null;
51
+ }
52
+ function isUndefined(v) {
53
+ return v === undefined;
54
+ }
55
+ function isUndefinedOrNull(v) {
56
+ return isNull(v) || isUndefined(v);
57
+ }
58
+
59
+ const enumToArray = (e) => {
60
+ if (isPlainObject(e)) {
61
+ return [Object.keys(e)];
62
+ }
63
+ return [];
64
+ };
65
+
66
+ /* eslint-disable @typescript-eslint/no-unsafe-return */
67
+ const loopObjectRead = (source, [head, ...tail]) => {
68
+ source = source[head];
69
+ return tail.length && source ? loopObjectRead(source, tail) : source;
70
+ };
71
+ /**
72
+ * Gets the value at path of object. TODO: typings.
73
+ * @param source The object to query.
74
+ * @param path The path of the property to get.
75
+ * @param [defaultValue] The value returned for undefined resolved values.
76
+ */
77
+ const get = (source, path, defaultValue) => {
78
+ const result = loopObjectRead(source || {}, path.split("."));
79
+ return isUndefined(result) || isNull(result) ? defaultValue : result;
80
+ };
81
+
82
+ const loopObjectSet = (source, [head, ...tail], value) => {
83
+ source = source[head] = tail.length ? source[head] || {} : value;
84
+ if (tail.length) {
85
+ if (isPlainObject(source) && !isArray(source)) {
86
+ loopObjectSet(source, tail, value);
87
+ }
88
+ else {
89
+ throw new Error(`path node ['.${head}'] must be plain object {}!`);
90
+ }
91
+ }
92
+ };
93
+ /**
94
+ * Sets the value at path of object. If a portion of path doesn't exist, it's created.
95
+ * @param source The object to modify.
96
+ * @param path The path of the property to set.
97
+ * @param value The value to set.
98
+ */
99
+ const set = (source, path, value) => {
100
+ source = source || {};
101
+ loopObjectSet(source, path.split("."), value);
102
+ return source;
103
+ };
104
+
105
+ /**
106
+ * A function that returns a universally unique identifier (uuid).
107
+ * Note: If used in long period data storage it is best to add a time stamp (e.g. logging)
108
+ * @example
109
+ * 1b83fd69-abe7-468c-bea1-306a8aa1c81d
110
+ * @returns `string` : 32 character uuid (see example)
111
+ */
112
+ const uuid = () => {
113
+ const hashTable = ['a', 'b', 'c', 'd', 'e', 'f', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
114
+ const uuid = [];
115
+ for (let i = 0; i < 36; i++) {
116
+ if (i === 8 || i === 13 || i === 18 || i === 23) {
117
+ uuid[i] = '-';
118
+ }
119
+ else {
120
+ uuid[i] = hashTable[Math.ceil(Math.random() * hashTable.length - 1)];
121
+ }
122
+ }
123
+ return uuid.join('');
124
+ };
125
+
126
+ export { VAL_TYPE, enumToArray, get, getParamType, isArray, isBoolean, isFunction, isNull, isObject, isPlainObject, isString, isType, isUndefined, isUndefinedOrNull, set, uuid };
@@ -0,0 +1,66 @@
1
+ declare const VAL_TYPE: {
2
+ readonly boolean: "boolean";
3
+ readonly undefined: "undefined";
4
+ readonly number: "undefined";
5
+ readonly string: "string";
6
+ readonly null: "null";
7
+ readonly "[object Object]": "object";
8
+ readonly "[object Function]": "function";
9
+ readonly "[object RegExp]": "regexp";
10
+ readonly "[object Array]": "array";
11
+ readonly "[object Date]": "date";
12
+ readonly "[object Error]": "error";
13
+ readonly "[object Blob]": "blob";
14
+ readonly "[object File]": "file";
15
+ readonly "[object ArrayBuffer]": "arrayBuffer";
16
+ };
17
+ type TVal = (typeof VAL_TYPE)[keyof typeof VAL_TYPE];
18
+
19
+ /**
20
+ * 获取参数类型
21
+ * @param p 参数
22
+ * @returns
23
+ */
24
+ declare function getParamType(p: unknown): TVal;
25
+ declare function isType(params: any, type: TVal): boolean;
26
+ declare function isString(s: any): s is String;
27
+ declare function isBoolean(b: any): b is Boolean;
28
+ declare function isFunction(b: any): b is Function;
29
+ declare function isPlainObject(b: any): b is Record<string, unknown>;
30
+ declare function isObject(b: any): b is Record<string, unknown>;
31
+ declare function isArray(a: any): a is any[];
32
+ declare function isNull(v: any): v is null;
33
+ declare function isUndefined(v: any): v is undefined;
34
+ declare function isUndefinedOrNull(v: any): v is undefined | null;
35
+
36
+ type TAny = any;
37
+ type TPlainObject = Record<string, TAny>;
38
+
39
+ declare const enumToArray: (e: TPlainObject) => string[][];
40
+
41
+ /**
42
+ * Gets the value at path of object. TODO: typings.
43
+ * @param source The object to query.
44
+ * @param path The path of the property to get.
45
+ * @param [defaultValue] The value returned for undefined resolved values.
46
+ */
47
+ declare const get: <T>(source: T, path: string, defaultValue?: any) => any;
48
+
49
+ /**
50
+ * Sets the value at path of object. If a portion of path doesn't exist, it's created.
51
+ * @param source The object to modify.
52
+ * @param path The path of the property to set.
53
+ * @param value The value to set.
54
+ */
55
+ declare const set: (source: object | null | undefined, path: string, value: any) => object;
56
+
57
+ /**
58
+ * A function that returns a universally unique identifier (uuid).
59
+ * Note: If used in long period data storage it is best to add a time stamp (e.g. logging)
60
+ * @example
61
+ * 1b83fd69-abe7-468c-bea1-306a8aa1c81d
62
+ * @returns `string` : 32 character uuid (see example)
63
+ */
64
+ declare const uuid: () => string;
65
+
66
+ export { type TAny, type TPlainObject, type TVal, VAL_TYPE, enumToArray, get, getParamType, isArray, isBoolean, isFunction, isNull, isObject, isPlainObject, isString, isType, isUndefined, isUndefinedOrNull, set, uuid };
package/package.json CHANGED
@@ -1,21 +1,19 @@
1
1
  {
2
2
  "name": "@cclr/lang",
3
- "version": "0.0.3",
4
- "description": "> TODO: description",
3
+ "version": "0.0.6",
4
+ "description": "js语言基础工具",
5
5
  "author": "cclr <18843152354@163.com>",
6
6
  "homepage": "",
7
7
  "license": "ISC",
8
- "main": "cjs/lang.js",
9
- "module": "esm/index.js",
10
- "types": "./index.d.ts",
8
+ "main": "lib/cjs/index.js",
9
+ "module": "lib/esm/index.js",
10
+ "types": "lib/type/index.d.ts",
11
11
  "directories": {
12
12
  "lib": "lib",
13
13
  "test": "__tests__"
14
14
  },
15
15
  "files": [
16
- "cjs",
17
- "esm",
18
- "index.d.ts",
16
+ "lib",
19
17
  "README.md"
20
18
  ],
21
19
  "publishConfig": {
@@ -25,5 +23,5 @@
25
23
  "scripts": {
26
24
  "test": "node ./__tests__/lang.test.js"
27
25
  },
28
- "gitHead": "3b2ad8f10a1334a9876d31e0bf4a16074da88269"
26
+ "gitHead": "e2a9c7f3d406d70510ed008f38fed383f4fa31c1"
29
27
  }
package/cjs/index.js DELETED
@@ -1,56 +0,0 @@
1
- 'use strict';
2
-
3
- const VAL_TYPE = {
4
- boolean: "boolean",
5
- undefined: "undefined",
6
- number: "undefined",
7
- string: "string",
8
- null: "null",
9
- "[object Object]": "object",
10
- "[object Function]": "function",
11
- "[object RegExp]": "regexp",
12
- "[object Array]": "array",
13
- "[object Date]": "date",
14
- "[object Error]": "error",
15
- "[object Blob]": "blob",
16
- "[object File]": "file",
17
- "[object ArrayBuffer]": "arrayBuffer",
18
- };
19
-
20
- /**
21
- * 获取参数类型
22
- * @param p 参数
23
- * @returns
24
- */
25
- function getParamType(p) {
26
- return (VAL_TYPE[typeof p] ||
27
- VAL_TYPE[Object.prototype.toString.call(p)] ||
28
- (p ? "object" : "null"));
29
- }
30
- function isType(params, type) {
31
- return getParamType(params) === type;
32
- }
33
- function isString(s) {
34
- return getParamType(s) === "string";
35
- }
36
- function isBoolean(b) {
37
- return getParamType(b) === "boolean";
38
- }
39
- function isFunction(b) {
40
- return getParamType(b) === "function";
41
- }
42
- function isPlainObject(b) {
43
- return getParamType(b) === "object";
44
- }
45
- function isArray(a) {
46
- return Array.isArray(a);
47
- }
48
-
49
- exports.VAL_TYPE = VAL_TYPE;
50
- exports.getParamType = getParamType;
51
- exports.isArray = isArray;
52
- exports.isBoolean = isBoolean;
53
- exports.isFunction = isFunction;
54
- exports.isPlainObject = isPlainObject;
55
- exports.isString = isString;
56
- exports.isType = isType;
@@ -1,2 +0,0 @@
1
- export type TAny = any;
2
- export type TPlainObject = Record<string, TAny>;
@@ -1,3 +0,0 @@
1
- export * from './type-asserts';
2
- export * from './common-type';
3
- export * from './value-type';
@@ -1,14 +0,0 @@
1
- import { TVal } from "./value-type";
2
- /**
3
- * 获取参数类型
4
- * @param p 参数
5
- * @returns
6
- */
7
- declare function getParamType(p: unknown): TVal;
8
- declare function isType(params: any, type: TVal): boolean;
9
- declare function isString(s: any): s is String;
10
- declare function isBoolean(b: any): b is Boolean;
11
- declare function isFunction(b: any): b is Function;
12
- declare function isPlainObject(b: any): b is Record<string, unknown>;
13
- declare function isArray(a: any): a is any[];
14
- export { getParamType, isType, isString, isBoolean, isFunction, isArray, isPlainObject, };
@@ -1,17 +0,0 @@
1
- export declare const VAL_TYPE: {
2
- readonly boolean: "boolean";
3
- readonly undefined: "undefined";
4
- readonly number: "undefined";
5
- readonly string: "string";
6
- readonly null: "null";
7
- readonly "[object Object]": "object";
8
- readonly "[object Function]": "function";
9
- readonly "[object RegExp]": "regexp";
10
- readonly "[object Array]": "array";
11
- readonly "[object Date]": "date";
12
- readonly "[object Error]": "error";
13
- readonly "[object Blob]": "blob";
14
- readonly "[object File]": "file";
15
- readonly "[object ArrayBuffer]": "arrayBuffer";
16
- };
17
- export type TVal = typeof VAL_TYPE[keyof typeof VAL_TYPE];
package/esm/index.js DELETED
@@ -1,47 +0,0 @@
1
- const VAL_TYPE = {
2
- boolean: "boolean",
3
- undefined: "undefined",
4
- number: "undefined",
5
- string: "string",
6
- null: "null",
7
- "[object Object]": "object",
8
- "[object Function]": "function",
9
- "[object RegExp]": "regexp",
10
- "[object Array]": "array",
11
- "[object Date]": "date",
12
- "[object Error]": "error",
13
- "[object Blob]": "blob",
14
- "[object File]": "file",
15
- "[object ArrayBuffer]": "arrayBuffer",
16
- };
17
-
18
- /**
19
- * 获取参数类型
20
- * @param p 参数
21
- * @returns
22
- */
23
- function getParamType(p) {
24
- return (VAL_TYPE[typeof p] ||
25
- VAL_TYPE[Object.prototype.toString.call(p)] ||
26
- (p ? "object" : "null"));
27
- }
28
- function isType(params, type) {
29
- return getParamType(params) === type;
30
- }
31
- function isString(s) {
32
- return getParamType(s) === "string";
33
- }
34
- function isBoolean(b) {
35
- return getParamType(b) === "boolean";
36
- }
37
- function isFunction(b) {
38
- return getParamType(b) === "function";
39
- }
40
- function isPlainObject(b) {
41
- return getParamType(b) === "object";
42
- }
43
- function isArray(a) {
44
- return Array.isArray(a);
45
- }
46
-
47
- export { VAL_TYPE, getParamType, isArray, isBoolean, isFunction, isPlainObject, isString, isType };
@@ -1,2 +0,0 @@
1
- export type TAny = any;
2
- export type TPlainObject = Record<string, TAny>;
@@ -1,3 +0,0 @@
1
- export * from './type-asserts';
2
- export * from './common-type';
3
- export * from './value-type';
@@ -1,14 +0,0 @@
1
- import { TVal } from "./value-type";
2
- /**
3
- * 获取参数类型
4
- * @param p 参数
5
- * @returns
6
- */
7
- declare function getParamType(p: unknown): TVal;
8
- declare function isType(params: any, type: TVal): boolean;
9
- declare function isString(s: any): s is String;
10
- declare function isBoolean(b: any): b is Boolean;
11
- declare function isFunction(b: any): b is Function;
12
- declare function isPlainObject(b: any): b is Record<string, unknown>;
13
- declare function isArray(a: any): a is any[];
14
- export { getParamType, isType, isString, isBoolean, isFunction, isArray, isPlainObject, };
@@ -1,17 +0,0 @@
1
- export declare const VAL_TYPE: {
2
- readonly boolean: "boolean";
3
- readonly undefined: "undefined";
4
- readonly number: "undefined";
5
- readonly string: "string";
6
- readonly null: "null";
7
- readonly "[object Object]": "object";
8
- readonly "[object Function]": "function";
9
- readonly "[object RegExp]": "regexp";
10
- readonly "[object Array]": "array";
11
- readonly "[object Date]": "date";
12
- readonly "[object Error]": "error";
13
- readonly "[object Blob]": "blob";
14
- readonly "[object File]": "file";
15
- readonly "[object ArrayBuffer]": "arrayBuffer";
16
- };
17
- export type TVal = typeof VAL_TYPE[keyof typeof VAL_TYPE];
package/index.d.ts DELETED
@@ -1 +0,0 @@
1
- export * from './esm/src';