@yh-kit/utils 1.1.3 → 1.2.0
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/utils.js +85 -27
- package/dist/utils.umd.cjs +1 -1
- package/package.json +1 -1
- package/types/array/array2obj.d.ts +3 -12
- package/types/array/index.d.ts +27 -0
- package/types/index.d.ts +3 -0
- package/types/number/index.d.ts +1 -1
- package/types/number/letter.d.ts +12 -4
- package/types/number/money.d.ts +10 -4
- package/types/phone/index.d.ts +11 -0
- package/types/string/index.d.ts +12 -0
- package/types/waterfall/index.d.ts +7 -0
package/dist/utils.js
CHANGED
|
@@ -1,37 +1,95 @@
|
|
|
1
|
-
const
|
|
2
|
-
|
|
1
|
+
const n = (e) => {
|
|
2
|
+
if (!e || !e.length) return {};
|
|
3
|
+
const r = {};
|
|
4
|
+
return e.forEach((t) => {
|
|
5
|
+
const o = Object.assign({ label: t.label, text: t.label }, t);
|
|
6
|
+
r[t == null ? void 0 : t.value] = o;
|
|
7
|
+
}), r;
|
|
8
|
+
}, s = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
9
|
+
__proto__: null,
|
|
10
|
+
toEnumObj: n
|
|
11
|
+
}, Symbol.toStringTag, { value: "Module" })), d = {
|
|
12
|
+
...s,
|
|
3
13
|
/**
|
|
4
|
-
*
|
|
14
|
+
* 数组去重
|
|
5
15
|
* @param arr 数组
|
|
6
|
-
* @returns
|
|
16
|
+
* @returns 去重后的数组
|
|
7
17
|
*/
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
18
|
+
unique: (e) => e ? Array.from(new Set(e)) : [],
|
|
19
|
+
/**
|
|
20
|
+
* 判断某元素是否在数组中
|
|
21
|
+
* @param arr 数组
|
|
22
|
+
* @param element 查询元素
|
|
23
|
+
* @returns 是否存在。true 存在;false 不存在
|
|
24
|
+
*/
|
|
25
|
+
isExist(e, r) {
|
|
26
|
+
return !e || !r ? !1 : e.indexOf(r) !== -1;
|
|
27
|
+
},
|
|
28
|
+
/**
|
|
29
|
+
* 统计数组中某元素出现的次数:对于大型数组,手动循环的性能略优于 filter 和 reduce。
|
|
30
|
+
* @param arr 数组
|
|
31
|
+
* @param target 目标元素
|
|
32
|
+
* @returns 出现次数
|
|
33
|
+
*/
|
|
34
|
+
countOfAppear(e, r) {
|
|
35
|
+
let t = 0;
|
|
36
|
+
for (const o of e)
|
|
37
|
+
o === r && t++;
|
|
38
|
+
return t;
|
|
39
|
+
},
|
|
40
|
+
/**
|
|
41
|
+
* 数组排序-默认升序
|
|
42
|
+
* @param arr 数组
|
|
43
|
+
* @param order 排序方式:asc 升序;desc 降序
|
|
44
|
+
* @returns 排序后的数组
|
|
45
|
+
*/
|
|
46
|
+
sort(e, r = "asc") {
|
|
47
|
+
return e ? e.sort((t, o) => r === "asc" ? t > o ? 1 : -1 : t < o ? 1 : -1) : [];
|
|
15
48
|
}
|
|
16
|
-
},
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
toMoney:
|
|
49
|
+
}, l = (e) => e > 25 || e < 0 ? "" : "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[e], c = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
50
|
+
__proto__: null,
|
|
51
|
+
toLetter: l
|
|
52
|
+
}, Symbol.toStringTag, { value: "Module" })), u = (e) => e.toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","), i = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
53
|
+
__proto__: null,
|
|
54
|
+
toMoney: u
|
|
55
|
+
}, Symbol.toStringTag, { value: "Module" })), g = {
|
|
56
|
+
...c,
|
|
57
|
+
...i
|
|
58
|
+
}, b = {
|
|
59
|
+
/**
|
|
60
|
+
* 脱敏
|
|
61
|
+
* @param phone 电话号码/手机号码
|
|
62
|
+
* @returns 脱敏后的电话号码/手机号码
|
|
63
|
+
*/
|
|
64
|
+
desensitize: (e) => e ? e.replace(/^(\d{3})\d{4}(\d{4})$/, "$1****$2") || e.slice(0, 3) + "****" + e.slice(7) : ""
|
|
22
65
|
}, y = {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
66
|
+
/**
|
|
67
|
+
* 判断某元素是否在字符串中
|
|
68
|
+
* @param str 字符串
|
|
69
|
+
* @param element 查询元素
|
|
70
|
+
* @returns 是否存在。true 存在;false 不存在
|
|
71
|
+
*/
|
|
72
|
+
isExist(e, r) {
|
|
73
|
+
if (!e || !r)
|
|
74
|
+
return !1;
|
|
75
|
+
const t = e.split(",");
|
|
76
|
+
return console.log("判断某元素是否在字符串中", t.indexOf(r) === -1), t.indexOf(r) !== -1;
|
|
77
|
+
}
|
|
78
|
+
}, f = (e) => {
|
|
26
79
|
const t = new RegExp("[?&]" + e + "=([^&#]*)", "i").exec(window.location.href);
|
|
27
80
|
return t ? decodeURIComponent(t[1]) : null;
|
|
28
|
-
},
|
|
29
|
-
getQueryInfoByName:
|
|
30
|
-
},
|
|
31
|
-
...
|
|
81
|
+
}, a = {
|
|
82
|
+
getQueryInfoByName: f
|
|
83
|
+
}, p = {
|
|
84
|
+
...a
|
|
85
|
+
}, O = (e, r = 2) => {
|
|
86
|
+
console.log(e, r);
|
|
32
87
|
};
|
|
33
88
|
export {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
89
|
+
d as arrayUtils,
|
|
90
|
+
g as numberUtils,
|
|
91
|
+
b as phoneUtils,
|
|
92
|
+
y as stringUtils,
|
|
93
|
+
O as toList,
|
|
94
|
+
p as urlUtils
|
|
37
95
|
};
|
package/dist/utils.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(o,s){typeof exports=="object"&&typeof module<"u"?s(exports):typeof define=="function"&&define.amd?define(["exports"],s):(o=typeof globalThis<"u"?globalThis:o||self,s(o.yhkitUtils={}))})(this,function(o){"use strict";const l={...Object.freeze(Object.defineProperty({__proto__:null,toEnumObj:e=>{if(!e||!e.length)return{};const n={};return e.forEach(t=>{const r=Object.assign({label:t.label,text:t.label},t);n[t==null?void 0:t.value]=r}),n}},Symbol.toStringTag,{value:"Module"})),unique:e=>e?Array.from(new Set(e)):[],isExist(e,n){return!e||!n?!1:e.indexOf(n)!==-1},countOfAppear(e,n){let t=0;for(const r of e)r===n&&t++;return t},sort(e,n="asc"){return e?e.sort((t,r)=>n==="asc"?t>r?1:-1:t<r?1:-1):[]}},i={...Object.freeze(Object.defineProperty({__proto__:null,toLetter:e=>e>25||e<0?"":"ABCDEFGHIJKLMNOPQRSTUVWXYZ"[e]},Symbol.toStringTag,{value:"Module"})),...Object.freeze(Object.defineProperty({__proto__:null,toMoney:e=>e.toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g,",")},Symbol.toStringTag,{value:"Module"}))},u={desensitize:e=>e?e.replace(/^(\d{3})\d{4}(\d{4})$/,"$1****$2")||e.slice(0,3)+"****"+e.slice(7):""},c={isExist(e,n){if(!e||!n)return!1;const t=e.split(",");return console.log("判断某元素是否在字符串中",t.indexOf(n)===-1),t.indexOf(n)!==-1}},f={...{getQueryInfoByName:e=>{const t=new RegExp("[?&]"+e+"=([^&#]*)","i").exec(window.location.href);return t?decodeURIComponent(t[1]):null}}},d=(e,n=2)=>{console.log(e,n)};o.arrayUtils=l,o.numberUtils=i,o.phoneUtils=u,o.stringUtils=c,o.toList=d,o.urlUtils=f,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -1,16 +1,7 @@
|
|
|
1
1
|
import { IOptionItem, TValueEnum } from "../typings";
|
|
2
2
|
/**
|
|
3
3
|
* 数组转枚举对象
|
|
4
|
-
* @param arr
|
|
5
|
-
* @returns
|
|
4
|
+
* @param arr 数组
|
|
5
|
+
* @returns 枚举对象
|
|
6
6
|
*/
|
|
7
|
-
|
|
8
|
-
declare const _default: {
|
|
9
|
-
/**
|
|
10
|
-
* 数组转枚举对象
|
|
11
|
-
* @param arr 数组
|
|
12
|
-
* @returns
|
|
13
|
-
*/
|
|
14
|
-
toEnumObj: (arr: IOptionItem[]) => TValueEnum;
|
|
15
|
-
};
|
|
16
|
-
export default _default;
|
|
7
|
+
export declare const toEnumObj: (arr: IOptionItem[]) => TValueEnum;
|
package/types/array/index.d.ts
CHANGED
|
@@ -2,5 +2,32 @@
|
|
|
2
2
|
* 数组相关操作工具函数
|
|
3
3
|
*/
|
|
4
4
|
export declare const arrayUtils: {
|
|
5
|
+
/**
|
|
6
|
+
* 数组去重
|
|
7
|
+
* @param arr 数组
|
|
8
|
+
* @returns 去重后的数组
|
|
9
|
+
*/
|
|
10
|
+
unique: <T>(arr: T[]) => T[];
|
|
11
|
+
/**
|
|
12
|
+
* 判断某元素是否在数组中
|
|
13
|
+
* @param arr 数组
|
|
14
|
+
* @param element 查询元素
|
|
15
|
+
* @returns 是否存在。true 存在;false 不存在
|
|
16
|
+
*/
|
|
17
|
+
isExist<T>(arr: T[], element: T): boolean;
|
|
18
|
+
/**
|
|
19
|
+
* 统计数组中某元素出现的次数:对于大型数组,手动循环的性能略优于 filter 和 reduce。
|
|
20
|
+
* @param arr 数组
|
|
21
|
+
* @param target 目标元素
|
|
22
|
+
* @returns 出现次数
|
|
23
|
+
*/
|
|
24
|
+
countOfAppear<T>(arr: T[], target: T): number;
|
|
25
|
+
/**
|
|
26
|
+
* 数组排序-默认升序
|
|
27
|
+
* @param arr 数组
|
|
28
|
+
* @param order 排序方式:asc 升序;desc 降序
|
|
29
|
+
* @returns 排序后的数组
|
|
30
|
+
*/
|
|
31
|
+
sort<T>(arr: T[], order?: "asc" | "desc"): T[];
|
|
5
32
|
toEnumObj: (arr: import("../typings").IOptionItem[]) => import("../typings").TValueEnum;
|
|
6
33
|
};
|
package/types/index.d.ts
CHANGED
package/types/number/index.d.ts
CHANGED
package/types/number/letter.d.ts
CHANGED
|
@@ -1,4 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
/**
|
|
2
|
+
* 数字转字母
|
|
3
|
+
* 例如:
|
|
4
|
+
* 0 => A
|
|
5
|
+
* 1 => B
|
|
6
|
+
* 25 => Z
|
|
7
|
+
* 26 => ""
|
|
8
|
+
* 27 => ""
|
|
9
|
+
* @param num 数字
|
|
10
|
+
* @returns
|
|
11
|
+
*/
|
|
12
|
+
export declare const toLetter: (num: number) => string;
|
package/types/number/money.d.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
/**
|
|
2
|
+
* 格式化金额 - 数字千分化,保留两位小数
|
|
3
|
+
* 例如:
|
|
4
|
+
* 123456789 => 123,456,789.00
|
|
5
|
+
* 0 => 0.00
|
|
6
|
+
* 123.456789 => 123.46
|
|
7
|
+
* @param x 数字
|
|
8
|
+
* @returns
|
|
9
|
+
*/
|
|
10
|
+
export declare const toMoney: (x: number) => string;
|