@pol-studios/utils 1.0.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/dist/array/index.d.ts +1 -0
- package/dist/array/index.js +136 -0
- package/dist/async/index.d.ts +1 -0
- package/dist/async/index.js +67 -0
- package/dist/cache/index.d.ts +1 -0
- package/dist/cache/index.js +42 -0
- package/dist/color/index.d.ts +1 -0
- package/dist/color/index.js +16 -0
- package/dist/date/index.d.ts +2 -0
- package/dist/date/index.js +102 -0
- package/dist/dev/index.d.ts +1 -0
- package/dist/dev/index.js +21 -0
- package/dist/device/index.d.ts +1 -0
- package/dist/device/index.js +31 -0
- package/dist/enum/index.d.ts +1 -0
- package/dist/enum/index.js +19 -0
- package/dist/error/index.d.ts +1 -0
- package/dist/error/index.js +198 -0
- package/dist/format/index.d.ts +1 -0
- package/dist/format/index.js +36 -0
- package/dist/index-B2oAfh_i.d.ts +63 -0
- package/dist/index-BDWEMkWa.d.ts +56 -0
- package/dist/index-BSNY-QEc.d.ts +15 -0
- package/dist/index-BaA8cg0E.d.ts +100 -0
- package/dist/index-Btn_GVfm.d.ts +13 -0
- package/dist/index-BxUoIlv_.d.ts +39 -0
- package/dist/index-ByjfWGR4.d.ts +151 -0
- package/dist/index-C-YwC08Y.d.ts +45 -0
- package/dist/index-C062CkPL.d.ts +31 -0
- package/dist/index-C8CxeaqZ.d.ts +24 -0
- package/dist/index-CZbs1QQN.d.ts +102 -0
- package/dist/index-Cp7KyTeB.d.ts +23 -0
- package/dist/index-CrplC0qf.d.ts +29 -0
- package/dist/index-Cvl_0aDq.d.ts +14 -0
- package/dist/index-D_IvPWYI.d.ts +77 -0
- package/dist/index-DdaZnk7j.d.ts +28 -0
- package/dist/index-vLuR45Km.d.ts +27 -0
- package/dist/index.d.ts +84 -0
- package/dist/index.js +1279 -0
- package/dist/object/index.d.ts +1 -0
- package/dist/object/index.js +115 -0
- package/dist/state/index.d.ts +1 -0
- package/dist/state/index.js +17 -0
- package/dist/string/index.d.ts +1 -0
- package/dist/string/index.js +55 -0
- package/dist/tailwind/index.d.ts +2 -0
- package/dist/tailwind/index.js +9 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.js +37 -0
- package/dist/uuid/index.d.ts +1 -0
- package/dist/uuid/index.js +10 -0
- package/dist/validation/index.d.ts +1 -0
- package/dist/validation/index.js +123 -0
- package/package.json +50 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { G as GroupedItems, d as chunk, e as chunkArray, k as convertArrayToObject, j as getAverage, g as groupBy, c as groupByMultipleCriteria, m as mapUnique, f as mapUnqiue, r as range, b as selectByMin, a as sort, s as sortBy, h as tryGetAverage, t as tryGetSum, u as unique } from '../index-ByjfWGR4.js';
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
// src/array/sortBy.ts
|
|
2
|
+
function sortBy(items, property, isAscending = false) {
|
|
3
|
+
if (items === null || items === void 0) return [];
|
|
4
|
+
return [...items]?.sort((a, b) => {
|
|
5
|
+
const aProp = a[property];
|
|
6
|
+
const bProp = b[property];
|
|
7
|
+
if (isAscending) {
|
|
8
|
+
return aProp === bProp ? 0 : aProp < bProp ? 1 : -1;
|
|
9
|
+
}
|
|
10
|
+
return aProp === bProp ? 0 : aProp < bProp ? -1 : 1;
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
function sort(items, func, isAscending = false) {
|
|
14
|
+
const startingValue = isAscending ? 1 : -1;
|
|
15
|
+
const endingValue = isAscending ? -1 : 1;
|
|
16
|
+
return [...items].sort(
|
|
17
|
+
(a, b) => func(a) === func(b) ? 0 : func(a) < func(b) ? startingValue : endingValue
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
function selectByMin(items, action) {
|
|
21
|
+
return items.reduce((min, current) => {
|
|
22
|
+
return action(current) < action(min) ? current : min;
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// src/internal/isUsable.ts
|
|
27
|
+
function isUsable(value) {
|
|
28
|
+
return value !== void 0 && value !== null && (typeof value !== "number" || isNaN(value) === false);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// src/array/groupBy.ts
|
|
32
|
+
function groupBy(list, key) {
|
|
33
|
+
const map = /* @__PURE__ */ new Map();
|
|
34
|
+
if (isUsable(list) === false) return map;
|
|
35
|
+
list.forEach((item) => {
|
|
36
|
+
const keyValue = typeof key === "function" ? key(item) : item[key];
|
|
37
|
+
const collection = map.get(keyValue);
|
|
38
|
+
if (!collection) {
|
|
39
|
+
map.set(keyValue, [item]);
|
|
40
|
+
} else {
|
|
41
|
+
collection.push(item);
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
return map;
|
|
45
|
+
}
|
|
46
|
+
function groupByMultipleCriteria(items, criteria) {
|
|
47
|
+
if (isUsable(items) === false) return [];
|
|
48
|
+
const grouped = {};
|
|
49
|
+
items.forEach((item) => {
|
|
50
|
+
const key = criteria.map((criterion) => item[criterion]).join("|");
|
|
51
|
+
if (!grouped[key]) {
|
|
52
|
+
grouped[key] = [];
|
|
53
|
+
}
|
|
54
|
+
grouped[key].push(item);
|
|
55
|
+
});
|
|
56
|
+
const results = Object.values(grouped).map((group) => ({
|
|
57
|
+
items: Array.isArray(group) ? group : []
|
|
58
|
+
}));
|
|
59
|
+
return results;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// src/array/chunk.ts
|
|
63
|
+
function chunk(array, chunkSize) {
|
|
64
|
+
if (chunkSize <= 0) throw new Error("chunkSize must be greater than 0");
|
|
65
|
+
const chunks = [];
|
|
66
|
+
for (let i = 0; i < array.length; i += chunkSize) {
|
|
67
|
+
chunks.push(array.slice(i, i + chunkSize));
|
|
68
|
+
}
|
|
69
|
+
return chunks;
|
|
70
|
+
}
|
|
71
|
+
var chunkArray = chunk;
|
|
72
|
+
|
|
73
|
+
// src/array/unique.ts
|
|
74
|
+
function unique(array, key) {
|
|
75
|
+
if (isUsable(array) === false) return [];
|
|
76
|
+
return Array.from(new Set(array.map((x) => x[key])));
|
|
77
|
+
}
|
|
78
|
+
var mapUnique = unique;
|
|
79
|
+
var mapUnqiue = unique;
|
|
80
|
+
|
|
81
|
+
// src/array/range.ts
|
|
82
|
+
function range(size, startAt = 0) {
|
|
83
|
+
return [...Array(size).keys()].map((i, index) => index + startAt);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// src/array/sum.ts
|
|
87
|
+
function tryGetSum(itemsArray, defaultValue = 0) {
|
|
88
|
+
if (isUsable(itemsArray) === false) return defaultValue;
|
|
89
|
+
if (itemsArray.length === 0) return defaultValue;
|
|
90
|
+
const results = itemsArray.reduce((a, b) => a + b, defaultValue);
|
|
91
|
+
if (Number.isNaN(results)) {
|
|
92
|
+
return defaultValue;
|
|
93
|
+
}
|
|
94
|
+
return results;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// src/array/average.ts
|
|
98
|
+
function tryGetAverage(items, defaultValue = 0) {
|
|
99
|
+
if (isUsable(items) === false) return defaultValue;
|
|
100
|
+
if (items.length === 0) return defaultValue;
|
|
101
|
+
const sum = tryGetSum(items, defaultValue);
|
|
102
|
+
const response = sum / items.length;
|
|
103
|
+
return response;
|
|
104
|
+
}
|
|
105
|
+
function getAverage(arr) {
|
|
106
|
+
const sum = arr.reduce((acc, curr) => acc + curr, 0);
|
|
107
|
+
return sum / arr.length;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// src/array/convertToObject.ts
|
|
111
|
+
function convertArrayToObject(array, key) {
|
|
112
|
+
return array?.reduce(
|
|
113
|
+
(obj, item) => {
|
|
114
|
+
obj[item[key]] = item;
|
|
115
|
+
return obj;
|
|
116
|
+
},
|
|
117
|
+
{}
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
export {
|
|
121
|
+
chunk,
|
|
122
|
+
chunkArray,
|
|
123
|
+
convertArrayToObject,
|
|
124
|
+
getAverage,
|
|
125
|
+
groupBy,
|
|
126
|
+
groupByMultipleCriteria,
|
|
127
|
+
mapUnique,
|
|
128
|
+
mapUnqiue,
|
|
129
|
+
range,
|
|
130
|
+
selectByMin,
|
|
131
|
+
sort,
|
|
132
|
+
sortBy,
|
|
133
|
+
tryGetAverage,
|
|
134
|
+
tryGetSum,
|
|
135
|
+
unique
|
|
136
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { S as Semaphore, c as concurrent, a as debounce, d as delay, p as promiseAllWithConcurrencyLimit } from '../index-C-YwC08Y.js';
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// src/async/delay.ts
|
|
2
|
+
function delay(ms) {
|
|
3
|
+
return new Promise((resolve) => {
|
|
4
|
+
setTimeout(resolve, ms);
|
|
5
|
+
});
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
// src/async/debounce.ts
|
|
9
|
+
var timeoutId = null;
|
|
10
|
+
function debounce(func, wait) {
|
|
11
|
+
if (timeoutId !== null) {
|
|
12
|
+
clearTimeout(timeoutId);
|
|
13
|
+
}
|
|
14
|
+
timeoutId = setTimeout(() => {
|
|
15
|
+
func();
|
|
16
|
+
}, wait);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// src/async/concurrent.ts
|
|
20
|
+
async function concurrent(tasks, concurrency) {
|
|
21
|
+
const results = [];
|
|
22
|
+
let current = 0;
|
|
23
|
+
async function worker() {
|
|
24
|
+
while (current < tasks.length) {
|
|
25
|
+
const index = current++;
|
|
26
|
+
results[index] = await tasks[index]();
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const workers = [];
|
|
30
|
+
for (let i = 0; i < concurrency; i++) {
|
|
31
|
+
workers.push(worker());
|
|
32
|
+
}
|
|
33
|
+
await Promise.all(workers);
|
|
34
|
+
return results;
|
|
35
|
+
}
|
|
36
|
+
var promiseAllWithConcurrencyLimit = concurrent;
|
|
37
|
+
|
|
38
|
+
// src/async/semaphore.ts
|
|
39
|
+
var Semaphore = class {
|
|
40
|
+
constructor(count) {
|
|
41
|
+
this.tasks = [];
|
|
42
|
+
this.count = count;
|
|
43
|
+
}
|
|
44
|
+
acquire() {
|
|
45
|
+
if (this.count > 0) {
|
|
46
|
+
this.count--;
|
|
47
|
+
return Promise.resolve();
|
|
48
|
+
} else {
|
|
49
|
+
return new Promise((resolve) => this.tasks.push(resolve));
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
release() {
|
|
53
|
+
if (this.tasks.length > 0) {
|
|
54
|
+
const resolve = this.tasks.shift();
|
|
55
|
+
if (resolve) resolve();
|
|
56
|
+
} else {
|
|
57
|
+
this.count++;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
export {
|
|
62
|
+
Semaphore,
|
|
63
|
+
concurrent,
|
|
64
|
+
debounce,
|
|
65
|
+
delay,
|
|
66
|
+
promiseAllWithConcurrencyLimit
|
|
67
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { c as cacheFunction, b as clearAllCache, a as clearCache } from '../index-CrplC0qf.js';
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// src/cache/index.ts
|
|
2
|
+
var cacheStore = /* @__PURE__ */ new Map();
|
|
3
|
+
async function cacheFunction(key, fn, duration) {
|
|
4
|
+
const now = Date.now();
|
|
5
|
+
const cached = cacheStore.get(key);
|
|
6
|
+
if (cached) {
|
|
7
|
+
if (now < cached.expiresAt) {
|
|
8
|
+
return cached.data;
|
|
9
|
+
}
|
|
10
|
+
if (cached.promise) {
|
|
11
|
+
return cached.promise;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
const promise = fn().then((result) => {
|
|
15
|
+
cacheStore.set(key, {
|
|
16
|
+
data: result,
|
|
17
|
+
expiresAt: Date.now() + duration
|
|
18
|
+
});
|
|
19
|
+
return result;
|
|
20
|
+
}).catch((error) => {
|
|
21
|
+
cacheStore.delete(key);
|
|
22
|
+
throw error;
|
|
23
|
+
});
|
|
24
|
+
cacheStore.set(key, {
|
|
25
|
+
data: cached?.data,
|
|
26
|
+
// Retain existing data until new data is fetched
|
|
27
|
+
expiresAt: cached?.expiresAt || now,
|
|
28
|
+
promise
|
|
29
|
+
});
|
|
30
|
+
return promise;
|
|
31
|
+
}
|
|
32
|
+
function clearCache(key) {
|
|
33
|
+
cacheStore.delete(key);
|
|
34
|
+
}
|
|
35
|
+
function clearAllCache() {
|
|
36
|
+
cacheStore.clear();
|
|
37
|
+
}
|
|
38
|
+
export {
|
|
39
|
+
cacheFunction,
|
|
40
|
+
clearAllCache,
|
|
41
|
+
clearCache
|
|
42
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { a as isDarkColor } from '../index-Cvl_0aDq.js';
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// src/color/index.ts
|
|
2
|
+
function isDarkColor(c) {
|
|
3
|
+
c = c.substring(1);
|
|
4
|
+
const rgb = parseInt(c, 16);
|
|
5
|
+
const r = rgb >> 16 & 255;
|
|
6
|
+
const g = rgb >> 8 & 255;
|
|
7
|
+
const b = rgb >> 0 & 255;
|
|
8
|
+
const luma = 0.2126 * r + 0.7152 * g + 0.0722 * b;
|
|
9
|
+
if (luma < 70) {
|
|
10
|
+
return true;
|
|
11
|
+
}
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
export {
|
|
15
|
+
isDarkColor
|
|
16
|
+
};
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// src/date/utc.ts
|
|
2
|
+
import moment from "moment";
|
|
3
|
+
|
|
4
|
+
// src/internal/isUsable.ts
|
|
5
|
+
function isUsable(value) {
|
|
6
|
+
return value !== void 0 && value !== null && (typeof value !== "number" || isNaN(value) === false);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
// src/date/utc.ts
|
|
10
|
+
function getUtcDate() {
|
|
11
|
+
const now = /* @__PURE__ */ new Date();
|
|
12
|
+
return new Date(
|
|
13
|
+
Date.UTC(
|
|
14
|
+
now.getUTCFullYear(),
|
|
15
|
+
now.getUTCMonth(),
|
|
16
|
+
now.getUTCDate(),
|
|
17
|
+
now.getUTCHours(),
|
|
18
|
+
now.getUTCMinutes(),
|
|
19
|
+
now.getUTCSeconds()
|
|
20
|
+
)
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
function toUtcDate(date) {
|
|
24
|
+
if (isUsable(date) === false) return null;
|
|
25
|
+
return new Date(
|
|
26
|
+
Date.UTC(
|
|
27
|
+
date.getUTCFullYear(),
|
|
28
|
+
date.getUTCMonth(),
|
|
29
|
+
date.getUTCDate(),
|
|
30
|
+
date.getUTCHours(),
|
|
31
|
+
date.getUTCMinutes(),
|
|
32
|
+
date.getUTCSeconds()
|
|
33
|
+
)
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
function getUtcMoment() {
|
|
37
|
+
return moment.utc(moment.now());
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// src/date/compare.ts
|
|
41
|
+
function areDatesEqual(date1, date2) {
|
|
42
|
+
return date1 === date2;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// src/date/parse.ts
|
|
46
|
+
import moment2 from "moment";
|
|
47
|
+
function dateReviver(key, value) {
|
|
48
|
+
if (typeof value !== "string") return value;
|
|
49
|
+
const isUtcOrLocalDate = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{1,7})?(Z)?$/.test(value);
|
|
50
|
+
if (isUtcOrLocalDate) {
|
|
51
|
+
return moment2.utc(value);
|
|
52
|
+
} else {
|
|
53
|
+
const isTimeZoneDate = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{1,7})?[+-]\d{2}:\d{2}$/.test(
|
|
54
|
+
value
|
|
55
|
+
);
|
|
56
|
+
if (isTimeZoneDate) {
|
|
57
|
+
return moment2(value);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return value;
|
|
61
|
+
}
|
|
62
|
+
function parseWithDate(key, value) {
|
|
63
|
+
if (typeof value !== "string") return value;
|
|
64
|
+
const isUtcOrLocalDate = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{1,7})?(Z)?$/.test(value);
|
|
65
|
+
if (isUtcOrLocalDate) {
|
|
66
|
+
return new Date(value);
|
|
67
|
+
} else {
|
|
68
|
+
const isTimeZoneDate = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{1,7})?[+-]\d{2}:\d{2}$/.test(
|
|
69
|
+
value
|
|
70
|
+
);
|
|
71
|
+
if (isTimeZoneDate) {
|
|
72
|
+
return new Date(value);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return value;
|
|
76
|
+
}
|
|
77
|
+
function isValidDate(date) {
|
|
78
|
+
if (isUsable(date) === false) return false;
|
|
79
|
+
return date.getTime() === date.getTime();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// src/date/add.ts
|
|
83
|
+
import moment3 from "moment";
|
|
84
|
+
function addDates(date1, date2) {
|
|
85
|
+
const duration = moment3.duration(date2.diff(date1));
|
|
86
|
+
return date1.add(duration);
|
|
87
|
+
}
|
|
88
|
+
function addDays(date1, days) {
|
|
89
|
+
const newDate = date1.add(days, "days");
|
|
90
|
+
return newDate;
|
|
91
|
+
}
|
|
92
|
+
export {
|
|
93
|
+
addDates,
|
|
94
|
+
addDays,
|
|
95
|
+
areDatesEqual,
|
|
96
|
+
dateReviver,
|
|
97
|
+
getUtcDate,
|
|
98
|
+
getUtcMoment,
|
|
99
|
+
isValidDate,
|
|
100
|
+
parseWithDate,
|
|
101
|
+
toUtcDate
|
|
102
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { d as debugLog, b as isDev, a as isDevEnvironment } from '../index-vLuR45Km.js';
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// src/dev/index.ts
|
|
2
|
+
function isDevEnvironment() {
|
|
3
|
+
if (typeof process === "undefined") return false;
|
|
4
|
+
const development = (process.env.WITH_MODE ?? "") === "development";
|
|
5
|
+
return development;
|
|
6
|
+
}
|
|
7
|
+
function isDev() {
|
|
8
|
+
if (typeof process === "undefined") return false;
|
|
9
|
+
const development = (process.env.WITH_MODE ?? "") === "development";
|
|
10
|
+
return development;
|
|
11
|
+
}
|
|
12
|
+
function debugLog(message, ...optionalParams) {
|
|
13
|
+
if (isDevEnvironment()) {
|
|
14
|
+
console.log(message, ...optionalParams);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export {
|
|
18
|
+
debugLog,
|
|
19
|
+
isDev,
|
|
20
|
+
isDevEnvironment
|
|
21
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { h as hasCameraAsync, b as isIphone, c as isPwaLaunched, a as isSmallDevice } from '../index-C062CkPL.js';
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// src/device/index.ts
|
|
2
|
+
function isSmallDevice() {
|
|
3
|
+
return window.innerWidth <= 640;
|
|
4
|
+
}
|
|
5
|
+
function isIphone() {
|
|
6
|
+
return /iPhone/.test(navigator.userAgent);
|
|
7
|
+
}
|
|
8
|
+
function isPwaLaunched() {
|
|
9
|
+
if (window.navigator.standalone) {
|
|
10
|
+
return true;
|
|
11
|
+
}
|
|
12
|
+
if (window.matchMedia("(display-mode: standalone)").matches) {
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
async function hasCameraAsync() {
|
|
18
|
+
try {
|
|
19
|
+
const devices = await navigator.mediaDevices?.enumerateDevices();
|
|
20
|
+
return devices?.some((device) => device.kind === "videoinput") ?? false;
|
|
21
|
+
} catch (error) {
|
|
22
|
+
console.error("Error checking for camera:", error);
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
export {
|
|
27
|
+
hasCameraAsync,
|
|
28
|
+
isIphone,
|
|
29
|
+
isPwaLaunched,
|
|
30
|
+
isSmallDevice
|
|
31
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { g as getEnumValues, p as parseStringToEnum } from '../index-Cp7KyTeB.js';
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// src/enum/index.ts
|
|
2
|
+
function getEnumValues(enumObject) {
|
|
3
|
+
const values = Object.values(enumObject);
|
|
4
|
+
return values.filter((value) => typeof value !== "number");
|
|
5
|
+
}
|
|
6
|
+
function parseStringToEnum(enumType, value) {
|
|
7
|
+
if (value in enumType) {
|
|
8
|
+
return enumType[value];
|
|
9
|
+
}
|
|
10
|
+
const numericValue = parseInt(value, 10);
|
|
11
|
+
if (!isNaN(numericValue) && Object.values(enumType).includes(numericValue)) {
|
|
12
|
+
return numericValue;
|
|
13
|
+
}
|
|
14
|
+
return void 0;
|
|
15
|
+
}
|
|
16
|
+
export {
|
|
17
|
+
getEnumValues,
|
|
18
|
+
parseStringToEnum
|
|
19
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { E as ErrorMapping, P as PostgrestError, U as UserFriendlyError, V as ValidationErrorMapping, e as extractFieldName, c as getFormFieldError, g as getUserFriendlyError, b as isNetworkError, a as isPostgrestError } from '../index-B2oAfh_i.js';
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
// src/error/mapper.ts
|
|
2
|
+
var errorMappings = [
|
|
3
|
+
// Unique constraint violation
|
|
4
|
+
{
|
|
5
|
+
code: "23505",
|
|
6
|
+
message: "This item already exists",
|
|
7
|
+
action: "Please use a different name or value"
|
|
8
|
+
},
|
|
9
|
+
// Foreign key violation
|
|
10
|
+
{
|
|
11
|
+
code: "23503",
|
|
12
|
+
message: "Cannot delete: this item is in use",
|
|
13
|
+
action: "Remove all references to this item before deleting"
|
|
14
|
+
},
|
|
15
|
+
// Check constraint violation
|
|
16
|
+
{
|
|
17
|
+
code: "23514",
|
|
18
|
+
message: "Invalid value provided",
|
|
19
|
+
action: "Please check your input and try again"
|
|
20
|
+
},
|
|
21
|
+
// Undefined table
|
|
22
|
+
{
|
|
23
|
+
code: "42P01",
|
|
24
|
+
message: "Service temporarily unavailable",
|
|
25
|
+
action: "Please try again in a moment"
|
|
26
|
+
},
|
|
27
|
+
// Insufficient privilege
|
|
28
|
+
{
|
|
29
|
+
code: "42501",
|
|
30
|
+
message: "You don't have permission to perform this action",
|
|
31
|
+
action: "Contact your administrator if you need access"
|
|
32
|
+
},
|
|
33
|
+
// Not found (PostgREST)
|
|
34
|
+
{
|
|
35
|
+
code: "PGRST116",
|
|
36
|
+
message: "Item not found",
|
|
37
|
+
action: "The item may have been deleted or moved"
|
|
38
|
+
},
|
|
39
|
+
// Network errors
|
|
40
|
+
{
|
|
41
|
+
pattern: /network|connection|fetch|timeout|offline/i,
|
|
42
|
+
message: "Connection problem",
|
|
43
|
+
action: "Please check your internet connection and try again"
|
|
44
|
+
},
|
|
45
|
+
// Authentication errors
|
|
46
|
+
{
|
|
47
|
+
pattern: /unauthorized|authentication|token|session/i,
|
|
48
|
+
message: "Your session has expired",
|
|
49
|
+
action: "Please sign in again"
|
|
50
|
+
},
|
|
51
|
+
// Validation errors
|
|
52
|
+
{
|
|
53
|
+
pattern: /validation|invalid|required|missing/i,
|
|
54
|
+
message: "Please check your input",
|
|
55
|
+
action: "Make sure all required fields are filled correctly"
|
|
56
|
+
},
|
|
57
|
+
// Generic fallback
|
|
58
|
+
{
|
|
59
|
+
message: "Something went wrong",
|
|
60
|
+
action: "Please try again"
|
|
61
|
+
}
|
|
62
|
+
];
|
|
63
|
+
function extractErrorCode(error) {
|
|
64
|
+
if (error?.code) return error.code;
|
|
65
|
+
if (error?.error?.code) return error.error.code;
|
|
66
|
+
return void 0;
|
|
67
|
+
}
|
|
68
|
+
function extractErrorMessage(error) {
|
|
69
|
+
if (error?.message) return error.message;
|
|
70
|
+
if (error?.error?.message) return error.error.message;
|
|
71
|
+
if (typeof error === "string") return error;
|
|
72
|
+
return "An unknown error occurred";
|
|
73
|
+
}
|
|
74
|
+
function getUserFriendlyError(error) {
|
|
75
|
+
const errorCode = extractErrorCode(error);
|
|
76
|
+
const errorMessage = extractErrorMessage(error);
|
|
77
|
+
if (errorCode) {
|
|
78
|
+
const codeMatch = errorMappings.find((mapping) => mapping.code === errorCode);
|
|
79
|
+
if (codeMatch) {
|
|
80
|
+
return {
|
|
81
|
+
message: codeMatch.message,
|
|
82
|
+
action: codeMatch.action
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
const patternMatch = errorMappings.find(
|
|
87
|
+
(mapping) => mapping.pattern && mapping.pattern.test(errorMessage)
|
|
88
|
+
);
|
|
89
|
+
if (patternMatch) {
|
|
90
|
+
return {
|
|
91
|
+
message: patternMatch.message,
|
|
92
|
+
action: patternMatch.action
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
const fallback = errorMappings[errorMappings.length - 1];
|
|
96
|
+
return {
|
|
97
|
+
message: fallback.message,
|
|
98
|
+
action: fallback.action
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
function isPostgrestError(error) {
|
|
102
|
+
return error && typeof error === "object" && "code" in error && "message" in error && typeof error.code === "string";
|
|
103
|
+
}
|
|
104
|
+
function isNetworkError(error) {
|
|
105
|
+
const message = extractErrorMessage(error).toLowerCase();
|
|
106
|
+
return /network|connection|fetch|timeout|offline|failed to fetch/i.test(message);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// src/error/form.ts
|
|
110
|
+
var validationMappings = [
|
|
111
|
+
{
|
|
112
|
+
pattern: /required|missing|empty/i,
|
|
113
|
+
message: "This field is required",
|
|
114
|
+
action: "Please fill in this field"
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
pattern: /min.*length|too short/i,
|
|
118
|
+
message: "This field is too short",
|
|
119
|
+
action: "Please enter more characters"
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
pattern: /max.*length|too long/i,
|
|
123
|
+
message: "This field is too long",
|
|
124
|
+
action: "Please enter fewer characters"
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
pattern: /email|invalid.*email/i,
|
|
128
|
+
message: "Please enter a valid email address",
|
|
129
|
+
action: "Example: name@example.com"
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
pattern: /password|weak/i,
|
|
133
|
+
message: "Password is too weak",
|
|
134
|
+
action: "Use at least 8 characters with numbers and letters"
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
pattern: /match|doesn't match/i,
|
|
138
|
+
message: "Values don't match",
|
|
139
|
+
action: "Please check that both fields match"
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
pattern: /number|numeric/i,
|
|
143
|
+
message: "Please enter a valid number",
|
|
144
|
+
action: "Only numbers are allowed"
|
|
145
|
+
},
|
|
146
|
+
{
|
|
147
|
+
pattern: /date|invalid.*date/i,
|
|
148
|
+
message: "Please enter a valid date",
|
|
149
|
+
action: "Use format: MM/DD/YYYY"
|
|
150
|
+
},
|
|
151
|
+
{
|
|
152
|
+
pattern: /url|invalid.*url/i,
|
|
153
|
+
message: "Please enter a valid URL",
|
|
154
|
+
action: "Example: https://example.com"
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
pattern: /phone|invalid.*phone/i,
|
|
158
|
+
message: "Please enter a valid phone number",
|
|
159
|
+
action: "Example: (555) 123-4567"
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
message: "Invalid value",
|
|
163
|
+
action: "Please check your input"
|
|
164
|
+
}
|
|
165
|
+
];
|
|
166
|
+
function getFormFieldError(error) {
|
|
167
|
+
if (!error) return null;
|
|
168
|
+
const errorMessage = typeof error === "string" ? error : error.message || "";
|
|
169
|
+
if (!errorMessage) return null;
|
|
170
|
+
const patternMatch = validationMappings.find(
|
|
171
|
+
(mapping) => mapping.pattern && mapping.pattern.test(errorMessage)
|
|
172
|
+
);
|
|
173
|
+
if (patternMatch) {
|
|
174
|
+
return {
|
|
175
|
+
message: patternMatch.message,
|
|
176
|
+
action: patternMatch.action
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
const fallback = validationMappings[validationMappings.length - 1];
|
|
180
|
+
return {
|
|
181
|
+
message: fallback.message,
|
|
182
|
+
action: fallback.action
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
function extractFieldName(errorMessage) {
|
|
186
|
+
const fieldMatch = errorMessage.match(/(?:field|property|column)\s+['"]?(\w+)['"]?/i);
|
|
187
|
+
if (fieldMatch && fieldMatch[1]) {
|
|
188
|
+
return fieldMatch[1];
|
|
189
|
+
}
|
|
190
|
+
return null;
|
|
191
|
+
}
|
|
192
|
+
export {
|
|
193
|
+
extractFieldName,
|
|
194
|
+
getFormFieldError,
|
|
195
|
+
getUserFriendlyError,
|
|
196
|
+
isNetworkError,
|
|
197
|
+
isPostgrestError
|
|
198
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { f as formatCurrency, b as toDecimalString, a as toNumberString, t as toUsdString } from '../index-DdaZnk7j.js';
|