@hairy/utils 1.49.0 → 1.50.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 +673 -21
- package/dist/index.cjs +83 -62
- package/dist/index.d.cts +23 -23
- package/dist/index.d.mts +23 -23
- package/dist/index.mjs +83 -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,84 @@ 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
|
+
enable: false,
|
|
6595
|
+
resolve: (value) => placeholder.proxy.update(value)
|
|
6596
|
+
}, { strictMessage: strictMessage || "Object is not enabled. Call ghost.resolve(value) to enable the object." });
|
|
6597
|
+
return placeholder;
|
|
6598
|
+
}
|
|
6599
|
+
|
|
6526
6600
|
//#endregion
|
|
6527
6601
|
//#region src/util/json.ts
|
|
6528
6602
|
function tryParseJson(text) {
|
|
@@ -6604,68 +6678,6 @@ function pipe(...fns) {
|
|
|
6604
6678
|
}
|
|
6605
6679
|
pipe.promise = pPipe;
|
|
6606
6680
|
|
|
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
6681
|
//#endregion
|
|
6670
6682
|
//#region src/util/random.ts
|
|
6671
6683
|
/**
|
|
@@ -6675,6 +6687,7 @@ function proxy(initObject, initExtend, options) {
|
|
|
6675
6687
|
* @example
|
|
6676
6688
|
* ```ts
|
|
6677
6689
|
* randomItem(['a', 'b', 'c']) // 'a' | 'b' | 'c'
|
|
6690
|
+
* ```
|
|
6678
6691
|
*/
|
|
6679
6692
|
function randomItem(array) {
|
|
6680
6693
|
return array[Math.floor(Math.random() * array.length)];
|
|
@@ -6687,6 +6700,7 @@ function randomItem(array) {
|
|
|
6687
6700
|
* @example
|
|
6688
6701
|
* ```ts
|
|
6689
6702
|
* randomNumber(0, 100) // 0-100
|
|
6703
|
+
* ```
|
|
6690
6704
|
*/
|
|
6691
6705
|
function randomNumber(min, max) {
|
|
6692
6706
|
return Math.random() * (max - min) + min;
|
|
@@ -6702,6 +6716,7 @@ const urlAlphabet = "useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwy
|
|
|
6702
6716
|
* randomString() // 10 characters long
|
|
6703
6717
|
* randomString(20) // 20 characters long
|
|
6704
6718
|
* randomString(20, 'abcdefghijklmnopqrstuvwxyz') // 20 characters long
|
|
6719
|
+
* ```
|
|
6705
6720
|
*/
|
|
6706
6721
|
function randomString(size = 10, chars = urlAlphabet) {
|
|
6707
6722
|
let id = "";
|
|
@@ -6775,6 +6790,7 @@ function numberish(value) {
|
|
|
6775
6790
|
* ```ts
|
|
6776
6791
|
* to(Promise.resolve('data')) // Promise<[null, 'data']>
|
|
6777
6792
|
* to(Promise.reject(new Error('error'))) // Promise<[Error, undefined]>
|
|
6793
|
+
* ```
|
|
6778
6794
|
*/
|
|
6779
6795
|
async function to(promise, error) {
|
|
6780
6796
|
return (isFunction(promise) ? promise() : promise).then((data) => [null, data]).catch((err) => {
|
|
@@ -6794,6 +6810,7 @@ async function to(promise, error) {
|
|
|
6794
6810
|
* ```ts
|
|
6795
6811
|
* toArray(arrorOrItemOrUndefined) // item[] | undefined
|
|
6796
6812
|
* toArray(arrayOrItemOrUndefined, true) // item[]
|
|
6813
|
+
* ```
|
|
6797
6814
|
*/
|
|
6798
6815
|
function toArray(value, required = false) {
|
|
6799
6816
|
if (!value) return required === false ? void 0 : [];
|
|
@@ -6862,6 +6879,7 @@ const riposte = select;
|
|
|
6862
6879
|
* ```ts
|
|
6863
6880
|
* unwrap({ name: 'John' }) // { name: 'John' }
|
|
6864
6881
|
* unwrap(() => { return { name: 'John' } }) // { name: 'John' }
|
|
6882
|
+
* ```
|
|
6865
6883
|
*/
|
|
6866
6884
|
function unwrap(value) {
|
|
6867
6885
|
return typeof value === "function" ? value() : value;
|
|
@@ -6874,6 +6892,7 @@ function unwrap(value) {
|
|
|
6874
6892
|
* @example
|
|
6875
6893
|
* ```ts
|
|
6876
6894
|
* whenever(value, (value) => { return 'value' }) // value
|
|
6895
|
+
* ```
|
|
6877
6896
|
*/
|
|
6878
6897
|
function whenever(value, callback) {
|
|
6879
6898
|
return value ? callback(value) : void 0;
|
|
@@ -7128,6 +7147,7 @@ const _reFullWs = /^\s*$/;
|
|
|
7128
7147
|
* b()
|
|
7129
7148
|
* }
|
|
7130
7149
|
* `
|
|
7150
|
+
* ```
|
|
7131
7151
|
*/
|
|
7132
7152
|
function unindent(str) {
|
|
7133
7153
|
const lines = (typeof str === "string" ? str : str[0]).split("\n");
|
|
@@ -7145,4 +7165,4 @@ function unindent(str) {
|
|
|
7145
7165
|
}
|
|
7146
7166
|
|
|
7147
7167
|
//#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 };
|
|
7168
|
+
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 };
|