@vope/utils 1.0.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/README.md +29 -0
- package/cjs/api.js +56 -0
- package/cjs/compare/value.js +41 -0
- package/cjs/convert/json.js +18 -0
- package/cjs/convert/object.js +18 -0
- package/cjs/convert/strings.js +12 -0
- package/cjs/index.js +27 -0
- package/cjs/is/class.js +33 -0
- package/cjs/is/empty.js +12 -0
- package/cjs/is/null.js +8 -0
- package/cjs/url/join.js +16 -0
- package/cjs/url/params.js +37 -0
- package/lib/api.js +48 -0
- package/lib/compare/value.js +39 -0
- package/lib/convert/json.js +16 -0
- package/lib/convert/object.js +16 -0
- package/lib/convert/strings.js +10 -0
- package/lib/index.js +10 -0
- package/lib/is/class.js +29 -0
- package/lib/is/empty.js +7 -0
- package/lib/is/null.js +6 -0
- package/lib/url/join.js +12 -0
- package/lib/url/params.js +35 -0
- package/package.json +62 -0
- package/types/api.d.ts +19 -0
- package/types/compare/value.d.ts +5 -0
- package/types/convert/json.d.ts +13 -0
- package/types/convert/object.d.ts +13 -0
- package/types/convert/strings.d.ts +7 -0
- package/types/index.d.ts +10 -0
- package/types/is/class.d.ts +4 -0
- package/types/is/empty.d.ts +5 -0
- package/types/is/null.d.ts +5 -0
- package/types/url/join.d.ts +4 -0
- package/types/url/params.d.ts +11 -0
package/README.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
### build
|
|
2
|
+
```
|
|
3
|
+
发布构建
|
|
4
|
+
```
|
|
5
|
+
|
|
6
|
+
### cjs
|
|
7
|
+
```
|
|
8
|
+
构建cjs版本
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
### esm
|
|
12
|
+
```
|
|
13
|
+
构建esm版本
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
### trove
|
|
17
|
+
```
|
|
18
|
+
构建统一导出入口(即生成src/index.ts文件)
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
### devs
|
|
22
|
+
```
|
|
23
|
+
构建开发环境工具代码
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### test
|
|
27
|
+
```
|
|
28
|
+
运行测试用例
|
|
29
|
+
```
|
package/cjs/api.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
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;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
* 值比较(深度浅比较)
|
|
5
|
+
*/
|
|
6
|
+
const compareValue = (from, to) => {
|
|
7
|
+
if (typeof from !== 'object'
|
|
8
|
+
|| from === null
|
|
9
|
+
|| typeof to !== 'object'
|
|
10
|
+
|| to === null)
|
|
11
|
+
return from === to;
|
|
12
|
+
if (Array.isArray(from) && Array.isArray(to)) {
|
|
13
|
+
if (from.length !== to.length)
|
|
14
|
+
return false;
|
|
15
|
+
return !from.some((t1, index) => {
|
|
16
|
+
if (!compareValue(t1, to[index])) {
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
const caches = {};
|
|
23
|
+
for (const key in from) {
|
|
24
|
+
if (!compareValue(from[key], to[key])) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
caches[key] = key;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
for (const key in to) {
|
|
32
|
+
if (key in caches)
|
|
33
|
+
continue;
|
|
34
|
+
if (!compareValue(from[key], to[key])) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return true;
|
|
40
|
+
};
|
|
41
|
+
exports.default = compareValue;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
* 对象数组转hash(要求主键值为string类型)
|
|
5
|
+
* @example
|
|
6
|
+
* hash8json(
|
|
7
|
+
* [
|
|
8
|
+
* { key: 'a', value: 1 },
|
|
9
|
+
* { key: 'b', value: 2 }
|
|
10
|
+
* ],
|
|
11
|
+
* 'key'
|
|
12
|
+
* ).a.value // 1
|
|
13
|
+
*/
|
|
14
|
+
const hash8json = (list, key) => list.reduce((caches, data) => {
|
|
15
|
+
caches[data[key]] = data;
|
|
16
|
+
return caches;
|
|
17
|
+
}, {});
|
|
18
|
+
exports.default = hash8json;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
* 对象数组转hash
|
|
5
|
+
* @example
|
|
6
|
+
* hash8object(
|
|
7
|
+
* [
|
|
8
|
+
* { key: String, value: 1 },
|
|
9
|
+
* { key: Object, value: 2 }
|
|
10
|
+
* ],
|
|
11
|
+
* 'key'
|
|
12
|
+
* ).get(Object).value // 2
|
|
13
|
+
*/
|
|
14
|
+
const hash8object = (list, key) => list.reduce((caches, data) => {
|
|
15
|
+
caches.set(data[key], data);
|
|
16
|
+
return caches;
|
|
17
|
+
}, new Map());
|
|
18
|
+
exports.default = hash8object;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
* 字符数组转hash
|
|
5
|
+
* @example
|
|
6
|
+
* hash8strings(['a', 'b']).a // 0
|
|
7
|
+
*/
|
|
8
|
+
const hash8strings = (list) => list.reduce((caches, data, index) => {
|
|
9
|
+
caches[data] = index;
|
|
10
|
+
return caches;
|
|
11
|
+
}, {});
|
|
12
|
+
exports.default = hash8strings;
|
package/cjs/index.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
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.connectURLParams = exports.joinURLs = exports.isNull = exports.isEmpty = exports.isClass = exports.hash8strings = exports.hash8object = exports.hash8json = exports.compareValue = exports.apiGet = exports.apiPost = void 0;
|
|
7
|
+
var api_1 = require("./api");
|
|
8
|
+
Object.defineProperty(exports, "apiPost", { enumerable: true, get: function () { return api_1.apiPost; } });
|
|
9
|
+
Object.defineProperty(exports, "apiGet", { enumerable: true, get: function () { return api_1.apiGet; } });
|
|
10
|
+
var value_1 = require("./compare/value");
|
|
11
|
+
Object.defineProperty(exports, "compareValue", { enumerable: true, get: function () { return __importDefault(value_1).default; } });
|
|
12
|
+
var json_1 = require("./convert/json");
|
|
13
|
+
Object.defineProperty(exports, "hash8json", { enumerable: true, get: function () { return __importDefault(json_1).default; } });
|
|
14
|
+
var object_1 = require("./convert/object");
|
|
15
|
+
Object.defineProperty(exports, "hash8object", { enumerable: true, get: function () { return __importDefault(object_1).default; } });
|
|
16
|
+
var strings_1 = require("./convert/strings");
|
|
17
|
+
Object.defineProperty(exports, "hash8strings", { enumerable: true, get: function () { return __importDefault(strings_1).default; } });
|
|
18
|
+
var class_1 = require("./is/class");
|
|
19
|
+
Object.defineProperty(exports, "isClass", { enumerable: true, get: function () { return class_1.isClass; } });
|
|
20
|
+
var empty_1 = require("./is/empty");
|
|
21
|
+
Object.defineProperty(exports, "isEmpty", { enumerable: true, get: function () { return __importDefault(empty_1).default; } });
|
|
22
|
+
var null_1 = require("./is/null");
|
|
23
|
+
Object.defineProperty(exports, "isNull", { enumerable: true, get: function () { return __importDefault(null_1).default; } });
|
|
24
|
+
var join_1 = require("./url/join");
|
|
25
|
+
Object.defineProperty(exports, "joinURLs", { enumerable: true, get: function () { return join_1.joinURLs; } });
|
|
26
|
+
var params_1 = require("./url/params");
|
|
27
|
+
Object.defineProperty(exports, "connectURLParams", { enumerable: true, get: function () { return __importDefault(params_1).default; } });
|
package/cjs/is/class.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isClass = void 0;
|
|
4
|
+
const re_anonymous = /^function\s+\(|^function\s+anonymous\(/;
|
|
5
|
+
const re_function = /^function\s+[A-Z]/;
|
|
6
|
+
const re_this = /\b\(this\b|\bthis[\.\[]\b/;
|
|
7
|
+
const re_check = /classCallCheck\(this/;
|
|
8
|
+
const re_default = /^function\sdefault_\d+\s*\(/;
|
|
9
|
+
/**
|
|
10
|
+
* 检测是否为class
|
|
11
|
+
*/
|
|
12
|
+
const isClass = (value, strict = true) => {
|
|
13
|
+
if (typeof value !== 'function')
|
|
14
|
+
return false;
|
|
15
|
+
const proto = value.prototype;
|
|
16
|
+
if (!proto || proto.constructor !== value)
|
|
17
|
+
return false;
|
|
18
|
+
const str = value.toString();
|
|
19
|
+
if (str.slice(0, 5) === 'class'
|
|
20
|
+
|| Object.getOwnPropertyNames(proto).length >= 2)
|
|
21
|
+
return true;
|
|
22
|
+
if (re_anonymous.test(str))
|
|
23
|
+
return false;
|
|
24
|
+
if (strict && re_function.test(str))
|
|
25
|
+
return true;
|
|
26
|
+
if (re_this.test(str)) {
|
|
27
|
+
if (!strict || re_check.test(str))
|
|
28
|
+
return true;
|
|
29
|
+
return re_default.test(str);
|
|
30
|
+
}
|
|
31
|
+
return false;
|
|
32
|
+
};
|
|
33
|
+
exports.isClass = isClass;
|
package/cjs/is/empty.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
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
|
+
const null_1 = __importDefault(require("./null"));
|
|
7
|
+
/**
|
|
8
|
+
* 检测是否为空(undefined/null/'')
|
|
9
|
+
*/
|
|
10
|
+
const isEmpty = (value) => (0, null_1.default)(value)
|
|
11
|
+
|| value === '';
|
|
12
|
+
exports.default = isEmpty;
|
package/cjs/is/null.js
ADDED
package/cjs/url/join.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.joinURLs = void 0;
|
|
4
|
+
const re = /(^|[^:/])\/{2,}/g;
|
|
5
|
+
/**
|
|
6
|
+
* 实现多个路径拼接
|
|
7
|
+
*/
|
|
8
|
+
const joinURLs = (...list) => {
|
|
9
|
+
const { length } = list;
|
|
10
|
+
if (length === 0)
|
|
11
|
+
return '';
|
|
12
|
+
return length === 1
|
|
13
|
+
? list[0]
|
|
14
|
+
: list.join('/').replace(re, ($0, $1) => `${$1}/`);
|
|
15
|
+
};
|
|
16
|
+
exports.joinURLs = joinURLs;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
* url拼接参数
|
|
5
|
+
* @param url
|
|
6
|
+
* @param params 需要让url携带的参数
|
|
7
|
+
* @param isOpenType 是否开启类型处理
|
|
8
|
+
* @description 开始类型处理的情况下,undefined会被转成null
|
|
9
|
+
*/
|
|
10
|
+
const connectURLParams = (url, params, isOpenType) => {
|
|
11
|
+
if (!params)
|
|
12
|
+
return url;
|
|
13
|
+
const list = [];
|
|
14
|
+
for (const key in params) {
|
|
15
|
+
const value = params[key];
|
|
16
|
+
if (value === void 0) {
|
|
17
|
+
list.push(key);
|
|
18
|
+
}
|
|
19
|
+
else if (Array.isArray(value)) {
|
|
20
|
+
list.push(key
|
|
21
|
+
+ '='
|
|
22
|
+
+ (isOpenType
|
|
23
|
+
? JSON.stringify(value).slice(1, -1)
|
|
24
|
+
: value.join(',')));
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
list.push(key + '=' + (isOpenType ? JSON.stringify(value) : value.toString()));
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (!list.length)
|
|
31
|
+
return url;
|
|
32
|
+
const caches = list.join('&');
|
|
33
|
+
return url.includes('?')
|
|
34
|
+
? url + '&' + caches
|
|
35
|
+
: url + '?' + caches;
|
|
36
|
+
};
|
|
37
|
+
exports.default = connectURLParams;
|
package/lib/api.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 值比较(深度浅比较)
|
|
3
|
+
*/
|
|
4
|
+
const compareValue = (from, to) => {
|
|
5
|
+
if (typeof from !== 'object'
|
|
6
|
+
|| from === null
|
|
7
|
+
|| typeof to !== 'object'
|
|
8
|
+
|| to === null)
|
|
9
|
+
return from === to;
|
|
10
|
+
if (Array.isArray(from) && Array.isArray(to)) {
|
|
11
|
+
if (from.length !== to.length)
|
|
12
|
+
return false;
|
|
13
|
+
return !from.some((t1, index) => {
|
|
14
|
+
if (!compareValue(t1, to[index])) {
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
const caches = {};
|
|
21
|
+
for (const key in from) {
|
|
22
|
+
if (!compareValue(from[key], to[key])) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
caches[key] = key;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
for (const key in to) {
|
|
30
|
+
if (key in caches)
|
|
31
|
+
continue;
|
|
32
|
+
if (!compareValue(from[key], to[key])) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return true;
|
|
38
|
+
};
|
|
39
|
+
export default compareValue;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 对象数组转hash(要求主键值为string类型)
|
|
3
|
+
* @example
|
|
4
|
+
* hash8json(
|
|
5
|
+
* [
|
|
6
|
+
* { key: 'a', value: 1 },
|
|
7
|
+
* { key: 'b', value: 2 }
|
|
8
|
+
* ],
|
|
9
|
+
* 'key'
|
|
10
|
+
* ).a.value // 1
|
|
11
|
+
*/
|
|
12
|
+
const hash8json = (list, key) => list.reduce((caches, data) => {
|
|
13
|
+
caches[data[key]] = data;
|
|
14
|
+
return caches;
|
|
15
|
+
}, {});
|
|
16
|
+
export default hash8json;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 对象数组转hash
|
|
3
|
+
* @example
|
|
4
|
+
* hash8object(
|
|
5
|
+
* [
|
|
6
|
+
* { key: String, value: 1 },
|
|
7
|
+
* { key: Object, value: 2 }
|
|
8
|
+
* ],
|
|
9
|
+
* 'key'
|
|
10
|
+
* ).get(Object).value // 2
|
|
11
|
+
*/
|
|
12
|
+
const hash8object = (list, key) => list.reduce((caches, data) => {
|
|
13
|
+
caches.set(data[key], data);
|
|
14
|
+
return caches;
|
|
15
|
+
}, new Map());
|
|
16
|
+
export default hash8object;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { apiPost, apiGet } from './api';
|
|
2
|
+
export { default as compareValue } from './compare/value';
|
|
3
|
+
export { default as hash8json } from './convert/json';
|
|
4
|
+
export { default as hash8object } from './convert/object';
|
|
5
|
+
export { default as hash8strings } from './convert/strings';
|
|
6
|
+
export { isClass } from './is/class';
|
|
7
|
+
export { default as isEmpty } from './is/empty';
|
|
8
|
+
export { default as isNull } from './is/null';
|
|
9
|
+
export { joinURLs } from './url/join';
|
|
10
|
+
export { default as connectURLParams } from './url/params';
|
package/lib/is/class.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
const re_anonymous = /^function\s+\(|^function\s+anonymous\(/;
|
|
2
|
+
const re_function = /^function\s+[A-Z]/;
|
|
3
|
+
const re_this = /\b\(this\b|\bthis[\.\[]\b/;
|
|
4
|
+
const re_check = /classCallCheck\(this/;
|
|
5
|
+
const re_default = /^function\sdefault_\d+\s*\(/;
|
|
6
|
+
/**
|
|
7
|
+
* 检测是否为class
|
|
8
|
+
*/
|
|
9
|
+
export const isClass = (value, strict = true) => {
|
|
10
|
+
if (typeof value !== 'function')
|
|
11
|
+
return false;
|
|
12
|
+
const proto = value.prototype;
|
|
13
|
+
if (!proto || proto.constructor !== value)
|
|
14
|
+
return false;
|
|
15
|
+
const str = value.toString();
|
|
16
|
+
if (str.slice(0, 5) === 'class'
|
|
17
|
+
|| Object.getOwnPropertyNames(proto).length >= 2)
|
|
18
|
+
return true;
|
|
19
|
+
if (re_anonymous.test(str))
|
|
20
|
+
return false;
|
|
21
|
+
if (strict && re_function.test(str))
|
|
22
|
+
return true;
|
|
23
|
+
if (re_this.test(str)) {
|
|
24
|
+
if (!strict || re_check.test(str))
|
|
25
|
+
return true;
|
|
26
|
+
return re_default.test(str);
|
|
27
|
+
}
|
|
28
|
+
return false;
|
|
29
|
+
};
|
package/lib/is/empty.js
ADDED
package/lib/is/null.js
ADDED
package/lib/url/join.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* url拼接参数
|
|
3
|
+
* @param url
|
|
4
|
+
* @param params 需要让url携带的参数
|
|
5
|
+
* @param isOpenType 是否开启类型处理
|
|
6
|
+
* @description 开始类型处理的情况下,undefined会被转成null
|
|
7
|
+
*/
|
|
8
|
+
const connectURLParams = (url, params, isOpenType) => {
|
|
9
|
+
if (!params)
|
|
10
|
+
return url;
|
|
11
|
+
const list = [];
|
|
12
|
+
for (const key in params) {
|
|
13
|
+
const value = params[key];
|
|
14
|
+
if (value === void 0) {
|
|
15
|
+
list.push(key);
|
|
16
|
+
}
|
|
17
|
+
else if (Array.isArray(value)) {
|
|
18
|
+
list.push(key
|
|
19
|
+
+ '='
|
|
20
|
+
+ (isOpenType
|
|
21
|
+
? JSON.stringify(value).slice(1, -1)
|
|
22
|
+
: value.join(',')));
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
list.push(key + '=' + (isOpenType ? JSON.stringify(value) : value.toString()));
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
if (!list.length)
|
|
29
|
+
return url;
|
|
30
|
+
const caches = list.join('&');
|
|
31
|
+
return url.includes('?')
|
|
32
|
+
? url + '&' + caches
|
|
33
|
+
: url + '?' + caches;
|
|
34
|
+
};
|
|
35
|
+
export default connectURLParams;
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vope/utils",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public",
|
|
8
|
+
"registry": "https://registry.npmjs.org/"
|
|
9
|
+
},
|
|
10
|
+
"types": "./types/index.d.ts",
|
|
11
|
+
"module": "./lib/index.js",
|
|
12
|
+
"main": "./cjs/index.js",
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "rimraf types & npm run cjs | npm run esm",
|
|
15
|
+
"cjs": "rimraf cjs & npx tsc -p ./tsconfig.cjs.json",
|
|
16
|
+
"esm": "rimraf lib & npx tsc -p ./tsconfig.esm.json",
|
|
17
|
+
"trove": "node ./__devs/trove.cjs",
|
|
18
|
+
"trove:build": "npm run devs & npm run trove",
|
|
19
|
+
"devs": "rimraf __devs & npx tsc -p ./tsconfig.devs.json",
|
|
20
|
+
"test": "mocha __tests/**/*.js",
|
|
21
|
+
"test:dev": "npm run test:build & npm run test",
|
|
22
|
+
"test:build": "rimraf __tests & rimraf lib & node ./__devs/test.cjs",
|
|
23
|
+
"test:all": "npm run devs & npm run test:build & npm run test",
|
|
24
|
+
"postinstall": "npm run devs & npm run trove & npm run test:build",
|
|
25
|
+
"prepublishOnly": "npm run devs & npm run trove & npm run build"
|
|
26
|
+
},
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"import": "./lib/index.js",
|
|
30
|
+
"require": "./cjs/index.js"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"author": "vope",
|
|
34
|
+
"license": "ISC",
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@babel/core": "^7.22.9",
|
|
37
|
+
"@babel/generator": "^7.22.9",
|
|
38
|
+
"@babel/parser": "^7.22.7",
|
|
39
|
+
"@babel/traverse": "^7.22.8",
|
|
40
|
+
"@babel/types": "^7.22.5",
|
|
41
|
+
"@rollup/plugin-alias": "^5.0.0",
|
|
42
|
+
"@rollup/plugin-typescript": "^11.1.2",
|
|
43
|
+
"@types/babel__generator": "^7.6.4",
|
|
44
|
+
"@types/babel__traverse": "^7.20.1",
|
|
45
|
+
"@types/mocha": "^10.0.1",
|
|
46
|
+
"@types/node": "^20.4.5",
|
|
47
|
+
"@vope/types": "^1.0.0",
|
|
48
|
+
"mocha": "^10.2.0",
|
|
49
|
+
"rimraf": "^5.0.1",
|
|
50
|
+
"rollup": "^3.27.1",
|
|
51
|
+
"tslib": "^2.6.1",
|
|
52
|
+
"typescript": "^5.1.6"
|
|
53
|
+
},
|
|
54
|
+
"dependencies": {
|
|
55
|
+
"axios": "^1.4.0"
|
|
56
|
+
},
|
|
57
|
+
"files": [
|
|
58
|
+
"cjs",
|
|
59
|
+
"lib",
|
|
60
|
+
"types"
|
|
61
|
+
]
|
|
62
|
+
}
|
package/types/api.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
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 {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 对象数组转hash(要求主键值为string类型)
|
|
3
|
+
* @example
|
|
4
|
+
* hash8json(
|
|
5
|
+
* [
|
|
6
|
+
* { key: 'a', value: 1 },
|
|
7
|
+
* { key: 'b', value: 2 }
|
|
8
|
+
* ],
|
|
9
|
+
* 'key'
|
|
10
|
+
* ).a.value // 1
|
|
11
|
+
*/
|
|
12
|
+
declare const hash8json: <K extends string | symbol, T extends Record<K, string>>(list: T[], key: K) => Record<T[K], T>;
|
|
13
|
+
export default hash8json;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 对象数组转hash
|
|
3
|
+
* @example
|
|
4
|
+
* hash8object(
|
|
5
|
+
* [
|
|
6
|
+
* { key: String, value: 1 },
|
|
7
|
+
* { key: Object, value: 2 }
|
|
8
|
+
* ],
|
|
9
|
+
* 'key'
|
|
10
|
+
* ).get(Object).value // 2
|
|
11
|
+
*/
|
|
12
|
+
declare const hash8object: <K extends string | symbol, T extends Record<K, unknown>>(list: T[], key: K) => Map<T[K], T>;
|
|
13
|
+
export default hash8object;
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { apiPost, apiGet } from './api';
|
|
2
|
+
export { default as compareValue } from './compare/value';
|
|
3
|
+
export { default as hash8json } from './convert/json';
|
|
4
|
+
export { default as hash8object } from './convert/object';
|
|
5
|
+
export { default as hash8strings } from './convert/strings';
|
|
6
|
+
export { isClass } from './is/class';
|
|
7
|
+
export { default as isEmpty } from './is/empty';
|
|
8
|
+
export { default as isNull } from './is/null';
|
|
9
|
+
export { joinURLs } from './url/join';
|
|
10
|
+
export { default as connectURLParams } from './url/params';
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
type Param = string | number | boolean | undefined;
|
|
2
|
+
type Params = Param | Param[];
|
|
3
|
+
/**
|
|
4
|
+
* url拼接参数
|
|
5
|
+
* @param url
|
|
6
|
+
* @param params 需要让url携带的参数
|
|
7
|
+
* @param isOpenType 是否开启类型处理
|
|
8
|
+
* @description 开始类型处理的情况下,undefined会被转成null
|
|
9
|
+
*/
|
|
10
|
+
declare const connectURLParams: (url: string, params?: Record<string, Params>, isOpenType?: boolean) => string;
|
|
11
|
+
export default connectURLParams;
|