@hairy/utils 1.0.21 → 1.5.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/{LICENSE → LICENSE.md} +21 -23
- package/README.md +36 -0
- package/dist/index.cjs +405 -0
- package/dist/index.d.ts +254 -0
- package/dist/index.global.js +1599 -0
- package/dist/index.js +312 -0
- package/package.json +44 -38
- package/index.cjs.js +0 -130
- package/index.d.ts +0 -121
- package/index.esm.js +0 -100
- package/index.iife.js +0 -131
- package/index.iife.min.js +0 -1
package/dist/index.js
ADDED
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
// src/number/index.ts
|
|
2
|
+
import Bignumber from "bignumber.js";
|
|
3
|
+
var BIG_INTS = {
|
|
4
|
+
t: { v: 10 ** 12, d: 13, n: "t" },
|
|
5
|
+
b: { v: 10 ** 9, d: 10, n: "b" },
|
|
6
|
+
m: { v: 10 ** 6, d: 7, n: "m" },
|
|
7
|
+
k: { v: 10 ** 3, d: 4, n: "k" }
|
|
8
|
+
};
|
|
9
|
+
function unum(num = "0") {
|
|
10
|
+
return new Bignumber(numerfix(num));
|
|
11
|
+
}
|
|
12
|
+
function gte(num, n) {
|
|
13
|
+
return unum(num).gte(unum(n));
|
|
14
|
+
}
|
|
15
|
+
function gt(num, n) {
|
|
16
|
+
return unum(num).gt(unum(n));
|
|
17
|
+
}
|
|
18
|
+
function lte(num, n) {
|
|
19
|
+
return unum(num).lte(unum(n));
|
|
20
|
+
}
|
|
21
|
+
function lt(num, n) {
|
|
22
|
+
return unum(num).lt(unum(n));
|
|
23
|
+
}
|
|
24
|
+
function plus(array, options) {
|
|
25
|
+
const rounding = options?.r || Bignumber.ROUND_DOWN;
|
|
26
|
+
const decimal2 = options?.d || 0;
|
|
27
|
+
return array.filter((v) => unum(v).gt(0)).reduce((t, v) => t.plus(unum(v)), unum(0)).toFixed(decimal2, rounding);
|
|
28
|
+
}
|
|
29
|
+
function average(array, options) {
|
|
30
|
+
const rounding = options?.r || Bignumber.ROUND_DOWN;
|
|
31
|
+
const decimal2 = options?.d || 0;
|
|
32
|
+
if (array.length === 0)
|
|
33
|
+
return "0";
|
|
34
|
+
return unum(plus(array)).div(array.length).toFixed(decimal2, rounding);
|
|
35
|
+
}
|
|
36
|
+
function percentage(total, count, options) {
|
|
37
|
+
options ??= { d: 3, r: Bignumber.ROUND_DOWN };
|
|
38
|
+
const rounding = options?.r || Bignumber.ROUND_DOWN;
|
|
39
|
+
const decimal2 = options?.d || 3;
|
|
40
|
+
if (unum(total).lte(0) || unum(count).lte(0))
|
|
41
|
+
return "0";
|
|
42
|
+
return unum(count).div(unum(total)).times(100).toFixed(decimal2, rounding);
|
|
43
|
+
}
|
|
44
|
+
function zerofill(value, n = 2, type = "positive") {
|
|
45
|
+
const _value = integer(value);
|
|
46
|
+
if (_value.length >= n)
|
|
47
|
+
return value;
|
|
48
|
+
const zero = "0".repeat(n - _value.length);
|
|
49
|
+
if (type === "positive")
|
|
50
|
+
return zero + value;
|
|
51
|
+
if (type === "reverse")
|
|
52
|
+
return zero + value;
|
|
53
|
+
return "";
|
|
54
|
+
}
|
|
55
|
+
function zeromove(value) {
|
|
56
|
+
return value.toString().replace(/\.?0+$/, "");
|
|
57
|
+
}
|
|
58
|
+
function numerfix(value) {
|
|
59
|
+
const _isNaN = Number.isNaN(Number(value)) || value.toString() === "NaN";
|
|
60
|
+
if (_isNaN)
|
|
61
|
+
console.warn(`numerfix(${value}): value is not the correct value. To ensure the normal operation of the program, it will be converted to zero`);
|
|
62
|
+
return _isNaN ? "0" : String(value);
|
|
63
|
+
}
|
|
64
|
+
function integer(value) {
|
|
65
|
+
return new Bignumber(numerfix(value)).toFixed(0);
|
|
66
|
+
}
|
|
67
|
+
function decimal(value, n = 2) {
|
|
68
|
+
let [integer2, decimal2] = numerfix(value).split(".");
|
|
69
|
+
if (n <= 0)
|
|
70
|
+
return integer2;
|
|
71
|
+
if (!decimal2)
|
|
72
|
+
decimal2 = "0";
|
|
73
|
+
decimal2 = decimal2.slice(0, n);
|
|
74
|
+
decimal2 = decimal2 + "0".repeat(n - decimal2.length);
|
|
75
|
+
return `${integer2}.${decimal2}`;
|
|
76
|
+
}
|
|
77
|
+
function parseNumeric(num, delimiters = ["t", "b", "m"]) {
|
|
78
|
+
const mappings = [
|
|
79
|
+
delimiters.includes("t") && ((n) => gte(n, BIG_INTS.t.v) && BIG_INTS.t),
|
|
80
|
+
delimiters.includes("b") && ((n) => gte(n, BIG_INTS.b.v) && lt(n, BIG_INTS.t.v) && BIG_INTS.b),
|
|
81
|
+
delimiters.includes("m") && ((n) => gte(n, BIG_INTS.m.v) && lt(n, BIG_INTS.b.v) && BIG_INTS.m),
|
|
82
|
+
delimiters.includes("k") && ((n) => gte(n, BIG_INTS.k.v) && lt(n, BIG_INTS.m.v) && BIG_INTS.k)
|
|
83
|
+
];
|
|
84
|
+
let options;
|
|
85
|
+
for (const analy of mappings) {
|
|
86
|
+
const opts = analy && analy(unum(num).toFixed(0));
|
|
87
|
+
opts && (options = opts);
|
|
88
|
+
}
|
|
89
|
+
return options || { v: 1, d: 0, n: "" };
|
|
90
|
+
}
|
|
91
|
+
function formatNumeric(value = "0", options) {
|
|
92
|
+
const {
|
|
93
|
+
rounding = Bignumber.ROUND_DOWN,
|
|
94
|
+
delimiters,
|
|
95
|
+
format,
|
|
96
|
+
decimals = 2
|
|
97
|
+
} = options || {};
|
|
98
|
+
const config = parseNumeric(value, delimiters || []);
|
|
99
|
+
let number = unum(value).div(config.v).toFormat(decimals, rounding, {
|
|
100
|
+
decimalSeparator: ".",
|
|
101
|
+
groupSeparator: ",",
|
|
102
|
+
groupSize: 3,
|
|
103
|
+
secondaryGroupSize: 0,
|
|
104
|
+
fractionGroupSeparator: " ",
|
|
105
|
+
fractionGroupSize: 0,
|
|
106
|
+
...format
|
|
107
|
+
});
|
|
108
|
+
number = options?.zeromove ? zeromove(number) : number;
|
|
109
|
+
return `${number}${config.n}`;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// src/util/compose.ts
|
|
113
|
+
var compose = (...fns) => fns.reduceRight((v, f) => f(v));
|
|
114
|
+
|
|
115
|
+
// src/util/converts.ts
|
|
116
|
+
function formToObject(formData) {
|
|
117
|
+
return Object.fromEntries(formData.entries());
|
|
118
|
+
}
|
|
119
|
+
function objectToForm(object) {
|
|
120
|
+
const formData = new FormData();
|
|
121
|
+
for (const [key, value] of Object.entries(object)) formData.append(key, value);
|
|
122
|
+
return formData;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// src/util/deferred.ts
|
|
126
|
+
var Deferred = class extends Promise {
|
|
127
|
+
resolve;
|
|
128
|
+
reject;
|
|
129
|
+
constructor() {
|
|
130
|
+
let _resolve, _reject;
|
|
131
|
+
super((resolve_, reject_) => {
|
|
132
|
+
_resolve = resolve_;
|
|
133
|
+
_reject = reject_;
|
|
134
|
+
});
|
|
135
|
+
this.resolve = (value) => {
|
|
136
|
+
_resolve(value);
|
|
137
|
+
return this;
|
|
138
|
+
};
|
|
139
|
+
this.reject = (reason) => {
|
|
140
|
+
_reject(reason);
|
|
141
|
+
return this;
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
// src/util/delay.ts
|
|
147
|
+
function delay(ms) {
|
|
148
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// src/util/is.ts
|
|
152
|
+
var isBrowser = () => typeof window !== "undefined";
|
|
153
|
+
var isWeex = () => typeof WXEnvironment !== "undefined" && !!WXEnvironment.platform;
|
|
154
|
+
var weexPlatform = () => isWeex() && WXEnvironment.platform.toLowerCase();
|
|
155
|
+
var UA = () => isBrowser() && window.navigator.userAgent.toLowerCase() || "";
|
|
156
|
+
var isIE = () => UA() && /msie|trident/.test(UA());
|
|
157
|
+
var isIE9 = () => UA() && UA().indexOf("msie 9.0") > 0;
|
|
158
|
+
var isIE11 = () => isBrowser() && navigator.userAgent.includes("Trident") && navigator.userAgent.includes("rv:11.0");
|
|
159
|
+
var isEdge = () => UA() && UA().indexOf("edge/") > 0;
|
|
160
|
+
var isAndroid = () => UA() && UA().indexOf("android") > 0 || weexPlatform() === "android";
|
|
161
|
+
var isIOS = () => UA() && /iphone|ipad|ipod|ios/.test(UA()) || weexPlatform() === "ios";
|
|
162
|
+
var isChrome = () => UA() && /chrome\/\d+/.test(UA()) && !isEdge();
|
|
163
|
+
var isPhantomJS = () => UA() && /phantomjs/.test(UA());
|
|
164
|
+
var isFF = () => typeof UA() === "string" && UA().match(/firefox\/(\d+)/);
|
|
165
|
+
var isMobile = () => isBrowser() && navigator.userAgent.toLowerCase().includes("mobile");
|
|
166
|
+
var isObject = (value) => typeof value === "object" && !Array.isArray(value);
|
|
167
|
+
var isNumber = (value) => typeof value === "number";
|
|
168
|
+
var isString = (value) => typeof value === "string";
|
|
169
|
+
var isArray = (value) => Array.isArray(value);
|
|
170
|
+
var isNull = (value) => value === null;
|
|
171
|
+
var isPlainObject = (value) => typeof value === "object" && value !== null && value.constructor === Object;
|
|
172
|
+
var isFormData = (value) => isObject(value) && isBrowser() && value instanceof FormData;
|
|
173
|
+
var isWindow = (value) => typeof window !== "undefined" && toString.call(value) === "[object Window]";
|
|
174
|
+
|
|
175
|
+
// src/util/noop.ts
|
|
176
|
+
var noop = () => {
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
// src/util/p-pipe.ts
|
|
180
|
+
function pPipe(...functions) {
|
|
181
|
+
if (functions.length === 0)
|
|
182
|
+
throw new Error("Expected at least one argument");
|
|
183
|
+
return async (input) => {
|
|
184
|
+
let currentValue = input;
|
|
185
|
+
for (const function_ of functions)
|
|
186
|
+
currentValue = await function_(currentValue);
|
|
187
|
+
return currentValue;
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// src/util/pipe.ts
|
|
192
|
+
var pipe = (...fns) => fns.reduce((v, f) => f(v));
|
|
193
|
+
|
|
194
|
+
// src/util/util.ts
|
|
195
|
+
function arange(x1, x2, stp = 1, z = [], z0 = z.length) {
|
|
196
|
+
if (!x2)
|
|
197
|
+
x1 -= x2 = x1;
|
|
198
|
+
for (let z1 = z0 + Math.max(Math.ceil((++x2 - x1) / stp), 0); z0 < z1; x1 += stp)
|
|
199
|
+
z[z0++] = x1;
|
|
200
|
+
return z;
|
|
201
|
+
}
|
|
202
|
+
function loop(fn) {
|
|
203
|
+
async function next(ms) {
|
|
204
|
+
await delay(ms);
|
|
205
|
+
return fn(next);
|
|
206
|
+
}
|
|
207
|
+
return fn(next);
|
|
208
|
+
}
|
|
209
|
+
function riposte(...args) {
|
|
210
|
+
for (const [cond, value] of args) {
|
|
211
|
+
if (cond)
|
|
212
|
+
return value;
|
|
213
|
+
}
|
|
214
|
+
return void 0;
|
|
215
|
+
}
|
|
216
|
+
function unwrap(value) {
|
|
217
|
+
return typeof value === "function" ? value() : value;
|
|
218
|
+
}
|
|
219
|
+
function whenever(value, callback) {
|
|
220
|
+
return value ? callback(value) : void 0;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// src/size/index.ts
|
|
224
|
+
function formatUnit(value, unit = "px") {
|
|
225
|
+
if (!(isString(value) || isNumber(value)))
|
|
226
|
+
return "";
|
|
227
|
+
value = String(value);
|
|
228
|
+
return /\D/.test(value) ? value : value + unit;
|
|
229
|
+
}
|
|
230
|
+
function formatSize(dimension, unit) {
|
|
231
|
+
const _formatUnit = (value) => formatUnit(value, unit);
|
|
232
|
+
if (typeof dimension === "string" || typeof dimension === "number")
|
|
233
|
+
return { width: _formatUnit(dimension), height: _formatUnit(dimension) };
|
|
234
|
+
if (Array.isArray(dimension))
|
|
235
|
+
return { width: _formatUnit(dimension[0]), height: _formatUnit(dimension[1]) };
|
|
236
|
+
if (typeof dimension === "object")
|
|
237
|
+
return { width: _formatUnit(dimension.width), height: _formatUnit(dimension.height) };
|
|
238
|
+
return { width: "", height: "" };
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// src/string/index.ts
|
|
242
|
+
function cover(value, mode, symbol = "*") {
|
|
243
|
+
return value.slice(0, mode[0]) + symbol.repeat(mode[1]) + value.slice(-mode[2]);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// src/typeof/index.ts
|
|
247
|
+
function getTypeof(target) {
|
|
248
|
+
const value = Object.prototype.toString.call(target).slice(8, -1).toLocaleLowerCase();
|
|
249
|
+
return value;
|
|
250
|
+
}
|
|
251
|
+
function isTypeof(target, type) {
|
|
252
|
+
return getTypeof(target) === type;
|
|
253
|
+
}
|
|
254
|
+
export {
|
|
255
|
+
BIG_INTS,
|
|
256
|
+
Bignumber,
|
|
257
|
+
Deferred,
|
|
258
|
+
UA,
|
|
259
|
+
arange,
|
|
260
|
+
average,
|
|
261
|
+
compose,
|
|
262
|
+
cover,
|
|
263
|
+
decimal,
|
|
264
|
+
delay,
|
|
265
|
+
formToObject,
|
|
266
|
+
formatNumeric,
|
|
267
|
+
formatSize,
|
|
268
|
+
formatUnit,
|
|
269
|
+
getTypeof,
|
|
270
|
+
gt,
|
|
271
|
+
gte,
|
|
272
|
+
integer,
|
|
273
|
+
isAndroid,
|
|
274
|
+
isArray,
|
|
275
|
+
isBrowser,
|
|
276
|
+
isChrome,
|
|
277
|
+
isEdge,
|
|
278
|
+
isFF,
|
|
279
|
+
isFormData,
|
|
280
|
+
isIE,
|
|
281
|
+
isIE11,
|
|
282
|
+
isIE9,
|
|
283
|
+
isIOS,
|
|
284
|
+
isMobile,
|
|
285
|
+
isNull,
|
|
286
|
+
isNumber,
|
|
287
|
+
isObject,
|
|
288
|
+
isPhantomJS,
|
|
289
|
+
isPlainObject,
|
|
290
|
+
isString,
|
|
291
|
+
isTypeof,
|
|
292
|
+
isWeex,
|
|
293
|
+
isWindow,
|
|
294
|
+
loop,
|
|
295
|
+
lt,
|
|
296
|
+
lte,
|
|
297
|
+
noop,
|
|
298
|
+
numerfix,
|
|
299
|
+
objectToForm,
|
|
300
|
+
pPipe,
|
|
301
|
+
parseNumeric,
|
|
302
|
+
percentage,
|
|
303
|
+
pipe,
|
|
304
|
+
plus,
|
|
305
|
+
riposte,
|
|
306
|
+
unum,
|
|
307
|
+
unwrap,
|
|
308
|
+
weexPlatform,
|
|
309
|
+
whenever,
|
|
310
|
+
zerofill,
|
|
311
|
+
zeromove
|
|
312
|
+
};
|
package/package.json
CHANGED
|
@@ -1,38 +1,44 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@hairy/utils",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
"
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@hairy/utils",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "1.5.0",
|
|
5
|
+
"description": "Library for anywhere",
|
|
6
|
+
"author": "Hairyf <wwu710632@gmail.com>",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"funding": "https://github.com/sponsors/hairyf",
|
|
9
|
+
"homepage": "https://github.com/hairyf/hairylib#readme",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/hairyf/hairylib.git"
|
|
13
|
+
},
|
|
14
|
+
"bugs": "https://github.com/hairyf/hairylib/issues",
|
|
15
|
+
"keywords": [],
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"main": "./dist/index.js",
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"jsdelivr": "./dist/index.global.js"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"bignumber.js": "^9.1.2",
|
|
26
|
+
"p-pipe": "^4.0.0"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "tsup",
|
|
30
|
+
"dev": "tsup --watch",
|
|
31
|
+
"start": "tsx src/index.ts"
|
|
32
|
+
},
|
|
33
|
+
"module": "./dist/index.js",
|
|
34
|
+
"types": "./dist/index.d.ts",
|
|
35
|
+
"unpkg": "./dist/index.global.js",
|
|
36
|
+
"exports": {
|
|
37
|
+
".": {
|
|
38
|
+
"import": "./dist/index.js",
|
|
39
|
+
"require": "./dist/index.cjs",
|
|
40
|
+
"types": "./dist/index.d.ts"
|
|
41
|
+
},
|
|
42
|
+
"./*": "./*"
|
|
43
|
+
}
|
|
44
|
+
}
|
package/index.cjs.js
DELETED
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var lodashEs = require('lodash-es');
|
|
6
|
-
|
|
7
|
-
const formDataToObject = (formData) => {
|
|
8
|
-
return Object.fromEntries(formData.entries());
|
|
9
|
-
};
|
|
10
|
-
const objectToFormData = (object) => {
|
|
11
|
-
const formData = new FormData();
|
|
12
|
-
for (const [key, value] of Object.entries(object)) {
|
|
13
|
-
formData.append(key, String(value));
|
|
14
|
-
}
|
|
15
|
-
return formData;
|
|
16
|
-
};
|
|
17
|
-
const pickByParams = (params, filter, deep = false) => {
|
|
18
|
-
deep && lodashEs.forIn(params, (value, key) => {
|
|
19
|
-
if (lodashEs.isObject(value))
|
|
20
|
-
params[key] = pickByParams(params[key], filter, deep);
|
|
21
|
-
});
|
|
22
|
-
const pickValue = lodashEs.pickBy(params, (value) => !filter.includes(value));
|
|
23
|
-
if (Array.isArray(params)) {
|
|
24
|
-
return Object.values(pickValue);
|
|
25
|
-
}
|
|
26
|
-
return pickValue;
|
|
27
|
-
};
|
|
28
|
-
const objectFlat = (object, deep = 1) => {
|
|
29
|
-
const flatDeep = (object2, deep2 = 1) => {
|
|
30
|
-
let _object = {};
|
|
31
|
-
for (const [key, value] of Object.entries(object2)) {
|
|
32
|
-
if (lodashEs.isPlainObject(value)) {
|
|
33
|
-
_object = { ..._object, ...deep2 > 0 ? flatDeep(value, deep2 - 1) : value };
|
|
34
|
-
continue;
|
|
35
|
-
}
|
|
36
|
-
_object[key] = value;
|
|
37
|
-
}
|
|
38
|
-
return _object;
|
|
39
|
-
};
|
|
40
|
-
return flatDeep(object, deep);
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
const toUnit = (value, unit = "px") => {
|
|
44
|
-
const empty = !(lodashEs.isString(value) || lodashEs.isNumber(value));
|
|
45
|
-
if (empty)
|
|
46
|
-
return "";
|
|
47
|
-
return lodashEs.isString(value) && /\D/g.test(value) ? value : value + unit;
|
|
48
|
-
};
|
|
49
|
-
const toSize = (size, unit) => {
|
|
50
|
-
if (typeof size === "string" || typeof size === "number") {
|
|
51
|
-
return { width: toUnit(size, unit), height: toUnit(size, unit) };
|
|
52
|
-
}
|
|
53
|
-
if (Array.isArray(size)) {
|
|
54
|
-
return { width: toUnit(size[0], unit), height: toUnit(size[1], unit) };
|
|
55
|
-
}
|
|
56
|
-
if (typeof size === "object") {
|
|
57
|
-
return { width: toUnit(size.width, unit), height: toUnit(size.height, unit) };
|
|
58
|
-
}
|
|
59
|
-
return { width: "", height: "" };
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
const urlParamsAnaly = (url, params) => {
|
|
63
|
-
const queryString = Object.keys(params).map((key) => `${key}=${params[key]}`);
|
|
64
|
-
if (queryString.length > 0)
|
|
65
|
-
url += "?" + queryString.join("&");
|
|
66
|
-
return url;
|
|
67
|
-
};
|
|
68
|
-
const awaitPromise = (ms = 1e3) => {
|
|
69
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
70
|
-
};
|
|
71
|
-
const generateArray = (start, end) => {
|
|
72
|
-
start = Number(start);
|
|
73
|
-
end = Number(end);
|
|
74
|
-
end = end > start ? end : start;
|
|
75
|
-
return [...new Array(end + 1).keys()].slice(start);
|
|
76
|
-
};
|
|
77
|
-
const checkedTypeof = (target) => {
|
|
78
|
-
const value = Object.prototype.toString.call(target).slice(8, -1).toLocaleLowerCase();
|
|
79
|
-
return value;
|
|
80
|
-
};
|
|
81
|
-
const assert = (condition, ...infos) => {
|
|
82
|
-
if (!condition)
|
|
83
|
-
console.warn(...infos);
|
|
84
|
-
return condition;
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
const isBrowser = typeof window !== "undefined";
|
|
88
|
-
const isWeex = typeof WXEnvironment !== "undefined" && !!WXEnvironment.platform;
|
|
89
|
-
const weexPlatform = isWeex && WXEnvironment.platform.toLowerCase();
|
|
90
|
-
const UA = isBrowser && window.navigator.userAgent.toLowerCase();
|
|
91
|
-
const isIE = UA && /msie|trident/.test(UA);
|
|
92
|
-
const isIE9 = UA && UA.indexOf("msie 9.0") > 0;
|
|
93
|
-
const isIE11 = isBrowser && navigator.userAgent.includes("Trident") && navigator.userAgent.includes("rv:11.0");
|
|
94
|
-
const isEdge = UA && UA.indexOf("edge/") > 0;
|
|
95
|
-
const isAndroid = UA && UA.indexOf("android") > 0 || weexPlatform === "android";
|
|
96
|
-
const isIOS = UA && /iphone|ipad|ipod|ios/.test(UA) || weexPlatform === "ios";
|
|
97
|
-
const isChrome = UA && /chrome\/\d+/.test(UA) && !isEdge;
|
|
98
|
-
const isPhantomJS = UA && /phantomjs/.test(UA);
|
|
99
|
-
const isFF = typeof UA == "string" && UA.match(/firefox\/(\d+)/);
|
|
100
|
-
const isMobile = isBrowser && navigator.userAgent.toLowerCase().includes("mobile");
|
|
101
|
-
const isFormData = (value) => lodashEs.isObject(value) && value instanceof FormData;
|
|
102
|
-
const isWindow = (value) => typeof window !== "undefined" && toString.call(value) === "[object Window]";
|
|
103
|
-
|
|
104
|
-
exports.UA = UA;
|
|
105
|
-
exports.assert = assert;
|
|
106
|
-
exports.awaitPromise = awaitPromise;
|
|
107
|
-
exports.checkedTypeof = checkedTypeof;
|
|
108
|
-
exports.formDataToObject = formDataToObject;
|
|
109
|
-
exports.generateArray = generateArray;
|
|
110
|
-
exports.isAndroid = isAndroid;
|
|
111
|
-
exports.isBrowser = isBrowser;
|
|
112
|
-
exports.isChrome = isChrome;
|
|
113
|
-
exports.isEdge = isEdge;
|
|
114
|
-
exports.isFF = isFF;
|
|
115
|
-
exports.isFormData = isFormData;
|
|
116
|
-
exports.isIE = isIE;
|
|
117
|
-
exports.isIE11 = isIE11;
|
|
118
|
-
exports.isIE9 = isIE9;
|
|
119
|
-
exports.isIOS = isIOS;
|
|
120
|
-
exports.isMobile = isMobile;
|
|
121
|
-
exports.isPhantomJS = isPhantomJS;
|
|
122
|
-
exports.isWeex = isWeex;
|
|
123
|
-
exports.isWindow = isWindow;
|
|
124
|
-
exports.objectFlat = objectFlat;
|
|
125
|
-
exports.objectToFormData = objectToFormData;
|
|
126
|
-
exports.pickByParams = pickByParams;
|
|
127
|
-
exports.toSize = toSize;
|
|
128
|
-
exports.toUnit = toUnit;
|
|
129
|
-
exports.urlParamsAnaly = urlParamsAnaly;
|
|
130
|
-
exports.weexPlatform = weexPlatform;
|
package/index.d.ts
DELETED
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 将 formData 转换为 object
|
|
3
|
-
* @param formData
|
|
4
|
-
*/
|
|
5
|
-
declare const formDataToObject: (formData: FormData) => Record<string, string>;
|
|
6
|
-
/**
|
|
7
|
-
* 将 object 转换为 formData
|
|
8
|
-
* @param object
|
|
9
|
-
*/
|
|
10
|
-
declare const objectToFormData: (object: Record<string, string | number>) => FormData;
|
|
11
|
-
/**
|
|
12
|
-
* 过滤对象|数组中 filter 中的值
|
|
13
|
-
* @param params
|
|
14
|
-
* @param filter
|
|
15
|
-
*/
|
|
16
|
-
declare const pickByParams: <T extends object>(params: T, filter: any[], deep?: boolean) => Partial<T>;
|
|
17
|
-
/**
|
|
18
|
-
* 对象扁平化处理
|
|
19
|
-
* @param object 对象
|
|
20
|
-
* @param deep 深度
|
|
21
|
-
*/
|
|
22
|
-
declare const objectFlat: (object: Record<string, any>, deep?: number) => Record<string, any>;
|
|
23
|
-
|
|
24
|
-
declare type PlainObject = {
|
|
25
|
-
[key: string]: any;
|
|
26
|
-
};
|
|
27
|
-
declare type LooseNumber = string | number;
|
|
28
|
-
declare type Key = string | number | symbol;
|
|
29
|
-
declare type DeepReadonly<T> = {
|
|
30
|
-
readonly [P in keyof T]: T[P] extends object ? DeepReadonly<T[P]> : T[P];
|
|
31
|
-
};
|
|
32
|
-
declare type DeepRequired<T> = {
|
|
33
|
-
[P in keyof T]-?: T[P] extends object ? DeepRequired<T[P]> : T[P];
|
|
34
|
-
};
|
|
35
|
-
declare type DeepPartial<T> = {
|
|
36
|
-
[P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P];
|
|
37
|
-
};
|
|
38
|
-
declare type DeepKeyof<T> = T extends object ? keyof T | DeepKeyof<T[keyof T]> : never;
|
|
39
|
-
declare type DeepReplace<T, K = unknown, V = unknown> = {
|
|
40
|
-
[P in keyof T]: K extends P ? V : DeepReplace<T[P], K, V>;
|
|
41
|
-
};
|
|
42
|
-
declare type Option<L extends Key = 'label', V extends Key = 'value', C extends Key = 'children'> = {
|
|
43
|
-
[P in L]?: string;
|
|
44
|
-
} & {
|
|
45
|
-
[P in V]?: LooseNumber;
|
|
46
|
-
} & {
|
|
47
|
-
[P in C]?: Option<L, V, C>[];
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* 如果有单位,如百分比,px单位等,直接返回,如果是纯粹的数值,则加上px单位
|
|
52
|
-
* @param value 尺寸
|
|
53
|
-
* @param unit 单位
|
|
54
|
-
* @returns string
|
|
55
|
-
*/
|
|
56
|
-
declare const toUnit: (value: LooseNumber, unit?: string) => string;
|
|
57
|
-
/** size 转换配置 */
|
|
58
|
-
declare type toSizeOption = LooseNumber | [LooseNumber, LooseNumber] | {
|
|
59
|
-
width: LooseNumber;
|
|
60
|
-
height: LooseNumber;
|
|
61
|
-
};
|
|
62
|
-
/**
|
|
63
|
-
* 将 size 转换为宽高
|
|
64
|
-
* @param size { toSizeOption }
|
|
65
|
-
* @returns
|
|
66
|
-
*/
|
|
67
|
-
declare const toSize: (size: toSizeOption, unit?: string | undefined) => {
|
|
68
|
-
width: string;
|
|
69
|
-
height: string;
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* 地址参数计算
|
|
74
|
-
* @param url 传入url
|
|
75
|
-
* @param params 请求参数
|
|
76
|
-
* @returns 拼接url
|
|
77
|
-
*/
|
|
78
|
-
declare const urlParamsAnaly: (url: string, params: Record<string, any>) => string;
|
|
79
|
-
/**
|
|
80
|
-
* 自定义 Promise 等待
|
|
81
|
-
* @param code 等待时间
|
|
82
|
-
*/
|
|
83
|
-
declare const awaitPromise: (ms?: number) => Promise<unknown>;
|
|
84
|
-
/**
|
|
85
|
-
* 生成递进的数组
|
|
86
|
-
* @param start 开始数值
|
|
87
|
-
* @param end 结束数值
|
|
88
|
-
* @returns 递进的数组
|
|
89
|
-
*/
|
|
90
|
-
declare const generateArray: (start: number, end: number) => number[];
|
|
91
|
-
/**
|
|
92
|
-
* 获取数据类型
|
|
93
|
-
* @param target 检测对象
|
|
94
|
-
* @returns 返回字符串
|
|
95
|
-
*/
|
|
96
|
-
declare const checkedTypeof: (target: any) => "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" | "null" | "regexp";
|
|
97
|
-
/**
|
|
98
|
-
* 不符合预期则弹出警告
|
|
99
|
-
* @param condition
|
|
100
|
-
* @param infos
|
|
101
|
-
*/
|
|
102
|
-
declare const assert: (condition: boolean, ...infos: any[]) => boolean;
|
|
103
|
-
|
|
104
|
-
declare const isBrowser: boolean;
|
|
105
|
-
declare const isWeex: boolean;
|
|
106
|
-
declare const weexPlatform: any;
|
|
107
|
-
declare const UA: string | false;
|
|
108
|
-
declare const isIE: boolean | "";
|
|
109
|
-
declare const isIE9: boolean | "";
|
|
110
|
-
declare const isIE11: boolean;
|
|
111
|
-
declare const isEdge: boolean | "";
|
|
112
|
-
declare const isAndroid: boolean;
|
|
113
|
-
declare const isIOS: boolean;
|
|
114
|
-
declare const isChrome: boolean | "";
|
|
115
|
-
declare const isPhantomJS: boolean | "";
|
|
116
|
-
declare const isFF: false | RegExpMatchArray | null;
|
|
117
|
-
declare const isMobile: boolean;
|
|
118
|
-
declare const isFormData: (value: any) => value is FormData;
|
|
119
|
-
declare const isWindow: (value: any) => value is Window;
|
|
120
|
-
|
|
121
|
-
export { DeepKeyof, DeepPartial, DeepReadonly, DeepReplace, DeepRequired, Key, LooseNumber, Option, PlainObject, UA, assert, awaitPromise, checkedTypeof, formDataToObject, generateArray, isAndroid, isBrowser, isChrome, isEdge, isFF, isFormData, isIE, isIE11, isIE9, isIOS, isMobile, isPhantomJS, isWeex, isWindow, objectFlat, objectToFormData, pickByParams, toSize, toSizeOption, toUnit, urlParamsAnaly, weexPlatform };
|