@fluixi/utils 1.0.0-alpha.83 → 1.0.0-alpha.84
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/index.cjs +2858 -1
- package/dist/index.mjs +2838 -1
- package/dist/lib/array/advanced.cjs +2227 -1
- package/dist/lib/array/advanced.mjs +2204 -1
- package/dist/lib/array/array.cjs +57 -1
- package/dist/lib/array/array.mjs +36 -1
- package/dist/lib/array/index.cjs +2344 -1
- package/dist/lib/array/index.mjs +2322 -1
- package/dist/lib/array/utils.cjs +103 -1
- package/dist/lib/array/utils.mjs +82 -1
- package/dist/lib/color/color.cjs +2190 -1
- package/dist/lib/color/color.mjs +2167 -1
- package/dist/lib/color/constants.cjs +380 -1
- package/dist/lib/color/constants.mjs +359 -1
- package/dist/lib/color/conversions.cjs +629 -1
- package/dist/lib/color/conversions.mjs +608 -1
- package/dist/lib/color/helpers.cjs +2534 -1
- package/dist/lib/color/helpers.mjs +2511 -1
- package/dist/lib/color/index.cjs +2641 -1
- package/dist/lib/color/index.mjs +2619 -1
- package/dist/lib/color/types.cjs +18 -1
- package/dist/lib/color/utils.cjs +962 -1
- package/dist/lib/color/utils.mjs +939 -1
- package/dist/lib/compare/comparators.cjs +171 -1
- package/dist/lib/compare/comparators.mjs +150 -1
- package/dist/lib/compare/index.cjs +172 -1
- package/dist/lib/compare/index.mjs +152 -1
- package/dist/lib/crypto/hash.cjs +83 -1
- package/dist/lib/crypto/hash.mjs +62 -1
- package/dist/lib/crypto/index.cjs +85 -1
- package/dist/lib/crypto/index.mjs +62 -1
- package/dist/lib/dom/events.cjs +40 -1
- package/dist/lib/dom/events.mjs +19 -1
- package/dist/lib/dom/index.cjs +78 -1
- package/dist/lib/dom/index.mjs +56 -1
- package/dist/lib/dom/utils.cjs +57 -1
- package/dist/lib/dom/utils.mjs +36 -1
- package/dist/lib/functions/functions.cjs +366 -1
- package/dist/lib/functions/functions.mjs +345 -1
- package/dist/lib/functions/index.cjs +369 -1
- package/dist/lib/functions/index.mjs +347 -1
- package/dist/lib/functions/types.cjs +18 -1
- package/dist/lib/helpers.cjs +78 -1
- package/dist/lib/helpers.mjs +59 -1
- package/dist/lib/index.cjs +2857 -1
- package/dist/lib/index.mjs +2836 -1
- package/dist/lib/maths/constants.cjs +70 -1
- package/dist/lib/maths/constants.mjs +50 -1
- package/dist/lib/maths/fn/advanced.cjs +231 -1
- package/dist/lib/maths/fn/advanced.mjs +208 -1
- package/dist/lib/maths/fn/basic.cjs +212 -1
- package/dist/lib/maths/fn/basic.mjs +191 -1
- package/dist/lib/maths/fn/complex.cjs +268 -1
- package/dist/lib/maths/fn/complex.mjs +247 -1
- package/dist/lib/maths/fn/index.cjs +877 -1
- package/dist/lib/maths/fn/index.mjs +855 -1
- package/dist/lib/maths/fn/trigo.cjs +179 -1
- package/dist/lib/maths/fn/trigo.mjs +156 -1
- package/dist/lib/maths/fn/vector.cjs +728 -1
- package/dist/lib/maths/fn/vector.mjs +707 -1
- package/dist/lib/maths/formatter.cjs +61 -1
- package/dist/lib/maths/formatter.mjs +40 -1
- package/dist/lib/maths/guards.cjs +164 -1
- package/dist/lib/maths/guards.mjs +144 -1
- package/dist/lib/maths/index.cjs +1152 -1
- package/dist/lib/maths/index.mjs +1130 -1
- package/dist/lib/maths/parser.cjs +75 -1
- package/dist/lib/maths/parser.mjs +54 -1
- package/dist/lib/maths/random.cjs +49 -1
- package/dist/lib/maths/random.mjs +28 -1
- package/dist/lib/maths/types/advanced.cjs +18 -1
- package/dist/lib/maths/types/basic.cjs +18 -1
- package/dist/lib/maths/types/index.cjs +19 -1
- package/dist/lib/maths/types/index.mjs +1 -0
- package/dist/lib/maths/types/negative.cjs +18 -1
- package/dist/lib/maths/types/operations.cjs +18 -1
- package/dist/lib/object/clone.cjs +88 -1
- package/dist/lib/object/clone.mjs +65 -1
- package/dist/lib/object/diff.cjs +573 -1
- package/dist/lib/object/diff.mjs +550 -1
- package/dist/lib/object/getter-setter.cjs +2351 -1
- package/dist/lib/object/getter-setter.mjs +2330 -1
- package/dist/lib/object/index.cjs +3956 -6
- package/dist/lib/object/index.mjs +3936 -6
- package/dist/lib/object/merge.cjs +1029 -1
- package/dist/lib/object/merge.mjs +1006 -1
- package/dist/lib/object/object.cjs +280 -1
- package/dist/lib/object/object.mjs +257 -1
- package/dist/lib/object/omit.cjs +2022 -1
- package/dist/lib/object/omit.mjs +1999 -1
- package/dist/lib/object/reconcile.cjs +126 -1
- package/dist/lib/object/reconcile.mjs +105 -1
- package/dist/lib/object/selector.cjs +303 -1
- package/dist/lib/object/selector.mjs +282 -1
- package/dist/lib/object/sort.cjs +209 -1
- package/dist/lib/object/sort.mjs +186 -1
- package/dist/lib/object/transform.cjs +2211 -1
- package/dist/lib/object/transform.mjs +2190 -1
- package/dist/lib/object/types.cjs +18 -1
- package/dist/lib/object/utils.cjs +326 -6
- package/dist/lib/object/utils.mjs +305 -6
- package/dist/lib/primitive/boolean/boolean.cjs +407 -1
- package/dist/lib/primitive/boolean/boolean.mjs +386 -1
- package/dist/lib/primitive/boolean/index.cjs +382 -1
- package/dist/lib/primitive/boolean/index.mjs +359 -1
- package/dist/lib/primitive/boolean/types.cjs +18 -1
- package/dist/lib/primitive/date/date.cjs +1126 -1
- package/dist/lib/primitive/date/date.mjs +1105 -1
- package/dist/lib/primitive/date/index.cjs +1128 -1
- package/dist/lib/primitive/date/index.mjs +1105 -1
- package/dist/lib/primitive/date/types.cjs +18 -1
- package/dist/lib/primitive/index.cjs +2338 -5
- package/dist/lib/primitive/index.mjs +2316 -5
- package/dist/lib/primitive/string/index.cjs +875 -5
- package/dist/lib/primitive/string/index.mjs +852 -5
- package/dist/lib/primitive/string/parser-2.cjs +543 -8
- package/dist/lib/primitive/string/parser-2.mjs +522 -8
- package/dist/lib/primitive/string/parser.cjs +236 -3
- package/dist/lib/primitive/string/parser.mjs +215 -3
- package/dist/lib/primitive/string/string.cjs +926 -5
- package/dist/lib/primitive/string/string.mjs +903 -5
- package/dist/lib/primitive/string/types.cjs +18 -1
- package/dist/lib/primitive/types.cjs +18 -1
- package/dist/lib/url/index.cjs +327 -1
- package/dist/lib/url/index.mjs +304 -1
- package/dist/lib/url/parser.cjs +325 -1
- package/dist/lib/url/parser.mjs +304 -1
- package/dist/lib/url/types.cjs +18 -1
- package/package.json +5 -5
|
@@ -1 +1,345 @@
|
|
|
1
|
-
|
|
1
|
+
// src/lib/functions/functions.ts
|
|
2
|
+
import { isClass } from "@fluixi/utils/object";
|
|
3
|
+
function isFunction(v) {
|
|
4
|
+
return typeof v === "function" && !isClass(v);
|
|
5
|
+
}
|
|
6
|
+
function isPromise(v) {
|
|
7
|
+
return v instanceof Promise || !!v && (typeof v === "object" || typeof v === "function") && "then" in v;
|
|
8
|
+
}
|
|
9
|
+
function isAsyncFunction(v) {
|
|
10
|
+
return isFunction(v) && v.constructor.name === "AsyncFunction";
|
|
11
|
+
}
|
|
12
|
+
function memoize(fn, resolver) {
|
|
13
|
+
const cache = /* @__PURE__ */ new Map();
|
|
14
|
+
const memoized = function(...args) {
|
|
15
|
+
const key = resolver ? resolver(...args) : JSON.stringify(args);
|
|
16
|
+
if (cache.has(key)) {
|
|
17
|
+
return cache.get(key);
|
|
18
|
+
}
|
|
19
|
+
const result = fn.apply(this, args);
|
|
20
|
+
cache.set(key, result);
|
|
21
|
+
return result;
|
|
22
|
+
};
|
|
23
|
+
memoized.cache = cache;
|
|
24
|
+
return memoized;
|
|
25
|
+
}
|
|
26
|
+
function debounce(fn, wait, options = {}) {
|
|
27
|
+
const { leading = false, trailing = true, maxWait } = options;
|
|
28
|
+
let timerId;
|
|
29
|
+
let maxTimerId;
|
|
30
|
+
let lastCallTime;
|
|
31
|
+
let lastInvokeTime = 0;
|
|
32
|
+
let lastArgs;
|
|
33
|
+
let lastThis;
|
|
34
|
+
let result;
|
|
35
|
+
function invokeFunc(time) {
|
|
36
|
+
const args = lastArgs;
|
|
37
|
+
const thisArg = lastThis;
|
|
38
|
+
lastArgs = lastThis = void 0;
|
|
39
|
+
lastInvokeTime = time;
|
|
40
|
+
result = fn.apply(thisArg, args);
|
|
41
|
+
return result;
|
|
42
|
+
}
|
|
43
|
+
function startTimer(pendingFunc, wait2) {
|
|
44
|
+
return setTimeout(pendingFunc, wait2);
|
|
45
|
+
}
|
|
46
|
+
function cancelTimer(id) {
|
|
47
|
+
if (id !== void 0) {
|
|
48
|
+
clearTimeout(id);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function leadingEdge(time) {
|
|
52
|
+
lastInvokeTime = time;
|
|
53
|
+
timerId = startTimer(timerExpired, wait);
|
|
54
|
+
return leading ? invokeFunc(time) : result;
|
|
55
|
+
}
|
|
56
|
+
function remainingWait(time) {
|
|
57
|
+
const timeSinceLastCall = time - lastCallTime;
|
|
58
|
+
const timeSinceLastInvoke = time - lastInvokeTime;
|
|
59
|
+
const timeWaiting = wait - timeSinceLastCall;
|
|
60
|
+
return maxWait !== void 0 ? Math.min(timeWaiting, maxWait - timeSinceLastInvoke) : timeWaiting;
|
|
61
|
+
}
|
|
62
|
+
function shouldInvoke(time) {
|
|
63
|
+
const timeSinceLastCall = time - lastCallTime;
|
|
64
|
+
const timeSinceLastInvoke = time - lastInvokeTime;
|
|
65
|
+
return lastCallTime === void 0 || timeSinceLastCall >= wait || timeSinceLastCall < 0 || maxWait !== void 0 && timeSinceLastInvoke >= maxWait;
|
|
66
|
+
}
|
|
67
|
+
function timerExpired() {
|
|
68
|
+
const time = Date.now();
|
|
69
|
+
if (shouldInvoke(time)) {
|
|
70
|
+
trailingEdge(time);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
timerId = startTimer(timerExpired, remainingWait(time));
|
|
74
|
+
}
|
|
75
|
+
function trailingEdge(time) {
|
|
76
|
+
timerId = void 0;
|
|
77
|
+
if (trailing && lastArgs) {
|
|
78
|
+
return invokeFunc(time);
|
|
79
|
+
}
|
|
80
|
+
lastArgs = lastThis = void 0;
|
|
81
|
+
return result;
|
|
82
|
+
}
|
|
83
|
+
function cancel() {
|
|
84
|
+
cancelTimer(timerId);
|
|
85
|
+
cancelTimer(maxTimerId);
|
|
86
|
+
lastInvokeTime = 0;
|
|
87
|
+
lastArgs = lastCallTime = lastThis = timerId = maxTimerId = void 0;
|
|
88
|
+
}
|
|
89
|
+
function flush() {
|
|
90
|
+
return timerId === void 0 ? result : trailingEdge(Date.now());
|
|
91
|
+
}
|
|
92
|
+
function pending() {
|
|
93
|
+
return timerId !== void 0;
|
|
94
|
+
}
|
|
95
|
+
function debounced(...args) {
|
|
96
|
+
const time = Date.now();
|
|
97
|
+
const isInvoking = shouldInvoke(time);
|
|
98
|
+
lastArgs = args;
|
|
99
|
+
lastThis = this;
|
|
100
|
+
lastCallTime = time;
|
|
101
|
+
if (isInvoking) {
|
|
102
|
+
if (timerId === void 0) {
|
|
103
|
+
return leadingEdge(lastCallTime);
|
|
104
|
+
}
|
|
105
|
+
if (maxWait !== void 0) {
|
|
106
|
+
timerId = startTimer(timerExpired, wait);
|
|
107
|
+
return invokeFunc(lastCallTime);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
if (timerId === void 0) {
|
|
111
|
+
timerId = startTimer(timerExpired, wait);
|
|
112
|
+
}
|
|
113
|
+
return result;
|
|
114
|
+
}
|
|
115
|
+
debounced.cancel = cancel;
|
|
116
|
+
debounced.flush = flush;
|
|
117
|
+
debounced.pending = pending;
|
|
118
|
+
return debounced;
|
|
119
|
+
}
|
|
120
|
+
function throttle(fn, wait, options = {}) {
|
|
121
|
+
const { leading = true, trailing = true } = options;
|
|
122
|
+
return debounce(fn, wait, {
|
|
123
|
+
leading,
|
|
124
|
+
trailing,
|
|
125
|
+
maxWait: wait
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
function once(fn) {
|
|
129
|
+
let called = false;
|
|
130
|
+
let result;
|
|
131
|
+
const wrapped = function(...args) {
|
|
132
|
+
if (!called) {
|
|
133
|
+
called = true;
|
|
134
|
+
result = fn.apply(this, args);
|
|
135
|
+
}
|
|
136
|
+
return result;
|
|
137
|
+
};
|
|
138
|
+
Object.defineProperty(wrapped, "called", {
|
|
139
|
+
get() {
|
|
140
|
+
return called;
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
return wrapped;
|
|
144
|
+
}
|
|
145
|
+
function rearg(fn, ...indexes) {
|
|
146
|
+
return function(...args) {
|
|
147
|
+
const reordered = indexes.map((i) => args[i]);
|
|
148
|
+
return fn.apply(this, reordered);
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
function curry(fn, arity = fn.length) {
|
|
152
|
+
return function curried(...args) {
|
|
153
|
+
if (args.length >= arity) {
|
|
154
|
+
return fn.apply(this, args);
|
|
155
|
+
}
|
|
156
|
+
return function(...nextArgs) {
|
|
157
|
+
return curried.apply(this, [...args, ...nextArgs]);
|
|
158
|
+
};
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
function compose(...fns) {
|
|
162
|
+
return fns.reduceRight(
|
|
163
|
+
(prevFn, nextFn) => (value) => nextFn(prevFn(value)),
|
|
164
|
+
(value) => value
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
function pipe(...fns) {
|
|
168
|
+
return fns.reduce(
|
|
169
|
+
(prevFn, nextFn) => (value) => nextFn(prevFn(value)),
|
|
170
|
+
(value) => value
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
function negate(predicate) {
|
|
174
|
+
return function(...args) {
|
|
175
|
+
return !predicate.apply(this, args);
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
function overArgs(fn, ...transforms) {
|
|
179
|
+
return function(...args) {
|
|
180
|
+
const transformedArgs = args.map(
|
|
181
|
+
(arg, index) => transforms[index] ? transforms[index](arg) : arg
|
|
182
|
+
);
|
|
183
|
+
return fn.apply(this, transformedArgs);
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
function partial(fn, ...partials) {
|
|
187
|
+
return function(...args) {
|
|
188
|
+
return fn.apply(this, [...partials, ...args]);
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
function partialRight(fn, ...partials) {
|
|
192
|
+
return function(...args) {
|
|
193
|
+
return fn.apply(this, [...args, ...partials]);
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
function bind(fn, thisArg, ...partials) {
|
|
197
|
+
return function(...args) {
|
|
198
|
+
return fn.apply(thisArg, [...partials, ...args]);
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
function flip(fn) {
|
|
202
|
+
return function(...args) {
|
|
203
|
+
return fn.apply(this, args.reverse());
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
function ary(fn, n) {
|
|
207
|
+
return function(...args) {
|
|
208
|
+
return fn.apply(this, args.slice(0, n));
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
function unary(fn) {
|
|
212
|
+
return function(arg) {
|
|
213
|
+
return fn.call(this, arg);
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
function defer(fn, ...args) {
|
|
217
|
+
return setTimeout(fn, 1, ...args);
|
|
218
|
+
}
|
|
219
|
+
function delay(fn, wait, ...args) {
|
|
220
|
+
return setTimeout(fn, wait, ...args);
|
|
221
|
+
}
|
|
222
|
+
function memoizeWithTTL(fn, ttl, resolver) {
|
|
223
|
+
const cache = /* @__PURE__ */ new Map();
|
|
224
|
+
const memoized = function(...args) {
|
|
225
|
+
const key = resolver ? resolver(...args) : JSON.stringify(args);
|
|
226
|
+
const now = Date.now();
|
|
227
|
+
const cached = cache.get(key);
|
|
228
|
+
if (cached && cached.expires > now) {
|
|
229
|
+
return cached.value;
|
|
230
|
+
}
|
|
231
|
+
const result = fn.apply(this, args);
|
|
232
|
+
cache.set(key, { value: result, expires: now + ttl });
|
|
233
|
+
for (const [k, v] of cache.entries()) {
|
|
234
|
+
if (v.expires <= now) {
|
|
235
|
+
cache.delete(k);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return result;
|
|
239
|
+
};
|
|
240
|
+
memoized.cache = cache;
|
|
241
|
+
return memoized;
|
|
242
|
+
}
|
|
243
|
+
function before(fn, n) {
|
|
244
|
+
let result;
|
|
245
|
+
let callCount = 0;
|
|
246
|
+
return function(...args) {
|
|
247
|
+
if (++callCount < n) {
|
|
248
|
+
result = fn.apply(this, args);
|
|
249
|
+
}
|
|
250
|
+
if (callCount >= n) {
|
|
251
|
+
fn = void 0;
|
|
252
|
+
}
|
|
253
|
+
return result;
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
function after(fn, n) {
|
|
257
|
+
let callCount = 0;
|
|
258
|
+
return function(...args) {
|
|
259
|
+
if (++callCount >= n) {
|
|
260
|
+
return fn.apply(this, args);
|
|
261
|
+
}
|
|
262
|
+
return void 0;
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
function tryCatch(fn, errorHandler) {
|
|
266
|
+
return function(...args) {
|
|
267
|
+
try {
|
|
268
|
+
return fn.apply(this, args);
|
|
269
|
+
} catch (error) {
|
|
270
|
+
return errorHandler(error, ...args);
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
function flow(...fns) {
|
|
275
|
+
return pipe(...fns);
|
|
276
|
+
}
|
|
277
|
+
function flowRight(...fns) {
|
|
278
|
+
return compose(...fns);
|
|
279
|
+
}
|
|
280
|
+
function retry(fn, options = {}) {
|
|
281
|
+
const { retries = 3, delay: delay2 = 1e3, backoff = 2, onRetry } = options;
|
|
282
|
+
return async function(...args) {
|
|
283
|
+
let lastError;
|
|
284
|
+
let currentDelay = delay2;
|
|
285
|
+
for (let attempt = 0; attempt <= retries; attempt++) {
|
|
286
|
+
try {
|
|
287
|
+
return await fn.apply(this, args);
|
|
288
|
+
} catch (error) {
|
|
289
|
+
lastError = error;
|
|
290
|
+
if (attempt < retries) {
|
|
291
|
+
if (onRetry) {
|
|
292
|
+
onRetry(lastError, attempt + 1);
|
|
293
|
+
}
|
|
294
|
+
await new Promise((resolve) => setTimeout(resolve, currentDelay));
|
|
295
|
+
currentDelay *= backoff;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
throw lastError;
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
function withTimeout(fn, timeout, onTimeout) {
|
|
303
|
+
return async function(...args) {
|
|
304
|
+
return Promise.race([
|
|
305
|
+
fn.apply(this, args),
|
|
306
|
+
new Promise(
|
|
307
|
+
(_, reject) => setTimeout(() => {
|
|
308
|
+
if (onTimeout) onTimeout();
|
|
309
|
+
reject(new Error(`Function timed out after ${timeout}ms`));
|
|
310
|
+
}, timeout)
|
|
311
|
+
)
|
|
312
|
+
]);
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
export {
|
|
316
|
+
after,
|
|
317
|
+
ary,
|
|
318
|
+
before,
|
|
319
|
+
bind,
|
|
320
|
+
compose,
|
|
321
|
+
curry,
|
|
322
|
+
debounce,
|
|
323
|
+
defer,
|
|
324
|
+
delay,
|
|
325
|
+
flip,
|
|
326
|
+
flow,
|
|
327
|
+
flowRight,
|
|
328
|
+
isAsyncFunction,
|
|
329
|
+
isFunction,
|
|
330
|
+
isPromise,
|
|
331
|
+
memoize,
|
|
332
|
+
memoizeWithTTL,
|
|
333
|
+
negate,
|
|
334
|
+
once,
|
|
335
|
+
overArgs,
|
|
336
|
+
partial,
|
|
337
|
+
partialRight,
|
|
338
|
+
pipe,
|
|
339
|
+
rearg,
|
|
340
|
+
retry,
|
|
341
|
+
throttle,
|
|
342
|
+
tryCatch,
|
|
343
|
+
unary,
|
|
344
|
+
withTimeout
|
|
345
|
+
};
|