@daysnap/utils 0.0.69 → 0.0.70

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/es/ato.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ /**
2
+ * 数组转换称对象
3
+ * const options = [
4
+ * {label: '男', value: '1'}
5
+ * {label: '女', value: '2'}
6
+ * ]
7
+ * const res = ato(options, 'value')
8
+ * res = { '1': {label: '男', value: '1'}, '2': {label: '女', value: '2'} }
9
+ * const res = ato(options, 'value', 'label')
10
+ * res = { '1': '男', '2': '女' }
11
+ */
12
+ export declare function ato<T extends Record<string, any>, K extends keyof T>(options: T[], labelKey: K): Record<T[K], T>;
13
+ export declare function ato<T extends Record<string, any>, K extends keyof T>(options: T[], labelKey: K, valueKey: K): Record<T[K], any>;
package/es/ato.js ADDED
@@ -0,0 +1,6 @@
1
+ export function ato(options, labelKey, valueKey) {
2
+ return options.reduce((res, item) => {
3
+ res[item[labelKey]] = valueKey ? item[valueKey] : item;
4
+ return res;
5
+ }, {});
6
+ }
package/es/clamp.js CHANGED
@@ -5,6 +5,7 @@
5
5
  * @param max 最大值
6
6
  */
7
7
  export function clamp(min, val, max) {
8
+ // Math.min(Math.max(min, val), max)
8
9
  if (val < min) {
9
10
  return min;
10
11
  }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * 过滤字符串
3
+ * const str = filterString('13188888888', '*', 3, 7)
4
+ * str = '131****8888'
5
+ */
6
+ export declare function filterString(val: string, sep?: string, start?: number, end?: number): string;
@@ -0,0 +1,16 @@
1
+ /**
2
+ * 过滤字符串
3
+ * const str = filterString('13188888888', '*', 3, 7)
4
+ * str = '131****8888'
5
+ */
6
+ export function filterString(val, sep = '*', start = 0, end) {
7
+ return val
8
+ .split('')
9
+ .map((s, index) => {
10
+ if (index >= start && index < (end !== null && end !== void 0 ? end : val.length)) {
11
+ return sep;
12
+ }
13
+ return s;
14
+ })
15
+ .join('');
16
+ }
package/es/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export * from './ato';
1
2
  export * from './base64ToBlob';
2
3
  export * from './blobToBase64';
3
4
  export * from './cached';
@@ -22,6 +23,7 @@ export * from './filterEmptyValue';
22
23
  export * from './filterIdCard';
23
24
  export * from './filterName';
24
25
  export * from './filterPhone';
26
+ export * from './filterString';
25
27
  export * from './formatAmount';
26
28
  export * from './formatDate';
27
29
  export * from './formatDateStr';
@@ -97,4 +99,5 @@ export * from './stringifyQuery';
97
99
  export * from './throttle';
98
100
  export * from './toCDB';
99
101
  export * from './toDBC';
102
+ export * from './trap';
100
103
  export * from './typeOf';
package/es/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  /* 本文件自动生成 './scripts/entry.js' */
2
+ export * from './ato';
2
3
  export * from './base64ToBlob';
3
4
  export * from './blobToBase64';
4
5
  export * from './cached';
@@ -23,6 +24,7 @@ export * from './filterEmptyValue';
23
24
  export * from './filterIdCard';
24
25
  export * from './filterName';
25
26
  export * from './filterPhone';
27
+ export * from './filterString';
26
28
  export * from './formatAmount';
27
29
  export * from './formatDate';
28
30
  export * from './formatDateStr';
@@ -98,4 +100,5 @@ export * from './stringifyQuery';
98
100
  export * from './throttle';
99
101
  export * from './toCDB';
100
102
  export * from './toDBC';
103
+ export * from './trap';
101
104
  export * from './typeOf';
package/es/trap.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ /**
2
+ * 陷阱圈套
3
+ * 主要用于缓存页面下、执行触发
4
+ */
5
+ export interface Trap {
6
+ list: Record<string, any>;
7
+ trigger: (id: string, data?: any) => void;
8
+ create: (id: string, cb: (...args: any[]) => any) => void;
9
+ delete: (id: string) => void;
10
+ clear: () => void;
11
+ }
12
+ export declare const trap: Trap;
package/es/trap.js ADDED
@@ -0,0 +1,30 @@
1
+ import { isUndefined } from './isUndefined';
2
+ export const trap = {
3
+ list: {},
4
+ // 触发
5
+ trigger(id, data = {}) {
6
+ if (!id.startsWith('trap')) {
7
+ throw new Error(`trap id 命名必须满足: trap:[componentName]:[action]`);
8
+ }
9
+ this.list[id] = data;
10
+ },
11
+ // 创建
12
+ create(id, cb) {
13
+ if (!id.startsWith('trap')) {
14
+ throw new Error(`trap id 命名必须满足: trap:[componentName]:[action]`);
15
+ }
16
+ const data = this.list[id];
17
+ this.delete(id);
18
+ if (!isUndefined(data)) {
19
+ cb(data);
20
+ }
21
+ },
22
+ // 删除
23
+ delete(id) {
24
+ delete this.list[id];
25
+ },
26
+ // 清除
27
+ clear() {
28
+ this.list = {};
29
+ },
30
+ };
package/lib/ato.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ /**
2
+ * 数组转换称对象
3
+ * const options = [
4
+ * {label: '男', value: '1'}
5
+ * {label: '女', value: '2'}
6
+ * ]
7
+ * const res = ato(options, 'value')
8
+ * res = { '1': {label: '男', value: '1'}, '2': {label: '女', value: '2'} }
9
+ * const res = ato(options, 'value', 'label')
10
+ * res = { '1': '男', '2': '女' }
11
+ */
12
+ export declare function ato<T extends Record<string, any>, K extends keyof T>(options: T[], labelKey: K): Record<T[K], T>;
13
+ export declare function ato<T extends Record<string, any>, K extends keyof T>(options: T[], labelKey: K, valueKey: K): Record<T[K], any>;
package/lib/ato.js ADDED
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ato = void 0;
4
+ function ato(options, labelKey, valueKey) {
5
+ return options.reduce((res, item) => {
6
+ res[item[labelKey]] = valueKey ? item[valueKey] : item;
7
+ return res;
8
+ }, {});
9
+ }
10
+ exports.ato = ato;
package/lib/clamp.js CHANGED
@@ -8,6 +8,7 @@ exports.clamp = void 0;
8
8
  * @param max 最大值
9
9
  */
10
10
  function clamp(min, val, max) {
11
+ // Math.min(Math.max(min, val), max)
11
12
  if (val < min) {
12
13
  return min;
13
14
  }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * 过滤字符串
3
+ * const str = filterString('13188888888', '*', 3, 7)
4
+ * str = '131****8888'
5
+ */
6
+ export declare function filterString(val: string, sep?: string, start?: number, end?: number): string;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.filterString = void 0;
4
+ /**
5
+ * 过滤字符串
6
+ * const str = filterString('13188888888', '*', 3, 7)
7
+ * str = '131****8888'
8
+ */
9
+ function filterString(val, sep = '*', start = 0, end) {
10
+ return val
11
+ .split('')
12
+ .map((s, index) => {
13
+ if (index >= start && index < (end !== null && end !== void 0 ? end : val.length)) {
14
+ return sep;
15
+ }
16
+ return s;
17
+ })
18
+ .join('');
19
+ }
20
+ exports.filterString = filterString;
package/lib/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export * from './ato';
1
2
  export * from './base64ToBlob';
2
3
  export * from './blobToBase64';
3
4
  export * from './cached';
@@ -22,6 +23,7 @@ export * from './filterEmptyValue';
22
23
  export * from './filterIdCard';
23
24
  export * from './filterName';
24
25
  export * from './filterPhone';
26
+ export * from './filterString';
25
27
  export * from './formatAmount';
26
28
  export * from './formatDate';
27
29
  export * from './formatDateStr';
@@ -97,4 +99,5 @@ export * from './stringifyQuery';
97
99
  export * from './throttle';
98
100
  export * from './toCDB';
99
101
  export * from './toDBC';
102
+ export * from './trap';
100
103
  export * from './typeOf';
package/lib/index.js CHANGED
@@ -15,6 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
16
16
  };
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
+ __exportStar(require("./ato"), exports);
18
19
  __exportStar(require("./base64ToBlob"), exports);
19
20
  __exportStar(require("./blobToBase64"), exports);
20
21
  __exportStar(require("./cached"), exports);
@@ -39,6 +40,7 @@ __exportStar(require("./filterEmptyValue"), exports);
39
40
  __exportStar(require("./filterIdCard"), exports);
40
41
  __exportStar(require("./filterName"), exports);
41
42
  __exportStar(require("./filterPhone"), exports);
43
+ __exportStar(require("./filterString"), exports);
42
44
  __exportStar(require("./formatAmount"), exports);
43
45
  __exportStar(require("./formatDate"), exports);
44
46
  __exportStar(require("./formatDateStr"), exports);
@@ -114,4 +116,5 @@ __exportStar(require("./stringifyQuery"), exports);
114
116
  __exportStar(require("./throttle"), exports);
115
117
  __exportStar(require("./toCDB"), exports);
116
118
  __exportStar(require("./toDBC"), exports);
119
+ __exportStar(require("./trap"), exports);
117
120
  __exportStar(require("./typeOf"), exports);
package/lib/trap.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ /**
2
+ * 陷阱圈套
3
+ * 主要用于缓存页面下、执行触发
4
+ */
5
+ export interface Trap {
6
+ list: Record<string, any>;
7
+ trigger: (id: string, data?: any) => void;
8
+ create: (id: string, cb: (...args: any[]) => any) => void;
9
+ delete: (id: string) => void;
10
+ clear: () => void;
11
+ }
12
+ export declare const trap: Trap;
package/lib/trap.js ADDED
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.trap = void 0;
4
+ const isUndefined_1 = require("./isUndefined");
5
+ exports.trap = {
6
+ list: {},
7
+ // 触发
8
+ trigger(id, data = {}) {
9
+ if (!id.startsWith('trap')) {
10
+ throw new Error(`trap id 命名必须满足: trap:[componentName]:[action]`);
11
+ }
12
+ this.list[id] = data;
13
+ },
14
+ // 创建
15
+ create(id, cb) {
16
+ if (!id.startsWith('trap')) {
17
+ throw new Error(`trap id 命名必须满足: trap:[componentName]:[action]`);
18
+ }
19
+ const data = this.list[id];
20
+ this.delete(id);
21
+ if (!(0, isUndefined_1.isUndefined)(data)) {
22
+ cb(data);
23
+ }
24
+ },
25
+ // 删除
26
+ delete(id) {
27
+ delete this.list[id];
28
+ },
29
+ // 清除
30
+ clear() {
31
+ this.list = {};
32
+ },
33
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@daysnap/utils",
3
- "version": "0.0.69",
3
+ "version": "0.0.70",
4
4
  "description": "通用的工具库",
5
5
  "main": "lib/index.js",
6
6
  "module": "es/index.js",