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.
@@ -0,0 +1,77 @@
1
+ /*!
2
+ * sculp-js v0.1.0
3
+ * (c) 2023-2023 chandq
4
+ * Released under the MIT License.
5
+ */
6
+
7
+ import { getGlobal } from './func.js';
8
+ import { STRING_ARABIC_NUMERALS, STRING_UPPERCASE_ALPHA, STRING_LOWERCASE_ALPHA } from './string.js';
9
+
10
+ const HEX_POOL = `${STRING_ARABIC_NUMERALS}${STRING_UPPERCASE_ALPHA}${STRING_LOWERCASE_ALPHA}`;
11
+ const supportBigInt = typeof BigInt !== 'undefined';
12
+ const jsbi = () => getGlobal('JSBI');
13
+ const toBigInt = (n) => (supportBigInt ? BigInt(n) : jsbi().BigInt(n));
14
+ const divide = (x, y) => (supportBigInt ? x / y : jsbi().divide(x, y));
15
+ const remainder = (x, y) => (supportBigInt ? x % y : jsbi().remainder(x, y));
16
+ /**
17
+ * 将十进制转换成任意进制
18
+ * @param {number | string} decimal 十进制数值或字符串,可以是任意长度,会使用大数进行计算
19
+ * @param {string} [hexPool] 进制池,默认 62 进制
20
+ * @returns {string}
21
+ */
22
+ const numberToHex = (decimal, hexPool = HEX_POOL) => {
23
+ if (hexPool.length < 2)
24
+ throw new Error('进制池长度不能少于 2');
25
+ if (!supportBigInt) {
26
+ throw new Error('需要安装 jsbi 模块并将 JSBI 设置为全局变量:\nimport JSBI from "jsbi"; window.JSBI = JSBI;');
27
+ }
28
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
29
+ let bigInt = toBigInt(decimal);
30
+ const ret = [];
31
+ const { length } = hexPool;
32
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
33
+ const bigLength = toBigInt(length);
34
+ const execute = () => {
35
+ const y = Number(remainder(bigInt, bigLength));
36
+ bigInt = divide(bigInt, bigLength);
37
+ ret.unshift(hexPool[y]);
38
+ if (bigInt > 0) {
39
+ execute();
40
+ }
41
+ };
42
+ execute();
43
+ return ret.join('');
44
+ };
45
+ /**
46
+ * 缩写
47
+ * @param {number | string} num
48
+ * @param {Array<string>} units
49
+ * @param {number} ratio
50
+ * @param {number} exponent
51
+ * @returns {string}
52
+ */
53
+ const numberAbbr = (num, units, ratio = 1000, exponent) => {
54
+ const { length } = units;
55
+ if (length === 0)
56
+ throw new Error('至少需要一个单位');
57
+ let num2 = Number(num);
58
+ let times = 0;
59
+ while (num2 >= ratio && times < length - 1) {
60
+ num2 = num2 / ratio;
61
+ times++;
62
+ }
63
+ const value = num2.toFixed(exponent);
64
+ const unit = units[times];
65
+ return value.toString() + '' + unit;
66
+ };
67
+ /**
68
+ * 将数字格式化成千位分隔符显示的字符串
69
+ * @param {number} val 数字
70
+ * @param {'int' | 'float'} type 展示分段显示的类型 int:整型 | float:浮点型
71
+ * @return {string}
72
+ */
73
+ function formatNumber(val, type = 'int') {
74
+ return type === 'int' ? parseInt(String(val)).toLocaleString() : Number(val).toLocaleString('en-US');
75
+ }
76
+
77
+ export { HEX_POOL, formatNumber, numberAbbr, numberToHex };
package/lib/es/object.js CHANGED
@@ -1,10 +1,10 @@
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
  */
6
6
 
7
- import typeIs, { isObject, isUndefined, isArray } from './type.js';
7
+ import { isObject, isUndefined, typeIs, isArray } from './type.js';
8
8
 
9
9
  /**
10
10
  * 判断对象是否为纯对象
package/lib/es/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/es/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
  */
@@ -0,0 +1,67 @@
1
+ /*!
2
+ * sculp-js v0.1.0
3
+ * (c) 2023-2023 chandq
4
+ * Released under the MIT License.
5
+ */
6
+
7
+ import { STRING_ARABIC_NUMERALS, STRING_UPPERCASE_ALPHA, STRING_LOWERCASE_ALPHA } from './string.js';
8
+ import { isString, isNumber } from './type.js';
9
+
10
+ /**
11
+ * 随机整数
12
+ * @param {number} min
13
+ * @param {number} max
14
+ * @returns {number}
15
+ */
16
+ const randomNumber = (min, max) => Math.floor(Math.random() * (max - min + 1) + min);
17
+ const STRING_POOL = `${STRING_ARABIC_NUMERALS}${STRING_UPPERCASE_ALPHA}${STRING_LOWERCASE_ALPHA}`;
18
+ /**
19
+ * 随机字符串
20
+ * @param {number | string} length
21
+ * @param {string} pool
22
+ * @returns {string}
23
+ */
24
+ const randomString = (length, pool) => {
25
+ let _length = 0;
26
+ let _pool = STRING_POOL;
27
+ if (isString(pool)) {
28
+ _length = length;
29
+ _pool = pool;
30
+ }
31
+ else if (isNumber(length)) {
32
+ _length = length;
33
+ }
34
+ else if (isString(length)) {
35
+ _pool = length;
36
+ }
37
+ let times = Math.max(_length, 1);
38
+ let result = '';
39
+ const min = 0;
40
+ const max = _pool.length - 1;
41
+ if (max < 2)
42
+ throw new Error('字符串池长度不能少于 2');
43
+ while (times--) {
44
+ const index = randomNumber(min, max);
45
+ result += _pool[index];
46
+ }
47
+ return result;
48
+ };
49
+ /**
50
+ * 优先浏览器原生能力获取 UUID v4
51
+ * @returns {string}
52
+ */
53
+ function randomUuid() {
54
+ if (typeof URL === 'undefined' || !URL.createObjectURL || typeof Blob === 'undefined') {
55
+ const hex = '0123456789abcdef';
56
+ const model = 'xxxxxxxx-xxxx-4xxx-xxxx-xxxxxxxxxxxx';
57
+ let str = '';
58
+ for (let i = 0; i < model.length; i++) {
59
+ const rnd = randomNumber(0, 15);
60
+ str += model[i] == '-' || model[i] == '4' ? model[i] : hex[rnd];
61
+ }
62
+ return str;
63
+ }
64
+ return /[^/]+$/.exec(URL.createObjectURL(new Blob()).slice())[0];
65
+ }
66
+
67
+ export { STRING_POOL, randomNumber, randomString, randomUuid };
package/lib/es/string.js CHANGED
@@ -1,9 +1,11 @@
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
  */
6
6
 
7
+ import { isString } from './type.js';
8
+
7
9
  /**
8
10
  * 将字符串转换为驼峰格式
9
11
  * @param {string} string
@@ -120,5 +122,35 @@ const stringEscapeHtml = (html) => {
120
122
  * @returns {string}
121
123
  */
122
124
  const stringFill = (length, value = ' ') => new Array(length).fill(value).join('');
125
+ /**
126
+ * 字符串的像素宽度
127
+ * @param {string} str 目标字符串
128
+ * @param {number} fontSize 字符串字体大小
129
+ * @param {boolean} isRemoveDom 计算后是否移除中间dom元素
130
+ * @return {*}
131
+ */
132
+ function getStrWidthPx(str, fontSize = 14, isRemoveDom = false) {
133
+ let strWidth = 0;
134
+ console.assert(isString(str), `${str} 不是有效的字符串`);
135
+ if (isString(str) && str.length > 0) {
136
+ let getEle = document.querySelector('#getStrWidth1494304949567');
137
+ if (!getEle) {
138
+ const _ele = document.createElement('span');
139
+ _ele.id = 'getStrWidth1494304949567';
140
+ _ele.style.fontSize = fontSize + 'px';
141
+ _ele.style.whiteSpace = 'nowrap';
142
+ _ele.style.visibility = 'hidden';
143
+ _ele.textContent = str;
144
+ document.body.appendChild(_ele);
145
+ getEle = _ele;
146
+ }
147
+ getEle.textContent = str;
148
+ strWidth = getEle.offsetWidth;
149
+ if (isRemoveDom) {
150
+ document.body.appendChild(getEle);
151
+ }
152
+ }
153
+ return strWidth;
154
+ }
123
155
 
124
- export { STRING_ARABIC_NUMERALS, STRING_LOWERCASE_ALPHA, STRING_UPPERCASE_ALPHA, stringAssign, stringCamelCase, stringEscapeHtml, stringFill, stringFormat, stringKebabCase };
156
+ export { STRING_ARABIC_NUMERALS, STRING_LOWERCASE_ALPHA, STRING_UPPERCASE_ALPHA, getStrWidthPx, stringAssign, stringCamelCase, stringEscapeHtml, stringFill, stringFormat, stringKebabCase };
package/lib/es/type.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
  */
@@ -25,4 +25,4 @@ const isDate = (any) => typeIs(any) === 'Date';
25
25
  const isError = (any) => typeIs(any) === 'Error';
26
26
  const isRegExp = (any) => typeIs(any) === 'RegExp';
27
27
 
28
- export { typeIs as default, isArray, isBigInt, isBoolean, isDate, isError, isFunction, isNaN, isNull, isNumber, isObject, isPrimitive, isRegExp, isString, isSymbol, isUndefined };
28
+ export { typeIs as default, isArray, isBigInt, isBoolean, isDate, isError, isFunction, isNaN, isNull, isNumber, isObject, isPrimitive, isRegExp, isString, isSymbol, isUndefined, typeIs };
@@ -0,0 +1,79 @@
1
+ /*!
2
+ * sculp-js v0.1.0
3
+ * (c) 2023-2023 chandq
4
+ * Released under the MIT License.
5
+ */
6
+
7
+ import { numberToHex, HEX_POOL } from './number.js';
8
+ import { randomNumber } from './random.js';
9
+ import { isString, isNumber } from './type.js';
10
+
11
+ const padStartWithZero = (str, maxLength = 2) => String(str).padStart(maxLength, '0');
12
+ let safeNo = 0;
13
+ let lastTimestamp = 0;
14
+ // 安全后缀长度,按 1 毫秒运算 99999 次 JS 代码来进行估算
15
+ // 那么,补足长度为 5
16
+ // 时间戳模式长度为 13
17
+ // 取最长 5 + 13 = 18
18
+ const UNIQUE_NUMBER_SAFE_LENGTH = 18;
19
+ const FIX_SAFE_LENGTH = 5;
20
+ const TIMESTAMP_LENGTH = 13;
21
+ /**
22
+ * 生成唯一不重复数值
23
+ * @param {number} length
24
+ * @returns {string}
25
+ */
26
+ const uniqueNumber = (length = UNIQUE_NUMBER_SAFE_LENGTH) => {
27
+ const now = Date.now();
28
+ length = Math.max(length, UNIQUE_NUMBER_SAFE_LENGTH);
29
+ if (now !== lastTimestamp) {
30
+ lastTimestamp = now;
31
+ safeNo = 0;
32
+ }
33
+ const timestamp = `${now}`;
34
+ let random = '';
35
+ const rndLength = length - FIX_SAFE_LENGTH - TIMESTAMP_LENGTH;
36
+ if (rndLength > 0) {
37
+ const rndMin = 10 ** (rndLength - 1);
38
+ const rndMax = 10 ** rndLength - 1;
39
+ const rnd = randomNumber(rndMin, rndMax);
40
+ random = `${rnd}`;
41
+ }
42
+ const safe = padStartWithZero(safeNo, FIX_SAFE_LENGTH);
43
+ safeNo++;
44
+ return `${timestamp}${random}${safe}`;
45
+ };
46
+ const randomFromPool = (pool) => {
47
+ const poolIndex = randomNumber(0, pool.length - 1);
48
+ return pool[poolIndex];
49
+ };
50
+ /**
51
+ * 生成唯一不重复字符串
52
+ * @param {number | string} length
53
+ * @param {string} pool
54
+ * @returns {string}
55
+ */
56
+ const uniqueString = (length, pool) => {
57
+ let _length = 0;
58
+ let _pool = HEX_POOL;
59
+ if (isString(pool)) {
60
+ _length = length;
61
+ _pool = pool;
62
+ }
63
+ else if (isNumber(length)) {
64
+ _length = length;
65
+ }
66
+ else if (isString(length)) {
67
+ _pool = length;
68
+ }
69
+ let uniqueString = numberToHex(uniqueNumber(), _pool);
70
+ let insertLength = _length - uniqueString.length;
71
+ if (insertLength <= 0)
72
+ return uniqueString;
73
+ while (insertLength--) {
74
+ uniqueString += randomFromPool(_pool);
75
+ }
76
+ return uniqueString;
77
+ };
78
+
79
+ export { UNIQUE_NUMBER_SAFE_LENGTH, uniqueNumber, uniqueString };
package/lib/es/url.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
  */