@hairy/utils 1.49.0 → 1.51.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.md → LICENSE} +1 -1
- package/README.md +730 -21
- package/dist/index.cjs +87 -62
- package/dist/index.d.cts +23 -23
- package/dist/index.d.mts +23 -23
- package/dist/index.mjs +87 -63
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -6487,6 +6487,7 @@ const noop = () => {};
|
|
|
6487
6487
|
* ```ts
|
|
6488
6488
|
* const deferred = new Deferred()
|
|
6489
6489
|
* deferred.resolve('value')
|
|
6490
|
+
* ```
|
|
6490
6491
|
*/
|
|
6491
6492
|
var Deferred = class extends Promise {
|
|
6492
6493
|
resolve;
|
|
@@ -6518,11 +6519,88 @@ var Deferred = class extends Promise {
|
|
|
6518
6519
|
* @example
|
|
6519
6520
|
* ```ts
|
|
6520
6521
|
* delay(1000).then(() => { console.log('1 second') })
|
|
6522
|
+
* ```
|
|
6521
6523
|
*/
|
|
6522
6524
|
function delay(ms) {
|
|
6523
6525
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
6524
6526
|
}
|
|
6525
6527
|
|
|
6528
|
+
//#endregion
|
|
6529
|
+
//#region src/util/proxy.ts
|
|
6530
|
+
/**
|
|
6531
|
+
* Creates a proxy object that updates the original object when the proxy is updated.
|
|
6532
|
+
* @param initObject - The initial object to proxy.
|
|
6533
|
+
* @returns The proxy object.
|
|
6534
|
+
*
|
|
6535
|
+
* @example
|
|
6536
|
+
* ```ts
|
|
6537
|
+
* const obj = proxy({ name: 'John' })
|
|
6538
|
+
* console.log(obj.name) // John
|
|
6539
|
+
*
|
|
6540
|
+
* obj.proxy.update({ name: 'Jane' })
|
|
6541
|
+
* console.log(obj.name) // Jane
|
|
6542
|
+
*
|
|
6543
|
+
* const obj2 = proxy()
|
|
6544
|
+
*
|
|
6545
|
+
* obj2.any // Error: Proxy not updated. Call object.proxy.update() to update the proxy.
|
|
6546
|
+
*
|
|
6547
|
+
* obj2.proxy.source // undefined
|
|
6548
|
+
* obj2.update({ name: 'John' })
|
|
6549
|
+
*
|
|
6550
|
+
* // get the original object
|
|
6551
|
+
* obj2.proxy.source // { name: 'John' }
|
|
6552
|
+
* ```
|
|
6553
|
+
*/
|
|
6554
|
+
function proxy(initObject, initExtend, options) {
|
|
6555
|
+
initObject && Reflect.set(initObject, "__proxy_updated", true);
|
|
6556
|
+
let target = initObject || { __proxy_updated: false };
|
|
6557
|
+
const extended = initExtend || {};
|
|
6558
|
+
const proxy = {
|
|
6559
|
+
update(object) {
|
|
6560
|
+
if (!object) {
|
|
6561
|
+
target = void 0;
|
|
6562
|
+
return;
|
|
6563
|
+
}
|
|
6564
|
+
Reflect.set(object, "__proxy_updated", true);
|
|
6565
|
+
target = object;
|
|
6566
|
+
},
|
|
6567
|
+
get source() {
|
|
6568
|
+
return Reflect.get(target, "__proxy_updated") ? target : void 0;
|
|
6569
|
+
},
|
|
6570
|
+
get extend() {
|
|
6571
|
+
return extended;
|
|
6572
|
+
},
|
|
6573
|
+
strictMessage: options?.strictMessage || "Proxy not updated. Call object.proxy.update() to update the proxy."
|
|
6574
|
+
};
|
|
6575
|
+
return new Proxy({}, {
|
|
6576
|
+
get: (_, p) => {
|
|
6577
|
+
if (p === "proxy") return proxy;
|
|
6578
|
+
if (p in extended) return Reflect.get(extended, p);
|
|
6579
|
+
if (!Reflect.get(target, "__proxy_updated")) throw new Error(proxy.strictMessage);
|
|
6580
|
+
return typeof target?.[p] === "function" ? target?.[p].bind(target) : target?.[p];
|
|
6581
|
+
},
|
|
6582
|
+
set: (_, p, v) => {
|
|
6583
|
+
if (p in extended) return Reflect.set(extended, p, v);
|
|
6584
|
+
target[p] = v;
|
|
6585
|
+
return true;
|
|
6586
|
+
}
|
|
6587
|
+
});
|
|
6588
|
+
}
|
|
6589
|
+
|
|
6590
|
+
//#endregion
|
|
6591
|
+
//#region src/util/ghost.ts
|
|
6592
|
+
function ghost(strictMessage) {
|
|
6593
|
+
const placeholder = proxy(void 0, {
|
|
6594
|
+
enabled: false,
|
|
6595
|
+
resolve
|
|
6596
|
+
}, { strictMessage: strictMessage || "Object is not enabled. Call ghost.resolve(value) to enable the object." });
|
|
6597
|
+
function resolve(value) {
|
|
6598
|
+
placeholder.proxy.update(value);
|
|
6599
|
+
placeholder.enabled = true;
|
|
6600
|
+
}
|
|
6601
|
+
return placeholder;
|
|
6602
|
+
}
|
|
6603
|
+
|
|
6526
6604
|
//#endregion
|
|
6527
6605
|
//#region src/util/json.ts
|
|
6528
6606
|
function tryParseJson(text) {
|
|
@@ -6604,68 +6682,6 @@ function pipe(...fns) {
|
|
|
6604
6682
|
}
|
|
6605
6683
|
pipe.promise = pPipe;
|
|
6606
6684
|
|
|
6607
|
-
//#endregion
|
|
6608
|
-
//#region src/util/proxy.ts
|
|
6609
|
-
/**
|
|
6610
|
-
* Creates a proxy object that updates the original object when the proxy is updated.
|
|
6611
|
-
* @param initObject - The initial object to proxy.
|
|
6612
|
-
* @returns The proxy object.
|
|
6613
|
-
*
|
|
6614
|
-
* @example
|
|
6615
|
-
* ```ts
|
|
6616
|
-
* const obj = proxy({ name: 'John' })
|
|
6617
|
-
* console.log(obj.name) // John
|
|
6618
|
-
*
|
|
6619
|
-
* obj.proxy.update({ name: 'Jane' })
|
|
6620
|
-
* console.log(obj.name) // Jane
|
|
6621
|
-
*
|
|
6622
|
-
* const obj2 = proxy()
|
|
6623
|
-
*
|
|
6624
|
-
* obj2.any // Error: Proxy not updated. Call object.proxy.update() to update the proxy.
|
|
6625
|
-
*
|
|
6626
|
-
* obj2.proxy.source // undefined
|
|
6627
|
-
* obj2.update({ name: 'John' })
|
|
6628
|
-
*
|
|
6629
|
-
* // get the original object
|
|
6630
|
-
* obj2.proxy.source // { name: 'John' }
|
|
6631
|
-
* ```
|
|
6632
|
-
*/
|
|
6633
|
-
function proxy(initObject, initExtend, options) {
|
|
6634
|
-
initObject && Reflect.set(initObject, "__proxy_updated", true);
|
|
6635
|
-
let target = initObject || { __proxy_updated: false };
|
|
6636
|
-
const extended = initExtend || {};
|
|
6637
|
-
const proxy = {
|
|
6638
|
-
update(object) {
|
|
6639
|
-
if (!object) {
|
|
6640
|
-
target = void 0;
|
|
6641
|
-
return;
|
|
6642
|
-
}
|
|
6643
|
-
Reflect.set(object, "__proxy_updated", true);
|
|
6644
|
-
target = object;
|
|
6645
|
-
},
|
|
6646
|
-
get source() {
|
|
6647
|
-
return Reflect.get(target, "__proxy_updated") ? target : void 0;
|
|
6648
|
-
},
|
|
6649
|
-
get extend() {
|
|
6650
|
-
return extended;
|
|
6651
|
-
},
|
|
6652
|
-
strictMessage: options?.strictMessage || "Proxy not updated. Call object.proxy.update() to update the proxy."
|
|
6653
|
-
};
|
|
6654
|
-
return new Proxy({}, {
|
|
6655
|
-
get: (_, p) => {
|
|
6656
|
-
if (p === "proxy") return proxy;
|
|
6657
|
-
if (p in extended) return Reflect.get(extended, p);
|
|
6658
|
-
if (!Reflect.get(target, "__proxy_updated")) throw new Error(proxy.strictMessage);
|
|
6659
|
-
return typeof target?.[p] === "function" ? target?.[p].bind(target) : target?.[p];
|
|
6660
|
-
},
|
|
6661
|
-
set: (_, p, v) => {
|
|
6662
|
-
if (p in extended) return Reflect.set(extended, p, v);
|
|
6663
|
-
target[p] = v;
|
|
6664
|
-
return true;
|
|
6665
|
-
}
|
|
6666
|
-
});
|
|
6667
|
-
}
|
|
6668
|
-
|
|
6669
6685
|
//#endregion
|
|
6670
6686
|
//#region src/util/random.ts
|
|
6671
6687
|
/**
|
|
@@ -6675,6 +6691,7 @@ function proxy(initObject, initExtend, options) {
|
|
|
6675
6691
|
* @example
|
|
6676
6692
|
* ```ts
|
|
6677
6693
|
* randomItem(['a', 'b', 'c']) // 'a' | 'b' | 'c'
|
|
6694
|
+
* ```
|
|
6678
6695
|
*/
|
|
6679
6696
|
function randomItem(array) {
|
|
6680
6697
|
return array[Math.floor(Math.random() * array.length)];
|
|
@@ -6687,6 +6704,7 @@ function randomItem(array) {
|
|
|
6687
6704
|
* @example
|
|
6688
6705
|
* ```ts
|
|
6689
6706
|
* randomNumber(0, 100) // 0-100
|
|
6707
|
+
* ```
|
|
6690
6708
|
*/
|
|
6691
6709
|
function randomNumber(min, max) {
|
|
6692
6710
|
return Math.random() * (max - min) + min;
|
|
@@ -6702,6 +6720,7 @@ const urlAlphabet = "useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwy
|
|
|
6702
6720
|
* randomString() // 10 characters long
|
|
6703
6721
|
* randomString(20) // 20 characters long
|
|
6704
6722
|
* randomString(20, 'abcdefghijklmnopqrstuvwxyz') // 20 characters long
|
|
6723
|
+
* ```
|
|
6705
6724
|
*/
|
|
6706
6725
|
function randomString(size = 10, chars = urlAlphabet) {
|
|
6707
6726
|
let id = "";
|
|
@@ -6775,6 +6794,7 @@ function numberish(value) {
|
|
|
6775
6794
|
* ```ts
|
|
6776
6795
|
* to(Promise.resolve('data')) // Promise<[null, 'data']>
|
|
6777
6796
|
* to(Promise.reject(new Error('error'))) // Promise<[Error, undefined]>
|
|
6797
|
+
* ```
|
|
6778
6798
|
*/
|
|
6779
6799
|
async function to(promise, error) {
|
|
6780
6800
|
return (isFunction(promise) ? promise() : promise).then((data) => [null, data]).catch((err) => {
|
|
@@ -6794,6 +6814,7 @@ async function to(promise, error) {
|
|
|
6794
6814
|
* ```ts
|
|
6795
6815
|
* toArray(arrorOrItemOrUndefined) // item[] | undefined
|
|
6796
6816
|
* toArray(arrayOrItemOrUndefined, true) // item[]
|
|
6817
|
+
* ```
|
|
6797
6818
|
*/
|
|
6798
6819
|
function toArray(value, required = false) {
|
|
6799
6820
|
if (!value) return required === false ? void 0 : [];
|
|
@@ -6862,6 +6883,7 @@ const riposte = select;
|
|
|
6862
6883
|
* ```ts
|
|
6863
6884
|
* unwrap({ name: 'John' }) // { name: 'John' }
|
|
6864
6885
|
* unwrap(() => { return { name: 'John' } }) // { name: 'John' }
|
|
6886
|
+
* ```
|
|
6865
6887
|
*/
|
|
6866
6888
|
function unwrap(value) {
|
|
6867
6889
|
return typeof value === "function" ? value() : value;
|
|
@@ -6874,6 +6896,7 @@ function unwrap(value) {
|
|
|
6874
6896
|
* @example
|
|
6875
6897
|
* ```ts
|
|
6876
6898
|
* whenever(value, (value) => { return 'value' }) // value
|
|
6899
|
+
* ```
|
|
6877
6900
|
*/
|
|
6878
6901
|
function whenever(value, callback) {
|
|
6879
6902
|
return value ? callback(value) : void 0;
|
|
@@ -7128,6 +7151,7 @@ const _reFullWs = /^\s*$/;
|
|
|
7128
7151
|
* b()
|
|
7129
7152
|
* }
|
|
7130
7153
|
* `
|
|
7154
|
+
* ```
|
|
7131
7155
|
*/
|
|
7132
7156
|
function unindent(str) {
|
|
7133
7157
|
const lines = (typeof str === "string" ? str : str[0]).split("\n");
|
|
@@ -7145,4 +7169,4 @@ function unindent(str) {
|
|
|
7145
7169
|
}
|
|
7146
7170
|
|
|
7147
7171
|
//#endregion
|
|
7148
|
-
export { BIG_INTS, Bignumber, DEFAULT_BIGNUM_CONFIG, Deferred, arange, average, bignumber, call, camelCase, capitalCase, chunk, clone, cloneDeep, cloneDeepWith, cloneWith, compose, concat, constantCase, cover, debounce, decimal, delay, dialsPhone, divide, dotCase, downloadBlobFile, downloadNetworkFile, downloadUrlFile, ensurePrefix, ensureSuffix, find, formatNumeric, formdataToObject, get, groupBy, gt, gte, integer, isAndroid, isArguments, isArray, isArrayBuffer, isArrayLike, isArrayLikeObject, isBoolean, isBrowser, isBuffer, isChrome, isDate, isEdge, isElement, isEmpty, isEqual, isEqualWith, isError, isFF, isFormData, isFunction, isIE, isIE11, isIE9, isIOS, isInteger, isMap, isMatch, isMatchWith, isMobile, isNaN, isNative, isNull$1 as isNull, isNumber, isObject, isObjectLike, isPhantomJS, isPlainObject, isRegExp as isRegexp, isSet, isString, isSymbol, isTruthy, isUndefined, isWeakMap, isWeakSet, isWeex, isWindow, join, kebabCase, keyBy, keys, loop, lt, lte, max, maxBy, merge, mergeWith, min, minBy, multiply, noCase, nonnanable, noop, numberify, numberish, objectToFormdata, off, omit, omitBy, on, once, openFilePicker, openImagePicker, parseNumeric, pascalCase, pascalSnakeCase, pathCase, percentage, pick, pickBy, pipe, plus, proxy, randomItem, randomNumber, randomString, range, readFileReader, redirectTo, riposte, select, selectImages, sentenceCase, set, shortenId, showOpenFilePicker, showOpenImagePicker, size, slash, snakeCase, stringify, template, to, toArray, trainCase, truncate, tryParseJson, unindent, uniq, uniqBy, uniqWith, unit, unwrap, values, whenever, zeroRemove, zerofill };
|
|
7172
|
+
export { BIG_INTS, Bignumber, DEFAULT_BIGNUM_CONFIG, Deferred, arange, average, bignumber, call, camelCase, capitalCase, chunk, clone, cloneDeep, cloneDeepWith, cloneWith, compose, concat, constantCase, cover, debounce, decimal, delay, dialsPhone, divide, dotCase, downloadBlobFile, downloadNetworkFile, downloadUrlFile, ensurePrefix, ensureSuffix, find, formatNumeric, formdataToObject, get, ghost, groupBy, gt, gte, integer, isAndroid, isArguments, isArray, isArrayBuffer, isArrayLike, isArrayLikeObject, isBoolean, isBrowser, isBuffer, isChrome, isDate, isEdge, isElement, isEmpty, isEqual, isEqualWith, isError, isFF, isFormData, isFunction, isIE, isIE11, isIE9, isIOS, isInteger, isMap, isMatch, isMatchWith, isMobile, isNaN, isNative, isNull$1 as isNull, isNumber, isObject, isObjectLike, isPhantomJS, isPlainObject, isRegExp as isRegexp, isSet, isString, isSymbol, isTruthy, isUndefined, isWeakMap, isWeakSet, isWeex, isWindow, join, kebabCase, keyBy, keys, loop, lt, lte, max, maxBy, merge, mergeWith, min, minBy, multiply, noCase, nonnanable, noop, numberify, numberish, objectToFormdata, off, omit, omitBy, on, once, openFilePicker, openImagePicker, parseNumeric, pascalCase, pascalSnakeCase, pathCase, percentage, pick, pickBy, pipe, plus, proxy, randomItem, randomNumber, randomString, range, readFileReader, redirectTo, riposte, select, selectImages, sentenceCase, set, shortenId, showOpenFilePicker, showOpenImagePicker, size, slash, snakeCase, stringify, template, to, toArray, trainCase, truncate, tryParseJson, unindent, uniq, uniqBy, uniqWith, unit, unwrap, values, whenever, zeroRemove, zerofill };
|