@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.
Files changed (79) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +10 -0
  3. package/dist/cjs/_ascii.js +76 -0
  4. package/dist/cjs/_boundingBox.js +59 -0
  5. package/dist/cjs/_countries.js +2805 -0
  6. package/dist/cjs/_debounce.js +53 -0
  7. package/dist/cjs/_dedupe.js +63 -0
  8. package/dist/cjs/_fetch.js +128 -0
  9. package/dist/cjs/_fileType.js +967 -0
  10. package/dist/cjs/_format.js +118 -0
  11. package/dist/cjs/_getByPath.js +125 -0
  12. package/dist/cjs/_hash.js +67 -0
  13. package/dist/cjs/_interpolate.js +47 -0
  14. package/dist/cjs/_isEqual.js +108 -0
  15. package/dist/cjs/_isType.js +98 -0
  16. package/dist/cjs/_mergeRefs.js +40 -0
  17. package/dist/cjs/_tokenize.js +67 -0
  18. package/dist/cjs/getImageColorset.js +70 -0
  19. package/dist/cjs/index.js +116 -0
  20. package/dist/cjs/useLogger.js +339 -0
  21. package/dist/cjs/yup.js +172 -0
  22. package/dist/esm/_ascii.js +76 -0
  23. package/dist/esm/_boundingBox.js +58 -0
  24. package/dist/esm/_countries.js +2805 -0
  25. package/dist/esm/_debounce.js +53 -0
  26. package/dist/esm/_dedupe.js +60 -0
  27. package/dist/esm/_fetch.js +128 -0
  28. package/dist/esm/_fileType.js +967 -0
  29. package/dist/esm/_format.js +120 -0
  30. package/dist/esm/_getByPath.js +124 -0
  31. package/dist/esm/_hash.js +65 -0
  32. package/dist/esm/_interpolate.js +46 -0
  33. package/dist/esm/_isEqual.js +106 -0
  34. package/dist/esm/_isType.js +85 -0
  35. package/dist/esm/_mergeRefs.js +40 -0
  36. package/dist/esm/_tokenize.js +66 -0
  37. package/dist/esm/getImageColorset.js +70 -0
  38. package/dist/esm/index.js +52 -0
  39. package/dist/esm/useLogger.js +339 -0
  40. package/dist/esm/yup.js +152 -0
  41. package/dist/types/_ascii.d.ts +5 -0
  42. package/dist/types/_boundingBox.d.ts +10 -0
  43. package/dist/types/_countries.d.ts +85 -0
  44. package/dist/types/_debounce.d.ts +8 -0
  45. package/dist/types/_dedupe.d.ts +4 -0
  46. package/dist/types/_fetch.d.ts +23 -0
  47. package/dist/types/_fileType.d.ts +926 -0
  48. package/dist/types/_format.d.ts +27 -0
  49. package/dist/types/_getByPath.d.ts +10 -0
  50. package/dist/types/_hash.d.ts +16 -0
  51. package/dist/types/_interpolate.d.ts +6 -0
  52. package/dist/types/_isEqual.d.ts +15 -0
  53. package/dist/types/_isType.d.ts +14 -0
  54. package/dist/types/_mergeRefs.d.ts +1 -0
  55. package/dist/types/_tokenize.d.ts +8 -0
  56. package/dist/types/getImageColorset.d.ts +1 -0
  57. package/dist/types/index.d.ts +22 -0
  58. package/dist/types/useLogger.d.ts +25 -0
  59. package/dist/types/yup.d.ts +1 -0
  60. package/package.json +48 -0
  61. package/src/_ascii.ts +93 -0
  62. package/src/_boundingBox.ts +72 -0
  63. package/src/_countries.ts +3046 -0
  64. package/src/_debounce.ts +42 -0
  65. package/src/_dedupe.ts +56 -0
  66. package/src/_fetch.ts +109 -0
  67. package/src/_fileType.ts +993 -0
  68. package/src/_format.ts +141 -0
  69. package/src/_getByPath.ts +108 -0
  70. package/src/_hash.ts +62 -0
  71. package/src/_interpolate.ts +42 -0
  72. package/src/_isEqual.ts +102 -0
  73. package/src/_isType.ts +105 -0
  74. package/src/_mergeRefs.ts +33 -0
  75. package/src/_tokenize.ts +69 -0
  76. package/src/getImageColorset.ts +81 -0
  77. package/src/index.ts +51 -0
  78. package/src/useLogger.ts +304 -0
  79. package/src/yup.ts +75 -0
package/src/_format.ts ADDED
@@ -0,0 +1,141 @@
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 { differenceInDays, format, parseISO } from "date-fns";
25
+ import {
26
+ parsePhoneNumberFromString,
27
+ type CountryCode,
28
+ } from "libphonenumber-js";
29
+ import numeral from "numeral";
30
+ import Countries from "./_countries";
31
+ import { isEmpty, isNil } from "./_isType";
32
+
33
+ // eslint-disable-next-line @typescript-eslint/no-namespace
34
+ export namespace Format {
35
+ /** ***************** get phone number object using `libphonenumber-js` *******************/
36
+ const getPhone = (value: string, country: string) => {
37
+ const phone =
38
+ value.match(/^[0+]/) != null ? value.replace(/^00/, "+") : `+${value}`;
39
+ const number = parsePhoneNumberFromString(phone, country as CountryCode);
40
+ /* istanbul ignore next */
41
+ return (
42
+ number ?? {
43
+ country: "",
44
+ formatInternational: () => value,
45
+ }
46
+ );
47
+ };
48
+
49
+ /** ***************** common number format method using `numeral` *******************/
50
+ const numberFormat = (number?: string | number, format = "0,0[.]00a") => {
51
+ if (isNil(number)) {
52
+ return "";
53
+ }
54
+ const prefix = `${number}`.startsWith("+") ? "+" : "";
55
+ return prefix + numeral(number).format(format);
56
+ };
57
+
58
+ const makeDuration = (value: number, fmt = "HH:mm:ss.SSS") => {
59
+ if (!isNaN(value)) {
60
+ const days = differenceInDays(value, 0);
61
+ const prefix = days > 0 ? `${days}d:` : "";
62
+ let time = format(
63
+ parseISO(new Date(value).toISOString().replace("Z", "")),
64
+ fmt,
65
+ );
66
+ if (!prefix) time = time.replace(/^00:/, "");
67
+ return `${prefix}${time}`;
68
+ }
69
+ };
70
+
71
+ /** ***************** format phone number using `libphonenumber-js` *******************/
72
+ export const phone = (value?: string, country = "AE", useCss = false) => {
73
+ if (isEmpty(value) || value.length < 6 || !/^[\d+\s\-()]+$/.test(value))
74
+ return value;
75
+ const phone = getPhone(value, country);
76
+ /* istanbul ignore next */
77
+ return `${
78
+ useCss
79
+ ? `<span class="flag ${phone.country}"></span>`
80
+ : Countries.emoji(phone.country ?? "")
81
+ } ${phone.formatInternational()}`;
82
+ };
83
+ /** ***************** format whole number using `numeral` *******************/
84
+ export const number = (number?: string | number, format?: string) => {
85
+ return numberFormat(number, format);
86
+ };
87
+ /** ***************** format bytes using `numeral` *******************/
88
+ export const bytes = (number?: string | number) => {
89
+ return numberFormat(number, "0,0[.]00b");
90
+ };
91
+ /** ***************** format percentage using `numeral` *******************/
92
+ export const percent = (number?: string | number) => {
93
+ return numberFormat(number, "0,0[.]00%");
94
+ };
95
+ /** ***************** format date using `date-fns` *******************/
96
+ export const date = (date?: Date | string | number, fmt = "d-M-y") => {
97
+ return date ? format(new Date(date), fmt) : "";
98
+ };
99
+ /** ***************** format time duration from total seconds/milliseconds *******************/
100
+ /**
101
+ * Format time duration with milliseconds
102
+ * @param number
103
+ * @param isFraction
104
+ * @returns
105
+ */
106
+ export const duration = (number?: string | number, isFraction?: boolean) => {
107
+ if (isNil(number)) {
108
+ return "00:00.000";
109
+ }
110
+ if (number === Infinity) {
111
+ return "∞";
112
+ }
113
+ const value = parseFloat(`${number}`) * (isFraction ? 1000 : 1);
114
+ if (!isNaN(value)) {
115
+ return makeDuration(value);
116
+ }
117
+ return "00:00.000";
118
+ };
119
+ /**
120
+ * Format time duration without milliseconds
121
+ * @param number
122
+ * @param isFraction
123
+ * @returns
124
+ */
125
+ export const durationSeconds = (
126
+ number?: string | number,
127
+ isFraction?: boolean,
128
+ ) => {
129
+ if (isNil(number)) {
130
+ return "00:00";
131
+ }
132
+ if (number === Infinity) {
133
+ return "∞";
134
+ }
135
+ const value = parseFloat(`${number}`) * (isFraction ? 1000 : 1);
136
+ if (!isNaN(value)) {
137
+ return makeDuration(value, "HH:mm:ss");
138
+ }
139
+ return "00:00";
140
+ };
141
+ }
@@ -0,0 +1,108 @@
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 { isArray, isEmpty, isNil, isObject } from "./_isType";
25
+
26
+ const _get = (hit: any, field: string): any => {
27
+ /** ***************** return if object is empty *******************/
28
+ /* istanbul ignore next */
29
+ if (isNil(hit)) {
30
+ return undefined;
31
+ }
32
+ /** ***************** return object if property is empty *******************/
33
+ /* istanbul ignore next */
34
+ if (isNil(field)) {
35
+ return hit;
36
+ }
37
+
38
+ if (isObject(hit)) {
39
+ /** ***************** return value if object.property is not empty *******************/
40
+ if (!isNil(hit[field])) {
41
+ return hit[field];
42
+ }
43
+
44
+ /** ***************** if property name contains `.` separator split to multiple property names *******************/
45
+ if (field.includes(".")) {
46
+ let currentValue: any = hit;
47
+
48
+ const path = field.split(".");
49
+ for (let i = path.length - 1; i >= 0; i--) {
50
+ /** ***************** when property name is `a.b.c` iterate until property found in object. eg. `a`, `a.b` *******************/
51
+ const outerPath = path.slice(0, i as any).join(".");
52
+
53
+ /** ***************** join rest of property list *******************/
54
+ const innerPath = path.slice(i as any).join(".");
55
+
56
+ /** ***************** if current outer path found in object *******************/
57
+ if (outerPath in currentValue) {
58
+ currentValue = currentValue[outerPath];
59
+
60
+ /** ***************** check for type array *******************/
61
+ if (isArray(currentValue)) {
62
+ /** ***************** return value if remaining path found *******************/
63
+ if (currentValue[innerPath as any]) {
64
+ return currentValue[innerPath as any];
65
+ }
66
+ /** ***************** return array map of inner propeties *******************/
67
+ return currentValue
68
+ .map((val) => _get(val, innerPath))
69
+ .filter((val) => !isEmpty(val));
70
+ } else {
71
+ /** ***************** return value of remaining path *******************/
72
+ return _get(currentValue, innerPath);
73
+ }
74
+ }
75
+ }
76
+ }
77
+ }
78
+
79
+ return undefined;
80
+ };
81
+
82
+ /**
83
+ * get property value by path
84
+ * @internal
85
+ */
86
+ export const getByPath = <T = any>(
87
+ obj: KeyValue,
88
+ path: string,
89
+ defaultValue: any = undefined,
90
+ ): T => {
91
+ /** ***************** if empty object/path return defaultValue *******************/
92
+ if (isEmpty(obj) || isEmpty(path)) {
93
+ return defaultValue;
94
+ }
95
+ const value = _get(obj, path);
96
+ if (!isNil(value)) {
97
+ return value;
98
+ }
99
+ return defaultValue;
100
+ };
101
+
102
+ /**
103
+ * get first available value in list
104
+ * @internal
105
+ */
106
+ export const getValue = (...args: any[]) => {
107
+ return args.find((a) => !isEmpty(a));
108
+ };
package/src/_hash.ts ADDED
@@ -0,0 +1,62 @@
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
+ /**
27
+ * calculate hash from string
28
+ * @param str
29
+ * @internal
30
+ */
31
+ export const hash = (str: string) => {
32
+ let hash = 0;
33
+ let i;
34
+ let chr;
35
+ if (isEmpty(str)) return hash;
36
+ for (i = 0; i < str.length; i++) {
37
+ chr = str.charCodeAt(i);
38
+ hash = (hash << 5) - hash + chr;
39
+ hash |= 0; // Convert to 32bit integer
40
+ }
41
+ return hash;
42
+ };
43
+
44
+ /**
45
+ * generate short hash from current time
46
+ * @internal
47
+ */
48
+ export const shortHash = () => {
49
+ return (new Date().getTime() + Math.trunc(Math.random() * 1000)).toString(36);
50
+ };
51
+
52
+ /**
53
+ * generate uuid using `crypto.getRandomValues`
54
+ * @internal
55
+ */
56
+ export const uuid = () => {
57
+ return [1e7, 1e3, 4e3, 8e3, 1e11]
58
+ .join("-")
59
+ .replace(/[018]/g, (c: string) =>
60
+ (+c ^ ((Math.random() * 16) & (15 >> (+c / 4)))).toString(16),
61
+ );
62
+ };
@@ -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
+ import { getByPath } from "./_getByPath";
25
+
26
+ /**
27
+ * interpolate string template using model
28
+ * @internal
29
+ */
30
+ export const interpolate = (str: string, model: KeyValue) => {
31
+ return str.replace(/\${([^{}]*)}/g, (a: string, b: string): string => {
32
+ const r = getByPath(model, b);
33
+ return typeof r === "string" || typeof r === "number" ? `${r}` : "";
34
+ });
35
+ };
36
+
37
+ export const renderTemplate = (str: string, model: KeyValue) => {
38
+ return str.replace(/{{([^{}]*)}}/g, (a: string, b: string): string => {
39
+ const r = getByPath(model, b);
40
+ return typeof r === "string" || typeof r === "number" ? `${r}` : "";
41
+ });
42
+ };
@@ -0,0 +1,102 @@
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 { ascii } from "./_ascii";
25
+ import { isBoolean, isNumber, isObject, isString } from "./_isType";
26
+
27
+ /**
28
+ * check equality of objects
29
+ * @internal
30
+ */
31
+ export const isEqual = (obj: unknown, test: unknown): boolean => {
32
+ if (isString(obj) || isNumber(obj) || isBoolean(obj)) {
33
+ return obj === test;
34
+ } else if (Array.isArray(obj) && Array.isArray(test)) {
35
+ // if length dont match return false
36
+ if (obj.length !== test.length) {
37
+ return false;
38
+ }
39
+ // check if some value is false
40
+ return !obj.some((o, i) => !isEqual(o, test[i]));
41
+ } else if (isObject(obj) && isObject(test)) {
42
+ const okeys = Object.keys(obj);
43
+ const tkeys = Object.keys(test);
44
+ // if keys length dont match return false
45
+ if (okeys.length !== tkeys.length) {
46
+ return false;
47
+ }
48
+ // if keys dont match return false
49
+ if (okeys.some((k) => !tkeys.includes(k))) {
50
+ return false;
51
+ }
52
+ // check if some value is false
53
+ return !Object.keys(obj).some((k) => !isEqual(obj[k], test[k]));
54
+ }
55
+ return false;
56
+ };
57
+
58
+ /**
59
+ * match strings
60
+ * @internal
61
+ */
62
+ export const matchString = (
63
+ base: string,
64
+ match: string,
65
+ equality: "start" | boolean = false,
66
+ ) => {
67
+ const _base = ascii(base).toLowerCase();
68
+ const _match = ascii(match).toLowerCase();
69
+
70
+ return equality === true
71
+ ? _base === _match
72
+ : equality === "start"
73
+ ? _base.startsWith(_match)
74
+ : _base.includes(_match);
75
+ };
76
+
77
+ /**
78
+ * compare values
79
+ * @internal
80
+ */
81
+ export const compareValues =
82
+ (order: "asc" | "desc" = "asc", key?: string) =>
83
+ (a: unknown, b: unknown) => {
84
+ const aValue: string = key && isObject(a) ? a[key] : a;
85
+ const bValue: string = key && isObject(b) ? b[key] : b;
86
+
87
+ const bigger = order === "asc" ? 1 : -1;
88
+ const smaller = order === "desc" ? 1 : -1;
89
+ // return 0 when equal, allowing for multiple sorting properties
90
+ if (aValue === bValue) return 0;
91
+ // if numbers check greater
92
+ if (isNumber(aValue) && isNumber(bValue)) {
93
+ return aValue > bValue ? bigger : smaller;
94
+ }
95
+ // if boolean sort true first
96
+ if (isBoolean(aValue) && isBoolean(bValue)) {
97
+ return aValue ? smaller : bigger;
98
+ }
99
+ const _a = ascii(aValue).toLowerCase();
100
+ const _b = ascii(bValue).toLowerCase();
101
+ return _a.localeCompare(_b) === 1 ? bigger : smaller;
102
+ };
package/src/_isType.ts ADDED
@@ -0,0 +1,105 @@
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
+ export const isUndefined = (value: unknown): value is undefined => {
25
+ return value === undefined;
26
+ };
27
+
28
+ export const isNull = (value: unknown): value is null => {
29
+ return value === null;
30
+ };
31
+
32
+ export const isNil = (val: unknown): val is undefined => {
33
+ return val === undefined || val === null;
34
+ };
35
+ export const isEmpty = (val: unknown): val is undefined => {
36
+ if (val instanceof Date) return false;
37
+ if (isNil(val) || val === "") {
38
+ return true;
39
+ }
40
+ if (Array.isArray(val) && val.filter(Boolean).length === 0) {
41
+ return true;
42
+ }
43
+ if (val instanceof Map || val instanceof Set) return val.size === 0;
44
+ if (["boolean", "number", "bigint", "function"].includes(typeof val)) {
45
+ return false;
46
+ }
47
+ return !!val && typeof val === "object" && Object.entries(val).length === 0;
48
+ };
49
+
50
+ export const isObject = (val: unknown): val is KeyValue => {
51
+ return (
52
+ !isNil(val) &&
53
+ !(val instanceof Date) &&
54
+ typeof val === "object" &&
55
+ !Array.isArray(val)
56
+ );
57
+ };
58
+ export const isArray = <T = unknown>(val: unknown): val is T[] => {
59
+ return Array.isArray(val);
60
+ };
61
+
62
+ export const isString = (value: unknown): value is string => {
63
+ return typeof value === "string";
64
+ };
65
+
66
+ export const isNumber = (value: unknown): value is number => {
67
+ return typeof value === "number";
68
+ };
69
+
70
+ export const isBoolean = (value: unknown): value is boolean => {
71
+ return typeof value === "boolean";
72
+ };
73
+ export const isTrue = (value: unknown = ""): value is true => {
74
+ return (
75
+ value === true ||
76
+ value === 1 ||
77
+ value?.toString().toLowerCase() === "yes" ||
78
+ value?.toString().toLowerCase() === "true"
79
+ );
80
+ };
81
+ export const isFalse = (value: unknown = ""): value is false => {
82
+ return (
83
+ value === false ||
84
+ value === 0 ||
85
+ value?.toString().toLowerCase() === "no" ||
86
+ value?.toString().toLowerCase() === "false"
87
+ );
88
+ };
89
+
90
+ export const isColor = (color: string) =>
91
+ color.startsWith?.("#") ||
92
+ color.startsWith?.("rgb") ||
93
+ color.startsWith?.("hsl") ||
94
+ color.startsWith?.("lab") ||
95
+ color === "transparent";
96
+
97
+ export const isSvgPath = (value: unknown): value is string => {
98
+ return (
99
+ typeof value === "string" && value.match(/^[Mm][\d.].*[\dzZ]$/) !== null
100
+ );
101
+ };
102
+
103
+ export const isRtl = () => {
104
+ return getComputedStyle(document.documentElement).direction === "rtl";
105
+ };
@@ -0,0 +1,33 @@
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
+ export const mergeRefs =
27
+ (...refs: AnyObject[]) =>
28
+ (el: AnyObject) => {
29
+ refs.forEach((ref) => {
30
+ if (typeof ref === "function") ref(el);
31
+ if (ref && "current" in ref) ref.current = el;
32
+ });
33
+ };
@@ -0,0 +1,69 @@
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 { isArray, isEmpty } from "./_isType";
25
+
26
+ /**
27
+ * split text into array of tokens by word boundaries
28
+ * @param str
29
+ * @param keywords
30
+ * @internal
31
+ */
32
+ export const tokenize = (str: string, keywords?: string | string[]) => {
33
+ if (isEmpty(keywords)) {
34
+ return [[str, ""]];
35
+ }
36
+ const keys = isArray(keywords) ? keywords : [keywords];
37
+ const keyMap = keys.map((key) => {
38
+ // escape regex control characters
39
+ let escapedKey = key.replace(/([+*.?^$()[\]\\!&|\-=])/gm, "\\$1");
40
+ // place token within word boundaries if start/end with alpha-numeric
41
+ if (key.match(/^[a-z0-9]/i) != null) escapedKey = `\\b${escapedKey}`;
42
+ if (key.match(/[a-z0-9]$/i) != null) escapedKey = `${escapedKey}\\b`;
43
+ return escapedKey;
44
+ });
45
+ const regx = new RegExp(`(${keyMap.join("|")})`, "img");
46
+ let match;
47
+ let lastIndex = 0;
48
+ const tokens = [];
49
+ while ((match = regx.exec(str)) !== null) {
50
+ tokens.push([
51
+ str.substring(lastIndex, match.index),
52
+ str.substring(match.index, regx.lastIndex),
53
+ ]);
54
+ lastIndex = regx.lastIndex;
55
+ }
56
+ tokens.push([str.substring(lastIndex), ""]);
57
+ return tokens;
58
+ };
59
+
60
+ export const iconToken = (str: string) => {
61
+ const split = str.trim().split(" ");
62
+ const first = split[0];
63
+ const last = split.length > 1 && split.pop();
64
+ return (
65
+ first && last
66
+ ? `${first.charAt(0)}${last.charAt(0)}`
67
+ : `${first.substring(0, 2)}`
68
+ ).toUpperCase();
69
+ };