@vope/utils 1.6.7 → 1.7.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/dist/async-lock.d.ts +27 -0
- package/dist/async-lock.js +40 -0
- package/dist/calc.d.ts +18 -7
- package/dist/calc.js +80 -10
- package/dist/disable-enumerable.d.ts +8 -0
- package/dist/disable-enumerable.js +13 -0
- package/dist/encoding.d.ts +1 -0
- package/dist/index-sort.d.ts +7 -0
- package/dist/index-sort.js +34 -0
- package/dist/index.cjs.js +246 -67
- package/dist/index.d.ts +4 -1
- package/dist/index.js +16 -10
- package/dist/memory/basic.d.ts +46 -11
- package/dist/memory/basic.js +47 -27
- package/dist/memory/list.d.ts +2 -3
- package/dist/memory/list.js +3 -2
- package/dist/memory/power.d.ts +2 -3
- package/dist/memory/power.js +3 -2
- package/dist/memory/simple.d.ts +1 -2
- package/dist/subscriber.js +1 -1
- package/package.json +81 -81
- package/dist/memory/type.d.ts +0 -11
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
interface Bean {
|
|
2
|
+
get(): boolean;
|
|
3
|
+
set(value: boolean): void;
|
|
4
|
+
}
|
|
5
|
+
interface Accessor {
|
|
6
|
+
get value(): boolean;
|
|
7
|
+
set value(value: boolean);
|
|
8
|
+
}
|
|
9
|
+
interface Options<V> {
|
|
10
|
+
/**
|
|
11
|
+
* 满足验证的值将开启锁
|
|
12
|
+
*/
|
|
13
|
+
validate?: V | ((data: V) => boolean);
|
|
14
|
+
/**
|
|
15
|
+
* 锁状态
|
|
16
|
+
*/
|
|
17
|
+
sp?: Bean | Accessor;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* 异步锁
|
|
21
|
+
*/
|
|
22
|
+
declare const _default: <const T extends unknown[], V extends unknown>(callback: (...args: T) => Promise<V>, { validate, sp, }?: Options<V>) => {
|
|
23
|
+
readonly spin: boolean;
|
|
24
|
+
callback(...args: T): Promise<V>;
|
|
25
|
+
unlock(): void;
|
|
26
|
+
};
|
|
27
|
+
export default _default;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { isBoolean, isFunction } from "lodash-es";
|
|
2
|
+
const isAccessor = (sp) => {
|
|
3
|
+
return sp !== void 0 && "value" in sp && isBoolean(sp.value);
|
|
4
|
+
};
|
|
5
|
+
var async_lock_default = (callback, {
|
|
6
|
+
validate,
|
|
7
|
+
sp = { value: false }
|
|
8
|
+
} = {}) => {
|
|
9
|
+
const isA = isAccessor(sp);
|
|
10
|
+
const getter = () => isA ? sp.value : sp.get();
|
|
11
|
+
const setter = (value) => isA ? sp.value = value : sp.set(value);
|
|
12
|
+
let lock = false;
|
|
13
|
+
let local;
|
|
14
|
+
return {
|
|
15
|
+
get spin() {
|
|
16
|
+
return getter();
|
|
17
|
+
},
|
|
18
|
+
async callback(...args) {
|
|
19
|
+
if (getter() || lock) return local;
|
|
20
|
+
setter(true);
|
|
21
|
+
local = callback(...args).then((result) => {
|
|
22
|
+
if (isFunction(validate)) {
|
|
23
|
+
lock = validate(result);
|
|
24
|
+
} else if (validate !== void 0) {
|
|
25
|
+
lock = validate === result;
|
|
26
|
+
}
|
|
27
|
+
return result;
|
|
28
|
+
});
|
|
29
|
+
await local;
|
|
30
|
+
setter(false);
|
|
31
|
+
return local;
|
|
32
|
+
},
|
|
33
|
+
unlock() {
|
|
34
|
+
lock = false;
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
export {
|
|
39
|
+
async_lock_default as default
|
|
40
|
+
};
|
package/dist/calc.d.ts
CHANGED
|
@@ -1,16 +1,27 @@
|
|
|
1
1
|
type Ls = `${string}(${string})` | `${number}${string}` | `-${number}${string}`;
|
|
2
|
-
type Lv = Ls | number;
|
|
3
2
|
export declare namespace Calc {
|
|
4
|
-
type Value =
|
|
3
|
+
type Value = Ls | number;
|
|
5
4
|
interface Clamp {
|
|
6
|
-
min?:
|
|
7
|
-
max?:
|
|
5
|
+
min?: Value;
|
|
6
|
+
max?: Value;
|
|
8
7
|
}
|
|
9
8
|
}
|
|
9
|
+
/**
|
|
10
|
+
* 判断一个值是否为 calc 表达式
|
|
11
|
+
*/
|
|
10
12
|
export declare const isCalc: (value: Ls) => value is `calc(${string})`;
|
|
13
|
+
/**
|
|
14
|
+
* CSS 长度值加减混合运算
|
|
15
|
+
*/
|
|
11
16
|
declare const calc: {
|
|
12
|
-
(...list:
|
|
13
|
-
|
|
14
|
-
|
|
17
|
+
(...list: Calc.Value[]): 0 | Ls;
|
|
18
|
+
getExpMN(value: Ls): string | null;
|
|
19
|
+
multiply(value: Calc.Value, multiple: number): 0 | Ls;
|
|
20
|
+
divide(value: Calc.Value, divisor: number): 0 | Ls;
|
|
21
|
+
/**
|
|
22
|
+
* CSS 长度值格式化
|
|
23
|
+
*/
|
|
24
|
+
formatter: (value: Calc.Value) => 0 | Ls;
|
|
25
|
+
clamp(value: Calc.Value, { min, max }: Calc.Clamp): 0 | Ls;
|
|
15
26
|
};
|
|
16
27
|
export default calc;
|
package/dist/calc.js
CHANGED
|
@@ -1,9 +1,20 @@
|
|
|
1
1
|
import { isNumber } from "lodash-es";
|
|
2
2
|
const { parseFloat } = Number;
|
|
3
|
-
const
|
|
4
|
-
const isCalc = (value) => value.slice(0, 5) === "calc(" && value.slice(-1) === ")";
|
|
5
|
-
const re_numerical = /^(-?(?:\.\d+|\d+(?:\.\d+)?))([a-zA-Z]+|%)$/;
|
|
3
|
+
const re_numerical = /^(-?(?:\.\d+|\d+(?:\.\d+)?)(?:e\d+)?)([a-zA-Z]+|%)$/;
|
|
6
4
|
const re_zero = /^0[a-zA-Z]+$/;
|
|
5
|
+
const re_method_start = /[a-z]/i;
|
|
6
|
+
const re_method_next = /^\w*\(/;
|
|
7
|
+
const formatter = (value) => {
|
|
8
|
+
if (isNumber(value)) {
|
|
9
|
+
return value && `${value}px`;
|
|
10
|
+
}
|
|
11
|
+
const matchs = value.match(re_numerical) || (isCalc(value) ? value.slice(5, -1).match(re_numerical) : null);
|
|
12
|
+
if (matchs) {
|
|
13
|
+
return matchs[1] === "0" ? 0 : matchs[1] + matchs[2];
|
|
14
|
+
}
|
|
15
|
+
return value;
|
|
16
|
+
};
|
|
17
|
+
const isCalc = (value) => value.slice(0, 5) === "calc(" && value.slice(-1) === ")";
|
|
7
18
|
class Memory {
|
|
8
19
|
data = {};
|
|
9
20
|
memory = {};
|
|
@@ -44,6 +55,7 @@ const calc = (...list) => {
|
|
|
44
55
|
if (size === 1) return formatter(list[0]);
|
|
45
56
|
const manager = new Manager();
|
|
46
57
|
list.forEach((value2, index) => manager.push(value2, index));
|
|
58
|
+
let count = 0;
|
|
47
59
|
const value = list.reduce((caches, _, index) => {
|
|
48
60
|
const data = manager.at(index);
|
|
49
61
|
if (!data || data.value === 0) return caches;
|
|
@@ -55,23 +67,81 @@ const calc = (...list) => {
|
|
|
55
67
|
tmp = value2;
|
|
56
68
|
}
|
|
57
69
|
if (!caches) return tmp;
|
|
70
|
+
count++;
|
|
58
71
|
return tmp[0] === "-" ? `${caches} - ${tmp.slice(1)}` : `${caches} + ${tmp}`;
|
|
59
72
|
}, "");
|
|
60
|
-
return !value
|
|
73
|
+
return !value ? 0 : count === 0 ? re_zero.test(value) ? 0 : value : `calc(${value})`;
|
|
74
|
+
};
|
|
75
|
+
let exp_max_method_name = null;
|
|
76
|
+
const isMethod = (value) => {
|
|
77
|
+
if (!re_method_start.test(value)) {
|
|
78
|
+
exp_max_method_name = null;
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
const matchs = value.slice(1).match(re_method_next);
|
|
82
|
+
if (!matchs || value.slice(-1) !== ")") {
|
|
83
|
+
exp_max_method_name = null;
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
const size = matchs[0].length;
|
|
87
|
+
let count = 0;
|
|
88
|
+
for (let i = size + 1; i < value.length - 1; i++) {
|
|
89
|
+
const c = value[i];
|
|
90
|
+
if (c === "(") {
|
|
91
|
+
count++;
|
|
92
|
+
} else if (c === ")" && count-- === 0) {
|
|
93
|
+
exp_max_method_name = null;
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
if (count === 0) {
|
|
98
|
+
exp_max_method_name = value.slice(0, size);
|
|
99
|
+
return true;
|
|
100
|
+
} else {
|
|
101
|
+
exp_max_method_name = null;
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
calc.getExpMN = (value) => {
|
|
106
|
+
isMethod(value);
|
|
107
|
+
return exp_max_method_name;
|
|
108
|
+
};
|
|
109
|
+
calc.multiply = (value, multiple) => {
|
|
110
|
+
if (value === 0 || multiple === 0) return 0;
|
|
111
|
+
if (isNumber(value)) return `${value * multiple}px`;
|
|
112
|
+
if (multiple === 1) return value;
|
|
113
|
+
const tmp = formatter(value);
|
|
114
|
+
if (!tmp) return 0;
|
|
115
|
+
const matchs = tmp.match(re_numerical);
|
|
116
|
+
return matchs ? parseFloat(matchs[1]) * multiple + matchs[2] : isCalc(tmp) ? `calc(${tmp.slice(4)} * ${multiple})` : isMethod(tmp) ? `calc(${tmp} * ${multiple})` : `calc((${value}) * ${multiple})`;
|
|
117
|
+
};
|
|
118
|
+
calc.divide = (value, divisor) => {
|
|
119
|
+
if (divisor === 0) throw new Error("The divisor cannot be 0");
|
|
120
|
+
if (value === 0) return 0;
|
|
121
|
+
if (divisor === 1) return formatter(value);
|
|
122
|
+
const tmp = formatter(value);
|
|
123
|
+
if (!tmp) return 0;
|
|
124
|
+
const matchs = tmp.match(re_numerical);
|
|
125
|
+
return matchs ? parseFloat(matchs[1]) / divisor + matchs[2] : isCalc(tmp) ? `calc(${tmp.slice(4)} / ${divisor})` : isMethod(tmp) ? `calc(${tmp} / ${divisor})` : `calc((${value}) / ${divisor})`;
|
|
61
126
|
};
|
|
62
127
|
calc.formatter = formatter;
|
|
63
128
|
const zero = "0px";
|
|
64
129
|
const u = void 0;
|
|
65
130
|
calc.clamp = (value, { min, max }) => {
|
|
66
|
-
const val =
|
|
131
|
+
const val = formatter(value) || zero;
|
|
67
132
|
if (min !== u && max !== u) {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
return `
|
|
133
|
+
min = formatter(min) || zero;
|
|
134
|
+
max = formatter(max) || zero;
|
|
135
|
+
return min === max ? max : `clamp(${min}, ${val}, ${max})`;
|
|
136
|
+
}
|
|
137
|
+
if (max !== u) {
|
|
138
|
+
max = formatter(max) || zero;
|
|
139
|
+
return max === zero ? val : `max(${max}, ${val})`;
|
|
71
140
|
} else if (min !== u) {
|
|
72
|
-
|
|
141
|
+
min = formatter(min) || zero;
|
|
142
|
+
return min === val ? val : `min(${min}, ${val})`;
|
|
73
143
|
} else {
|
|
74
|
-
return val;
|
|
144
|
+
return val === zero ? 0 : val;
|
|
75
145
|
}
|
|
76
146
|
};
|
|
77
147
|
var calc_default = calc;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
var disable_enumerable_default = (data, key) => {
|
|
2
|
+
const descriptor = Object.getOwnPropertyDescriptor(data, key) || {
|
|
3
|
+
configurable: true,
|
|
4
|
+
writable: true
|
|
5
|
+
};
|
|
6
|
+
Object.defineProperty(data, key, {
|
|
7
|
+
...descriptor,
|
|
8
|
+
enumerable: false
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
export {
|
|
12
|
+
disable_enumerable_default as default
|
|
13
|
+
};
|
package/dist/encoding.d.ts
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
* 根据文件内容判断文件的编码
|
|
3
3
|
* @param uint 文件内容Uint8Array格式
|
|
4
4
|
* @param limit 用于约束GBK和utf_8的判定上限
|
|
5
|
+
* @description 目前支持utf-8、utf-16le、utf-16be、gbk四种编码
|
|
5
6
|
*/
|
|
6
7
|
export declare const getEncoding: (uint: Uint8Array, limit?: number) => {
|
|
7
8
|
encoding: "utf-8" | "utf-16le" | "utf-16be" | "gbk";
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
var index_sort_default = (list, key) => {
|
|
2
|
+
const { length } = list;
|
|
3
|
+
if (length < 2) return list;
|
|
4
|
+
const first = list[0][key];
|
|
5
|
+
const caches = { [first]: first };
|
|
6
|
+
let isA = false;
|
|
7
|
+
let max = first;
|
|
8
|
+
let min = first;
|
|
9
|
+
for (let i = 1; i < length; i++) {
|
|
10
|
+
const value = list[i][key];
|
|
11
|
+
if (value === Math.trunc(value) && value in caches === false) {
|
|
12
|
+
if (value > max) {
|
|
13
|
+
max = value;
|
|
14
|
+
} else if (value < min) {
|
|
15
|
+
min = value;
|
|
16
|
+
}
|
|
17
|
+
caches[value] = value;
|
|
18
|
+
} else {
|
|
19
|
+
isA = true;
|
|
20
|
+
break;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
if (!isA && max - min === length - 1) {
|
|
24
|
+
return list.reduce((cache, data) => {
|
|
25
|
+
cache[data[key] - min] = data;
|
|
26
|
+
return cache;
|
|
27
|
+
}, []);
|
|
28
|
+
} else {
|
|
29
|
+
return list.sort((a, b) => a[key] - b[key]);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
export {
|
|
33
|
+
index_sort_default as default
|
|
34
|
+
};
|