@vope/utils 1.4.18 → 1.4.19

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/cjs/index.js CHANGED
@@ -49,6 +49,7 @@ __export(src_exports, {
49
49
  joinPaths: () => joinPaths,
50
50
  loadImage: () => loadImage,
51
51
  once: () => once,
52
+ parseNum: () => parseNum,
52
53
  rotateAngle: () => rotateAngle,
53
54
  rotateRad: () => rotateRad,
54
55
  wait: () => wait
@@ -596,6 +597,40 @@ var once = (func, thisArg) => {
596
597
  return (...list) => inited ? local : (inited = true, local = func.apply(thisArg, list));
597
598
  };
598
599
 
600
+ // src/parse/num.ts
601
+ var import_lodash_es3 = require("lodash-es");
602
+ var pow = Math.floor(Math.log10(Number.MAX_SAFE_INTEGER));
603
+ var re_num = /^([-+]?)0*(\d+)(?:(n)|(?:\.(\d+))?(?:[Ee]([-+]?)(\d+))?)$/;
604
+ var parseNum = (num) => {
605
+ if ((0, import_lodash_es3.isNumber)(num) || typeof num === "bigint")
606
+ return num;
607
+ const match = num.match(re_num);
608
+ if (!match)
609
+ return Number.NaN;
610
+ const [, valueSymbol, integer, bigint, decimal, powSymbol, powValue] = match;
611
+ if (bigint === "n")
612
+ return BigInt(valueSymbol + integer);
613
+ const powSize = integer.length - 1 + (powValue ? powSymbol === "-" ? -powValue : +powValue : 0);
614
+ if (powSize < pow || powSize === pow && +integer.slice(0, -1) < Number.MAX_SAFE_INTEGER / 10)
615
+ return +num;
616
+ try {
617
+ const bigValue = BigInt(integer.slice(0, powSize + 1).padEnd(powSize + 1, "0"));
618
+ let smallValue;
619
+ if (decimal && powValue && powSymbol !== "-") {
620
+ const powNum = +powValue;
621
+ const effectDecimal = decimal.slice(0, Math.min(pow, powNum));
622
+ const tinyValue = decimal.length > pow && powNum > pow ? BigInt(+decimal.slice(pow, powNum)) * BigInt(10 ** Math.max(powNum - decimal.length, 0)) : BigInt(0);
623
+ smallValue = BigInt(effectDecimal) * BigInt(10 ** (powNum - effectDecimal.length)) + tinyValue;
624
+ } else {
625
+ smallValue = BigInt(0);
626
+ }
627
+ const value = bigValue + smallValue;
628
+ return valueSymbol === "-" ? value * BigInt(-1) : value;
629
+ } catch {
630
+ return Number.NaN;
631
+ }
632
+ };
633
+
599
634
  // src/rotate/rad.ts
600
635
  var rotateRad = (from, rad, visit = [0, 0]) => {
601
636
  const x = from[0] - visit[0];
@@ -610,7 +645,7 @@ var rotateRad = (from, rad, visit = [0, 0]) => {
610
645
  var rotateAngle = (from, angle, visit) => rotateRad(from, angle / 180 * Math.PI, visit);
611
646
 
612
647
  // src/style/css.ts
613
- var import_lodash_es3 = require("lodash-es");
648
+ var import_lodash_es4 = require("lodash-es");
614
649
  var hump = /[A-Z]/g;
615
650
  var css = (...args) => {
616
651
  const list = [];
@@ -618,7 +653,7 @@ var css = (...args) => {
618
653
  const data = args[i];
619
654
  if (!data)
620
655
  continue;
621
- if ((0, import_lodash_es3.isString)(data)) {
656
+ if ((0, import_lodash_es4.isString)(data)) {
622
657
  list.push(data);
623
658
  } else if (Array.isArray(data)) {
624
659
  const result = css(...data);
package/lib/index.js CHANGED
@@ -18,6 +18,7 @@ import { MemoryList } from "./memory/list";
18
18
  import { MemoryPower } from "./memory/power";
19
19
  import { MemorySimple } from "./memory/simple";
20
20
  import { once } from "./once";
21
+ import { parseNum } from "./parse/num";
21
22
  import { deconPowersSum } from "./power/decon";
22
23
  import { rotateAngle } from "./rotate/angle";
23
24
  import { rotateRad } from "./rotate/rad";
@@ -58,6 +59,7 @@ export {
58
59
  joinPaths,
59
60
  loadImage,
60
61
  once,
62
+ parseNum,
61
63
  rotateAngle,
62
64
  rotateRad,
63
65
  wait
@@ -0,0 +1,35 @@
1
+ import { isNumber } from "lodash-es";
2
+ const pow = Math.floor(Math.log10(Number.MAX_SAFE_INTEGER));
3
+ const re_num = /^([-+]?)0*(\d+)(?:(n)|(?:\.(\d+))?(?:[Ee]([-+]?)(\d+))?)$/;
4
+ const parseNum = (num) => {
5
+ if (isNumber(num) || typeof num === "bigint")
6
+ return num;
7
+ const match = num.match(re_num);
8
+ if (!match)
9
+ return Number.NaN;
10
+ const [, valueSymbol, integer, bigint, decimal, powSymbol, powValue] = match;
11
+ if (bigint === "n")
12
+ return BigInt(valueSymbol + integer);
13
+ const powSize = integer.length - 1 + (powValue ? powSymbol === "-" ? -powValue : +powValue : 0);
14
+ if (powSize < pow || powSize === pow && +integer.slice(0, -1) < Number.MAX_SAFE_INTEGER / 10)
15
+ return +num;
16
+ try {
17
+ const bigValue = BigInt(integer.slice(0, powSize + 1).padEnd(powSize + 1, "0"));
18
+ let smallValue;
19
+ if (decimal && powValue && powSymbol !== "-") {
20
+ const powNum = +powValue;
21
+ const effectDecimal = decimal.slice(0, Math.min(pow, powNum));
22
+ const tinyValue = decimal.length > pow && powNum > pow ? BigInt(+decimal.slice(pow, powNum)) * BigInt(10 ** Math.max(powNum - decimal.length, 0)) : BigInt(0);
23
+ smallValue = BigInt(effectDecimal) * BigInt(10 ** (powNum - effectDecimal.length)) + tinyValue;
24
+ } else {
25
+ smallValue = BigInt(0);
26
+ }
27
+ const value = bigValue + smallValue;
28
+ return valueSymbol === "-" ? value * BigInt(-1) : value;
29
+ } catch {
30
+ return Number.NaN;
31
+ }
32
+ };
33
+ export {
34
+ parseNum
35
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vope/utils",
3
- "version": "1.4.18",
3
+ "version": "1.4.19",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "publishConfig": {
package/types/index.d.ts CHANGED
@@ -18,6 +18,7 @@ export { MemoryList } from './memory/list';
18
18
  export { MemoryPower } from './memory/power';
19
19
  export { MemorySimple } from './memory/simple';
20
20
  export { once } from './once';
21
+ export { parseNum } from './parse/num';
21
22
  export { deconPowersSum } from './power/decon';
22
23
  export { rotateAngle } from './rotate/angle';
23
24
  export { rotateRad } from './rotate/rad';
@@ -0,0 +1,8 @@
1
+ /**
2
+ * 解析一个字符串并返回一个数字
3
+ * @description 1、number最多支持16位小数
4
+ * @description 2、最终值为bigint则会把小数部分舍去
5
+ * @description 3、支持科学计数法形式的字符串
6
+ * @description 4、无法正确解析的值则会返回NaN
7
+ */
8
+ export declare const parseNum: (num: number | bigint | string) => number | bigint;