sculp-js 1.8.4 → 1.9.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 (63) hide show
  1. package/lib/cjs/array.js +1 -1
  2. package/lib/cjs/async.js +1 -1
  3. package/lib/cjs/base64.js +1 -1
  4. package/lib/cjs/clipboard.js +7 -11
  5. package/lib/cjs/cloneDeep.js +1 -1
  6. package/lib/cjs/cookie.js +1 -1
  7. package/lib/cjs/date.js +1 -1
  8. package/lib/cjs/dom.js +1 -1
  9. package/lib/cjs/download.js +1 -1
  10. package/lib/cjs/easing.js +1 -1
  11. package/lib/cjs/file.js +1 -1
  12. package/lib/cjs/func.js +1 -1
  13. package/lib/cjs/index.js +3 -1
  14. package/lib/cjs/isEqual.js +1 -1
  15. package/lib/cjs/math.js +1 -1
  16. package/lib/cjs/number.js +49 -12
  17. package/lib/cjs/object.js +1 -1
  18. package/lib/cjs/path.js +1 -1
  19. package/lib/cjs/qs.js +1 -1
  20. package/lib/cjs/random.js +1 -1
  21. package/lib/cjs/string.js +1 -1
  22. package/lib/cjs/tooltip.js +1 -1
  23. package/lib/cjs/tree.js +2 -2
  24. package/lib/cjs/type.js +1 -1
  25. package/lib/cjs/unique.js +1 -1
  26. package/lib/cjs/url.js +15 -5
  27. package/lib/cjs/validator.js +1 -1
  28. package/lib/cjs/variable.js +1 -1
  29. package/lib/cjs/watermark.js +1 -1
  30. package/lib/cjs/we-decode.js +1 -1
  31. package/lib/es/array.js +1 -1
  32. package/lib/es/async.js +1 -1
  33. package/lib/es/base64.js +1 -1
  34. package/lib/es/clipboard.js +7 -11
  35. package/lib/es/cloneDeep.js +1 -1
  36. package/lib/es/cookie.js +1 -1
  37. package/lib/es/date.js +1 -1
  38. package/lib/es/dom.js +1 -1
  39. package/lib/es/download.js +1 -1
  40. package/lib/es/easing.js +1 -1
  41. package/lib/es/file.js +1 -1
  42. package/lib/es/func.js +1 -1
  43. package/lib/es/index.js +2 -2
  44. package/lib/es/isEqual.js +1 -1
  45. package/lib/es/math.js +1 -1
  46. package/lib/es/number.js +48 -13
  47. package/lib/es/object.js +1 -1
  48. package/lib/es/path.js +1 -1
  49. package/lib/es/qs.js +1 -1
  50. package/lib/es/random.js +1 -1
  51. package/lib/es/string.js +1 -1
  52. package/lib/es/tooltip.js +1 -1
  53. package/lib/es/tree.js +2 -2
  54. package/lib/es/type.js +1 -1
  55. package/lib/es/unique.js +1 -1
  56. package/lib/es/url.js +15 -5
  57. package/lib/es/validator.js +1 -1
  58. package/lib/es/variable.js +1 -1
  59. package/lib/es/watermark.js +1 -1
  60. package/lib/es/we-decode.js +1 -1
  61. package/lib/index.d.ts +32 -9
  62. package/lib/umd/index.js +561 -518
  63. package/package.json +1 -1
package/lib/es/number.js CHANGED
@@ -1,11 +1,12 @@
1
1
  /*!
2
- * sculp-js v1.8.4
2
+ * sculp-js v1.9.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
6
6
 
7
7
  import { getGlobal } from './func.js';
8
8
  import { STRING_ARABIC_NUMERALS, STRING_UPPERCASE_ALPHA, STRING_LOWERCASE_ALPHA } from './string.js';
9
+ import { isNullOrUnDef, isNumber } from './type.js';
9
10
 
10
11
  const HEX_POOL = `${STRING_ARABIC_NUMERALS}${STRING_UPPERCASE_ALPHA}${STRING_LOWERCASE_ALPHA}`;
11
12
  const supportBigInt = typeof BigInt !== 'undefined';
@@ -43,35 +44,69 @@ function numberToHex(decimal, hexPool = HEX_POOL) {
43
44
  return ret.join('');
44
45
  }
45
46
  /**
46
- * 缩写
47
+ * 将数字转换为携带单位的字符串
47
48
  * @param {number | string} num
48
49
  * @param {Array<string>} units
49
- * @param {number} ratio
50
- * @param {number} exponent
50
+ * @param {INumberAbbr} options default: { ratio: 1000, decimals: 0, separator: ' ' }
51
51
  * @returns {string}
52
52
  */
53
- const numberAbbr = (num, units, ratio = 1000, exponent) => {
53
+ const numberAbbr = (num, units, options = { ratio: 1000, decimals: 0, separator: ' ' }) => {
54
+ const { ratio = 1000, decimals = 0, separator = ' ' } = options;
54
55
  const { length } = units;
55
56
  if (length === 0)
56
- throw new Error('至少需要一个单位');
57
+ throw new Error('At least one unit is required');
57
58
  let num2 = Number(num);
58
59
  let times = 0;
59
60
  while (num2 >= ratio && times < length - 1) {
60
61
  num2 = num2 / ratio;
61
62
  times++;
62
63
  }
63
- const value = num2.toFixed(exponent);
64
+ const value = num2.toFixed(decimals);
64
65
  const unit = units[times];
65
- return value.toString() + '' + unit;
66
+ return String(value) + separator + unit;
66
67
  };
68
+ /**
69
+ * Converting file size in bytes to human-readable string
70
+ * reference: https://zh.wikipedia.org/wiki/%E5%8D%83%E5%AD%97%E8%8A%82
71
+ * @param {number | string} num bytes Number in Bytes
72
+ * @param {IHumanFileSizeOptions} options default: { decimals = 0, si = false, separator = ' ' }
73
+ * si: True to use metric (SI) units, aka powers of 1000, the units is
74
+ * ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'].
75
+ * False to use binary (IEC), aka powers of 1024, the units is
76
+ * ['Byte', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']
77
+ * @returns
78
+ */
79
+ function humanFileSize(num, options) {
80
+ const { decimals = 0, si = false, separator = ' ', maxUnit } = options;
81
+ const units = si
82
+ ? ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
83
+ : ['Byte', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
84
+ if (!isNullOrUnDef(maxUnit)) {
85
+ const targetIndex = units.findIndex(el => el === maxUnit);
86
+ if (targetIndex !== -1) {
87
+ units.splice(targetIndex + 1);
88
+ }
89
+ }
90
+ return numberAbbr(num, units, { ratio: si ? 1000 : 1024, decimals, separator });
91
+ }
67
92
  /**
68
93
  * 将数字格式化成千位分隔符显示的字符串
69
- * @param {number} val 数字
70
- * @param {'int' | 'float'} type 展示分段显示的类型 int:整型 | float:浮点型
94
+ * @param {number|string} num 数字
95
+ * @param {number} decimals 格式化成指定小数位精度的参数
71
96
  * @returns {string}
72
97
  */
73
- function formatNumber(val, type = 'int') {
74
- return type === 'int' ? parseInt(String(val)).toLocaleString() : Number(val).toLocaleString('en-US');
98
+ function formatNumber(num, decimals) {
99
+ if (isNullOrUnDef(decimals)) {
100
+ return parseInt(String(num)).toLocaleString();
101
+ }
102
+ let prec = 0;
103
+ if (!isNumber(decimals)) {
104
+ throw new Error('Decimals must be a positive number not less than zero');
105
+ }
106
+ else if (decimals > 0) {
107
+ prec = decimals;
108
+ }
109
+ return Number(Number(num).toFixed(prec)).toLocaleString('en-US');
75
110
  }
76
111
 
77
- export { HEX_POOL, formatNumber, numberAbbr, numberToHex };
112
+ export { HEX_POOL, formatNumber as formatMoney, formatNumber, humanFileSize, numberAbbr, numberToHex };
package/lib/es/object.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.8.4
2
+ * sculp-js v1.9.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/path.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.8.4
2
+ * sculp-js v1.9.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/qs.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.8.4
2
+ * sculp-js v1.9.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/random.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.8.4
2
+ * sculp-js v1.9.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/string.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.8.4
2
+ * sculp-js v1.9.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/tooltip.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.8.4
2
+ * sculp-js v1.9.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/tree.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.8.4
2
+ * sculp-js v1.9.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -210,7 +210,7 @@ function flatTree(treeList, options = defaultFieldOptions) {
210
210
  ...node,
211
211
  [childField]: [] // 清空子级
212
212
  };
213
- item.hasOwnProperty([childField]) && delete item[childField];
213
+ objectHas(item, childField) && delete item[childField];
214
214
  res.push(item);
215
215
  if (node[childField]) {
216
216
  const children = node[childField].map(item => ({
package/lib/es/type.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.8.4
2
+ * sculp-js v1.9.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/unique.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.8.4
2
+ * sculp-js v1.9.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/url.js CHANGED
@@ -1,27 +1,37 @@
1
1
  /*!
2
- * sculp-js v1.8.4
2
+ * sculp-js v1.9.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
6
6
 
7
+ import { isFunction } from './type.js';
7
8
  import { pathJoin } from './path.js';
8
9
  import { qsParse, qsStringify } from './qs.js';
9
10
 
10
- const anchorEl = document.createElement('a');
11
11
  /**
12
12
  * url 解析
13
13
  * @param {string} url
14
+ * @param {boolean} isModernApi 使用现代API:URL, 默认true (对无效url解析会抛错), 否则使用a标签来解析(兼容性更强)
14
15
  * @returns {Url}
15
16
  */
16
- const urlParse = (url) => {
17
- anchorEl.href = url;
18
- const { protocol, username, password, host, port, hostname, hash, search, pathname: _pathname } = anchorEl;
17
+ const urlParse = (url, isModernApi = true) => {
18
+ // @ts-ignore
19
+ let urlObj = null;
20
+ if (isFunction(URL) && isModernApi) {
21
+ urlObj = new URL(url);
22
+ }
23
+ else {
24
+ urlObj = document.createElement('a');
25
+ urlObj.href = url;
26
+ }
27
+ const { protocol, username, password, host, port, hostname, hash, search, pathname: _pathname } = urlObj;
19
28
  // fix: ie 浏览器下,解析出来的 pathname 是没有 / 根的
20
29
  const pathname = pathJoin('/', _pathname);
21
30
  const auth = username && password ? `${username}:${password}` : '';
22
31
  const query = search.replace(/^\?/, '');
23
32
  const searchParams = qsParse(query);
24
33
  const path = `${pathname}${search}`;
34
+ urlObj = null;
25
35
  return {
26
36
  protocol,
27
37
  auth,
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.8.4
2
+ * sculp-js v1.9.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.8.4
2
+ * sculp-js v1.9.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.8.4
2
+ * sculp-js v1.9.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.8.4
2
+ * sculp-js v1.9.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/index.d.ts CHANGED
@@ -526,9 +526,10 @@ interface Url {
526
526
  /**
527
527
  * url 解析
528
528
  * @param {string} url
529
+ * @param {boolean} isModernApi 使用现代API:URL, 默认true (对无效url解析会抛错), 否则使用a标签来解析(兼容性更强)
529
530
  * @returns {Url}
530
531
  */
531
- declare const urlParse: (url: string) => Url;
532
+ declare const urlParse: (url: string, isModernApi?: boolean) => Url;
532
533
  /**
533
534
  * url 字符化,url 对象里的 searchParams 会覆盖 url 原有的查询参数
534
535
  * @param {Url} url
@@ -697,22 +698,44 @@ declare const HEX_POOL: string;
697
698
  * @returns {string}
698
699
  */
699
700
  declare function numberToHex(decimal: number | string, hexPool?: string): string;
701
+ interface INumberAbbr {
702
+ ratio?: number;
703
+ decimals?: number;
704
+ separator?: string;
705
+ }
700
706
  /**
701
- * 缩写
707
+ * 将数字转换为携带单位的字符串
702
708
  * @param {number | string} num
703
709
  * @param {Array<string>} units
704
- * @param {number} ratio
705
- * @param {number} exponent
710
+ * @param {INumberAbbr} options default: { ratio: 1000, decimals: 0, separator: ' ' }
706
711
  * @returns {string}
707
712
  */
708
- declare const numberAbbr: (num: number | string, units: Array<string>, ratio?: number, exponent?: number) => string;
713
+ declare const numberAbbr: (num: number | string, units: Array<string>, options?: INumberAbbr) => string;
714
+ interface IHumanFileSizeOptions {
715
+ decimals?: number;
716
+ si?: boolean;
717
+ separator?: string;
718
+ maxUnit?: string;
719
+ }
720
+ /**
721
+ * Converting file size in bytes to human-readable string
722
+ * reference: https://zh.wikipedia.org/wiki/%E5%8D%83%E5%AD%97%E8%8A%82
723
+ * @param {number | string} num bytes Number in Bytes
724
+ * @param {IHumanFileSizeOptions} options default: { decimals = 0, si = false, separator = ' ' }
725
+ * si: True to use metric (SI) units, aka powers of 1000, the units is
726
+ * ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'].
727
+ * False to use binary (IEC), aka powers of 1024, the units is
728
+ * ['Byte', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']
729
+ * @returns
730
+ */
731
+ declare function humanFileSize(num: number | string, options: IHumanFileSizeOptions): string;
709
732
  /**
710
733
  * 将数字格式化成千位分隔符显示的字符串
711
- * @param {number} val 数字
712
- * @param {'int' | 'float'} type 展示分段显示的类型 int:整型 | float:浮点型
734
+ * @param {number|string} num 数字
735
+ * @param {number} decimals 格式化成指定小数位精度的参数
713
736
  * @returns {string}
714
737
  */
715
- declare function formatNumber(val: number, type?: string): string;
738
+ declare function formatNumber(num: number | string, decimals?: number): string;
716
739
 
717
740
  declare const UNIQUE_NUMBER_SAFE_LENGTH = 18;
718
741
  /**
@@ -1066,4 +1089,4 @@ type Comparable = null | undefined | boolean | number | string | Date | RegExp |
1066
1089
  */
1067
1090
  declare function isEqual(a: Comparable, b: Comparable): boolean;
1068
1091
 
1069
- export { type AnyArray, type AnyFunc, type AnyObject, type ArrayElements, type Comparable, type DateObj, type DateValue, type DebounceFunc, EMAIL_REGEX, type FileType, HEX_POOL, HTTP_URL_REGEX, type ICanvasWM, type ICompressOptions, type IFieldOptions, type IFilterCondition, IPV4_REGEX, IPV6_REGEX, type ISearchTreeOpts, type ITreeConf, type IdLike, type LooseParamValue, type LooseParams, type ObjectAssignItem, type OnceFunc, PHONE_REGEX, type Params, type PartialDeep, type RandomString, type Replacer, STRING_ARABIC_NUMERALS, STRING_LOWERCASE_ALPHA, STRING_POOL, STRING_UPPERCASE_ALPHA, type SetStyle, type SmoothScrollOptions, type Style, type ThrottleFunc, UNIQUE_NUMBER_SAFE_LENGTH, URL_REGEX, type UniqueString, type Url, add, addClass, arrayEach, arrayEachAsync, arrayInsertBefore, arrayLike, arrayRemove, asyncMap, calculateDate, calculateDateTime, chooseLocalFile, cloneDeep, compressImg, cookieDel, cookieGet, cookieSet, copyText, crossOriginDownload, dateParse, dateToEnd, dateToStart, debounce, decodeFromBase64, divide, downloadBlob, downloadData, downloadHref, downloadURL, encodeToBase64, escapeRegExp, executeInScope, flatTree, forEachDeep, formatDate, formatNumber, formatTree, fuzzySearchTree, genCanvasWM, getComputedCssVal, getGlobal, getStrWidthPx, getStyle, hasClass, isArray, isBigInt, isBoolean, isDate, isDigit, isEmail, isEmpty, isEqual, isError, isFloat, isFunction, isIdNo, isInteger, isIpV4, isIpV6, isJsonString, isNaN, isNull, isNullOrUnDef, isNullOrUnDef as isNullish, isNumber, isNumerical, isObject, isPhone, isPlainObject, isPrimitive, isRegExp, isString, isSymbol, isUndefined, isUrl, isValidDate, mapDeep, multiply, numberAbbr, numberToHex, objectAssign, objectEach, objectEachAsync, objectFill, objectGet, objectHas, objectMap, objectAssign as objectMerge, objectOmit, objectPick, once, parseQueryParams, parseVarFromString, pathJoin, pathNormalize, qsParse, qsStringify, randomNumber, randomString, randomUuid, removeClass, replaceVarFromString, searchTreeById, setGlobal, setStyle, smoothScroll, stringAssign, stringCamelCase, stringEscapeHtml, stringFill, stringFormat, stringKebabCase, strip, subtract, supportCanvas, throttle, tooltipEvent, typeIs, uniqueNumber, uniqueString, uniqueSymbol, urlDelParams, urlParse, urlSetParams, urlStringify, wait, weAtob, weBtoa };
1092
+ export { type AnyArray, type AnyFunc, type AnyObject, type ArrayElements, type Comparable, type DateObj, type DateValue, type DebounceFunc, EMAIL_REGEX, type FileType, HEX_POOL, HTTP_URL_REGEX, type ICanvasWM, type ICompressOptions, type IFieldOptions, type IFilterCondition, IPV4_REGEX, IPV6_REGEX, type ISearchTreeOpts, type ITreeConf, type IdLike, type LooseParamValue, type LooseParams, type ObjectAssignItem, type OnceFunc, PHONE_REGEX, type Params, type PartialDeep, type RandomString, type Replacer, STRING_ARABIC_NUMERALS, STRING_LOWERCASE_ALPHA, STRING_POOL, STRING_UPPERCASE_ALPHA, type SetStyle, type SmoothScrollOptions, type Style, type ThrottleFunc, UNIQUE_NUMBER_SAFE_LENGTH, URL_REGEX, type UniqueString, type Url, add, addClass, arrayEach, arrayEachAsync, arrayInsertBefore, arrayLike, arrayRemove, asyncMap, calculateDate, calculateDateTime, chooseLocalFile, cloneDeep, compressImg, cookieDel, cookieGet, cookieSet, copyText, crossOriginDownload, dateParse, dateToEnd, dateToStart, debounce, decodeFromBase64, divide, downloadBlob, downloadData, downloadHref, downloadURL, encodeToBase64, escapeRegExp, executeInScope, flatTree, forEachDeep, formatDate, formatNumber as formatMoney, formatNumber, formatTree, fuzzySearchTree, genCanvasWM, getComputedCssVal, getGlobal, getStrWidthPx, getStyle, hasClass, humanFileSize, isArray, isBigInt, isBoolean, isDate, isDigit, isEmail, isEmpty, isEqual, isError, isFloat, isFunction, isIdNo, isInteger, isIpV4, isIpV6, isJsonString, isNaN, isNull, isNullOrUnDef, isNullOrUnDef as isNullish, isNumber, isNumerical, isObject, isPhone, isPlainObject, isPrimitive, isRegExp, isString, isSymbol, isUndefined, isUrl, isValidDate, mapDeep, multiply, numberAbbr, numberToHex, objectAssign, objectEach, objectEachAsync, objectFill, objectGet, objectHas, objectMap, objectAssign as objectMerge, objectOmit, objectPick, once, parseQueryParams, parseVarFromString, pathJoin, pathNormalize, qsParse, qsStringify, randomNumber, randomString, randomUuid, removeClass, replaceVarFromString, searchTreeById, setGlobal, setStyle, smoothScroll, stringAssign, stringCamelCase, stringEscapeHtml, stringFill, stringFormat, stringKebabCase, strip, subtract, supportCanvas, throttle, tooltipEvent, typeIs, uniqueNumber, uniqueString, uniqueSymbol, urlDelParams, urlParse, urlSetParams, urlStringify, wait, weAtob, weBtoa };