@react-fabric/utilities 1.0.0-beta.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 +21 -0
- package/README.md +10 -0
- package/dist/cjs/_ascii.js +76 -0
- package/dist/cjs/_boundingBox.js +59 -0
- package/dist/cjs/_countries.js +2805 -0
- package/dist/cjs/_debounce.js +53 -0
- package/dist/cjs/_dedupe.js +63 -0
- package/dist/cjs/_fetch.js +128 -0
- package/dist/cjs/_fileType.js +967 -0
- package/dist/cjs/_format.js +118 -0
- package/dist/cjs/_getByPath.js +125 -0
- package/dist/cjs/_hash.js +67 -0
- package/dist/cjs/_interpolate.js +47 -0
- package/dist/cjs/_isEqual.js +108 -0
- package/dist/cjs/_isType.js +98 -0
- package/dist/cjs/_mergeRefs.js +40 -0
- package/dist/cjs/_tokenize.js +67 -0
- package/dist/cjs/getImageColorset.js +70 -0
- package/dist/cjs/index.js +116 -0
- package/dist/cjs/useLogger.js +339 -0
- package/dist/cjs/yup.js +172 -0
- package/dist/esm/_ascii.js +76 -0
- package/dist/esm/_boundingBox.js +58 -0
- package/dist/esm/_countries.js +2805 -0
- package/dist/esm/_debounce.js +53 -0
- package/dist/esm/_dedupe.js +60 -0
- package/dist/esm/_fetch.js +128 -0
- package/dist/esm/_fileType.js +967 -0
- package/dist/esm/_format.js +120 -0
- package/dist/esm/_getByPath.js +124 -0
- package/dist/esm/_hash.js +65 -0
- package/dist/esm/_interpolate.js +46 -0
- package/dist/esm/_isEqual.js +106 -0
- package/dist/esm/_isType.js +85 -0
- package/dist/esm/_mergeRefs.js +40 -0
- package/dist/esm/_tokenize.js +66 -0
- package/dist/esm/getImageColorset.js +70 -0
- package/dist/esm/index.js +52 -0
- package/dist/esm/useLogger.js +339 -0
- package/dist/esm/yup.js +152 -0
- package/dist/types/_ascii.d.ts +5 -0
- package/dist/types/_boundingBox.d.ts +10 -0
- package/dist/types/_countries.d.ts +85 -0
- package/dist/types/_debounce.d.ts +8 -0
- package/dist/types/_dedupe.d.ts +4 -0
- package/dist/types/_fetch.d.ts +23 -0
- package/dist/types/_fileType.d.ts +926 -0
- package/dist/types/_format.d.ts +27 -0
- package/dist/types/_getByPath.d.ts +10 -0
- package/dist/types/_hash.d.ts +16 -0
- package/dist/types/_interpolate.d.ts +6 -0
- package/dist/types/_isEqual.d.ts +15 -0
- package/dist/types/_isType.d.ts +14 -0
- package/dist/types/_mergeRefs.d.ts +1 -0
- package/dist/types/_tokenize.d.ts +8 -0
- package/dist/types/getImageColorset.d.ts +1 -0
- package/dist/types/index.d.ts +22 -0
- package/dist/types/useLogger.d.ts +25 -0
- package/dist/types/yup.d.ts +1 -0
- package/package.json +48 -0
- package/src/_ascii.ts +93 -0
- package/src/_boundingBox.ts +72 -0
- package/src/_countries.ts +3046 -0
- package/src/_debounce.ts +42 -0
- package/src/_dedupe.ts +56 -0
- package/src/_fetch.ts +109 -0
- package/src/_fileType.ts +993 -0
- package/src/_format.ts +141 -0
- package/src/_getByPath.ts +108 -0
- package/src/_hash.ts +62 -0
- package/src/_interpolate.ts +42 -0
- package/src/_isEqual.ts +102 -0
- package/src/_isType.ts +105 -0
- package/src/_mergeRefs.ts +33 -0
- package/src/_tokenize.ts +69 -0
- package/src/getImageColorset.ts +81 -0
- package/src/index.ts +51 -0
- package/src/useLogger.ts +304 -0
- package/src/yup.ts +75 -0
package/src/_debounce.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* React Fabric
|
|
3
|
+
* @version: 1.0.0
|
|
4
|
+
*
|
|
5
|
+
*
|
|
6
|
+
* The MIT License (MIT)
|
|
7
|
+
* Copyright (c) 2024 Adarsh Pastakia
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
|
10
|
+
* and associated documentation files (the "Software"), to deal in the Software without restriction,
|
|
11
|
+
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
12
|
+
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
|
|
13
|
+
* subject to the following conditions:
|
|
14
|
+
*
|
|
15
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
16
|
+
* portions of the Software.
|
|
17
|
+
*
|
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
|
19
|
+
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
20
|
+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
21
|
+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/* istanbul ignore file */
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* debounce callback
|
|
28
|
+
* @internal
|
|
29
|
+
*/
|
|
30
|
+
export const debounce = (fn: (...rest: any) => any, timeout = 500) => {
|
|
31
|
+
/** ***************** create debounce timer id using callback hash *******************/
|
|
32
|
+
let _timer: any;
|
|
33
|
+
|
|
34
|
+
/** ***************** return debounced function *******************/
|
|
35
|
+
const cb = (...args: any) => {
|
|
36
|
+
clearTimeout(_timer);
|
|
37
|
+
if (typeof window !== "undefined")
|
|
38
|
+
_timer = window.setTimeout(() => fn?.(...args), timeout);
|
|
39
|
+
};
|
|
40
|
+
cb.cancel = () => clearTimeout(_timer);
|
|
41
|
+
return cb;
|
|
42
|
+
};
|
package/src/_dedupe.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* React Fabric
|
|
3
|
+
* @version: 1.0.0
|
|
4
|
+
*
|
|
5
|
+
*
|
|
6
|
+
* The MIT License (MIT)
|
|
7
|
+
* Copyright (c) 2024 Adarsh Pastakia
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
|
10
|
+
* and associated documentation files (the "Software"), to deal in the Software without restriction,
|
|
11
|
+
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
12
|
+
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
|
|
13
|
+
* subject to the following conditions:
|
|
14
|
+
*
|
|
15
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
16
|
+
* portions of the Software.
|
|
17
|
+
*
|
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
|
19
|
+
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
20
|
+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
21
|
+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import { isEmpty } from "./_isType";
|
|
25
|
+
|
|
26
|
+
export const dedupe = (list: any[], key?: string) => {
|
|
27
|
+
const filtered = list.filter((i) => !isEmpty(i));
|
|
28
|
+
if (!key) {
|
|
29
|
+
return Array.from(new Set(filtered).values());
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return Array.from(
|
|
33
|
+
new Map(filtered.map((obj) => [obj[key] ?? obj, obj])).values(),
|
|
34
|
+
);
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export const flatten = (list: any[]) => {
|
|
38
|
+
return list.flat(5).filter((i) => !isEmpty(i));
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export const flattenAndDedupe = (list: any[] = [], key?: string) => {
|
|
42
|
+
return dedupe(flatten(list), key);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export const groupBy = <T extends KeyValue>(
|
|
46
|
+
list: T[],
|
|
47
|
+
prop: keyof T,
|
|
48
|
+
missingKey?: string,
|
|
49
|
+
): KeyValue<T[]> => {
|
|
50
|
+
return list.reduce((r: AnyObject, i) => {
|
|
51
|
+
const key = i[prop] ?? missingKey;
|
|
52
|
+
if (!r[key]) r[key] = [];
|
|
53
|
+
r[key].push(i);
|
|
54
|
+
return r;
|
|
55
|
+
}, {});
|
|
56
|
+
};
|
package/src/_fetch.ts
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* React Fabric
|
|
3
|
+
* @version: 1.0.0
|
|
4
|
+
*
|
|
5
|
+
*
|
|
6
|
+
* The MIT License (MIT)
|
|
7
|
+
* Copyright (c) 2024 Adarsh Pastakia
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
|
10
|
+
* and associated documentation files (the "Software"), to deal in the Software without restriction,
|
|
11
|
+
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
12
|
+
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
|
|
13
|
+
* subject to the following conditions:
|
|
14
|
+
*
|
|
15
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
16
|
+
* portions of the Software.
|
|
17
|
+
*
|
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
|
19
|
+
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
20
|
+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
21
|
+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/* istanbul ignore file */
|
|
25
|
+
|
|
26
|
+
import { isString } from "./_isType";
|
|
27
|
+
|
|
28
|
+
declare type METHODS = "get" | "post" | "put" | "delete";
|
|
29
|
+
declare interface FetchOptions {
|
|
30
|
+
body?: KeyValue | FormData;
|
|
31
|
+
headers?: KeyValue;
|
|
32
|
+
signal?: string | AbortSignal;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const _signals = new Map<string, AbortController>();
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Fetch helper
|
|
39
|
+
* @param method
|
|
40
|
+
* @param url
|
|
41
|
+
* @param options.headers
|
|
42
|
+
* @param options.body
|
|
43
|
+
* @param options.signal
|
|
44
|
+
* @internal
|
|
45
|
+
*/
|
|
46
|
+
export const _fetch = async (
|
|
47
|
+
method: METHODS,
|
|
48
|
+
url: string,
|
|
49
|
+
{ headers = {}, body, signal }: FetchOptions,
|
|
50
|
+
) => {
|
|
51
|
+
const options: KeyValue = {
|
|
52
|
+
method,
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
headers.accept = "application/json";
|
|
56
|
+
if (method === "post" || method === "put") {
|
|
57
|
+
options.body = body;
|
|
58
|
+
/** ***************** set body to json when not FormData *******************/
|
|
59
|
+
if (!(body instanceof FormData)) {
|
|
60
|
+
headers["content-type"] = "application/json";
|
|
61
|
+
options.body = JSON.stringify(body);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** ***************** reset abort signal *******************/
|
|
66
|
+
if (signal && isString(signal)) {
|
|
67
|
+
if (_signals.has(signal)) {
|
|
68
|
+
const _s = _signals.get(signal);
|
|
69
|
+
_s?.abort();
|
|
70
|
+
_signals.delete(signal);
|
|
71
|
+
}
|
|
72
|
+
const ac = new AbortController();
|
|
73
|
+
options.signal = ac.signal;
|
|
74
|
+
_signals.set(signal, ac);
|
|
75
|
+
} else if (signal instanceof AbortSignal) {
|
|
76
|
+
options.signal = signal;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return await fetch(url, { ...options, headers })
|
|
80
|
+
.then(async (resp) => {
|
|
81
|
+
if (resp.status >= 200 && resp.status < 300) {
|
|
82
|
+
return await resp.json();
|
|
83
|
+
}
|
|
84
|
+
// eslint-disable-next-line @typescript-eslint/no-throw-literal
|
|
85
|
+
throw { status: resp.status, code: -1, message: resp.statusText };
|
|
86
|
+
})
|
|
87
|
+
.then((resp) => {
|
|
88
|
+
/** ***************** check for possible error code propeties in response *******************/
|
|
89
|
+
const code = resp.error_code ?? resp.errorCode ?? resp.error;
|
|
90
|
+
const message = resp.error_message ?? resp.errorMessage ?? resp.message;
|
|
91
|
+
if (resp.status === "error" || code) {
|
|
92
|
+
// eslint-disable-next-line @typescript-eslint/no-throw-literal
|
|
93
|
+
throw { status: 500, code, message };
|
|
94
|
+
}
|
|
95
|
+
return resp;
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
/** ***************** attach isAborted helper to fetch method *******************/
|
|
100
|
+
_fetch.isAborted = ({ name }: Error) => name === "AbortError";
|
|
101
|
+
|
|
102
|
+
/** ***************** attach abort helper to fetch method *******************/
|
|
103
|
+
_fetch.abort = (signal: string) => {
|
|
104
|
+
if (_signals.has(signal)) {
|
|
105
|
+
const _s = _signals.get(signal);
|
|
106
|
+
_s?.abort();
|
|
107
|
+
_signals.delete(signal);
|
|
108
|
+
}
|
|
109
|
+
};
|