@cclr/lang 0.0.6 → 0.0.9

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/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ MIT License
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
package/lib/cjs/index.js CHANGED
@@ -1,143 +1 @@
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;
1
+ "use strict";const e={boolean:"boolean",undefined:"undefined",number:"undefined",string:"string",symbol:"symbol",null:"null","[object Object]":"object","[object Function]":"function","[object RegExp]":"regexp","[object Array]":"array","[object Date]":"date","[object Error]":"error","[object Blob]":"blob","[object File]":"file","[object ArrayBuffer]":"arrayBuffer"};function t(t){return e[typeof t]||e[Object.prototype.toString.call(t)]||(t?"object":"null")}function r(e){return null===e}function o(e){return void 0===e}function n(e){return"object"==typeof e&&null!==e&&"[object Object]"===Object.prototype.toString.call(e)}function c(e){return"object"===t(e)}function i(e){return Array.isArray(e)}const s=(e,[t,...r])=>(e=e[t],r.length&&e?s(e,r):e),u=(e,[t,...r],o)=>{if(e=e[t]=r.length?e[t]||{}:o,r.length){if(!n(e)||i(e))throw new Error(`path node ['.${t}'] must be plain object {}!`);u(e,r,o)}};var l=Object.freeze({__proto__:null,extend:(e,t)=>{c(e)&&Object.keys(t).forEach((r=>{e[r]=t[r]}))},filter:(e,t)=>{const r={};return n(e)&&Object.keys(e).forEach((o=>{const n=e[o];t(e[o],o)&&(r[o]=n)})),r},forEach:(e,t)=>{Object.keys(e).forEach((r=>{t(e[r],r)}))},pick:(e,t)=>t.reduce(((t,r)=>(r in e&&(t[r]=e[r]),t)),{})});exports.VAL_TYPE=e,exports.enumToArray=e=>n(e)?[Object.keys(e)]:[],exports.get=(e,t,n)=>{const c=s(e||{},t.split("."));return o(c)||r(c)?n:c},exports.getParamType=t,exports.isArray=i,exports.isBoolean=function(e){return"boolean"===t(e)},exports.isFunction=function(e){return"function"===t(e)},exports.isNull=r,exports.isObject=c,exports.isPlainObject=n,exports.isString=function(e){return"string"===t(e)},exports.isSymbol=function(e){return"symbol"===t(e)},exports.isType=function(e,r){return t(e)===r},exports.isUndefined=o,exports.isUndefinedOrNull=function(e){return r(e)||o(e)},exports.obj=l,exports.set=(e,t,r)=>(u(e=e||{},t.split("."),r),e),exports.uuid=()=>{const e=["a","b","c","d","e","f","0","1","2","3","4","5","6","7","8","9"],t=[];for(let r=0;r<36;r++)t[r]=8===r||13===r||18===r||23===r?"-":e[Math.ceil(Math.random()*e.length-1)];return t.join("")};
package/lib/esm/index.js CHANGED
@@ -1,126 +1 @@
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 };
1
+ const t={boolean:"boolean",undefined:"undefined",number:"undefined",string:"string",symbol:"symbol",null:"null","[object Object]":"object","[object Function]":"function","[object RegExp]":"regexp","[object Array]":"array","[object Date]":"date","[object Error]":"error","[object Blob]":"blob","[object File]":"file","[object ArrayBuffer]":"arrayBuffer"};function e(e){return t[typeof e]||t[Object.prototype.toString.call(e)]||(e?"object":"null")}function n(t,n){return e(t)===n}function r(t){return"string"===e(t)}function o(t){return"boolean"===e(t)}function c(t){return"symbol"===e(t)}function u(t){return null===t}function i(t){return void 0===t}function b(t){return u(t)||i(t)}function l(t){return"function"===e(t)}function f(t){return"object"==typeof t&&null!==t&&"[object Object]"===Object.prototype.toString.call(t)}function a(t){return"object"===e(t)}function j(t){return Array.isArray(t)}const s=t=>f(t)?[Object.keys(t)]:[],y=(t,[e,...n])=>(t=t[e],n.length&&t?y(t,n):t),p=(t,e,n)=>{const r=y(t||{},e.split("."));return i(r)||u(r)?n:r},d=(t,[e,...n],r)=>{if(t=t[e]=n.length?t[e]||{}:r,n.length){if(!f(t)||j(t))throw new Error(`path node ['.${e}'] must be plain object {}!`);d(t,n,r)}},h=(t,e,n)=>(d(t=t||{},e.split("."),n),t),g=()=>{const t=["a","b","c","d","e","f","0","1","2","3","4","5","6","7","8","9"],e=[];for(let n=0;n<36;n++)e[n]=8===n||13===n||18===n||23===n?"-":t[Math.ceil(Math.random()*t.length-1)];return e.join("")};var O=Object.freeze({__proto__:null,extend:(t,e)=>{a(t)&&Object.keys(e).forEach((n=>{t[n]=e[n]}))},filter:(t,e)=>{const n={};return f(t)&&Object.keys(t).forEach((r=>{const o=t[r];e(t[r],r)&&(n[r]=o)})),n},forEach:(t,e)=>{Object.keys(t).forEach((n=>{e(t[n],n)}))},pick:(t,e)=>e.reduce(((e,n)=>(n in t&&(e[n]=t[n]),e)),{})});export{t as VAL_TYPE,s as enumToArray,p as get,e as getParamType,j as isArray,o as isBoolean,l as isFunction,u as isNull,a as isObject,f as isPlainObject,r as isString,c as isSymbol,n as isType,i as isUndefined,b as isUndefinedOrNull,O as obj,h as set,g as uuid};
@@ -3,6 +3,7 @@ declare const VAL_TYPE: {
3
3
  readonly undefined: "undefined";
4
4
  readonly number: "undefined";
5
5
  readonly string: "string";
6
+ readonly symbol: "symbol";
6
7
  readonly null: "null";
7
8
  readonly "[object Object]": "object";
8
9
  readonly "[object Function]": "function";
@@ -22,16 +23,17 @@ type TVal = (typeof VAL_TYPE)[keyof typeof VAL_TYPE];
22
23
  * @returns
23
24
  */
24
25
  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;
26
+ declare function isType(params: unknown, type: TVal): boolean;
27
+ declare function isString(s: unknown): s is string;
28
+ declare function isBoolean(b: unknown): b is boolean;
29
+ declare function isSymbol(b: unknown): b is symbol;
30
+ declare function isNull(v: unknown): v is null;
31
+ declare function isUndefined(v: unknown): v is undefined;
32
+ declare function isUndefinedOrNull(v: unknown): v is undefined | null;
33
+ declare function isFunction(b: unknown): b is Function;
34
+ declare function isPlainObject(b: unknown): b is Record<string, unknown>;
35
+ declare function isObject(b: unknown): b is Record<string, unknown>;
36
+ declare function isArray(a: unknown): a is any[];
35
37
 
36
38
  type TAny = any;
37
39
  type TPlainObject = Record<string, TAny>;
@@ -63,4 +65,28 @@ declare const set: (source: object | null | undefined, path: string, value: any)
63
65
  */
64
66
  declare const uuid: () => string;
65
67
 
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 };
68
+ declare const filter: (sourceObj: any, filter: any) => {};
69
+ declare const extend: (sourceObj: any, injectObj: any) => void;
70
+ /**
71
+ * 对象挑选
72
+ * @param obj
73
+ * @param keys
74
+ * @returns
75
+ */
76
+ declare const pick: <T extends object, K extends keyof T>(obj: T, keys: K[]) => Pick<T, K>;
77
+ /**
78
+ * 遍历对象
79
+ * @param obj
80
+ * @param callback
81
+ */
82
+ declare const forEach: <T extends object, K extends keyof T>(obj: T, callback: (val: T[K], key: K) => void) => void;
83
+
84
+ declare const obj_extend: typeof extend;
85
+ declare const obj_filter: typeof filter;
86
+ declare const obj_forEach: typeof forEach;
87
+ declare const obj_pick: typeof pick;
88
+ declare namespace obj {
89
+ export { obj_extend as extend, obj_filter as filter, obj_forEach as forEach, obj_pick as pick };
90
+ }
91
+
92
+ export { type TAny, type TPlainObject, type TVal, VAL_TYPE, enumToArray, get, getParamType, isArray, isBoolean, isFunction, isNull, isObject, isPlainObject, isString, isSymbol, isType, isUndefined, isUndefinedOrNull, obj, set, uuid };
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@cclr/lang",
3
- "version": "0.0.6",
3
+ "version": "0.0.9",
4
4
  "description": "js语言基础工具",
5
5
  "author": "cclr <18843152354@163.com>",
6
6
  "homepage": "",
7
- "license": "ISC",
7
+ "license": "MIT",
8
8
  "main": "lib/cjs/index.js",
9
9
  "module": "lib/esm/index.js",
10
10
  "types": "lib/type/index.d.ts",
@@ -23,5 +23,5 @@
23
23
  "scripts": {
24
24
  "test": "node ./__tests__/lang.test.js"
25
25
  },
26
- "gitHead": "e2a9c7f3d406d70510ed008f38fed383f4fa31c1"
26
+ "gitHead": "ad2e0a50411f99a529d84eb5901748d9c3cf5f3a"
27
27
  }