@jhqn/utils-core 0.0.1-beta.2

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,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 金华青鸟
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # utils
2
+ A collection of tool functions🧷
package/dist/index.cjs ADDED
@@ -0,0 +1,208 @@
1
+ 'use strict';
2
+
3
+ const snake2Kebab = (str) => str.replace(/_([a-z])/g, (_, letter) => `-${letter.toLowerCase()}`);
4
+ const snake2Pascal = (str) => str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase()).replace(/^[a-z]/, (letter) => letter.toUpperCase());
5
+ const snake2Camel = (str) => str.replace(/(_([a-z]))/g, (match) => match[1].toUpperCase());
6
+ const snake2Title = (str) => str.replace(/_([a-z])/g, (_, letter) => ` ${letter.toUpperCase()}`).replace(/^[a-z]/, (letter) => letter.toUpperCase());
7
+ const kebab2Snake = (str) => str.replace(/-([a-z])/g, (_, letter) => `_${letter.toLowerCase()}`);
8
+ const kebab2Pascal = (str) => str.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase()).replace(/^[a-z]/, (letter) => letter.toUpperCase());
9
+ const kebab2Camel = (str) => {
10
+ const [first, ...rest] = str.split("-");
11
+ return [first, ...rest.map((s) => s.replace(/^[a-z]/, s[0].toUpperCase()))].join("");
12
+ };
13
+ const kebab2Title = (str) => str.replace(/-([a-z])/g, (_, letter) => ` ${letter.toUpperCase()}`).replace(/^[a-z]/, (letter) => letter.toUpperCase());
14
+ const pascal2Snake = (str) => str.replace(/([A-Z])/g, (_, letter) => `_${letter.toLowerCase()}`).replace(/^_/, "");
15
+ const pascal2Kebab = (str) => str.replace(/([A-Z])/g, (_, letter) => `-${letter.toLowerCase()}`).replace(/^-/, "");
16
+ const pascal2Camel = (str) => str.replace(/^[A-Z]/, (letter) => letter.toLowerCase());
17
+ const pascal2Title = (str) => str.replace(/([A-Z])/g, (_, letter) => ` ${letter}`).replace(/^ /, "");
18
+ const camel2Snake = (str) => str.replace(/([A-Z])/g, (_, letter) => `_${letter.toLowerCase()}`);
19
+ const camel2Kebab = (str) => str.replace(/([A-Z])/g, (_, letter) => `-${letter.toLowerCase()}`);
20
+ const camel2Pascal = (str) => str.replace(/(^[a-z])|(\s+[a-z])/g, (match) => match.toUpperCase());
21
+ const camel2Title = (str) => str.replace(/([A-Z])/g, (_, letter) => ` ${letter}`).replace(/^[a-z]/, (match) => match.toUpperCase());
22
+ const title2Snake = (str) => str.replace(/([A-Z])/g, (match) => match.toLowerCase()).replace(/ /g, "_");
23
+ const title2Kebab = (str) => str.replace(/([A-Z])/g, (match) => match.toLowerCase()).replace(/ /g, "-");
24
+ const title2Pascal = (str) => str.replace(/ /g, "");
25
+ const title2Camel = (str) => str.replace(/ /g, "").replace(/^[A-Z]/, (match) => match.toLowerCase());
26
+
27
+ const sharedStyle = "color: white; padding: 1px 2px;";
28
+ const jLog = (...args) => __DEV__ ? console.log.bind(
29
+ console,
30
+ `%c[${args.length === 1 ? "LOG" : "%s"}]`,
31
+ `${sharedStyle} background: seagreen;`
32
+ )(...args) : () => {
33
+ };
34
+ const jDebug = (...args) => console.log.bind(
35
+ console,
36
+ `%c[${args.length === 1 ? "DEBUG" : "%s"}]`,
37
+ `${sharedStyle} background: linear-gradient(LightSalmon, Tomato); border-radius: 2px; font-weight: bold; border-top: 2px solid Crimson; border-bottom: 2px solid Crimson;`
38
+ )(...args);
39
+ const jWarn = console.error.bind(console, "%c[WARN]", `${sharedStyle} background: tomato;`);
40
+ const jError = console.error.bind(console, "%c[ERROR]", `${sharedStyle} background: crimson;`);
41
+ const jInfo = (...args) => console.info.bind(
42
+ console,
43
+ `%c[${args.length === 1 ? "INFO" : "%s"}]`,
44
+ `${sharedStyle} background: dodgerblue;`
45
+ )(...args);
46
+ const jGroup = console.group.bind(console, "%c[%s]", `${sharedStyle} background: slateblue;`);
47
+ const jGroupC = console.groupCollapsed.bind(console, "%c[%s]", `${sharedStyle} background: slateblue;`);
48
+ const jGroupE = console.groupEnd.bind(console);
49
+ const jTime = (str) => console.time(`[${str}]`);
50
+ const jTimeEnd = (str) => console.timeEnd(`[${str}]`);
51
+ const jClear = console.clear.bind(console);
52
+
53
+ const cnPhoneRE = /^1[3-9]\d{9}$/;
54
+ const cnTelRE = /^0\d{9,11}$/;
55
+ const isDef = (val) => val !== void 0 && val !== null;
56
+ const isNumeric = (val) => typeof val === "number" || /^\d+(?:\.\d+)?$/.test(val);
57
+ const checkImg = (ext) => {
58
+ return /\.(?:jpg|jpeg|png|gif)$/.test((ext || "").toLowerCase());
59
+ };
60
+ const isJSONStr = (str) => {
61
+ try {
62
+ JSON.parse(str);
63
+ return true;
64
+ } catch (e) {
65
+ return false;
66
+ }
67
+ };
68
+ const isArrStr = (str) => {
69
+ try {
70
+ return Array.isArray(JSON.parse(str));
71
+ } catch (e) {
72
+ return false;
73
+ }
74
+ };
75
+ const isValidKey = (key, object) => key in object;
76
+ const isPhone = (val) => {
77
+ if (!isDef(val))
78
+ return false;
79
+ return cnPhoneRE.test(val.toString());
80
+ };
81
+ const isTel = (val) => {
82
+ if (!isDef(val))
83
+ return false;
84
+ return cnTelRE.test(val.toString());
85
+ };
86
+
87
+ const hidePhone = (str) => {
88
+ if (str) {
89
+ if (isPhone(str)) {
90
+ return str.replace(/^(\d{3})\d{4}(\d{4})$/, "$1****$2");
91
+ }
92
+ if (isTel(str)) {
93
+ return str.replace(/^(\d{3,4})-?\d{3,4}(\d{4})$/, "$1****$2");
94
+ }
95
+ return str;
96
+ }
97
+ return "";
98
+ };
99
+ const hideCardNo = (cardNo) => {
100
+ if (cardNo) {
101
+ return cardNo.replace(/^(.{4})\d+(.{2})$/, "$1*******$2");
102
+ }
103
+ return "";
104
+ };
105
+ const hideName = (name) => {
106
+ if (name) {
107
+ let res = "";
108
+ for (let i = 0; i < name.length; i += 1) {
109
+ if (i === 0) {
110
+ res += "*";
111
+ } else {
112
+ res += name[i];
113
+ }
114
+ }
115
+ return res;
116
+ }
117
+ return "";
118
+ };
119
+ const hideMiddle = (str) => {
120
+ if (!isDef(str))
121
+ return "";
122
+ const len = str.length;
123
+ if (len === 1)
124
+ return "*";
125
+ if (len === 2)
126
+ return "**";
127
+ return str[0] + "*".repeat(len - 2) + str[len - 1];
128
+ };
129
+ const hideThird = (str) => {
130
+ if (!isDef(str))
131
+ return "";
132
+ const len = str.length;
133
+ const partLen = Math.round(len / 3);
134
+ if (len === 1)
135
+ return "*";
136
+ if (len === 2)
137
+ return "**";
138
+ return str.slice(0, partLen) + "*".repeat(len - 2 * partLen) + str.slice(len - partLen);
139
+ };
140
+
141
+ const parseToJSON = (str, reviver2) => isJSONStr(str) ? JSON.parse(str, reviver2) : null;
142
+ const replacer = (_, value) => {
143
+ if (value instanceof Map) {
144
+ return {
145
+ dataType: "Map",
146
+ value: [...value]
147
+ };
148
+ }
149
+ return value;
150
+ };
151
+ const reviver = (_, value) => {
152
+ if (typeof value === "object" && value !== null) {
153
+ if (value.dataType === "Map") {
154
+ return new Map(value.value);
155
+ }
156
+ }
157
+ return value;
158
+ };
159
+ const filterObj = (obj, keys, reverse = false) => Object.fromEntries(Object.entries(obj).filter(([key]) => reverse ? !keys.includes(key) : keys.includes(key)));
160
+
161
+ exports.camel2Kebab = camel2Kebab;
162
+ exports.camel2Pascal = camel2Pascal;
163
+ exports.camel2Snake = camel2Snake;
164
+ exports.camel2Title = camel2Title;
165
+ exports.checkImg = checkImg;
166
+ exports.filterObj = filterObj;
167
+ exports.hideCardNo = hideCardNo;
168
+ exports.hideMiddle = hideMiddle;
169
+ exports.hideName = hideName;
170
+ exports.hidePhone = hidePhone;
171
+ exports.hideThird = hideThird;
172
+ exports.isArrStr = isArrStr;
173
+ exports.isDef = isDef;
174
+ exports.isJSONStr = isJSONStr;
175
+ exports.isNumeric = isNumeric;
176
+ exports.isPhone = isPhone;
177
+ exports.isTel = isTel;
178
+ exports.isValidKey = isValidKey;
179
+ exports.jClear = jClear;
180
+ exports.jDebug = jDebug;
181
+ exports.jError = jError;
182
+ exports.jGroup = jGroup;
183
+ exports.jGroupC = jGroupC;
184
+ exports.jGroupE = jGroupE;
185
+ exports.jInfo = jInfo;
186
+ exports.jLog = jLog;
187
+ exports.jTime = jTime;
188
+ exports.jTimeEnd = jTimeEnd;
189
+ exports.jWarn = jWarn;
190
+ exports.kebab2Camel = kebab2Camel;
191
+ exports.kebab2Pascal = kebab2Pascal;
192
+ exports.kebab2Snake = kebab2Snake;
193
+ exports.kebab2Title = kebab2Title;
194
+ exports.parseToJSON = parseToJSON;
195
+ exports.pascal2Camel = pascal2Camel;
196
+ exports.pascal2Kebab = pascal2Kebab;
197
+ exports.pascal2Snake = pascal2Snake;
198
+ exports.pascal2Title = pascal2Title;
199
+ exports.replacer = replacer;
200
+ exports.reviver = reviver;
201
+ exports.snake2Camel = snake2Camel;
202
+ exports.snake2Kebab = snake2Kebab;
203
+ exports.snake2Pascal = snake2Pascal;
204
+ exports.snake2Title = snake2Title;
205
+ exports.title2Camel = title2Camel;
206
+ exports.title2Kebab = title2Kebab;
207
+ exports.title2Pascal = title2Pascal;
208
+ exports.title2Snake = title2Snake;
@@ -0,0 +1,307 @@
1
+ /**
2
+ * snake2Kebab
3
+ * @param {string} str 字符串
4
+ * @returns {string} 转换后的字符串
5
+ * @example snake2Kebab('hello_world') // hello_world => hello-world
6
+ */
7
+ declare const snake2Kebab: (str: string) => string;
8
+ /**
9
+ * snake2Pascal
10
+ * @param {string} str 字符串
11
+ * @returns {string} 转换后的字符串
12
+ * @example snake2Pascal('hello_world') // hello_world => HelloWorld
13
+ */
14
+ declare const snake2Pascal: (str: string) => string;
15
+ /**
16
+ * snake2Camel
17
+ * @param {string} str 字符串
18
+ * @returns {string} 转换后的字符串
19
+ * @example snake2Camel('hello_world') // hello_world => helloWorld
20
+ */
21
+ declare const snake2Camel: (str: string) => string;
22
+ /**
23
+ * snake2Title
24
+ * @param {string} str 字符串
25
+ * @returns {string} 转换后的字符串
26
+ * @example snake2Title('hello_world') // hello_world => Hello World
27
+ */
28
+ declare const snake2Title: (str: string) => string;
29
+ /**
30
+ * kebab2Snake
31
+ * @param {string} str 字符串
32
+ * @returns {string} 转换后的字符串
33
+ * @example kebab2Snake('hello-world') // hello-world => hello_world
34
+ */
35
+ declare const kebab2Snake: (str: string) => string;
36
+ /**
37
+ * kebab2Pascal
38
+ * @param {string} str 字符串
39
+ * @returns {string} 转换后的字符串
40
+ * @example kebab2Pascal('hello-world') // hello-world => HelloWorld
41
+ */
42
+ declare const kebab2Pascal: (str: string) => string;
43
+ /**
44
+ * kebab2Camel
45
+ * @param {string} str 字符串
46
+ * @returns {string} 转换后的字符串
47
+ * @example kebab2Camel('hello-world') // hello-world => helloWorld
48
+ */
49
+ declare const kebab2Camel: (str: string) => string;
50
+ /**
51
+ * kebab2Title
52
+ * @param {string} str 字符串
53
+ * @returns {string} 转换后的字符串
54
+ * @example kebab2Title('hello-world') // hello-world => Hello World
55
+ */
56
+ declare const kebab2Title: (str: string) => string;
57
+ /**
58
+ * pascal2Snake
59
+ * @param {string} str 字符串
60
+ * @returns {string} 转换后的字符串
61
+ * @example pascal2Snake('HelloWorld') // HelloWorld => hello_world
62
+ */
63
+ declare const pascal2Snake: (str: string) => string;
64
+ /**
65
+ * pascal2Kebab
66
+ * @param {string} str 字符串
67
+ * @returns {string} 转换后的字符串
68
+ * @example pascal2Kebab('HelloWorld') // HelloWorld => hello-world
69
+ */
70
+ declare const pascal2Kebab: (str: string) => string;
71
+ /**
72
+ * pascal2Camel
73
+ * @param {string} str 字符串
74
+ * @returns {string} 转换后的字符串
75
+ * @example pascal2Camel('HelloWorld') // HelloWorld => helloWorld
76
+ */
77
+ declare const pascal2Camel: (str: string) => string;
78
+ /**
79
+ * pascal2Title
80
+ * @param {string} str 字符串
81
+ * @returns {string} 转换后的字符串
82
+ * @example pascal2Title('HelloWorld') // HelloWorld => Hello World
83
+ */
84
+ declare const pascal2Title: (str: string) => string;
85
+ /**
86
+ * camel2Snake
87
+ * @param {string} str 字符串
88
+ * @returns {string} 转换后的字符串
89
+ * @example camel2Snake('helloWorld') // helloWorld => hello_world
90
+ */
91
+ declare const camel2Snake: (str: string) => string;
92
+ /**
93
+ * camel2Kebab
94
+ * @param {string} str 字符串
95
+ * @returns {string} 转换后的字符串
96
+ * @example camel2Kebab('helloWorld') // helloWorld => hello-world
97
+ */
98
+ declare const camel2Kebab: (str: string) => string;
99
+ /**
100
+ * camel2Pascal
101
+ * @param {string} str 字符串
102
+ * @returns {string} 转换后的字符串
103
+ * @example camel2Pascal('helloWorld') // helloWorld => HelloWorld
104
+ */
105
+ declare const camel2Pascal: (str: string) => string;
106
+ /**
107
+ * camel2Title
108
+ * @param {string} str 字符串
109
+ * @returns {string} 转换后的字符串
110
+ * @example camel2Title('helloWorld') // helloWorld => Hello World
111
+ */
112
+ declare const camel2Title: (str: string) => string;
113
+ /**
114
+ * title2Snake
115
+ * @param {string} str 字符串
116
+ * @returns {string} 转换后的字符串
117
+ * @example title2Snake('Hello World') // Hello World => hello_world
118
+ */
119
+ declare const title2Snake: (str: string) => string;
120
+ /**
121
+ * title2Kebab
122
+ * @param {string} str 字符串
123
+ * @returns {string} 转换后的字符串
124
+ * @example title2Kebab('Hello World') // Hello World => hello-world
125
+ */
126
+ declare const title2Kebab: (str: string) => string;
127
+ /**
128
+ * title2Pascal
129
+ * @param {string} str 字符串
130
+ * @returns {string} 转换后的字符串
131
+ * @example title2Pascal('Hello World') // Hello World => HelloWorld
132
+ */
133
+ declare const title2Pascal: (str: string) => string;
134
+ /**
135
+ * title2Camel
136
+ * @param {string} str 字符串
137
+ * @returns {string} 转换后的字符串
138
+ * @example title2Camel('Hello World') // Hello World => helloWorld
139
+ */
140
+ declare const title2Camel: (str: string) => string;
141
+
142
+ /**
143
+ * jade console.log
144
+ * only for development
145
+ * @param {...any} args
146
+ */
147
+ declare const jLog: (...args: any[]) => void | (() => void);
148
+ /**
149
+ * jade console.debug
150
+ * @param {...any} args
151
+ */
152
+ declare const jDebug: (...args: any[]) => void;
153
+ /**
154
+ * jade console.warn
155
+ */
156
+ declare const jWarn: (...args: any[]) => void;
157
+ /**
158
+ * jade console.error
159
+ */
160
+ declare const jError: (...args: any[]) => void;
161
+ /**
162
+ * jade console.info
163
+ * @param {...any} args
164
+ */
165
+ declare const jInfo: (...args: any[]) => void;
166
+ /**
167
+ * jade console.group
168
+ */
169
+ declare const jGroup: (...args: any[]) => void;
170
+ /**
171
+ * jade console.groupCollapsed
172
+ */
173
+ declare const jGroupC: (...args: any[]) => void;
174
+ /**
175
+ * jade console.groupEnd
176
+ */
177
+ declare const jGroupE: {
178
+ (): void;
179
+ (): void;
180
+ };
181
+ /**
182
+ * jade console.time
183
+ * @param str
184
+ */
185
+ declare const jTime: (str: string) => void;
186
+ /**
187
+ * jade console.timeEnd
188
+ * @param str
189
+ */
190
+ declare const jTimeEnd: (str: string) => void;
191
+ /**
192
+ * jade console.clear
193
+ */
194
+ declare const jClear: {
195
+ (): void;
196
+ (): void;
197
+ };
198
+
199
+ /**
200
+ * 数字或字符串
201
+ */
202
+ declare type Numeric = number | string;
203
+ /**
204
+ * 可 Null
205
+ */
206
+ declare type Nullable<T> = T | null;
207
+ /**
208
+ * 可 Undefined
209
+ */
210
+ declare type Undefinable<T> = T | undefined;
211
+ /**
212
+ * 可 Null 或 Undefined
213
+ */
214
+ declare type NullOrUndefinable<T> = Nullable<Undefinable<T>>;
215
+
216
+ /**
217
+ * 隐藏手机号
218
+ * @param str
219
+ */
220
+ declare const hidePhone: (str: NullOrUndefinable<string>) => string;
221
+ /**
222
+ * 隐藏身份证号
223
+ * @param cardNo
224
+ */
225
+ declare const hideCardNo: (cardNo: NullOrUndefinable<string>) => string;
226
+ /**
227
+ * 隐藏姓名
228
+ * @param name
229
+ */
230
+ declare const hideName: (name: NullOrUndefinable<string>) => string;
231
+ /**
232
+ * 隐藏中间部分
233
+ * @param str
234
+ */
235
+ declare const hideMiddle: (str: NullOrUndefinable<string>) => string;
236
+ /**
237
+ * 隐藏首尾仅保留中间3分之1
238
+ * @param str
239
+ */
240
+ declare const hideThird: (str: NullOrUndefinable<string>) => string;
241
+
242
+ /**
243
+ * Json字符串转对象
244
+ * @param str
245
+ * @param reviver
246
+ */
247
+ declare const parseToJSON: <T = any>(str: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => T | null;
248
+ /**
249
+ * JSON.stringify() second param
250
+ * @param _
251
+ * @param value
252
+ */
253
+ declare const replacer: (_: any, value: any) => any;
254
+ /**
255
+ * JSON.parse() second param
256
+ * @param _
257
+ * @param value
258
+ */
259
+ declare const reviver: (_: any, value: any) => any;
260
+ /**
261
+ * 过滤obj中仅包括keys的部分
262
+ * @param {Record<string,any>} obj 对象
263
+ * @param {string[]} keys 属性
264
+ * @param {boolean} reverse 为true时过滤取反
265
+ */
266
+ declare const filterObj: (obj: Record<string, any>, keys: string[], reverse?: boolean) => {
267
+ [k: string]: any;
268
+ };
269
+
270
+ /**
271
+ * 是否有意义的值
272
+ * @param val
273
+ */
274
+ declare const isDef: <T>(val: T) => val is NonNullable<T>;
275
+ /**
276
+ * 是否是数字或字符
277
+ * @param val
278
+ */
279
+ declare const isNumeric: (val: Numeric) => val is string;
280
+ /**
281
+ * 是否是需要的图片格式
282
+ * @param ext
283
+ */
284
+ declare const checkImg: (ext?: string) => boolean;
285
+ /**
286
+ * 判断是否合法JSON字符串
287
+ * @param str
288
+ */
289
+ declare const isJSONStr: (str: string) => boolean;
290
+ /**
291
+ * 判断是否合法JSON数组字符串
292
+ * @param str
293
+ */
294
+ declare const isArrStr: (str: string) => boolean;
295
+ declare const isValidKey: (key: string | number | symbol, object: object) => key is never;
296
+ /**
297
+ * 是否是中国手机号
298
+ * @param val
299
+ */
300
+ declare const isPhone: (val: NullOrUndefinable<Numeric>) => boolean;
301
+ /**
302
+ * 是否是中国座机号
303
+ * @param val
304
+ */
305
+ declare const isTel: (val: NullOrUndefinable<Numeric>) => boolean;
306
+
307
+ export { NullOrUndefinable, Nullable, Numeric, Undefinable, camel2Kebab, camel2Pascal, camel2Snake, camel2Title, checkImg, filterObj, hideCardNo, hideMiddle, hideName, hidePhone, hideThird, isArrStr, isDef, isJSONStr, isNumeric, isPhone, isTel, isValidKey, jClear, jDebug, jError, jGroup, jGroupC, jGroupE, jInfo, jLog, jTime, jTimeEnd, jWarn, kebab2Camel, kebab2Pascal, kebab2Snake, kebab2Title, parseToJSON, pascal2Camel, pascal2Kebab, pascal2Snake, pascal2Title, replacer, reviver, snake2Camel, snake2Kebab, snake2Pascal, snake2Title, title2Camel, title2Kebab, title2Pascal, title2Snake };
package/dist/index.mjs ADDED
@@ -0,0 +1,159 @@
1
+ const snake2Kebab = (str) => str.replace(/_([a-z])/g, (_, letter) => `-${letter.toLowerCase()}`);
2
+ const snake2Pascal = (str) => str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase()).replace(/^[a-z]/, (letter) => letter.toUpperCase());
3
+ const snake2Camel = (str) => str.replace(/(_([a-z]))/g, (match) => match[1].toUpperCase());
4
+ const snake2Title = (str) => str.replace(/_([a-z])/g, (_, letter) => ` ${letter.toUpperCase()}`).replace(/^[a-z]/, (letter) => letter.toUpperCase());
5
+ const kebab2Snake = (str) => str.replace(/-([a-z])/g, (_, letter) => `_${letter.toLowerCase()}`);
6
+ const kebab2Pascal = (str) => str.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase()).replace(/^[a-z]/, (letter) => letter.toUpperCase());
7
+ const kebab2Camel = (str) => {
8
+ const [first, ...rest] = str.split("-");
9
+ return [first, ...rest.map((s) => s.replace(/^[a-z]/, s[0].toUpperCase()))].join("");
10
+ };
11
+ const kebab2Title = (str) => str.replace(/-([a-z])/g, (_, letter) => ` ${letter.toUpperCase()}`).replace(/^[a-z]/, (letter) => letter.toUpperCase());
12
+ const pascal2Snake = (str) => str.replace(/([A-Z])/g, (_, letter) => `_${letter.toLowerCase()}`).replace(/^_/, "");
13
+ const pascal2Kebab = (str) => str.replace(/([A-Z])/g, (_, letter) => `-${letter.toLowerCase()}`).replace(/^-/, "");
14
+ const pascal2Camel = (str) => str.replace(/^[A-Z]/, (letter) => letter.toLowerCase());
15
+ const pascal2Title = (str) => str.replace(/([A-Z])/g, (_, letter) => ` ${letter}`).replace(/^ /, "");
16
+ const camel2Snake = (str) => str.replace(/([A-Z])/g, (_, letter) => `_${letter.toLowerCase()}`);
17
+ const camel2Kebab = (str) => str.replace(/([A-Z])/g, (_, letter) => `-${letter.toLowerCase()}`);
18
+ const camel2Pascal = (str) => str.replace(/(^[a-z])|(\s+[a-z])/g, (match) => match.toUpperCase());
19
+ const camel2Title = (str) => str.replace(/([A-Z])/g, (_, letter) => ` ${letter}`).replace(/^[a-z]/, (match) => match.toUpperCase());
20
+ const title2Snake = (str) => str.replace(/([A-Z])/g, (match) => match.toLowerCase()).replace(/ /g, "_");
21
+ const title2Kebab = (str) => str.replace(/([A-Z])/g, (match) => match.toLowerCase()).replace(/ /g, "-");
22
+ const title2Pascal = (str) => str.replace(/ /g, "");
23
+ const title2Camel = (str) => str.replace(/ /g, "").replace(/^[A-Z]/, (match) => match.toLowerCase());
24
+
25
+ const sharedStyle = "color: white; padding: 1px 2px;";
26
+ const jLog = (...args) => __DEV__ ? console.log.bind(
27
+ console,
28
+ `%c[${args.length === 1 ? "LOG" : "%s"}]`,
29
+ `${sharedStyle} background: seagreen;`
30
+ )(...args) : () => {
31
+ };
32
+ const jDebug = (...args) => console.log.bind(
33
+ console,
34
+ `%c[${args.length === 1 ? "DEBUG" : "%s"}]`,
35
+ `${sharedStyle} background: linear-gradient(LightSalmon, Tomato); border-radius: 2px; font-weight: bold; border-top: 2px solid Crimson; border-bottom: 2px solid Crimson;`
36
+ )(...args);
37
+ const jWarn = console.error.bind(console, "%c[WARN]", `${sharedStyle} background: tomato;`);
38
+ const jError = console.error.bind(console, "%c[ERROR]", `${sharedStyle} background: crimson;`);
39
+ const jInfo = (...args) => console.info.bind(
40
+ console,
41
+ `%c[${args.length === 1 ? "INFO" : "%s"}]`,
42
+ `${sharedStyle} background: dodgerblue;`
43
+ )(...args);
44
+ const jGroup = console.group.bind(console, "%c[%s]", `${sharedStyle} background: slateblue;`);
45
+ const jGroupC = console.groupCollapsed.bind(console, "%c[%s]", `${sharedStyle} background: slateblue;`);
46
+ const jGroupE = console.groupEnd.bind(console);
47
+ const jTime = (str) => console.time(`[${str}]`);
48
+ const jTimeEnd = (str) => console.timeEnd(`[${str}]`);
49
+ const jClear = console.clear.bind(console);
50
+
51
+ const cnPhoneRE = /^1[3-9]\d{9}$/;
52
+ const cnTelRE = /^0\d{9,11}$/;
53
+ const isDef = (val) => val !== void 0 && val !== null;
54
+ const isNumeric = (val) => typeof val === "number" || /^\d+(?:\.\d+)?$/.test(val);
55
+ const checkImg = (ext) => {
56
+ return /\.(?:jpg|jpeg|png|gif)$/.test((ext || "").toLowerCase());
57
+ };
58
+ const isJSONStr = (str) => {
59
+ try {
60
+ JSON.parse(str);
61
+ return true;
62
+ } catch (e) {
63
+ return false;
64
+ }
65
+ };
66
+ const isArrStr = (str) => {
67
+ try {
68
+ return Array.isArray(JSON.parse(str));
69
+ } catch (e) {
70
+ return false;
71
+ }
72
+ };
73
+ const isValidKey = (key, object) => key in object;
74
+ const isPhone = (val) => {
75
+ if (!isDef(val))
76
+ return false;
77
+ return cnPhoneRE.test(val.toString());
78
+ };
79
+ const isTel = (val) => {
80
+ if (!isDef(val))
81
+ return false;
82
+ return cnTelRE.test(val.toString());
83
+ };
84
+
85
+ const hidePhone = (str) => {
86
+ if (str) {
87
+ if (isPhone(str)) {
88
+ return str.replace(/^(\d{3})\d{4}(\d{4})$/, "$1****$2");
89
+ }
90
+ if (isTel(str)) {
91
+ return str.replace(/^(\d{3,4})-?\d{3,4}(\d{4})$/, "$1****$2");
92
+ }
93
+ return str;
94
+ }
95
+ return "";
96
+ };
97
+ const hideCardNo = (cardNo) => {
98
+ if (cardNo) {
99
+ return cardNo.replace(/^(.{4})\d+(.{2})$/, "$1*******$2");
100
+ }
101
+ return "";
102
+ };
103
+ const hideName = (name) => {
104
+ if (name) {
105
+ let res = "";
106
+ for (let i = 0; i < name.length; i += 1) {
107
+ if (i === 0) {
108
+ res += "*";
109
+ } else {
110
+ res += name[i];
111
+ }
112
+ }
113
+ return res;
114
+ }
115
+ return "";
116
+ };
117
+ const hideMiddle = (str) => {
118
+ if (!isDef(str))
119
+ return "";
120
+ const len = str.length;
121
+ if (len === 1)
122
+ return "*";
123
+ if (len === 2)
124
+ return "**";
125
+ return str[0] + "*".repeat(len - 2) + str[len - 1];
126
+ };
127
+ const hideThird = (str) => {
128
+ if (!isDef(str))
129
+ return "";
130
+ const len = str.length;
131
+ const partLen = Math.round(len / 3);
132
+ if (len === 1)
133
+ return "*";
134
+ if (len === 2)
135
+ return "**";
136
+ return str.slice(0, partLen) + "*".repeat(len - 2 * partLen) + str.slice(len - partLen);
137
+ };
138
+
139
+ const parseToJSON = (str, reviver2) => isJSONStr(str) ? JSON.parse(str, reviver2) : null;
140
+ const replacer = (_, value) => {
141
+ if (value instanceof Map) {
142
+ return {
143
+ dataType: "Map",
144
+ value: [...value]
145
+ };
146
+ }
147
+ return value;
148
+ };
149
+ const reviver = (_, value) => {
150
+ if (typeof value === "object" && value !== null) {
151
+ if (value.dataType === "Map") {
152
+ return new Map(value.value);
153
+ }
154
+ }
155
+ return value;
156
+ };
157
+ const filterObj = (obj, keys, reverse = false) => Object.fromEntries(Object.entries(obj).filter(([key]) => reverse ? !keys.includes(key) : keys.includes(key)));
158
+
159
+ export { camel2Kebab, camel2Pascal, camel2Snake, camel2Title, checkImg, filterObj, hideCardNo, hideMiddle, hideName, hidePhone, hideThird, isArrStr, isDef, isJSONStr, isNumeric, isPhone, isTel, isValidKey, jClear, jDebug, jError, jGroup, jGroupC, jGroupE, jInfo, jLog, jTime, jTimeEnd, jWarn, kebab2Camel, kebab2Pascal, kebab2Snake, kebab2Title, parseToJSON, pascal2Camel, pascal2Kebab, pascal2Snake, pascal2Title, replacer, reviver, snake2Camel, snake2Kebab, snake2Pascal, snake2Title, title2Camel, title2Kebab, title2Pascal, title2Snake };
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@jhqn/utils-core",
3
+ "author": "jade-gjz <jiangang.zhang@jaderd.com>",
4
+ "version": "0.0.1-beta.2",
5
+ "keywords": [
6
+ "jade",
7
+ "utils"
8
+ ],
9
+ "homepage": "https://github.com/jaderd-jh/utils/tree/main/packages/core#readme",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/jaderd-jh/utils",
13
+ "directory": "packages/core"
14
+ },
15
+ "bugs": {
16
+ "url": "https://github.com/jaderd-jh/utils/issues"
17
+ },
18
+ "type": "module",
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "main": "dist/index.cjs",
23
+ "module": "dist/index.mjs",
24
+ "types": "dist/index.d.ts",
25
+ "exports": {
26
+ ".": {
27
+ "import": "./dist/index.mjs",
28
+ "require": "./dist/index.cjs",
29
+ "types": "./dist/index.d.ts"
30
+ }
31
+ },
32
+ "sideEffects": false,
33
+ "scripts": {
34
+ "test": "vitest",
35
+ "//build": "tsup",
36
+ "build": "unbuild"
37
+ }
38
+ }