@jhqn/utils-core 0.0.1-beta.22 → 0.0.1-beta.24

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/index.cjs CHANGED
@@ -1,13 +1,13 @@
1
1
  'use strict';
2
2
 
3
3
  const detectBrowser = require('detect-browser');
4
- const dayjs = require('dayjs/esm');
5
- const localeData = require('dayjs/esm/plugin/localeData');
6
- const duration = require('dayjs/esm/plugin/duration');
7
- const relativeTime = require('dayjs/esm/plugin/relativeTime');
8
- const customParseFormat = require('dayjs/esm/plugin/customParseFormat');
9
- const objectSupport = require('dayjs/esm/plugin/objectSupport');
10
- const zhCN = require('dayjs/esm/locale/zh-cn');
4
+ const dayjs = require('dayjs');
5
+ const localeData = require('dayjs/plugin/localeData');
6
+ const duration = require('dayjs/plugin/duration');
7
+ const relativeTime = require('dayjs/plugin/relativeTime');
8
+ const customParseFormat = require('dayjs/plugin/customParseFormat');
9
+ const objectSupport = require('dayjs/plugin/objectSupport');
10
+ const zhCN = require('dayjs/locale/zh-cn');
11
11
 
12
12
  function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
13
13
 
@@ -76,6 +76,9 @@ const isNumeric = (val) => typeof val === "number" || /^\d+(?:\.\d+)?$/.test(val
76
76
  const checkImg = (ext) => {
77
77
  return /\.(?:jpg|jpeg|png|gif)$/.test((ext || "").toLowerCase());
78
78
  };
79
+ const isEmpty = (val) => {
80
+ return !isDef(val) || val === "" || Array.isArray(val) && val.length === 0 || Object.prototype.toString.call(val) === "[object Object]" && Object.keys(val).length === 0 || val instanceof Map && val.size === 0 || val instanceof Set && val.size === 0;
81
+ };
79
82
  const isJSONStr = (str) => {
80
83
  try {
81
84
  JSON.parse(str);
@@ -116,19 +119,80 @@ const isIdCard = (val) => {
116
119
  return card15.test(val) || card18.test(val);
117
120
  };
118
121
  const nonASCII = (text) => /[^\x20-\x7E]/.test(text);
119
- const isValidRes = (res) => res.code === 200 && isDef(res.data);
120
- const isValidArrRes = (res) => res.code === 200 && Array.isArray(res.data);
121
- const isValidPageRes = (res) => res.code === 200 && isDef(res.data) && Array.isArray(res.data.list || res.data.records);
122
+ const isValidResCode = (res, code = 200) => res.code === code;
123
+ const isValidRes = (res, code) => isValidResCode(res, code) && isDef(res.data);
124
+ const isValidArrRes = (res, code) => isValidResCode(res, code) && Array.isArray(res.data);
125
+ const isValidPageRes = (res, code) => isValidResCode(res, code) && isDef(res.data) && Array.isArray(res.data.list || res.data.records);
126
+ const isValidFileType = (file, type) => {
127
+ if (type === "*")
128
+ return true;
129
+ const fileType = file.type;
130
+ const fileName = file.name;
131
+ function validate(item) {
132
+ if (item.startsWith(".")) {
133
+ return fileName.endsWith(item);
134
+ }
135
+ if (item.endsWith("/*")) {
136
+ return fileType.startsWith(item.replace("/*", ""));
137
+ }
138
+ if (item.includes("/")) {
139
+ return fileType === item;
140
+ }
141
+ return false;
142
+ }
143
+ const typeArrStr = type.split(",").map((item) => item.trim());
144
+ if (typeArrStr.length > 1) {
145
+ return typeArrStr.some(validate);
146
+ }
147
+ return validate(type);
148
+ };
122
149
 
123
- const hidePhone = (str) => {
124
- if (str) {
125
- if (isPhone(str)) {
126
- return str.replace(/^(\d{3})\d{4}(\d{4})$/, "$1****$2");
150
+ const hideMiddle = (str) => {
151
+ if (!isDef(str))
152
+ return "";
153
+ const len = str.length;
154
+ if (len === 1)
155
+ return "*";
156
+ if (len === 2)
157
+ return "**";
158
+ return str[0] + "*".repeat(len - 2) + str[len - 1];
159
+ };
160
+ const hideThird = (str) => {
161
+ if (!isDef(str))
162
+ return "";
163
+ const len = str.length;
164
+ const partLen = Math.round(len / 3);
165
+ if (len === 1)
166
+ return "*";
167
+ if (len === 2)
168
+ return "**";
169
+ return str.slice(0, partLen) + "*".repeat(len - 2 * partLen) + str.slice(len - partLen);
170
+ };
171
+ const hideHead = (str) => {
172
+ if (!isDef(str))
173
+ return "";
174
+ const len = str.length;
175
+ if (len === 1)
176
+ return "*";
177
+ return "*".repeat(len - 1) + str[len - 1];
178
+ };
179
+ const hideTail = (str) => {
180
+ if (!isDef(str))
181
+ return "";
182
+ const len = str.length;
183
+ if (len === 1)
184
+ return "*";
185
+ return str[0] + "*".repeat(len - 1);
186
+ };
187
+ const hidePhone = (phone) => {
188
+ if (phone) {
189
+ if (isPhone(phone)) {
190
+ return phone.replace(/^(\d{3})\d{4}(\d{4})$/, "$1****$2");
127
191
  }
128
- if (isTel(str)) {
129
- return str.replace(/^(\d{3,4})-?\d{3,4}(\d{4})$/, "$1****$2");
192
+ if (isTel(phone)) {
193
+ return phone.replace(/^(\d{3,4})-?\d{3,4}(\d{4})$/, "$1****$2");
130
194
  }
131
- return str;
195
+ return phone;
132
196
  }
133
197
  return "";
134
198
  };
@@ -152,26 +216,25 @@ const hideName = (name) => {
152
216
  }
153
217
  return "";
154
218
  };
155
- const hideMiddle = (str) => {
156
- if (!isDef(str))
157
- return "";
158
- const len = str.length;
159
- if (len === 1)
160
- return "*";
161
- if (len === 2)
162
- return "**";
163
- return str[0] + "*".repeat(len - 2) + str[len - 1];
219
+ const hideSurname = (name) => {
220
+ return hideHead(name);
164
221
  };
165
- const hideThird = (str) => {
166
- if (!isDef(str))
167
- return "";
168
- const len = str.length;
169
- const partLen = Math.round(len / 3);
170
- if (len === 1)
171
- return "*";
172
- if (len === 2)
173
- return "**";
174
- return str.slice(0, partLen) + "*".repeat(len - 2 * partLen) + str.slice(len - partLen);
222
+ const hideFirstName = (name) => {
223
+ return hideTail(name);
224
+ };
225
+ const hideEmail = (email) => {
226
+ if (email) {
227
+ if (!email.includes("@"))
228
+ return "";
229
+ const arr = email.split("@");
230
+ const name = arr[0];
231
+ const domain = arr[1];
232
+ const len = name?.length;
233
+ if (!len)
234
+ return email;
235
+ return `${hideTail(name)}@${domain}`;
236
+ }
237
+ return "";
175
238
  };
176
239
 
177
240
  const inBrowser = typeof window !== "undefined";
@@ -282,35 +345,119 @@ const dateDuration = (start, end) => {
282
345
  return dd.format(format);
283
346
  };
284
347
 
348
+ let BASE_ATTACH_URL;
349
+ function setBaseAttachUrl(baseUrl) {
350
+ BASE_ATTACH_URL = baseUrl;
351
+ }
352
+ function getBaseAttachUrl() {
353
+ return BASE_ATTACH_URL;
354
+ }
355
+ if (undefined?.VITE_BASE_ATTACH_URL) {
356
+ setBaseAttachUrl(undefined.VITE_BASE_ATTACH_URL);
357
+ }
358
+
359
+ const toThousands = (num) => {
360
+ const arr = num.toString().split(".");
361
+ const integer = arr[0]?.replace(/(\d)(?=(?:\d{3})+$)/g, "$1,");
362
+ if (arr.length > 1)
363
+ return `${integer}.${arr[1]}`;
364
+ return integer;
365
+ };
366
+ const currencyFmt = (currency, fraction = 0) => {
367
+ if (!isDef(currency))
368
+ return "";
369
+ return new Intl.NumberFormat("zh-CN", { style: "currency", currency: "CNY", maximumFractionDigits: fraction }).format(
370
+ currency
371
+ );
372
+ };
373
+
374
+ const resUrl = (url) => {
375
+ if (!isDef(url))
376
+ return void 0;
377
+ if (/^https?:\/\//.test(url) || /^blob:/.test(url) || /^data:/.test(url)) {
378
+ return url;
379
+ }
380
+ const baseUrl = getBaseAttachUrl();
381
+ if (!isDef(baseUrl)) {
382
+ throw new Error("\u8BF7\u5148\u8BBE\u7F6E import.meta\u200B.env.VITE_BASE_ATTACH_URL");
383
+ }
384
+ return `${baseUrl}/${url}`.replace(/([^:]\/)\/+/g, "$1");
385
+ };
386
+ const recoverFile = (resource) => {
387
+ const { uri, url, ...item } = resource;
388
+ const validUrl = uri || url;
389
+ return {
390
+ ...item,
391
+ url: resUrl(validUrl),
392
+ content: resUrl(validUrl),
393
+ uri: validUrl || "",
394
+ status: "done",
395
+ percent: 100
396
+ };
397
+ };
398
+ const attachFmt = (data) => {
399
+ let attachList = [];
400
+ const prototype = Object.prototype.toString.call(data);
401
+ if (!data)
402
+ attachList = [];
403
+ else if (Array.isArray(data))
404
+ attachList = data.filter(Boolean);
405
+ else if (typeof data === "string") {
406
+ if (isArrStr(data)) {
407
+ attachList = (parseToJSON(data) || []).filter(Boolean);
408
+ } else {
409
+ const jsonData = parseToJSON(data);
410
+ const prototypeJson = Object.prototype.toString.call(jsonData);
411
+ if (prototypeJson === "[object Object]")
412
+ attachList = [jsonData].filter(Boolean);
413
+ else
414
+ attachList = [{ id: data, name: "name", uri: data, group: "default" }];
415
+ }
416
+ } else if (prototype === "[object Object]")
417
+ attachList = [data];
418
+ return attachList.map(recoverFile);
419
+ };
420
+
285
421
  exports.dayjs = dayjs__default;
422
+ exports.attachFmt = attachFmt;
286
423
  exports.camel2Kebab = camel2Kebab;
287
424
  exports.camel2Pascal = camel2Pascal;
288
425
  exports.camel2Snake = camel2Snake;
289
426
  exports.camel2Title = camel2Title;
290
427
  exports.checkImg = checkImg;
428
+ exports.currencyFmt = currencyFmt;
291
429
  exports.dateDuration = dateDuration;
292
430
  exports.dateFmt = dateFmt;
293
431
  exports.filterObj = filterObj;
294
432
  exports.fixiOSInputAutoZoomIn = fixiOSInputAutoZoomIn;
433
+ exports.getBaseAttachUrl = getBaseAttachUrl;
295
434
  exports.getHostEnv = getHostEnv;
296
435
  exports.hideCardNo = hideCardNo;
436
+ exports.hideEmail = hideEmail;
437
+ exports.hideFirstName = hideFirstName;
438
+ exports.hideHead = hideHead;
297
439
  exports.hideMiddle = hideMiddle;
298
440
  exports.hideName = hideName;
299
441
  exports.hidePhone = hidePhone;
442
+ exports.hideSurname = hideSurname;
443
+ exports.hideTail = hideTail;
300
444
  exports.hideThird = hideThird;
301
445
  exports.inBrowser = inBrowser;
302
446
  exports.isArrStr = isArrStr;
303
447
  exports.isDef = isDef;
304
448
  exports.isEmail = isEmail;
449
+ exports.isEmpty = isEmpty;
305
450
  exports.isIdCard = isIdCard;
306
451
  exports.isJSONStr = isJSONStr;
307
452
  exports.isNumeric = isNumeric;
308
453
  exports.isPhone = isPhone;
309
454
  exports.isTel = isTel;
310
455
  exports.isValidArrRes = isValidArrRes;
456
+ exports.isValidFileType = isValidFileType;
311
457
  exports.isValidKey = isValidKey;
312
458
  exports.isValidPageRes = isValidPageRes;
313
459
  exports.isValidRes = isValidRes;
460
+ exports.isValidResCode = isValidResCode;
314
461
  exports.jClear = jClear;
315
462
  exports.jDebug = jDebug;
316
463
  exports.jError = jError;
@@ -332,8 +479,11 @@ exports.pascal2Camel = pascal2Camel;
332
479
  exports.pascal2Kebab = pascal2Kebab;
333
480
  exports.pascal2Snake = pascal2Snake;
334
481
  exports.pascal2Title = pascal2Title;
482
+ exports.recoverFile = recoverFile;
335
483
  exports.replacer = replacer;
484
+ exports.resUrl = resUrl;
336
485
  exports.reviver = reviver;
486
+ exports.setBaseAttachUrl = setBaseAttachUrl;
337
487
  exports.snake2Camel = snake2Camel;
338
488
  exports.snake2Kebab = snake2Kebab;
339
489
  exports.snake2Pascal = snake2Pascal;
@@ -342,5 +492,6 @@ exports.title2Camel = title2Camel;
342
492
  exports.title2Kebab = title2Kebab;
343
493
  exports.title2Pascal = title2Pascal;
344
494
  exports.title2Snake = title2Snake;
495
+ exports.toThousands = toThousands;
345
496
  exports.userAgent = userAgent;
346
497
  exports.waitTime = waitTime;