@vope/utils 1.3.1 → 1.4.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/cjs/clamp.js +29 -12
- package/cjs/classnames.js +34 -20
- package/cjs/compare/version.js +35 -18
- package/cjs/compare/weak-equal.js +49 -36
- package/cjs/convert/json.js +25 -15
- package/cjs/convert/object.js +26 -16
- package/cjs/convert/strings.js +25 -9
- package/cjs/encoding.js +64 -51
- package/cjs/index.js +76 -53
- package/cjs/input-regex.js +24 -5
- package/cjs/is/class.js +40 -23
- package/cjs/is/empty.js +34 -10
- package/cjs/is/null.js +24 -7
- package/cjs/memory/basic.js +159 -139
- package/cjs/memory/list.js +39 -17
- package/cjs/memory/power.js +40 -16
- package/cjs/memory/type.d.js +16 -0
- package/cjs/once.js +26 -13
- package/cjs/power/decon.js +38 -26
- package/cjs/rotate/angle.js +34 -12
- package/cjs/rotate/rad.js +29 -14
- package/cjs/union.js +41 -26
- package/cjs/url/join.js +27 -11
- package/cjs/url/params.js +41 -33
- package/cjs/wait.js +23 -5
- package/lib/clamp.js +7 -10
- package/lib/classnames.js +12 -18
- package/lib/compare/version.js +13 -16
- package/lib/compare/weak-equal.js +27 -34
- package/lib/convert/json.js +3 -13
- package/lib/convert/object.js +4 -14
- package/lib/convert/strings.js +3 -7
- package/lib/encoding.js +42 -49
- package/lib/index.js +24 -24
- package/lib/input-regex.js +2 -3
- package/lib/is/class.js +18 -21
- package/lib/is/empty.js +3 -6
- package/lib/is/null.js +2 -5
- package/lib/memory/basic.js +129 -135
- package/lib/memory/list.js +8 -12
- package/lib/memory/power.js +9 -11
- package/lib/memory/type.d.js +1 -0
- package/lib/once.js +4 -11
- package/lib/power/decon.js +16 -24
- package/lib/rotate/angle.js +2 -7
- package/lib/rotate/rad.js +7 -12
- package/lib/union.js +20 -24
- package/lib/url/join.js +5 -9
- package/lib/url/params.js +19 -31
- package/lib/wait.js +1 -3
- package/package.json +6 -8
- package/types/index.d.ts +0 -1
- package/cjs/api.js +0 -56
- package/lib/api.js +0 -48
- package/types/api.d.ts +0 -19
package/lib/memory/list.js
CHANGED
|
@@ -1,14 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
* 分类存储(基于普通的键值存储)
|
|
4
|
-
*/
|
|
1
|
+
"use strict";
|
|
2
|
+
import MemoryBasic from "./basic";
|
|
5
3
|
export default class MemoryList extends MemoryBasic {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
});
|
|
13
|
-
}
|
|
4
|
+
constructor(key) {
|
|
5
|
+
super(key, {
|
|
6
|
+
mount: (index, reactor) => reactor ? (reactor.push(index), reactor) : [index],
|
|
7
|
+
reflex: (reactor) => reactor
|
|
8
|
+
});
|
|
9
|
+
}
|
|
14
10
|
}
|
package/lib/memory/power.js
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
* 分类存储(基于2的n次幂之和构建唯一键进行存储)
|
|
5
|
-
*/
|
|
1
|
+
"use strict";
|
|
2
|
+
import deconPowersSum from "../power/decon";
|
|
3
|
+
import MemoryBasic from "./basic";
|
|
6
4
|
export default class MemoryPower extends MemoryBasic {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
5
|
+
constructor(key) {
|
|
6
|
+
super(key, {
|
|
7
|
+
mount: (index, reactor) => (reactor || 0) + 2 ** index,
|
|
8
|
+
reflex: (reactor) => deconPowersSum(reactor, "asc")
|
|
9
|
+
});
|
|
10
|
+
}
|
|
13
11
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
package/lib/once.js
CHANGED
|
@@ -1,13 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
* 缓存函数返回值
|
|
3
|
-
* @param callback 运行返回值的函数
|
|
4
|
-
* @param thisArg 绑定到运行函数的this对象
|
|
5
|
-
*/
|
|
1
|
+
"use strict";
|
|
6
2
|
export default (func, thisArg) => {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
? local
|
|
11
|
-
: (inited = true,
|
|
12
|
-
local = func.apply(thisArg, list));
|
|
3
|
+
let local;
|
|
4
|
+
let inited = false;
|
|
5
|
+
return (...list) => inited ? local : (inited = true, local = func.apply(thisArg, list));
|
|
13
6
|
};
|
package/lib/power/decon.js
CHANGED
|
@@ -1,29 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
const ERROR_MESSAGE = {
|
|
2
|
-
|
|
3
|
-
|
|
3
|
+
INT: "\u53C2\u6570\u5FC5\u987B\u662F\u6574\u6570",
|
|
4
|
+
MIN: "\u591A\u4E2A2\u7684\u6B63\u6574\u6570\u5E42(\u4E24\u4E24\u4E0D\u76F8\u7B49)\u4E4B\u548C\u5FC5\u5B9A\u5927\u4E8E\u7B49\u4E8E1"
|
|
4
5
|
};
|
|
5
|
-
/**
|
|
6
|
-
* 解构多个2的正整数幂(两两不相等)之和
|
|
7
|
-
* @param sum 幂和
|
|
8
|
-
* @param sort 排序方式(默认降序)
|
|
9
|
-
* @description sum > 2**53则会因为精度问题计算报错
|
|
10
|
-
*/
|
|
11
6
|
const deconPowersSum = (sum, sort) => {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
return sort === 'asc'
|
|
26
|
-
? list.reverse()
|
|
27
|
-
: list;
|
|
7
|
+
if (sum < 1) {
|
|
8
|
+
throw new Error(ERROR_MESSAGE.MIN);
|
|
9
|
+
} else if (sum !== Math.floor(sum)) {
|
|
10
|
+
throw new Error(ERROR_MESSAGE.INT);
|
|
11
|
+
}
|
|
12
|
+
const list = [];
|
|
13
|
+
let cache = sum;
|
|
14
|
+
while (cache) {
|
|
15
|
+
const power = Math.log2(cache) ^ 0;
|
|
16
|
+
list.push(power);
|
|
17
|
+
cache -= Math.pow(2, power);
|
|
18
|
+
}
|
|
19
|
+
return sort === "asc" ? list.reverse() : list;
|
|
28
20
|
};
|
|
29
21
|
export default deconPowersSum;
|
package/lib/rotate/angle.js
CHANGED
|
@@ -1,9 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
* 返回点被旋转指定角度后新的位置
|
|
4
|
-
* @param from 旋转点
|
|
5
|
-
* @param angle 旋转角度
|
|
6
|
-
* @param visit 旋转原点
|
|
7
|
-
*/
|
|
1
|
+
"use strict";
|
|
2
|
+
import rotateRad from "./rad";
|
|
8
3
|
const rotateAngle = (from, angle, visit) => rotateRad(from, angle / 180 * Math.PI, visit);
|
|
9
4
|
export default rotateAngle;
|
package/lib/rotate/rad.js
CHANGED
|
@@ -1,15 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
* 返回点被旋转指定弧度后新的位置
|
|
3
|
-
* @param from 旋转点
|
|
4
|
-
* @param rad 旋转弧度
|
|
5
|
-
* @param visit 旋转原点
|
|
6
|
-
*/
|
|
1
|
+
"use strict";
|
|
7
2
|
const rotateRad = (from, rad, visit = [0, 0]) => {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
3
|
+
const x = from[0] - visit[0];
|
|
4
|
+
const y = from[1] - visit[1];
|
|
5
|
+
return [
|
|
6
|
+
x * Math.cos(rad) + y * Math.sin(rad) + visit[0],
|
|
7
|
+
y * Math.cos(rad) - x * Math.sin(rad) + visit[1]
|
|
8
|
+
];
|
|
14
9
|
};
|
|
15
10
|
export default rotateRad;
|
package/lib/union.js
CHANGED
|
@@ -1,26 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
* 数据联合(用于需要对加入的数据进行智能提示)
|
|
3
|
-
*/
|
|
1
|
+
"use strict";
|
|
4
2
|
export default class Union {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
return this;
|
|
25
|
-
}
|
|
3
|
+
constructor(key, formatter) {
|
|
4
|
+
this.key = key;
|
|
5
|
+
this.formatter = formatter;
|
|
6
|
+
}
|
|
7
|
+
#data = {};
|
|
8
|
+
#options = {};
|
|
9
|
+
get data() {
|
|
10
|
+
return this.#data;
|
|
11
|
+
}
|
|
12
|
+
get options() {
|
|
13
|
+
return this.#options;
|
|
14
|
+
}
|
|
15
|
+
add(data) {
|
|
16
|
+
const key = data[this.key];
|
|
17
|
+
const result = this.formatter(data);
|
|
18
|
+
this.#data[key] = result.data;
|
|
19
|
+
this.#options[key] = result.option;
|
|
20
|
+
return this;
|
|
21
|
+
}
|
|
26
22
|
}
|
package/lib/url/join.js
CHANGED
|
@@ -1,13 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
const re = /([^:])\/{2,}/g;
|
|
2
|
-
/**
|
|
3
|
-
* 实现多个路径拼接
|
|
4
|
-
*/
|
|
5
3
|
const joinPaths = (...list) => {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
? list[0]
|
|
11
|
-
: list.join('/').replace(re, ($0, $1) => `${$1}/`);
|
|
4
|
+
const { length } = list;
|
|
5
|
+
if (length === 0)
|
|
6
|
+
return "";
|
|
7
|
+
return length === 1 ? list[0] : list.join("/").replace(re, ($0, $1) => `${$1}/`);
|
|
12
8
|
};
|
|
13
9
|
export default joinPaths;
|
package/lib/url/params.js
CHANGED
|
@@ -1,35 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
* url拼接参数
|
|
3
|
-
* @param url
|
|
4
|
-
* @param params 需要让url携带的参数
|
|
5
|
-
* @param isOpenType 是否开启类型处理
|
|
6
|
-
* @description 开始类型处理的情况下,undefined会被转成null
|
|
7
|
-
*/
|
|
1
|
+
"use strict";
|
|
8
2
|
const connectURLParams = (url, params, isOpenType) => {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
: value.join(',')));
|
|
23
|
-
}
|
|
24
|
-
else {
|
|
25
|
-
list.push(key + '=' + (isOpenType ? JSON.stringify(value) : value.toString()));
|
|
26
|
-
}
|
|
3
|
+
if (!params)
|
|
4
|
+
return url;
|
|
5
|
+
const list = [];
|
|
6
|
+
for (const key in params) {
|
|
7
|
+
const value = params[key];
|
|
8
|
+
if (value === void 0) {
|
|
9
|
+
list.push(key);
|
|
10
|
+
} else if (Array.isArray(value)) {
|
|
11
|
+
list.push(
|
|
12
|
+
key + "=" + (isOpenType ? JSON.stringify(value).slice(1, -1) : value.join(","))
|
|
13
|
+
);
|
|
14
|
+
} else {
|
|
15
|
+
list.push(key + "=" + (isOpenType ? JSON.stringify(value) : value.toString()));
|
|
27
16
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
: url + '?' + caches;
|
|
17
|
+
}
|
|
18
|
+
if (!list.length)
|
|
19
|
+
return url;
|
|
20
|
+
const caches = list.join("&");
|
|
21
|
+
return url.includes("?") ? url + "&" + caches : url + "?" + caches;
|
|
34
22
|
};
|
|
35
23
|
export default connectURLParams;
|
package/lib/wait.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vope/utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"publishConfig": {
|
|
@@ -12,12 +12,12 @@
|
|
|
12
12
|
"main": "./cjs/index.js",
|
|
13
13
|
"scripts": {
|
|
14
14
|
"build": "npm run cjs | npm run esm | npm run types",
|
|
15
|
-
"cjs": "rimraf cjs &
|
|
16
|
-
"esm": "rimraf lib &
|
|
17
|
-
"types": "rimraf types &
|
|
15
|
+
"cjs": "rimraf cjs & esbuild ./src/**/*.ts --format=cjs --outdir=cjs",
|
|
16
|
+
"esm": "rimraf lib & esbuild ./src/**/*.ts --outdir=lib",
|
|
17
|
+
"types": "rimraf types & tsc -p ./tsconfig.types.json & node ./__devs/type.cjs",
|
|
18
18
|
"trove": "node ./__devs/trove.cjs",
|
|
19
19
|
"trove:build": "npm run devs & npm run trove",
|
|
20
|
-
"devs": "rimraf __devs &
|
|
20
|
+
"devs": "rimraf __devs & tsc -p ./tsconfig.devs.json",
|
|
21
21
|
"test": "node ./__devs/test.cjs & mocha __tests/**/*.js",
|
|
22
22
|
"test:build": "npm run devs & npm run trove & npm run test",
|
|
23
23
|
"prepublishOnly": "npm run devs & npm run trove & npm run test & npm run build"
|
|
@@ -43,6 +43,7 @@
|
|
|
43
43
|
"@types/mocha": "^10.0.1",
|
|
44
44
|
"@types/node": "^20.4.5",
|
|
45
45
|
"@vope/types": "^1.0.2",
|
|
46
|
+
"esbuild": "^0.19.4",
|
|
46
47
|
"iconv-lite": "^0.6.3",
|
|
47
48
|
"mocha": "^10.2.0",
|
|
48
49
|
"rimraf": "^5.0.1",
|
|
@@ -50,9 +51,6 @@
|
|
|
50
51
|
"tslib": "^2.6.1",
|
|
51
52
|
"typescript": "^5.1.6"
|
|
52
53
|
},
|
|
53
|
-
"dependencies": {
|
|
54
|
-
"axios": "^1.4.0"
|
|
55
|
-
},
|
|
56
54
|
"files": [
|
|
57
55
|
"cjs",
|
|
58
56
|
"lib",
|
package/types/index.d.ts
CHANGED
package/cjs/api.js
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.apiGet = exports.apiPost = void 0;
|
|
7
|
-
const params_1 = __importDefault(require("./url/params"));
|
|
8
|
-
const cut = (config, key) => {
|
|
9
|
-
if (key in config) {
|
|
10
|
-
const value = config[key];
|
|
11
|
-
delete config[key];
|
|
12
|
-
return value;
|
|
13
|
-
}
|
|
14
|
-
};
|
|
15
|
-
const apiPost = (axios, handle) => {
|
|
16
|
-
function post(url, options, config) {
|
|
17
|
-
let twist;
|
|
18
|
-
let rejectValue = false;
|
|
19
|
-
if (config) {
|
|
20
|
-
twist = cut(config, 'twist');
|
|
21
|
-
const rejectValue0 = cut(config, 'rejectValue');
|
|
22
|
-
rejectValue0 !== void 0 && (rejectValue = rejectValue0);
|
|
23
|
-
}
|
|
24
|
-
return new Promise((resolve) => {
|
|
25
|
-
axios.post(url, options, config).then(
|
|
26
|
-
// @ts-ignore
|
|
27
|
-
resolve, (e) => {
|
|
28
|
-
twist ? twist(e) : handle(e);
|
|
29
|
-
resolve(rejectValue);
|
|
30
|
-
});
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
return post;
|
|
34
|
-
};
|
|
35
|
-
exports.apiPost = apiPost;
|
|
36
|
-
const apiGet = (axios, handle) => {
|
|
37
|
-
function get(url, options, config) {
|
|
38
|
-
let twist;
|
|
39
|
-
let rejectValue = false;
|
|
40
|
-
if (config) {
|
|
41
|
-
twist = cut(config, 'twist');
|
|
42
|
-
const rejectValue0 = cut(config, 'rejectValue');
|
|
43
|
-
rejectValue0 !== void 0 && (rejectValue = rejectValue0);
|
|
44
|
-
}
|
|
45
|
-
return new Promise((resolve) => {
|
|
46
|
-
axios.get((0, params_1.default)(url, options), config).then(
|
|
47
|
-
// @ts-ignore
|
|
48
|
-
resolve, (e) => {
|
|
49
|
-
twist ? twist(e) : handle(e);
|
|
50
|
-
resolve(rejectValue);
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
return get;
|
|
55
|
-
};
|
|
56
|
-
exports.apiGet = apiGet;
|
package/lib/api.js
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import connectURLParams from './url/params';
|
|
2
|
-
const cut = (config, key) => {
|
|
3
|
-
if (key in config) {
|
|
4
|
-
const value = config[key];
|
|
5
|
-
delete config[key];
|
|
6
|
-
return value;
|
|
7
|
-
}
|
|
8
|
-
};
|
|
9
|
-
export const apiPost = (axios, handle) => {
|
|
10
|
-
function post(url, options, config) {
|
|
11
|
-
let twist;
|
|
12
|
-
let rejectValue = false;
|
|
13
|
-
if (config) {
|
|
14
|
-
twist = cut(config, 'twist');
|
|
15
|
-
const rejectValue0 = cut(config, 'rejectValue');
|
|
16
|
-
rejectValue0 !== void 0 && (rejectValue = rejectValue0);
|
|
17
|
-
}
|
|
18
|
-
return new Promise((resolve) => {
|
|
19
|
-
axios.post(url, options, config).then(
|
|
20
|
-
// @ts-ignore
|
|
21
|
-
resolve, (e) => {
|
|
22
|
-
twist ? twist(e) : handle(e);
|
|
23
|
-
resolve(rejectValue);
|
|
24
|
-
});
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
return post;
|
|
28
|
-
};
|
|
29
|
-
export const apiGet = (axios, handle) => {
|
|
30
|
-
function get(url, options, config) {
|
|
31
|
-
let twist;
|
|
32
|
-
let rejectValue = false;
|
|
33
|
-
if (config) {
|
|
34
|
-
twist = cut(config, 'twist');
|
|
35
|
-
const rejectValue0 = cut(config, 'rejectValue');
|
|
36
|
-
rejectValue0 !== void 0 && (rejectValue = rejectValue0);
|
|
37
|
-
}
|
|
38
|
-
return new Promise((resolve) => {
|
|
39
|
-
axios.get(connectURLParams(url, options), config).then(
|
|
40
|
-
// @ts-ignore
|
|
41
|
-
resolve, (e) => {
|
|
42
|
-
twist ? twist(e) : handle(e);
|
|
43
|
-
resolve(rejectValue);
|
|
44
|
-
});
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
return get;
|
|
48
|
-
};
|
package/types/api.d.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { ReviseRequired } from '@vope/types';
|
|
2
|
-
import { AxiosInstance, AxiosRequestConfig } from 'axios';
|
|
3
|
-
import connectURLParams from './url/params';
|
|
4
|
-
interface Config<V> extends AxiosRequestConfig {
|
|
5
|
-
/** 变更默认错误返回值 */
|
|
6
|
-
rejectValue?: V;
|
|
7
|
-
/** 错误拦截 */
|
|
8
|
-
twist?: (e: Error) => void;
|
|
9
|
-
}
|
|
10
|
-
type Options = NonNullable<Parameters<typeof connectURLParams>[1]>;
|
|
11
|
-
export declare const apiPost: (axios: AxiosInstance, handle: (e: Error) => void) => {
|
|
12
|
-
<T, V extends false = false>(url: string, options?: Options, config?: Config<V> | undefined): Promise<T | V>;
|
|
13
|
-
<T_1, V_1 extends string | symbol>(url: string, options: Options | undefined, config: ReviseRequired<Config<V_1>, "rejectValue">): Promise<T_1 | V_1>;
|
|
14
|
-
};
|
|
15
|
-
export declare const apiGet: (axios: AxiosInstance, handle: (e: Error) => void) => {
|
|
16
|
-
<T, V extends false = false>(url: string, options?: Options, config?: Config<V> | undefined): Promise<T | V>;
|
|
17
|
-
<T_1, V_1 extends string | symbol>(url: string, options: Options | undefined, config: ReviseRequired<Config<V_1>, "rejectValue">): Promise<T_1 | V_1>;
|
|
18
|
-
};
|
|
19
|
-
export {};
|