@zcrkey/js-utils 0.0.5 → 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.
Files changed (48) hide show
  1. package/README.md +9 -0
  2. package/{src/eventCenter.ts → dist/cjs/eventCenter.js} +120 -112
  3. package/dist/cjs/index.js +51 -0
  4. package/dist/cjs/objUtil.js +37 -0
  5. package/{src/storage.ts → dist/cjs/storage.js} +118 -101
  6. package/dist/cjs/treeUtil.js +145 -0
  7. package/{src/util.ts → dist/cjs/util.js} +170 -254
  8. package/dist/esm/eventCenter.d.ts +59 -0
  9. package/{src/index.ts → dist/esm/index.d.ts} +8 -8
  10. package/dist/esm/objUtil.d.ts +12 -0
  11. package/dist/esm/storage.d.ts +44 -0
  12. package/dist/esm/treeUtil.d.ts +48 -0
  13. package/dist/esm/util.d.ts +209 -0
  14. package/package.json +15 -4
  15. package/.dumi/global.less +0 -1396
  16. package/.dumirc.ts +0 -36
  17. package/.fatherrc.ts +0 -14
  18. package/.husky/commit-msg +0 -4
  19. package/.husky/pre-commit +0 -4
  20. package/.prettierignore +0 -2
  21. package/.stylelintrc +0 -10
  22. package/docs/api/eventCenter/index.md +0 -34
  23. package/docs/api/index.md +0 -5
  24. package/docs/api/storage/index.md +0 -9
  25. package/docs/api/storage/local.tsx +0 -91
  26. package/docs/api/storage/session.tsx +0 -85
  27. package/docs/api/treeUtil/index.md +0 -5
  28. package/docs/api/treeUtil/index.tsx +0 -266
  29. package/docs/api/util/index.md +0 -6
  30. package/docs/api/util/index.tsx +0 -405
  31. package/docs/api/util/is.tsx +0 -196
  32. package/docs/guide.md +0 -24
  33. package/src/objUtil.ts +0 -20
  34. package/src/treeUtil.ts +0 -164
  35. package/tsconfig.json +0 -18
  36. /package/dist/{eventCenter.d.ts → cjs/eventCenter.d.ts} +0 -0
  37. /package/dist/{index.d.ts → cjs/index.d.ts} +0 -0
  38. /package/dist/{objUtil.d.ts → cjs/objUtil.d.ts} +0 -0
  39. /package/dist/{storage.d.ts → cjs/storage.d.ts} +0 -0
  40. /package/dist/{treeUtil.d.ts → cjs/treeUtil.d.ts} +0 -0
  41. /package/dist/{util.d.ts → cjs/util.d.ts} +0 -0
  42. /package/dist/{eventCenter.js → esm/eventCenter.js} +0 -0
  43. /package/dist/{index.js → esm/index.js} +0 -0
  44. /package/dist/{objUtil.js → esm/objUtil.js} +0 -0
  45. /package/dist/{storage.js → esm/storage.js} +0 -0
  46. /package/dist/{treeUtil.js → esm/treeUtil.js} +0 -0
  47. /package/dist/{util.js → esm/util.js} +0 -0
  48. /package/dist/{index.umd.js → umd/index.umd.js} +0 -0
package/README.md CHANGED
@@ -38,6 +38,15 @@ const App = () => (
38
38
  export default App;
39
39
  ```
40
40
 
41
+ ### NODE 环境下使用
42
+
43
+ ```
44
+ const { CrUtil } = require('@zcrkey/js-utils');
45
+
46
+ console.log(CrUtil.trim(' @zcrkey/js-utils '))
47
+
48
+ ```
49
+
41
50
  ### 通过独立版本安装
42
51
 
43
52
  - 下载 @zcrkey/js-utils 的独立版本文件(如 index.umd.js)
@@ -1,112 +1,120 @@
1
- export type TSubscribers = {
2
- key: string;
3
- listeners: Array<(...args: any) => void>;
4
- };
5
-
6
- export type TSubscription = {
7
- key: string;
8
- listener: (...args: any) => void;
9
- remove: () => void;
10
- };
11
-
12
- export default class CrEventCenter {
13
- /**
14
- * 储存订阅者
15
- */
16
- static subscribers: TSubscribers[] = [];
17
-
18
- /**
19
- * 新增订阅
20
- *
21
- * @param {*} key 名称
22
- * @param {*} listener 执行函数
23
- * @returns 订阅信息
24
- * @example
25
- this.subscription = CrEventCenter.on('名称', (参数) => {});
26
- */
27
- static on(key: string, listener: (...args: any) => void): TSubscription {
28
- const subscriber = this.subscribers.find((item) => item.key == key);
29
- if (subscriber) {
30
- subscriber.listeners.push(listener);
31
- } else {
32
- this.subscribers.push({
33
- key: key,
34
- listeners: [listener],
35
- });
36
- }
37
- let subscription: TSubscription = {
38
- key: key,
39
- listener: listener,
40
- remove: () => {
41
- this.off(subscription);
42
- },
43
- };
44
- return subscription;
45
- }
46
-
47
- /**
48
- * 移除订阅
49
- *
50
- * @param {*} subscription
51
- * @param {*} type 'all'
52
- * @example
53
- CrEventCenter.off(this.subscription);
54
- */
55
- static off(subscription: TSubscription, type?: 'all') {
56
- const index = this.subscribers.findIndex(
57
- (item) => item.key == subscription.key,
58
- );
59
- if (index > -1) {
60
- if (type == 'all') {
61
- this.subscribers.splice(index, 1);
62
- } else {
63
- const subscriber = this.subscribers[index];
64
- const _index = subscriber.listeners.findIndex(
65
- (item) => item == subscription.listener,
66
- );
67
- if (_index > -1) {
68
- subscriber.listeners.splice(_index, 1);
69
- }
70
- }
71
- }
72
- }
73
-
74
- /**
75
- * 派发事件
76
- *
77
- * @param {*} key 名称
78
- * @param {*} args 其余参数
79
- * @example
80
- CrEventCenter.emit('名称', 参数数据);
81
- */
82
- static emit(key: string, ...args: any) {
83
- const subscriber = this.subscribers.find((item) => item.key == key);
84
- if (subscriber && subscriber.listeners && subscriber.listeners.length > 0) {
85
- subscriber.listeners.forEach((listener) => {
86
- listener(...args);
87
- });
88
- }
89
- }
90
-
91
- /**
92
- * 查找事件
93
- *
94
- * @param {*} key 名称
95
- * @example
96
- let subscriber = CrEventCenter.find('名称');
97
- */
98
- static find(key: string): TSubscribers | undefined {
99
- return this.subscribers.find((item) => item.key == key);
100
- }
101
-
102
- /**
103
- * 清理所有事件
104
- *
105
- * @memberof CrEventCenter
106
- * @example
107
- CrEventCenter.clear();
108
- */
109
- static clear() {
110
- this.subscribers.length = 0;
111
- }
112
- }
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/eventCenter.ts
20
+ var eventCenter_exports = {};
21
+ __export(eventCenter_exports, {
22
+ default: () => CrEventCenter
23
+ });
24
+ module.exports = __toCommonJS(eventCenter_exports);
25
+ var CrEventCenter = class {
26
+ /**
27
+ * 新增订阅
28
+ *
29
+ * @param {*} key 名称
30
+ * @param {*} listener 执行函数
31
+ * @returns 订阅信息
32
+ * @example
33
+ this.subscription = CrEventCenter.on('名称', (参数) => {});
34
+ */
35
+ static on(key, listener) {
36
+ const subscriber = this.subscribers.find((item) => item.key == key);
37
+ if (subscriber) {
38
+ subscriber.listeners.push(listener);
39
+ } else {
40
+ this.subscribers.push({
41
+ key,
42
+ listeners: [listener]
43
+ });
44
+ }
45
+ let subscription = {
46
+ key,
47
+ listener,
48
+ remove: () => {
49
+ this.off(subscription);
50
+ }
51
+ };
52
+ return subscription;
53
+ }
54
+ /**
55
+ * 移除订阅
56
+ *
57
+ * @param {*} subscription
58
+ * @param {*} type 'all'
59
+ * @example
60
+ CrEventCenter.off(this.subscription);
61
+ */
62
+ static off(subscription, type) {
63
+ const index = this.subscribers.findIndex(
64
+ (item) => item.key == subscription.key
65
+ );
66
+ if (index > -1) {
67
+ if (type == "all") {
68
+ this.subscribers.splice(index, 1);
69
+ } else {
70
+ const subscriber = this.subscribers[index];
71
+ const _index = subscriber.listeners.findIndex(
72
+ (item) => item == subscription.listener
73
+ );
74
+ if (_index > -1) {
75
+ subscriber.listeners.splice(_index, 1);
76
+ }
77
+ }
78
+ }
79
+ }
80
+ /**
81
+ * 派发事件
82
+ *
83
+ * @param {*} key 名称
84
+ * @param {*} args 其余参数
85
+ * @example
86
+ CrEventCenter.emit('名称', 参数数据);
87
+ */
88
+ static emit(key, ...args) {
89
+ const subscriber = this.subscribers.find((item) => item.key == key);
90
+ if (subscriber && subscriber.listeners && subscriber.listeners.length > 0) {
91
+ subscriber.listeners.forEach((listener) => {
92
+ listener(...args);
93
+ });
94
+ }
95
+ }
96
+ /**
97
+ * 查找事件
98
+ *
99
+ * @param {*} key 名称
100
+ * @example
101
+ let subscriber = CrEventCenter.find('名称');
102
+ */
103
+ static find(key) {
104
+ return this.subscribers.find((item) => item.key == key);
105
+ }
106
+ /**
107
+ * 清理所有事件
108
+ *
109
+ * @memberof CrEventCenter
110
+ * @example
111
+ CrEventCenter.clear();
112
+ */
113
+ static clear() {
114
+ this.subscribers.length = 0;
115
+ }
116
+ };
117
+ /**
118
+ * 储存订阅者
119
+ */
120
+ CrEventCenter.subscribers = [];
@@ -0,0 +1,51 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+
29
+ // src/index.ts
30
+ var src_exports = {};
31
+ __export(src_exports, {
32
+ CrEventCenter: () => import_eventCenter.default,
33
+ CrObjUtil: () => import_objUtil.default,
34
+ CrStorage: () => import_storage.default,
35
+ CrTreeUtil: () => import_treeUtil.default,
36
+ CrUtil: () => import_util.default
37
+ });
38
+ module.exports = __toCommonJS(src_exports);
39
+ var import_eventCenter = __toESM(require("./eventCenter"));
40
+ var import_objUtil = __toESM(require("./objUtil"));
41
+ var import_storage = __toESM(require("./storage"));
42
+ var import_treeUtil = __toESM(require("./treeUtil"));
43
+ var import_util = __toESM(require("./util"));
44
+ // Annotate the CommonJS export names for ESM import in node:
45
+ 0 && (module.exports = {
46
+ CrEventCenter,
47
+ CrObjUtil,
48
+ CrStorage,
49
+ CrTreeUtil,
50
+ CrUtil
51
+ });
@@ -0,0 +1,37 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/objUtil.ts
20
+ var objUtil_exports = {};
21
+ __export(objUtil_exports, {
22
+ default: () => CrObjUtil
23
+ });
24
+ module.exports = __toCommonJS(objUtil_exports);
25
+ var import_lodash = require("lodash");
26
+ var CrObjUtil = class {
27
+ /**
28
+ * 根据字段路径获取对象值
29
+ * @param obj { 'a': [{ 'b': { 'c': 3 } }] }
30
+ * @param path 'a[0].b.c' | ['a', '0', 'b', 'c']
31
+ * @param defaultValue 找不到时返回的默认值
32
+ * @returns
33
+ */
34
+ static getValueByPath(obj, propertyPath, defaultValue) {
35
+ return (0, import_lodash.get)(obj, propertyPath, defaultValue);
36
+ }
37
+ };
@@ -1,101 +1,118 @@
1
- export default class CrStorage {
2
- /**
3
- * 设置本地存储
4
- * @param key
5
- * @param data
6
- */
7
- static setLocalItem(key: string, data: any) {
8
- try {
9
- localStorage.setItem(key, JSON.stringify(data));
10
- } catch (error) {
11
- console.warn(`setLocalItem:${key}:${error}`);
12
- }
13
- }
14
-
15
- /**
16
- * 获取本地存储
17
- * @param key
18
- * @returns
19
- */
20
- static getLocalItem<T = any>(key: string): T | null {
21
- const str = localStorage.getItem(key);
22
- if (str) {
23
- if (str === 'undefined') {
24
- return null;
25
- }
26
- try {
27
- return JSON.parse(str);
28
- } catch (error) {
29
- console.warn(`getLocalItem:${key}:${error}`);
30
- return null;
31
- }
32
- } else {
33
- return null;
34
- }
35
- }
36
-
37
- /**
38
- * 清除某个本地存储
39
- * @param key
40
- */
41
- static removeLocalItem(key: string) {
42
- localStorage.removeItem(key);
43
- }
44
-
45
- /**
46
- * 清除所有本地存储
47
- */
48
- static clearLocal() {
49
- localStorage.clear();
50
- }
51
-
52
- /**
53
- * 设置会话存储
54
- * @param key
55
- * @param data
56
- */
57
- static setSessionItem(key: string, data: any) {
58
- try {
59
- sessionStorage.setItem(key, JSON.stringify(data));
60
- } catch (error) {
61
- console.warn(`setSessionItem:${key}:${error}`);
62
- }
63
- }
64
-
65
- /**
66
- * 获取会话存储
67
- * @param key
68
- * @returns
69
- */
70
- static getSessionItem<T = any>(key: string): T | null {
71
- const str = sessionStorage.getItem(key);
72
- if (str) {
73
- if (str === 'undefined') {
74
- return null;
75
- }
76
- try {
77
- return JSON.parse(str);
78
- } catch (error) {
79
- console.warn(`getSessionItem:${key}:${error}`);
80
- return null;
81
- }
82
- } else {
83
- return null;
84
- }
85
- }
86
-
87
- /**
88
- * 清除某个会话存储
89
- * @param key
90
- */
91
- static removeSessionItem(key: string) {
92
- sessionStorage.removeItem(key);
93
- }
94
-
95
- /**
96
- * 清除所有会话存储
97
- */
98
- static clearSession() {
99
- sessionStorage.clear();
100
- }
101
- }
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/storage.ts
20
+ var storage_exports = {};
21
+ __export(storage_exports, {
22
+ default: () => CrStorage
23
+ });
24
+ module.exports = __toCommonJS(storage_exports);
25
+ var CrStorage = class {
26
+ /**
27
+ * 设置本地存储
28
+ * @param key
29
+ * @param data
30
+ */
31
+ static setLocalItem(key, data) {
32
+ try {
33
+ localStorage.setItem(key, JSON.stringify(data));
34
+ } catch (error) {
35
+ console.warn(`setLocalItem:${key}:${error}`);
36
+ }
37
+ }
38
+ /**
39
+ * 获取本地存储
40
+ * @param key
41
+ * @returns
42
+ */
43
+ static getLocalItem(key) {
44
+ const str = localStorage.getItem(key);
45
+ if (str) {
46
+ if (str === "undefined") {
47
+ return null;
48
+ }
49
+ try {
50
+ return JSON.parse(str);
51
+ } catch (error) {
52
+ console.warn(`getLocalItem:${key}:${error}`);
53
+ return null;
54
+ }
55
+ } else {
56
+ return null;
57
+ }
58
+ }
59
+ /**
60
+ * 清除某个本地存储
61
+ * @param key
62
+ */
63
+ static removeLocalItem(key) {
64
+ localStorage.removeItem(key);
65
+ }
66
+ /**
67
+ * 清除所有本地存储
68
+ */
69
+ static clearLocal() {
70
+ localStorage.clear();
71
+ }
72
+ /**
73
+ * 设置会话存储
74
+ * @param key
75
+ * @param data
76
+ */
77
+ static setSessionItem(key, data) {
78
+ try {
79
+ sessionStorage.setItem(key, JSON.stringify(data));
80
+ } catch (error) {
81
+ console.warn(`setSessionItem:${key}:${error}`);
82
+ }
83
+ }
84
+ /**
85
+ * 获取会话存储
86
+ * @param key
87
+ * @returns
88
+ */
89
+ static getSessionItem(key) {
90
+ const str = sessionStorage.getItem(key);
91
+ if (str) {
92
+ if (str === "undefined") {
93
+ return null;
94
+ }
95
+ try {
96
+ return JSON.parse(str);
97
+ } catch (error) {
98
+ console.warn(`getSessionItem:${key}:${error}`);
99
+ return null;
100
+ }
101
+ } else {
102
+ return null;
103
+ }
104
+ }
105
+ /**
106
+ * 清除某个会话存储
107
+ * @param key
108
+ */
109
+ static removeSessionItem(key) {
110
+ sessionStorage.removeItem(key);
111
+ }
112
+ /**
113
+ * 清除所有会话存储
114
+ */
115
+ static clearSession() {
116
+ sessionStorage.clear();
117
+ }
118
+ };