sculp-js 1.4.0 → 1.5.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.
Files changed (52) hide show
  1. package/README.md +12 -1
  2. package/lib/cjs/array.js +1 -1
  3. package/lib/cjs/async.js +1 -1
  4. package/lib/cjs/clipboard.js +1 -1
  5. package/lib/cjs/cookie.js +1 -1
  6. package/lib/cjs/date.js +1 -1
  7. package/lib/cjs/dom.js +32 -1
  8. package/lib/cjs/download.js +35 -5
  9. package/lib/cjs/easing.js +1 -1
  10. package/lib/cjs/file.js +1 -1
  11. package/lib/cjs/func.js +1 -1
  12. package/lib/cjs/index.js +5 -3
  13. package/lib/cjs/number.js +1 -1
  14. package/lib/cjs/object.js +1 -1
  15. package/lib/cjs/path.js +1 -1
  16. package/lib/cjs/qs.js +1 -1
  17. package/lib/cjs/random.js +1 -1
  18. package/lib/cjs/string.js +17 -29
  19. package/lib/cjs/tooltip.js +5 -5
  20. package/lib/cjs/tree.js +1 -1
  21. package/lib/cjs/type.js +16 -1
  22. package/lib/cjs/unique.js +1 -1
  23. package/lib/cjs/url.js +1 -1
  24. package/lib/cjs/watermark.js +1 -1
  25. package/lib/cjs/we-decode.js +44 -45
  26. package/lib/es/array.js +1 -1
  27. package/lib/es/async.js +1 -1
  28. package/lib/es/clipboard.js +1 -1
  29. package/lib/es/cookie.js +1 -1
  30. package/lib/es/date.js +1 -1
  31. package/lib/es/dom.js +33 -3
  32. package/lib/es/download.js +35 -6
  33. package/lib/es/easing.js +1 -1
  34. package/lib/es/file.js +1 -1
  35. package/lib/es/func.js +1 -1
  36. package/lib/es/index.js +6 -6
  37. package/lib/es/number.js +1 -1
  38. package/lib/es/object.js +1 -1
  39. package/lib/es/path.js +1 -1
  40. package/lib/es/qs.js +1 -1
  41. package/lib/es/random.js +1 -1
  42. package/lib/es/string.js +17 -29
  43. package/lib/es/tooltip.js +2 -2
  44. package/lib/es/tree.js +1 -1
  45. package/lib/es/type.js +16 -2
  46. package/lib/es/unique.js +1 -1
  47. package/lib/es/url.js +1 -1
  48. package/lib/es/watermark.js +1 -1
  49. package/lib/es/we-decode.js +45 -45
  50. package/lib/index.d.ts +30 -10
  51. package/lib/umd/index.js +138 -74
  52. package/package.json +1 -1
package/lib/umd/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.4.0
2
+ * sculp-js v1.4.1
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -42,6 +42,20 @@
42
42
  const isDate = (any) => typeIs(any) === 'Date';
43
43
  const isError = (any) => typeIs(any) === 'Error';
44
44
  const isRegExp = (any) => typeIs(any) === 'RegExp';
45
+ /**
46
+ * 判断一个字符串是否为有效的 JSON, 若有效则返回有效的JSON对象,否则false
47
+ * @param {string} str
48
+ * @return {Object | boolean}
49
+ */
50
+ function isJsonString(str) {
51
+ try {
52
+ const parsed = JSON.parse(str);
53
+ return typeof parsed === 'object' && parsed !== null ? parsed : false;
54
+ }
55
+ catch (e) {
56
+ return false;
57
+ }
58
+ }
45
59
 
46
60
  /**
47
61
  * 判断对象是否为纯对象
@@ -510,34 +524,24 @@
510
524
  */
511
525
  const stringFill = (length, value = ' ') => new Array(length).fill(value).join('');
512
526
  /**
513
- * 字符串的像素宽度
514
- * @param {string} str 目标字符串
515
- * @param {number} fontSize 字符串字体大小
516
- * @param {boolean} isRemoveDom 计算后是否移除中间dom元素
517
- * @returns {*}
527
+ * 解析URL查询参数
528
+ * @param {string} searchStr
529
+ * @return {Record<string, string | string[]>}
518
530
  */
519
- function getStrWidthPx(str, fontSize = 14, isRemoveDom = false) {
520
- let strWidth = 0;
521
- console.assert(isString(str), `${str} 不是有效的字符串`);
522
- if (isString(str) && str.length > 0) {
523
- let getEle = document.querySelector('#getStrWidth1494304949567');
524
- if (!getEle) {
525
- const _ele = document.createElement('span');
526
- _ele.id = 'getStrWidth1494304949567';
527
- _ele.style.fontSize = fontSize + 'px';
528
- _ele.style.whiteSpace = 'nowrap';
529
- _ele.style.visibility = 'hidden';
530
- _ele.textContent = str;
531
- document.body.appendChild(_ele);
532
- getEle = _ele;
531
+ function parseQueryParams(searchStr = location.search) {
532
+ const queryObj = {};
533
+ Array.from(searchStr.matchAll(/[&?]?([^=&]+)=?([^=&]*)/g)).forEach((item, i) => {
534
+ if (!queryObj[item[1]]) {
535
+ queryObj[item[1]] = item[2];
533
536
  }
534
- getEle.textContent = str;
535
- strWidth = getEle.offsetWidth;
536
- if (isRemoveDom) {
537
- document.body.appendChild(getEle);
537
+ else if (typeof queryObj[item[1]] === 'string') {
538
+ queryObj[item[1]] = [queryObj[item[1]], item[2]];
538
539
  }
539
- }
540
- return strWidth;
540
+ else {
541
+ queryObj[item[1]].push(item[2]);
542
+ }
543
+ });
544
+ return queryObj;
541
545
  }
542
546
 
543
547
  /**
@@ -682,6 +686,36 @@
682
686
  const originVal = getComputedStyle(el).getPropertyValue(property) ?? '';
683
687
  return reNumber ? Number(originVal.replace(/([0-9]*)(.*)/g, '$1')) : originVal;
684
688
  }
689
+ /**
690
+ * 字符串的像素宽度
691
+ * @param {string} str 目标字符串
692
+ * @param {number} fontSize 字符串字体大小
693
+ * @param {boolean} isRemoveDom 计算后是否移除中间dom元素
694
+ * @returns {*}
695
+ */
696
+ function getStrWidthPx(str, fontSize = 14, isRemoveDom = false) {
697
+ let strWidth = 0;
698
+ console.assert(isString(str), `${str} 不是有效的字符串`);
699
+ if (isString(str) && str.length > 0) {
700
+ let getEle = document.querySelector('#getStrWidth1494304949567');
701
+ if (!getEle) {
702
+ const _ele = document.createElement('span');
703
+ _ele.id = 'getStrWidth1494304949567';
704
+ _ele.style.fontSize = fontSize + 'px';
705
+ _ele.style.whiteSpace = 'nowrap';
706
+ _ele.style.visibility = 'hidden';
707
+ _ele.textContent = str;
708
+ document.body.appendChild(_ele);
709
+ getEle = _ele;
710
+ }
711
+ getEle.textContent = str;
712
+ strWidth = getEle.offsetWidth;
713
+ if (isRemoveDom) {
714
+ document.body.appendChild(getEle);
715
+ }
716
+ }
717
+ return strWidth;
718
+ }
685
719
 
686
720
  const textEl = document.createElement('textarea');
687
721
  setStyle(textEl, {
@@ -1154,25 +1188,53 @@
1154
1188
  * 通过 A 链接的方式下载
1155
1189
  * @param {string} href
1156
1190
  * @param {string} filename
1191
+ * @param {Function} callback
1157
1192
  */
1158
- function downloadHref(href, filename) {
1193
+ function downloadHref(href, filename, callback) {
1159
1194
  const eleLink = document.createElement('a');
1160
1195
  eleLink.download = filename;
1161
1196
  eleLink.style.display = 'none';
1162
1197
  eleLink.href = href;
1163
1198
  document.body.appendChild(eleLink);
1164
1199
  eleLink.click();
1165
- setTimeout(() => document.body.removeChild(eleLink));
1200
+ setTimeout(() => {
1201
+ document.body.removeChild(eleLink);
1202
+ if (isFunction(callback)) {
1203
+ callback();
1204
+ }
1205
+ });
1166
1206
  }
1167
1207
  /**
1168
1208
  * 将大文件对象通过 A 链接的方式下载
1169
1209
  * @param {Blob} blob
1170
1210
  * @param {string} filename
1211
+ * @param {Function} callback
1171
1212
  */
1172
- function downloadBlob(blob, filename) {
1213
+ function downloadBlob(blob, filename, callback) {
1173
1214
  const objURL = URL.createObjectURL(blob);
1174
1215
  downloadHref(objURL, filename);
1175
- setTimeout(() => URL.revokeObjectURL(objURL));
1216
+ setTimeout(() => {
1217
+ URL.revokeObjectURL(objURL);
1218
+ if (isFunction(callback)) {
1219
+ callback();
1220
+ }
1221
+ });
1222
+ }
1223
+ /**
1224
+ * 根据URL下载文件(解决跨域a.download不生效问题)
1225
+ * @param {string} url
1226
+ * @param {string} filename
1227
+ * @param {Function} callback
1228
+ */
1229
+ function crossDomainDownload(url, filename, callback) {
1230
+ const xhr = new XMLHttpRequest();
1231
+ xhr.open('GET', url, true);
1232
+ xhr.responseType = 'blob';
1233
+ xhr.onload = function () {
1234
+ if (xhr.status === 200)
1235
+ downloadBlob(xhr.response, filename, callback);
1236
+ };
1237
+ xhr.send();
1176
1238
  }
1177
1239
  /**
1178
1240
  * 将指定数据格式通过 A 链接的方式下载
@@ -1315,49 +1377,49 @@
1315
1377
  }
1316
1378
  return result;
1317
1379
  }
1318
- function b64DecodeUnicode(str) {
1319
- return decodeURIComponent(exports.weAtob(str).replace(/(.)/g, function (p) {
1320
- let code = p.charCodeAt(0).toString(16).toUpperCase();
1321
- if (code.length < 2) {
1322
- code = '0' + code;
1323
- }
1324
- return '%' + code;
1325
- }));
1326
- }
1327
- function base64_url_decode(str) {
1328
- let output = str.replace(/-/g, '+').replace(/_/g, '/');
1329
- switch (output.length % 4) {
1330
- case 0:
1331
- break;
1332
- case 2:
1333
- output += '==';
1334
- break;
1335
- case 3:
1336
- output += '=';
1337
- break;
1338
- default:
1339
- throw new Error('Illegal base64url string!');
1340
- }
1341
- try {
1342
- return b64DecodeUnicode(output);
1343
- }
1344
- catch (err) {
1345
- return exports.weAtob(output);
1346
- }
1347
- }
1348
- function weAppJwtDecode(token, options) {
1349
- if (typeof token !== 'string') {
1350
- throw new Error('Invalid token specified');
1351
- }
1352
- options = options || {};
1353
- const pos = options.header === true ? 0 : 1;
1354
- try {
1355
- return JSON.parse(base64_url_decode(token.split('.')[pos]));
1356
- }
1357
- catch (e) {
1358
- throw new Error('Invalid token specified: ' + e.message);
1359
- }
1360
- }
1380
+ // function b64DecodeUnicode(str) {
1381
+ // return decodeURIComponent(
1382
+ // exports.weAtob(str).replace(/(.)/g, function (p) {
1383
+ // let code = p.charCodeAt(0).toString(16).toUpperCase();
1384
+ // if (code.length < 2) {
1385
+ // code = '0' + code;
1386
+ // }
1387
+ // return '%' + code;
1388
+ // })
1389
+ // );
1390
+ // }
1391
+ // function base64_url_decode(str) {
1392
+ // let output = str.replace(/-/g, '+').replace(/_/g, '/');
1393
+ // switch (output.length % 4) {
1394
+ // case 0:
1395
+ // break;
1396
+ // case 2:
1397
+ // output += '==';
1398
+ // break;
1399
+ // case 3:
1400
+ // output += '=';
1401
+ // break;
1402
+ // default:
1403
+ // throw new Error('Illegal base64url string!');
1404
+ // }
1405
+ // try {
1406
+ // return b64DecodeUnicode(output);
1407
+ // } catch (err) {
1408
+ // return exports.weAtob(output);
1409
+ // }
1410
+ // }
1411
+ // export function weAppJwtDecode(token, options) {
1412
+ // if (typeof token !== 'string') {
1413
+ // throw new Error('Invalid token specified');
1414
+ // }
1415
+ // options = options || {};
1416
+ // const pos = options.header === true ? 0 : 1;
1417
+ // try {
1418
+ // return JSON.parse(base64_url_decode(token.split('.')[pos]));
1419
+ // } catch (e) {
1420
+ // throw new Error('Invalid token specified: ' + (e as Error).message);
1421
+ // }
1422
+ // }
1361
1423
 
1362
1424
  /**
1363
1425
  * 判断是否支持canvas
@@ -2394,6 +2456,7 @@
2394
2456
  exports.cookieGet = cookieGet;
2395
2457
  exports.cookieSet = cookieSet;
2396
2458
  exports.copyText = copyText;
2459
+ exports.crossDomainDownload = crossDomainDownload;
2397
2460
  exports.dateParse = dateParse;
2398
2461
  exports.dateToEnd = dateToEnd;
2399
2462
  exports.dateToStart = dateToStart;
@@ -2422,6 +2485,7 @@
2422
2485
  exports.isDomReady = isDomReady;
2423
2486
  exports.isError = isError;
2424
2487
  exports.isFunction = isFunction;
2488
+ exports.isJsonString = isJsonString;
2425
2489
  exports.isNaN = isNaN;
2426
2490
  exports.isNull = isNull;
2427
2491
  exports.isNullOrUnDef = isNullOrUnDef;
@@ -2448,6 +2512,7 @@
2448
2512
  exports.objectPick = objectPick;
2449
2513
  exports.onDomReady = onDomReady;
2450
2514
  exports.once = once;
2515
+ exports.parseQueryParams = parseQueryParams;
2451
2516
  exports.pathJoin = pathJoin;
2452
2517
  exports.pathNormalize = pathNormalize;
2453
2518
  exports.qsParse = qsParse;
@@ -2477,7 +2542,6 @@
2477
2542
  exports.urlSetParams = urlSetParams;
2478
2543
  exports.urlStringify = urlStringify;
2479
2544
  exports.wait = wait;
2480
- exports.weAppJwtDecode = weAppJwtDecode;
2481
2545
  exports.weAtob = weAtob;
2482
2546
  exports.weBtoa = weBtoa;
2483
2547
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sculp-js",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "packageManager": "npm@8.19.2",
5
5
  "description": "js工具库",
6
6
  "scripts": {