@oipage/core.js 1.0.0-alpha.1 → 1.1.0-alpha.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.
- package/CHANGELOG +52 -5
- package/LICENSE +20 -20
- package/README.md +170 -50
- package/dist/core.esm.js +2711 -0
- package/dist/core.js +2715 -147
- package/dist/core.min.js +2 -6
- package/package.json +63 -34
- package/src/Array/group.js +35 -0
- package/src/Array/index.js +21 -0
- package/src/Array/list.js +79 -0
- package/src/Array/sort.js +40 -0
- package/src/Array/tool.js +20 -0
- package/src/Array/unique.js +30 -0
- package/src/Cache/driver.js +86 -0
- package/src/Cache/expire.js +21 -0
- package/src/Cache/index.js +33 -0
- package/src/Cache/store.js +113 -0
- package/src/Color/adjust.js +67 -0
- package/src/Color/convert.js +100 -0
- package/src/Color/index.js +19 -0
- package/src/Color/tool.js +39 -0
- package/src/Digits/compare.js +33 -0
- package/src/Digits/format.js +55 -0
- package/src/Digits/index.js +32 -0
- package/src/Digits/math.js +75 -0
- package/src/Digits/precise.js +230 -0
- package/src/Digits/tool.js +29 -0
- package/src/Id/index.js +17 -0
- package/src/Id/nanoid.js +24 -0
- package/src/Id/random.js +40 -0
- package/src/Id/tool.js +38 -0
- package/src/Id/uuid.js +21 -0
- package/src/Object/check.js +15 -0
- package/src/Object/clone.js +66 -0
- package/src/Object/equal.js +63 -0
- package/src/Object/index.js +25 -0
- package/src/Object/merge.js +35 -0
- package/src/Object/path.js +58 -0
- package/src/Object/pick.js +34 -0
- package/src/Query/index.js +14 -0
- package/src/Query/parse.js +40 -0
- package/src/Query/stringify.js +45 -0
- package/src/Query/tool.js +58 -0
- package/src/Query/url.js +63 -0
- package/src/String/case.js +35 -0
- package/src/String/escape.js +40 -0
- package/src/String/index.js +28 -0
- package/src/String/mask.js +70 -0
- package/src/String/pad.js +23 -0
- package/src/String/trim.js +10 -0
- package/src/String/truncate.js +36 -0
- package/src/String/width.js +27 -0
- package/src/Time/arithmetic.js +61 -0
- package/src/Time/calendar.js +37 -0
- package/src/Time/compare.js +28 -0
- package/src/Time/field.js +37 -0
- package/src/Time/format.js +28 -0
- package/src/Time/index.js +97 -0
- package/src/Time/tool.js +20 -0
- package/src/Validate/contact.js +14 -0
- package/src/Validate/identity.js +57 -0
- package/src/Validate/index.js +24 -0
- package/src/Validate/network.js +59 -0
- package/src/Validate/type.js +28 -0
- package/src/index.js +10 -1
- package/src/shared/date.js +123 -0
- package/src/shared/is.js +63 -0
- package/src/tool.js +3 -0
- package/types/Array.d.ts +33 -0
- package/types/Cache.d.ts +51 -0
- package/types/Color.d.ts +43 -0
- package/types/Digits.d.ts +92 -0
- package/types/Id.d.ts +25 -0
- package/types/Object.d.ts +45 -0
- package/types/Query.d.ts +35 -0
- package/types/String.d.ts +62 -0
- package/types/Time.d.ts +136 -0
- package/types/Validate.d.ts +39 -0
- package/types/index.d.ts +30 -3
- package/.github/FUNDING.yml +0 -12
- package/.mailmap +0 -1
- package/AUTHORS.txt +0 -6
- package/src/Dater/index.js +0 -61
- package/src/Dater/tool.js +0 -25
- package/types/Dater.ts +0 -31
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Color 调色
|
|
3
|
+
*/
|
|
4
|
+
import { hex2rgb, rgb2hex } from "./convert.js"
|
|
5
|
+
import { clamp } from "./tool.js"
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 按权重混合两个颜色
|
|
9
|
+
*
|
|
10
|
+
* @param {number} weight 第二个颜色的权重,0-1
|
|
11
|
+
*/
|
|
12
|
+
export function mix(color1, color2, weight) {
|
|
13
|
+
weight = weight === undefined ? 0.5 : clamp(weight, 0, 1)
|
|
14
|
+
|
|
15
|
+
let a = hex2rgb(color1)
|
|
16
|
+
let b = hex2rgb(color2)
|
|
17
|
+
|
|
18
|
+
return rgb2hex({
|
|
19
|
+
r: Math.round(a.r * (1 - weight) + b.r * weight),
|
|
20
|
+
g: Math.round(a.g * (1 - weight) + b.g * weight),
|
|
21
|
+
b: Math.round(a.b * (1 - weight) + b.b * weight)
|
|
22
|
+
})
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 变亮:与白色混合
|
|
27
|
+
*
|
|
28
|
+
* @param {number} amount 0-1
|
|
29
|
+
*/
|
|
30
|
+
export function lighten(color, amount) {
|
|
31
|
+
return mix(color, "#ffffff", amount === undefined ? 0.3 : amount)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 变暗:与黑色混合
|
|
36
|
+
*
|
|
37
|
+
* @param {number} amount 0-1
|
|
38
|
+
*/
|
|
39
|
+
export function darken(color, amount) {
|
|
40
|
+
return mix(color, "#000000", amount === undefined ? 0.3 : amount)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 根据背景色返回可读的前景色(纯黑或纯白)
|
|
45
|
+
*/
|
|
46
|
+
export function contrast(color) {
|
|
47
|
+
let rgb = hex2rgb(color)
|
|
48
|
+
|
|
49
|
+
// W3C 相对亮度
|
|
50
|
+
let channel = function (value) {
|
|
51
|
+
let ratio = value / 255
|
|
52
|
+
return ratio <= 0.03928 ? ratio / 12.92 : Math.pow((ratio + 0.055) / 1.055, 2.4)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
let luminance = 0.2126 * channel(rgb.r) + 0.7152 * channel(rgb.g) + 0.0722 * channel(rgb.b)
|
|
56
|
+
|
|
57
|
+
return luminance > 0.5 ? "#000000" : "#ffffff"
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* 转成 rgba() 字符串
|
|
62
|
+
*/
|
|
63
|
+
export function toRgba(color, alpha) {
|
|
64
|
+
let rgb = hex2rgb(color)
|
|
65
|
+
alpha = alpha === undefined ? 1 : clamp(alpha, 0, 1)
|
|
66
|
+
return "rgba(" + rgb.r + ", " + rgb.g + ", " + rgb.b + ", " + alpha + ")"
|
|
67
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Color 格式互转
|
|
3
|
+
*/
|
|
4
|
+
import { clamp, normalizeHex, pad2 } from "./tool.js"
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* #rrggbb / #rgb / #rrggbbaa 转 { r, g, b, a }
|
|
8
|
+
*/
|
|
9
|
+
export function hex2rgb(hex) {
|
|
10
|
+
let str = normalizeHex(hex).substring(1)
|
|
11
|
+
|
|
12
|
+
let result = {
|
|
13
|
+
r: parseInt(str.substring(0, 2), 16),
|
|
14
|
+
g: parseInt(str.substring(2, 4), 16),
|
|
15
|
+
b: parseInt(str.substring(4, 6), 16),
|
|
16
|
+
a: 1
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (str.length === 8) {
|
|
20
|
+
result.a = Math.round(parseInt(str.substring(6, 8), 16) / 255 * 100) / 100
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return result
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* { r, g, b, a } 转 #rrggbb,alpha 小于 1 时输出 #rrggbbaa
|
|
28
|
+
*/
|
|
29
|
+
export function rgb2hex(rgb) {
|
|
30
|
+
if (!rgb || typeof rgb !== "object") throw new TypeError("rgb2hex 需要一个 { r, g, b } 对象")
|
|
31
|
+
|
|
32
|
+
let alpha = rgb.a === undefined ? 1 : clamp(rgb.a, 0, 1)
|
|
33
|
+
let hex = "#" + pad2(rgb.r) + pad2(rgb.g) + pad2(rgb.b)
|
|
34
|
+
|
|
35
|
+
return alpha < 1 ? hex + pad2(alpha * 255) : hex
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* { r, g, b } 转 { h, s, l }
|
|
40
|
+
*/
|
|
41
|
+
export function rgb2hsl(rgb) {
|
|
42
|
+
let r = clamp(rgb.r, 0, 255) / 255
|
|
43
|
+
let g = clamp(rgb.g, 0, 255) / 255
|
|
44
|
+
let b = clamp(rgb.b, 0, 255) / 255
|
|
45
|
+
|
|
46
|
+
let max = Math.max(r, g, b)
|
|
47
|
+
let min = Math.min(r, g, b)
|
|
48
|
+
let delta = max - min
|
|
49
|
+
let l = (max + min) / 2
|
|
50
|
+
|
|
51
|
+
let h = 0
|
|
52
|
+
let s = 0
|
|
53
|
+
|
|
54
|
+
if (delta !== 0) {
|
|
55
|
+
s = delta / (1 - Math.abs(2 * l - 1))
|
|
56
|
+
|
|
57
|
+
switch (max) {
|
|
58
|
+
case r: h = ((g - b) / delta) % 6; break
|
|
59
|
+
case g: h = (b - r) / delta + 2; break
|
|
60
|
+
default: h = (r - g) / delta + 4
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
h *= 60
|
|
64
|
+
if (h < 0) h += 360
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return {
|
|
68
|
+
h: Math.round(h * 100) / 100,
|
|
69
|
+
s: Math.round(s * 10000) / 100,
|
|
70
|
+
l: Math.round(l * 10000) / 100
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* { h, s, l } 转 { r, g, b },h 单位是度,s/l 单位是百分比
|
|
76
|
+
*/
|
|
77
|
+
export function hsl2rgb(hsl) {
|
|
78
|
+
let h = ((hsl.h % 360) + 360) % 360
|
|
79
|
+
let s = clamp(hsl.s, 0, 100) / 100
|
|
80
|
+
let l = clamp(hsl.l, 0, 100) / 100
|
|
81
|
+
|
|
82
|
+
let c = (1 - Math.abs(2 * l - 1)) * s
|
|
83
|
+
let x = c * (1 - Math.abs((h / 60) % 2 - 1))
|
|
84
|
+
let m = l - c / 2
|
|
85
|
+
|
|
86
|
+
let rgb
|
|
87
|
+
|
|
88
|
+
if (h < 60) rgb = [c, x, 0]
|
|
89
|
+
else if (h < 120) rgb = [x, c, 0]
|
|
90
|
+
else if (h < 180) rgb = [0, c, x]
|
|
91
|
+
else if (h < 240) rgb = [0, x, c]
|
|
92
|
+
else if (h < 300) rgb = [x, 0, c]
|
|
93
|
+
else rgb = [c, 0, x]
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
r: Math.round((rgb[0] + m) * 255),
|
|
97
|
+
g: Math.round((rgb[1] + m) * 255),
|
|
98
|
+
b: Math.round((rgb[2] + m) * 255)
|
|
99
|
+
}
|
|
100
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Color 对外入口
|
|
3
|
+
*/
|
|
4
|
+
import { contrast, darken, lighten, mix, toRgba } from "./adjust.js"
|
|
5
|
+
import { hex2rgb, hsl2rgb, rgb2hex, rgb2hsl } from "./convert.js"
|
|
6
|
+
|
|
7
|
+
const ColorUtil = {
|
|
8
|
+
hex2rgb: hex2rgb,
|
|
9
|
+
rgb2hex: rgb2hex,
|
|
10
|
+
rgb2hsl: rgb2hsl,
|
|
11
|
+
hsl2rgb: hsl2rgb,
|
|
12
|
+
mix: mix,
|
|
13
|
+
lighten: lighten,
|
|
14
|
+
darken: darken,
|
|
15
|
+
contrast: contrast,
|
|
16
|
+
toRgba: toRgba
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export { ColorUtil as Color }
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Color 内部工具
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Color:颜色换算与调色
|
|
7
|
+
* 输入输出统一使用 #rrggbb(必要时带 alpha),内部按 0-255 / 0-360 计算
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export function clamp(value, min, max) {
|
|
11
|
+
return value < min ? min : (value > max ? max : value)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function pad2(value) {
|
|
15
|
+
let str = Math.round(clamp(value, 0, 255)).toString(16)
|
|
16
|
+
return str.length === 1 ? "0" + str : str
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function normalizeHex(hex) {
|
|
20
|
+
let str = String(hex === undefined || hex === null ? "" : hex).trim().replace(/^#/, "")
|
|
21
|
+
|
|
22
|
+
if (/^[0-9a-fA-F]{3}$/.test(str)) {
|
|
23
|
+
str = str.split("").map(function (char) {
|
|
24
|
+
return char + char
|
|
25
|
+
}).join("")
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (/^[0-9a-fA-F]{4}$/.test(str)) {
|
|
29
|
+
str = str.split("").map(function (char) {
|
|
30
|
+
return char + char
|
|
31
|
+
}).join("")
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (!/^[0-9a-fA-F]{6}([0-9a-fA-F]{2})?$/.test(str)) {
|
|
35
|
+
throw new TypeError("非法的颜色值:" + hex)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return "#" + str.toLowerCase()
|
|
39
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Digits 比较
|
|
3
|
+
*/
|
|
4
|
+
import { compare as compareDecimal } from "./precise"
|
|
5
|
+
|
|
6
|
+
export function useCompare(Digits) {
|
|
7
|
+
/**
|
|
8
|
+
* 比较大小:大于返回 1,相等返回 0,小于返回 -1
|
|
9
|
+
*/
|
|
10
|
+
Digits.prototype.compare = function (value) {
|
|
11
|
+
return compareDecimal(this._decimal, value)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
Digits.prototype.eq = function (value) {
|
|
15
|
+
return this.compare(value) === 0
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
Digits.prototype.gt = function (value) {
|
|
19
|
+
return this.compare(value) > 0
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
Digits.prototype.gte = function (value) {
|
|
23
|
+
return this.compare(value) >= 0
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
Digits.prototype.lt = function (value) {
|
|
27
|
+
return this.compare(value) < 0
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
Digits.prototype.lte = function (value) {
|
|
31
|
+
return this.compare(value) <= 0
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Digits 格式化输出
|
|
3
|
+
*/
|
|
4
|
+
import { fixedString, thousands } from "./tool"
|
|
5
|
+
import { toStr, round as roundDecimal } from "./precise"
|
|
6
|
+
|
|
7
|
+
export function useFormat(Digits) {
|
|
8
|
+
/**
|
|
9
|
+
* 返回格式化后的数字,默认保留 2 位小数、四舍五入、带千分位
|
|
10
|
+
*
|
|
11
|
+
* @param {number} scale 小数位数,默认 2
|
|
12
|
+
* @param {object} options
|
|
13
|
+
* @param {string} options.rounding 舍入模式:half-up、half-even、ceil、floor、trunc,默认 half-up
|
|
14
|
+
* @param {boolean} options.thousands 是否加千分位,默认 true
|
|
15
|
+
*/
|
|
16
|
+
Digits.prototype.format = function (scale, options) {
|
|
17
|
+
scale = scale === undefined ? 2 : scale
|
|
18
|
+
options = options || {}
|
|
19
|
+
|
|
20
|
+
let rounded = roundDecimal(this._decimal, scale, options.rounding || "half-up")
|
|
21
|
+
let str = fixedString(rounded, scale)
|
|
22
|
+
|
|
23
|
+
return options.thousands === false ? str : thousands(str)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 当前值的精确字符串表示(不受浮点精度影响)
|
|
28
|
+
*/
|
|
29
|
+
Digits.prototype.toString = function () {
|
|
30
|
+
return toStr(this._decimal)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
Digits.prototype.valueOf = function () {
|
|
34
|
+
return this.value
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
Digits.prototype.toJSON = function () {
|
|
38
|
+
return this.value
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* 取当前值(等价于 digits.value)
|
|
43
|
+
*/
|
|
44
|
+
Digits.prototype.getValue = function () {
|
|
45
|
+
return this.value
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 返回保留 scale 位小数的字符串
|
|
50
|
+
*/
|
|
51
|
+
Digits.prototype.toFixed = function (scale, rounding) {
|
|
52
|
+
scale = scale === undefined ? 2 : scale
|
|
53
|
+
return fixedString(roundDecimal(this._decimal, scale, rounding), scale)
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { toDecimal, toNumber } from "./precise"
|
|
2
|
+
import { useFormat } from "./format"
|
|
3
|
+
import { useMath } from "./math"
|
|
4
|
+
import { useCompare } from "./compare"
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 数字对象:内部用 BigInt 定点表示,加减乘除都不经过浮点
|
|
8
|
+
*
|
|
9
|
+
* @param {number|string} value 初始值,默认 0
|
|
10
|
+
*/
|
|
11
|
+
export function Digits(value = 0) {
|
|
12
|
+
|
|
13
|
+
Object.defineProperty(this, "_decimal", {
|
|
14
|
+
value: toDecimal(value),
|
|
15
|
+
writable: true,
|
|
16
|
+
enumerable: false
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
Object.defineProperty(this, "value", {
|
|
20
|
+
enumerable: true,
|
|
21
|
+
get: function () {
|
|
22
|
+
return toNumber(this._decimal)
|
|
23
|
+
},
|
|
24
|
+
set: function (value) {
|
|
25
|
+
this._decimal = toDecimal(value)
|
|
26
|
+
}
|
|
27
|
+
})
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
useFormat(Digits)
|
|
31
|
+
useMath(Digits)
|
|
32
|
+
useCompare(Digits)
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Digits 运算
|
|
3
|
+
*/
|
|
4
|
+
import { add as addDecimal, subtract as subtractDecimal, multiply as multiplyDecimal, divide as divideDecimal, round as roundDecimal } from "./precise"
|
|
5
|
+
|
|
6
|
+
export function useMath(Digits) {
|
|
7
|
+
/**
|
|
8
|
+
* 高精度加法
|
|
9
|
+
*/
|
|
10
|
+
Digits.prototype.add = function (value) {
|
|
11
|
+
this._decimal = addDecimal(this._decimal, value)
|
|
12
|
+
return this
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 高精度减法
|
|
17
|
+
*/
|
|
18
|
+
Digits.prototype.subtract = function (value) {
|
|
19
|
+
this._decimal = subtractDecimal(this._decimal, value)
|
|
20
|
+
return this
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 高精度乘法
|
|
25
|
+
*/
|
|
26
|
+
Digits.prototype.multiply = function (value) {
|
|
27
|
+
this._decimal = multiplyDecimal(this._decimal, value)
|
|
28
|
+
return this
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 高精度除法
|
|
33
|
+
*
|
|
34
|
+
* @param {number|string} value 除数,为 0 时抛错
|
|
35
|
+
* @param {object} options
|
|
36
|
+
* @param {number} options.scale 结果保留的小数位,默认 16
|
|
37
|
+
* @param {string} options.rounding 舍入模式,默认 half-up
|
|
38
|
+
*/
|
|
39
|
+
Digits.prototype.divide = function (value, options) {
|
|
40
|
+
options = options || {}
|
|
41
|
+
this._decimal = divideDecimal(
|
|
42
|
+
this._decimal,
|
|
43
|
+
value,
|
|
44
|
+
options.scale === undefined ? 16 : options.scale,
|
|
45
|
+
options.rounding
|
|
46
|
+
)
|
|
47
|
+
return this
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 就地四舍五入到 scale 位小数
|
|
52
|
+
*/
|
|
53
|
+
Digits.prototype.round = function (scale, rounding) {
|
|
54
|
+
this._decimal = roundDecimal(this._decimal, scale, rounding)
|
|
55
|
+
return this
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 取绝对值
|
|
60
|
+
*/
|
|
61
|
+
Digits.prototype.abs = function () {
|
|
62
|
+
if (this._decimal.big < BigInt(0)) {
|
|
63
|
+
this._decimal = { big: -this._decimal.big, scale: this._decimal.scale }
|
|
64
|
+
}
|
|
65
|
+
return this
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* 取相反数
|
|
70
|
+
*/
|
|
71
|
+
Digits.prototype.negate = function () {
|
|
72
|
+
this._decimal = { big: -this._decimal.big, scale: this._decimal.scale }
|
|
73
|
+
return this
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 十进制高精度运算引擎
|
|
3
|
+
*
|
|
4
|
+
* 内部用 BigInt 保存“整数部分 + 小数位数”({ big, scale },实际值 = big / 10^scale),
|
|
5
|
+
* 全部运算都在整数域完成,不经过浮点,避免 0.1 + 0.2 这类精度问题。
|
|
6
|
+
* 注意:这里一律用 BigInt() 构造而不是 10n 字面量,保证产物不需要提高语法目标。
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
// 部分旧引擎(老版本小程序基础库、iOS 14 以下 Safari)没有 BigInt,
|
|
10
|
+
// 这里不能在模块加载期就调用 BigInt(),否则整个包都会 import 失败
|
|
11
|
+
const HAS_BIGINT = typeof BigInt === "function"
|
|
12
|
+
const TEN = HAS_BIGINT ? BigInt(10) : 10
|
|
13
|
+
const ZERO = HAS_BIGINT ? BigInt(0) : 0
|
|
14
|
+
const TWO = HAS_BIGINT ? BigInt(2) : 2
|
|
15
|
+
|
|
16
|
+
const NUMBER_RE = /^([+-]?)(\d+)?(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/
|
|
17
|
+
|
|
18
|
+
const ROUNDINGS = ["half-up", "half-even", "ceil", "floor", "trunc"]
|
|
19
|
+
|
|
20
|
+
function pow10(n) {
|
|
21
|
+
let result = BigInt(1)
|
|
22
|
+
for (let i = 0; i < n; i++) result = result * TEN
|
|
23
|
+
return result
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function abs(value) {
|
|
27
|
+
return value < ZERO ? -value : value
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// 去掉小数末尾多余的 0
|
|
31
|
+
function normalize(big, scale) {
|
|
32
|
+
while (scale > 0 && big % TEN === ZERO) {
|
|
33
|
+
big = big / TEN
|
|
34
|
+
scale -= 1
|
|
35
|
+
}
|
|
36
|
+
return { big: big, scale: scale }
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function checkRounding(rounding) {
|
|
40
|
+
if (ROUNDINGS.indexOf(rounding) === -1) {
|
|
41
|
+
throw new TypeError("不支持的舍入模式:" + rounding + ",可选 " + ROUNDINGS.join("、"))
|
|
42
|
+
}
|
|
43
|
+
return rounding
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 把任意数字形态转成内部十进制表示
|
|
48
|
+
* 支持 number、string(可带千分位和小数)、bigint、以及内部表示
|
|
49
|
+
*/
|
|
50
|
+
export function toDecimal(value) {
|
|
51
|
+
if (!HAS_BIGINT) {
|
|
52
|
+
throw new TypeError("当前运行环境不支持 BigInt,Digits 无法使用:请升级小程序基础库 / iOS 14+ / Node 10.4+")
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (value && typeof value === "object" && typeof value.big === "bigint") {
|
|
56
|
+
return value
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (typeof value === "bigint") {
|
|
60
|
+
return { big: value, scale: 0 }
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
let str
|
|
64
|
+
if (typeof value === "number") {
|
|
65
|
+
if (!isFinite(value)) throw new TypeError("不是合法的数字:" + value)
|
|
66
|
+
str = String(value)
|
|
67
|
+
} else if (typeof value === "string") {
|
|
68
|
+
str = value.trim().replace(/,/g, "")
|
|
69
|
+
} else {
|
|
70
|
+
throw new TypeError("不是合法的数字:" + value)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
let matched = NUMBER_RE.exec(str)
|
|
74
|
+
if (!matched || (!matched[2] && !matched[3])) {
|
|
75
|
+
throw new TypeError("不是合法的数字:" + value)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
let negative = matched[1] === "-"
|
|
79
|
+
let integerPart = matched[2] || ""
|
|
80
|
+
let decimalPart = matched[3] || ""
|
|
81
|
+
let exponent = matched[4] ? parseInt(matched[4], 10) : 0
|
|
82
|
+
|
|
83
|
+
let digits = integerPart + decimalPart
|
|
84
|
+
let scale = decimalPart.length - exponent
|
|
85
|
+
|
|
86
|
+
if (scale < 0) {
|
|
87
|
+
digits += "0".repeat(-scale)
|
|
88
|
+
scale = 0
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
digits = digits.replace(/^0+(?=\d)/, "")
|
|
92
|
+
|
|
93
|
+
let big = BigInt(digits)
|
|
94
|
+
if (negative) big = -big
|
|
95
|
+
|
|
96
|
+
return normalize(big, scale)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// 内部十进制表示转字符串(精确值,不丢精度)
|
|
100
|
+
export function toStr(decimal) {
|
|
101
|
+
let negative = decimal.big < ZERO
|
|
102
|
+
let digits = (negative ? -decimal.big : decimal.big).toString()
|
|
103
|
+
let scale = decimal.scale
|
|
104
|
+
|
|
105
|
+
if (scale === 0) return (negative ? "-" : "") + digits
|
|
106
|
+
|
|
107
|
+
if (digits.length <= scale) {
|
|
108
|
+
digits = "0".repeat(scale - digits.length + 1) + digits
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
let cut = digits.length - scale
|
|
112
|
+
return (negative ? "-" : "") + digits.substring(0, cut) + "." + digits.substring(cut)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function toNumber(decimal) {
|
|
116
|
+
return Number(toStr(decimal))
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function compare(a, b) {
|
|
120
|
+
a = toDecimal(a)
|
|
121
|
+
b = toDecimal(b)
|
|
122
|
+
|
|
123
|
+
let maxScale = Math.max(a.scale, b.scale)
|
|
124
|
+
let left = a.big * pow10(maxScale - a.scale)
|
|
125
|
+
let right = b.big * pow10(maxScale - b.scale)
|
|
126
|
+
|
|
127
|
+
return left === right ? 0 : (left > right ? 1 : -1)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export function add(a, b) {
|
|
131
|
+
a = toDecimal(a)
|
|
132
|
+
b = toDecimal(b)
|
|
133
|
+
|
|
134
|
+
let maxScale = Math.max(a.scale, b.scale)
|
|
135
|
+
let left = a.big * pow10(maxScale - a.scale)
|
|
136
|
+
let right = b.big * pow10(maxScale - b.scale)
|
|
137
|
+
|
|
138
|
+
return normalize(left + right, maxScale)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function subtract(a, b) {
|
|
142
|
+
a = toDecimal(a)
|
|
143
|
+
b = toDecimal(b)
|
|
144
|
+
|
|
145
|
+
let maxScale = Math.max(a.scale, b.scale)
|
|
146
|
+
let left = a.big * pow10(maxScale - a.scale)
|
|
147
|
+
let right = b.big * pow10(maxScale - b.scale)
|
|
148
|
+
|
|
149
|
+
return normalize(left - right, maxScale)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export function multiply(a, b) {
|
|
153
|
+
a = toDecimal(a)
|
|
154
|
+
b = toDecimal(b)
|
|
155
|
+
|
|
156
|
+
return normalize(a.big * b.big, a.scale + b.scale)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* 在“已截断的商 q 和余数 r”上按指定模式进位,q、denominator 都是非负的绝对值
|
|
161
|
+
*/
|
|
162
|
+
function roundQuotient(q, r, denominator, negative, rounding) {
|
|
163
|
+
if (r === ZERO) return q
|
|
164
|
+
|
|
165
|
+
let doubled = r * TWO
|
|
166
|
+
|
|
167
|
+
switch (rounding) {
|
|
168
|
+
case "half-up":
|
|
169
|
+
if (doubled >= denominator) q += BigInt(1)
|
|
170
|
+
break
|
|
171
|
+
case "half-even":
|
|
172
|
+
if (doubled > denominator || (doubled === denominator && q % TWO !== ZERO)) q += BigInt(1)
|
|
173
|
+
break
|
|
174
|
+
case "ceil":
|
|
175
|
+
if (!negative) q += BigInt(1)
|
|
176
|
+
break
|
|
177
|
+
case "floor":
|
|
178
|
+
if (negative) q += BigInt(1)
|
|
179
|
+
break
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return q
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export function divide(a, b, scale, rounding) {
|
|
186
|
+
a = toDecimal(a)
|
|
187
|
+
b = toDecimal(b)
|
|
188
|
+
|
|
189
|
+
if (b.big === ZERO) throw new RangeError("除数不能为0")
|
|
190
|
+
|
|
191
|
+
scale = scale === undefined ? 16 : scale
|
|
192
|
+
rounding = checkRounding(rounding || "half-up")
|
|
193
|
+
|
|
194
|
+
let negative = (a.big < ZERO) !== (b.big < ZERO)
|
|
195
|
+
|
|
196
|
+
let left = abs(a.big) * pow10(b.scale + scale)
|
|
197
|
+
let right = abs(b.big) * pow10(a.scale)
|
|
198
|
+
|
|
199
|
+
let q = left / right
|
|
200
|
+
let r = left % right
|
|
201
|
+
|
|
202
|
+
q = roundQuotient(q, r, right, negative, rounding)
|
|
203
|
+
|
|
204
|
+
return normalize(negative ? -q : q, scale)
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* 把十进制表示按 scale 位小数舍入
|
|
209
|
+
*/
|
|
210
|
+
export function round(decimal, scale, rounding) {
|
|
211
|
+
decimal = toDecimal(decimal)
|
|
212
|
+
scale = scale === undefined ? 0 : scale
|
|
213
|
+
rounding = checkRounding(rounding || "half-up")
|
|
214
|
+
|
|
215
|
+
if (scale < 0) throw new RangeError("scale 不能为负数:" + scale)
|
|
216
|
+
|
|
217
|
+
if (decimal.scale <= scale) {
|
|
218
|
+
return { big: decimal.big * pow10(scale - decimal.scale), scale: scale }
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
let negative = decimal.big < ZERO
|
|
222
|
+
let drop = pow10(decimal.scale - scale)
|
|
223
|
+
|
|
224
|
+
let q = abs(decimal.big) / drop
|
|
225
|
+
let r = abs(decimal.big) % drop
|
|
226
|
+
|
|
227
|
+
q = roundQuotient(q, r, drop, negative, rounding)
|
|
228
|
+
|
|
229
|
+
return normalize(negative ? -q : q, scale)
|
|
230
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Digits 内部工具
|
|
3
|
+
*/
|
|
4
|
+
import { toStr } from "./precise"
|
|
5
|
+
|
|
6
|
+
export function fixedString(decimal, scale) {
|
|
7
|
+
let str = toStr(decimal)
|
|
8
|
+
let dotIndex = str.indexOf(".")
|
|
9
|
+
|
|
10
|
+
if (dotIndex === -1) {
|
|
11
|
+
return scale === 0 ? str : str + "." + "0".repeat(scale)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
let current = str.length - dotIndex - 1
|
|
15
|
+
if (current >= scale) return str
|
|
16
|
+
|
|
17
|
+
return str + "0".repeat(scale - current)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function thousands(str) {
|
|
21
|
+
let negative = str.charAt(0) === "-"
|
|
22
|
+
let body = negative ? str.substring(1) : str
|
|
23
|
+
let dotIndex = body.indexOf(".")
|
|
24
|
+
|
|
25
|
+
let integerPart = dotIndex === -1 ? body : body.substring(0, dotIndex)
|
|
26
|
+
let decimalPart = dotIndex === -1 ? "" : body.substring(dotIndex)
|
|
27
|
+
|
|
28
|
+
return (negative ? "-" : "") + integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, ",") + decimalPart
|
|
29
|
+
}
|
package/src/Id/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Id 对外入口
|
|
3
|
+
*/
|
|
4
|
+
import { nanoid } from "./nanoid.js"
|
|
5
|
+
import { randomInt, randomString } from "./random.js"
|
|
6
|
+
import { isSecure } from "./tool.js"
|
|
7
|
+
import { uuid } from "./uuid.js"
|
|
8
|
+
|
|
9
|
+
const IdUtil = {
|
|
10
|
+
uuid: uuid,
|
|
11
|
+
nanoid: nanoid,
|
|
12
|
+
randomInt: randomInt,
|
|
13
|
+
randomString: randomString,
|
|
14
|
+
isSecure: isSecure
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { IdUtil as Id }
|