sculp-js 0.0.1 → 0.1.1

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.md CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2020-present, [chandq](https://github.com/chandq)
3
+ Copyright (c) 2020-present, chandq
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,3 +1,21 @@
1
+ [![Node.js CI](https://github.com/chandq/sculp-js/actions/workflows/node.js.yml/badge.svg)](https://github.com/chandq/sculp-js/actions/workflows/node.js.yml)
2
+ [![sculp-js](https://img.shields.io/github/package-json/v/chandq/sculp-js?style=flat-square)](https://github.com/chandq/sculp-js)
3
+ [![node](https://img.shields.io/badge/node-v12.0.0-blue)](https://nodejs.org/download/release/v12.0.0/)
4
+ [![node](https://img.shields.io/badge/language-typescript-orange.svg)](https://nodejs.org/download/release/v12.0.0/)
5
+ [![license:MIT](https://img.shields.io/npm/l/vue.svg?sanitize=true)](https://github.com/chandq/sculp-js/blob/main/LICENSE.md)
6
+ [![Downloads:?](https://img.shields.io/npm/dm/sculp-js.svg?sanitize=true)](https://npmcharts.com/compare/sculp-js?minimal=true)
7
+
1
8
  # sculp-js
2
9
 
3
- > js 工具库
10
+ > TS + Rollup, 原生实现,不依赖任何第三方库,输出 esm、cjs、umd三种模块方式的产物
11
+
12
+ js 工具函数库, 包含类型判断模块:type, 数据处理模块:`array`、`object`、`string`、`number`,功能性模块:下载`download`、复制`clipboard`、`cookie`、日期`date`、qs、水印`watermark`, 文件处理模块:`file`,自定义悬浮提示模块: `tooltip`, dom处理模块:`dom`;
13
+
14
+ - Array
15
+ - arrayLike 判断类数组
16
+ - arrayEach 可中断的数组遍历, 支持倒序
17
+ - arrayEachAsync 异步遍历数组,可中断,支持倒序
18
+ - arrayInsertBefore 改变数组元素位置
19
+ - arrayRemove 数组删除指定元素
20
+ - deepTraversal 深度优先遍历函数, 支持continue、break,可定制id、children
21
+ - getTreeIds 在树中找到 id 为某个值的节点,并返回上游的所有父级节点
package/lib/cjs/array.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v0.0.1
2
+ * sculp-js v0.1.0
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -33,14 +33,14 @@ const arrayEach = (array, iterator, reverse = false) => {
33
33
  if (reverse) {
34
34
  for (let idx = array.length - 1; idx >= 0; idx--) {
35
35
  const val = array[idx];
36
- if (iterator(val, idx) === false)
36
+ if (iterator(val, idx, array) === false)
37
37
  break;
38
38
  }
39
39
  }
40
40
  else {
41
41
  for (let idx = 0; idx < array.length; idx++) {
42
42
  const val = array[idx];
43
- if (iterator(val, idx) === false)
43
+ if (iterator(val, idx, array) === false)
44
44
  break;
45
45
  }
46
46
  }
@@ -172,46 +172,17 @@ function getTreeIds(tree, nodeId, config) {
172
172
  while (child && child.parentId) {
173
173
  ids = [child.parentId, ...ids];
174
174
  nodes = [child.parent, ...nodes];
175
- child = flatArray.find(_ => _[id] === child.parentId);
175
+ child = flatArray.find(_ => _[id] === child.parentId); // eslint-disable-line
176
176
  }
177
177
  return [ids, nodes];
178
178
  };
179
179
  return getIds(toFlatArray(tree));
180
180
  }
181
- /**
182
- * 异步ForEach函数
183
- * @param {array} array
184
- * @param {asyncFuntion} callback
185
- * // asyncForEach 使用范例如下
186
- // const start = async () => {
187
- // await asyncForEach(result, async (item) => {
188
- // await request(item);
189
- // count++;
190
- // });
191
-
192
- // console.log('发送次数', count);
193
- // }
194
-
195
- // for await...of 使用范例如下
196
- // const loadImages = async (images) => {
197
- // for await (const item of images) {
198
- // await request(item);
199
- // count++;
200
- // }
201
- // }
202
- * @return {*}
203
- */
204
- async function asyncForEach(array, callback) {
205
- for (let index = 0, len = array.length; index < len; index++) {
206
- await callback(array[index], index, array);
207
- }
208
- }
209
181
 
210
182
  exports.arrayEach = arrayEach;
211
183
  exports.arrayEachAsync = arrayEachAsync;
212
184
  exports.arrayInsertBefore = arrayInsertBefore;
213
185
  exports.arrayLike = arrayLike;
214
186
  exports.arrayRemove = arrayRemove;
215
- exports.asyncForEach = asyncForEach;
216
187
  exports.deepTraversal = deepTraversal;
217
188
  exports.getTreeIds = getTreeIds;
package/lib/cjs/async.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v0.0.1
2
+ * sculp-js v0.1.0
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v0.0.1
2
+ * sculp-js v0.1.0
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/cookie.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v0.0.1
2
+ * sculp-js v0.1.0
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/date.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v0.0.1
2
+ * sculp-js v0.1.0
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -56,16 +56,16 @@ const formatDate = (date = new Date(), format = 'YYYY-MM-DD HH:mm:ss') => {
56
56
  */
57
57
  function calculateDate(strDate, n, sep = '-') {
58
58
  //strDate 为字符串日期 如:'2019-01-01' n为你要传入的参数,当前为0,前一天为-1,后一天为1
59
- let dateArr = strDate.split(sep); //这边给定一个特定时间
60
- let newDate = new Date(+dateArr[0], +dateArr[1] - 1, +dateArr[2]);
61
- let befminuts = newDate.getTime() + 1000 * 60 * 60 * 24 * parseInt(String(n)); //计算前几天用减,计算后几天用加,最后一个就是多少天的数量
62
- let beforeDat = new Date();
59
+ const dateArr = strDate.split(sep); //这边给定一个特定时间
60
+ const newDate = new Date(+dateArr[0], +dateArr[1] - 1, +dateArr[2]);
61
+ const befminuts = newDate.getTime() + 1000 * 60 * 60 * 24 * parseInt(String(n)); //计算前几天用减,计算后几天用加,最后一个就是多少天的数量
62
+ const beforeDat = new Date();
63
63
  beforeDat.setTime(befminuts);
64
- let befMonth = beforeDat.getMonth() + 1;
65
- let mon = befMonth >= 10 ? befMonth : '0' + befMonth;
66
- let befDate = beforeDat.getDate();
67
- let da = befDate >= 10 ? befDate : '0' + befDate;
68
- let finalNewDate = beforeDat.getFullYear() + '-' + mon + '-' + da;
64
+ const befMonth = beforeDat.getMonth() + 1;
65
+ const mon = befMonth >= 10 ? befMonth : '0' + befMonth;
66
+ const befDate = beforeDat.getDate();
67
+ const da = befDate >= 10 ? befDate : '0' + befDate;
68
+ const finalNewDate = beforeDat.getFullYear() + '-' + mon + '-' + da;
69
69
  return finalNewDate;
70
70
  }
71
71
  /**
@@ -76,9 +76,9 @@ function calculateDate(strDate, n, sep = '-') {
76
76
  * @return {*}
77
77
  */
78
78
  function calculateDateTime(n, dateSep = '-', timeSep = ':') {
79
- let date = new Date();
80
- let separator1 = '-';
81
- let separator2 = ':';
79
+ const date = new Date();
80
+ const separator1 = '-';
81
+ const separator2 = ':';
82
82
  let year = date.getFullYear();
83
83
  let month = date.getMonth() + 1;
84
84
  let strDate = date.getDate() + n;
package/lib/cjs/dom.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v0.0.1
2
+ * sculp-js v0.1.0
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -141,8 +141,20 @@ function onDomReady(callback) {
141
141
  domReadyCallbacks.push(callback);
142
142
  }
143
143
  }
144
+ /**
145
+ * 获取元素样式属性的计算值
146
+ * @param {HTMLElement} el
147
+ * @param {string} property
148
+ * @param {boolean} reNumber
149
+ * @return {string|number}
150
+ */
151
+ function getComputedCssVal(el, property, reNumber = true) {
152
+ const originVal = getComputedStyle(el).getPropertyValue(property) ?? '';
153
+ return reNumber ? Number(originVal.replace(/([0-9]*)(.*)/g, '$1')) : originVal;
154
+ }
144
155
 
145
156
  exports.addClass = addClass;
157
+ exports.getComputedCssVal = getComputedCssVal;
146
158
  exports.getStyle = getStyle;
147
159
  exports.hasClass = hasClass;
148
160
  exports.isDomReady = isDomReady;
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v0.0.1
2
+ * sculp-js v0.1.0
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/easing.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v0.0.1
2
+ * sculp-js v0.1.0
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/file.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v0.0.1
2
+ * sculp-js v0.1.0
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -0,0 +1,160 @@
1
+ /*!
2
+ * sculp-js v0.1.0
3
+ * (c) 2023-2023 chandq
4
+ * Released under the MIT License.
5
+ */
6
+
7
+ 'use strict';
8
+
9
+ /**
10
+ * 防抖函数
11
+ * 当函数被连续调用时,该函数并不执行,只有当其全部停止调用超过一定时间后才执行1次。
12
+ * 例如:上电梯的时候,大家陆陆续续进来,电梯的门不会关上,只有当一段时间都没有人上来,电梯才会关门。
13
+ * @param {F} func
14
+ * @param {number} wait
15
+ * @returns {DebounceFunc<F>}
16
+ */
17
+ const debounce = (func, wait) => {
18
+ let timeout;
19
+ let canceled = false;
20
+ const f = function (...args) {
21
+ if (canceled)
22
+ return;
23
+ clearTimeout(timeout);
24
+ timeout = setTimeout(() => {
25
+ func.call(this, ...args);
26
+ }, wait);
27
+ };
28
+ f.cancel = () => {
29
+ clearTimeout(timeout);
30
+ canceled = true;
31
+ };
32
+ return f;
33
+ };
34
+ /**
35
+ * 节流函数
36
+ * 节流就是节约流量,将连续触发的事件稀释成预设评率。 比如每间隔1秒执行一次函数,无论这期间触发多少次事件。
37
+ * 这有点像公交车,无论在站点等车的人多不多,公交车只会按时来一班,不会来一个人就来一辆公交车。
38
+ * @param {F} func
39
+ * @param {number} wait
40
+ * @param {boolean} immediate
41
+ * @returns {ThrottleFunc<F>}
42
+ */
43
+ const throttle = (func, wait, immediate) => {
44
+ let timeout;
45
+ let canceled = false;
46
+ let lastCalledTime = 0;
47
+ const f = function (...args) {
48
+ if (canceled)
49
+ return;
50
+ const now = Date.now();
51
+ const call = () => {
52
+ lastCalledTime = now;
53
+ func.call(this, ...args);
54
+ };
55
+ // 第一次执行
56
+ if (lastCalledTime === 0) {
57
+ if (immediate) {
58
+ return call();
59
+ }
60
+ else {
61
+ lastCalledTime = now;
62
+ return;
63
+ }
64
+ }
65
+ const remain = lastCalledTime + wait - now;
66
+ if (remain > 0) {
67
+ clearTimeout(timeout);
68
+ timeout = setTimeout(() => call(), wait);
69
+ }
70
+ else {
71
+ call();
72
+ }
73
+ };
74
+ f.cancel = () => {
75
+ clearTimeout(timeout);
76
+ canceled = true;
77
+ };
78
+ return f;
79
+ };
80
+ /**
81
+ * 单次函数
82
+ * @param {AnyFunc} func
83
+ * @returns {AnyFunc}
84
+ */
85
+ const once = (func) => {
86
+ let called = false;
87
+ let result;
88
+ return function (...args) {
89
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return
90
+ if (called)
91
+ return result;
92
+ called = true;
93
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
94
+ result = func.call(this, ...args);
95
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return
96
+ return result;
97
+ };
98
+ };
99
+ /**
100
+ * 设置全局变量
101
+ * @param {string | number | symbol} key
102
+ * @param val
103
+ */
104
+ function setGlobal(key, val) {
105
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
106
+ // @ts-ignore
107
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
108
+ if (typeof globalThis !== 'undefined')
109
+ globalThis[key] = val;
110
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
111
+ // @ts-ignore
112
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
113
+ else if (typeof window !== 'undefined')
114
+ window[key] = val;
115
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
116
+ // @ts-ignore
117
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
118
+ else if (typeof global !== 'undefined')
119
+ global[key] = val;
120
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
121
+ // @ts-ignore
122
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
123
+ else if (typeof self !== 'undefined')
124
+ self[key] = val;
125
+ else
126
+ throw new SyntaxError('当前环境下无法设置全局属性');
127
+ }
128
+ /**
129
+ * 设置全局变量
130
+ * @param {string | number | symbol} key
131
+ * @param val
132
+ */
133
+ function getGlobal(key) {
134
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
135
+ // @ts-ignore
136
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
137
+ if (typeof globalThis !== 'undefined')
138
+ return globalThis[key];
139
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
140
+ // @ts-ignore
141
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
142
+ else if (typeof window !== 'undefined')
143
+ return window[key];
144
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
145
+ // @ts-ignore
146
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
147
+ else if (typeof global !== 'undefined')
148
+ return global[key];
149
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
150
+ // @ts-ignore
151
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
152
+ else if (typeof self !== 'undefined')
153
+ return self[key];
154
+ }
155
+
156
+ exports.debounce = debounce;
157
+ exports.getGlobal = getGlobal;
158
+ exports.once = once;
159
+ exports.setGlobal = setGlobal;
160
+ exports.throttle = throttle;
package/lib/cjs/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v0.0.1
2
+ * sculp-js v0.1.0
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -21,21 +21,103 @@ var url = require('./url.js');
21
21
  var async = require('./async.js');
22
22
  var file = require('./file.js');
23
23
  var watermark = require('./watermark.js');
24
+ var func = require('./func.js');
25
+ var random = require('./random.js');
26
+ var number = require('./number.js');
27
+ var unique = require('./unique.js');
24
28
 
25
29
 
26
30
 
27
- exports.array = array;
28
- exports.clipboard = clipboard;
29
- exports.cookie = cookie;
30
- exports.date = date;
31
- exports.dom = dom;
32
- exports.download = download;
33
- exports.object = object;
34
- exports.path = path;
35
- exports.qs = qs;
36
- exports.string = string;
37
- exports.type = type;
38
- exports.url = url;
39
- exports.async = async;
40
- exports.file = file;
31
+ exports.arrayEach = array.arrayEach;
32
+ exports.arrayEachAsync = array.arrayEachAsync;
33
+ exports.arrayInsertBefore = array.arrayInsertBefore;
34
+ exports.arrayLike = array.arrayLike;
35
+ exports.arrayRemove = array.arrayRemove;
36
+ exports.deepTraversal = array.deepTraversal;
37
+ exports.getTreeIds = array.getTreeIds;
38
+ exports.copyText = clipboard.copyText;
39
+ exports.cookieDel = cookie.cookieDel;
40
+ exports.cookieGet = cookie.cookieGet;
41
+ exports.cookieSet = cookie.cookieSet;
42
+ exports.calculateDate = date.calculateDate;
43
+ exports.calculateDateTime = date.calculateDateTime;
44
+ exports.formatDate = date.formatDate;
45
+ exports.addClass = dom.addClass;
46
+ exports.getComputedCssVal = dom.getComputedCssVal;
47
+ exports.getStyle = dom.getStyle;
48
+ exports.hasClass = dom.hasClass;
49
+ exports.isDomReady = dom.isDomReady;
50
+ exports.onDomReady = dom.onDomReady;
51
+ exports.removeClass = dom.removeClass;
52
+ exports.setStyle = dom.setStyle;
53
+ exports.smoothScroll = dom.smoothScroll;
54
+ exports.downloadBlob = download.downloadBlob;
55
+ exports.downloadData = download.downloadData;
56
+ exports.downloadHref = download.downloadHref;
57
+ exports.downloadURL = download.downloadURL;
58
+ exports.cloneDeep = object.cloneDeep;
59
+ exports.isPlainObject = object.isPlainObject;
60
+ exports.objectAssign = object.objectAssign;
61
+ exports.objectEach = object.objectEach;
62
+ exports.objectEachAsync = object.objectEachAsync;
63
+ exports.objectFill = object.objectFill;
64
+ exports.objectGet = object.objectGet;
65
+ exports.objectHas = object.objectHas;
66
+ exports.objectMap = object.objectMap;
67
+ exports.objectMerge = object.objectAssign;
68
+ exports.objectOmit = object.objectOmit;
69
+ exports.objectPick = object.objectPick;
70
+ exports.pathJoin = path.pathJoin;
71
+ exports.pathNormalize = path.pathNormalize;
72
+ exports.qsParse = qs.qsParse;
73
+ exports.qsStringify = qs.qsStringify;
74
+ exports.STRING_ARABIC_NUMERALS = string.STRING_ARABIC_NUMERALS;
75
+ exports.STRING_LOWERCASE_ALPHA = string.STRING_LOWERCASE_ALPHA;
76
+ exports.STRING_UPPERCASE_ALPHA = string.STRING_UPPERCASE_ALPHA;
77
+ exports.getStrWidthPx = string.getStrWidthPx;
78
+ exports.stringAssign = string.stringAssign;
79
+ exports.stringCamelCase = string.stringCamelCase;
80
+ exports.stringEscapeHtml = string.stringEscapeHtml;
81
+ exports.stringFill = string.stringFill;
82
+ exports.stringFormat = string.stringFormat;
83
+ exports.stringKebabCase = string.stringKebabCase;
84
+ exports.isArray = type.isArray;
85
+ exports.isBigInt = type.isBigInt;
86
+ exports.isBoolean = type.isBoolean;
87
+ exports.isDate = type.isDate;
88
+ exports.isError = type.isError;
89
+ exports.isFunction = type.isFunction;
90
+ exports.isNaN = type.isNaN;
91
+ exports.isNull = type.isNull;
92
+ exports.isNumber = type.isNumber;
93
+ exports.isObject = type.isObject;
94
+ exports.isPrimitive = type.isPrimitive;
95
+ exports.isRegExp = type.isRegExp;
96
+ exports.isString = type.isString;
97
+ exports.isSymbol = type.isSymbol;
98
+ exports.isUndefined = type.isUndefined;
99
+ exports.typeIs = type.typeIs;
100
+ exports.urlDelParams = url.urlDelParams;
101
+ exports.urlParse = url.urlParse;
102
+ exports.urlSetParams = url.urlSetParams;
103
+ exports.urlStringify = url.urlStringify;
104
+ exports.asyncMap = async.asyncMap;
105
+ exports.wait = async.wait;
106
+ exports.chooseLocalFile = file.chooseLocalFile;
41
107
  exports.genCanvasWM = watermark.genCanvasWM;
108
+ exports.debounce = func.debounce;
109
+ exports.getGlobal = func.getGlobal;
110
+ exports.once = func.once;
111
+ exports.setGlobal = func.setGlobal;
112
+ exports.throttle = func.throttle;
113
+ exports.STRING_POOL = random.STRING_POOL;
114
+ exports.randomNumber = random.randomNumber;
115
+ exports.randomString = random.randomString;
116
+ exports.randomUuid = random.randomUuid;
117
+ exports.HEX_POOL = number.HEX_POOL;
118
+ exports.formatNumber = number.formatNumber;
119
+ exports.numberAbbr = number.numberAbbr;
120
+ exports.numberToHex = number.numberToHex;
121
+ exports.UNIQUE_NUMBER_SAFE_LENGTH = unique.UNIQUE_NUMBER_SAFE_LENGTH;
122
+ exports.uniqueNumber = unique.uniqueNumber;
123
+ exports.uniqueString = unique.uniqueString;
@@ -0,0 +1,82 @@
1
+ /*!
2
+ * sculp-js v0.1.0
3
+ * (c) 2023-2023 chandq
4
+ * Released under the MIT License.
5
+ */
6
+
7
+ 'use strict';
8
+
9
+ var func = require('./func.js');
10
+ var string = require('./string.js');
11
+
12
+ const HEX_POOL = `${string.STRING_ARABIC_NUMERALS}${string.STRING_UPPERCASE_ALPHA}${string.STRING_LOWERCASE_ALPHA}`;
13
+ const supportBigInt = typeof BigInt !== 'undefined';
14
+ const jsbi = () => func.getGlobal('JSBI');
15
+ const toBigInt = (n) => (supportBigInt ? BigInt(n) : jsbi().BigInt(n));
16
+ const divide = (x, y) => (supportBigInt ? x / y : jsbi().divide(x, y));
17
+ const remainder = (x, y) => (supportBigInt ? x % y : jsbi().remainder(x, y));
18
+ /**
19
+ * 将十进制转换成任意进制
20
+ * @param {number | string} decimal 十进制数值或字符串,可以是任意长度,会使用大数进行计算
21
+ * @param {string} [hexPool] 进制池,默认 62 进制
22
+ * @returns {string}
23
+ */
24
+ const numberToHex = (decimal, hexPool = HEX_POOL) => {
25
+ if (hexPool.length < 2)
26
+ throw new Error('进制池长度不能少于 2');
27
+ if (!supportBigInt) {
28
+ throw new Error('需要安装 jsbi 模块并将 JSBI 设置为全局变量:\nimport JSBI from "jsbi"; window.JSBI = JSBI;');
29
+ }
30
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
31
+ let bigInt = toBigInt(decimal);
32
+ const ret = [];
33
+ const { length } = hexPool;
34
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
35
+ const bigLength = toBigInt(length);
36
+ const execute = () => {
37
+ const y = Number(remainder(bigInt, bigLength));
38
+ bigInt = divide(bigInt, bigLength);
39
+ ret.unshift(hexPool[y]);
40
+ if (bigInt > 0) {
41
+ execute();
42
+ }
43
+ };
44
+ execute();
45
+ return ret.join('');
46
+ };
47
+ /**
48
+ * 缩写
49
+ * @param {number | string} num
50
+ * @param {Array<string>} units
51
+ * @param {number} ratio
52
+ * @param {number} exponent
53
+ * @returns {string}
54
+ */
55
+ const numberAbbr = (num, units, ratio = 1000, exponent) => {
56
+ const { length } = units;
57
+ if (length === 0)
58
+ throw new Error('至少需要一个单位');
59
+ let num2 = Number(num);
60
+ let times = 0;
61
+ while (num2 >= ratio && times < length - 1) {
62
+ num2 = num2 / ratio;
63
+ times++;
64
+ }
65
+ const value = num2.toFixed(exponent);
66
+ const unit = units[times];
67
+ return value.toString() + '' + unit;
68
+ };
69
+ /**
70
+ * 将数字格式化成千位分隔符显示的字符串
71
+ * @param {number} val 数字
72
+ * @param {'int' | 'float'} type 展示分段显示的类型 int:整型 | float:浮点型
73
+ * @return {string}
74
+ */
75
+ function formatNumber(val, type = 'int') {
76
+ return type === 'int' ? parseInt(String(val)).toLocaleString() : Number(val).toLocaleString('en-US');
77
+ }
78
+
79
+ exports.HEX_POOL = HEX_POOL;
80
+ exports.formatNumber = formatNumber;
81
+ exports.numberAbbr = numberAbbr;
82
+ exports.numberToHex = numberToHex;
package/lib/cjs/object.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v0.0.1
2
+ * sculp-js v0.1.0
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -109,8 +109,8 @@ function objectOmit(obj, keys) {
109
109
  const merge = (map, source, target) => {
110
110
  if (type.isUndefined(target))
111
111
  return source;
112
- const sourceType = type.default(source);
113
- const targetType = type.default(target);
112
+ const sourceType = type.typeIs(source);
113
+ const targetType = type.typeIs(target);
114
114
  if (sourceType !== targetType) {
115
115
  if (type.isArray(target))
116
116
  return merge(map, [], target);
package/lib/cjs/path.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v0.0.1
2
+ * sculp-js v0.1.0
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/qs.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v0.0.1
2
+ * sculp-js v0.1.0
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */