@kikiutils/shared 9.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/LICENSE +21 -0
- package/README.md +142 -0
- package/dist/consola.cjs +35 -0
- package/dist/consola.cjs.map +1 -0
- package/dist/consola.d.ts +24 -0
- package/dist/consola.d.ts.map +1 -0
- package/dist/consola.mjs +32 -0
- package/dist/consola.mjs.map +1 -0
- package/dist/crypto-hash.cjs +60 -0
- package/dist/crypto-hash.cjs.map +1 -0
- package/dist/crypto-hash.d.ts +26 -0
- package/dist/crypto-hash.d.ts.map +1 -0
- package/dist/crypto-hash.mjs +49 -0
- package/dist/crypto-hash.mjs.map +1 -0
- package/dist/datetime.cjs +131 -0
- package/dist/datetime.cjs.map +1 -0
- package/dist/datetime.d.ts +79 -0
- package/dist/datetime.d.ts.map +1 -0
- package/dist/datetime.mjs +127 -0
- package/dist/datetime.mjs.map +1 -0
- package/dist/enum.cjs +65 -0
- package/dist/enum.cjs.map +1 -0
- package/dist/enum.d.ts +43 -0
- package/dist/enum.d.ts.map +1 -0
- package/dist/enum.mjs +62 -0
- package/dist/enum.mjs.map +1 -0
- package/dist/env.cjs +54 -0
- package/dist/env.cjs.map +1 -0
- package/dist/env.d.ts +40 -0
- package/dist/env.d.ts.map +1 -0
- package/dist/env.mjs +51 -0
- package/dist/env.mjs.map +1 -0
- package/dist/general.cjs +8 -0
- package/dist/general.cjs.map +1 -0
- package/dist/general.d.ts +27 -0
- package/dist/general.d.ts.map +1 -0
- package/dist/general.mjs +6 -0
- package/dist/general.mjs.map +1 -0
- package/dist/hash.cjs +27 -0
- package/dist/hash.cjs.map +1 -0
- package/dist/hash.d.ts +17 -0
- package/dist/hash.d.ts.map +1 -0
- package/dist/hash.mjs +22 -0
- package/dist/hash.mjs.map +1 -0
- package/dist/math.cjs +37 -0
- package/dist/math.cjs.map +1 -0
- package/dist/math.d.ts +43 -0
- package/dist/math.d.ts.map +1 -0
- package/dist/math.mjs +35 -0
- package/dist/math.mjs.map +1 -0
- package/dist/number.cjs +31 -0
- package/dist/number.cjs.map +1 -0
- package/dist/number.d.ts +20 -0
- package/dist/number.d.ts.map +1 -0
- package/dist/number.mjs +29 -0
- package/dist/number.mjs.map +1 -0
- package/dist/pino.cjs +42 -0
- package/dist/pino.cjs.map +1 -0
- package/dist/pino.d.ts +24 -0
- package/dist/pino.d.ts.map +1 -0
- package/dist/pino.mjs +39 -0
- package/dist/pino.mjs.map +1 -0
- package/dist/random.cjs +30 -0
- package/dist/random.cjs.map +1 -0
- package/dist/random.d.ts +21 -0
- package/dist/random.d.ts.map +1 -0
- package/dist/random.mjs +28 -0
- package/dist/random.mjs.map +1 -0
- package/dist/string.cjs +44 -0
- package/dist/string.cjs.map +1 -0
- package/dist/string.d.ts +21 -0
- package/dist/string.d.ts.map +1 -0
- package/dist/string.mjs +42 -0
- package/dist/string.mjs.map +1 -0
- package/package.json +82 -0
- package/src/consola.ts +27 -0
- package/src/crypto-hash.ts +60 -0
- package/src/datetime.ts +154 -0
- package/src/enum.ts +60 -0
- package/src/env.ts +49 -0
- package/src/general.ts +29 -0
- package/src/hash.ts +25 -0
- package/src/math.ts +56 -0
- package/src/number.ts +29 -0
- package/src/pino.ts +37 -0
- package/src/random.ts +31 -0
- package/src/string.ts +49 -0
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { DateArg, Day, FormatOptions } from 'date-fns';
|
|
2
|
+
export type DateRangeType = 'lastMonth' | 'lastWeek' | 'thisMonth' | 'thisWeek' | 'today' | 'yesterday';
|
|
3
|
+
/**
|
|
4
|
+
* Formats a given date, timestamp, or date string into a specified format.
|
|
5
|
+
*
|
|
6
|
+
* This function is a wrapper around `date-fns/format`.
|
|
7
|
+
*
|
|
8
|
+
* @param {DateArg<Date>} date - The input date to format. Can be a Date object, a timestamp, or a string.
|
|
9
|
+
* @param {string} [format] - The target format string.
|
|
10
|
+
* @param {FormatOptions} [options] - Optional formatting options passed to `date-fns/format`.
|
|
11
|
+
* @returns {string} The formatted date string.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* import { formatDate } from '@kikiutils/shared/datetime';
|
|
16
|
+
*
|
|
17
|
+
* // Format a Date object
|
|
18
|
+
* console.log(formatDate(new Date(), 'yyyy-MM-dd')); // 2024-07-10
|
|
19
|
+
*
|
|
20
|
+
* // Format a timestamp
|
|
21
|
+
* console.log(formatDate(1657814400000, 'yyyy-MM-dd')); // 2022-07-15
|
|
22
|
+
*
|
|
23
|
+
* // Format a date string
|
|
24
|
+
* console.log(formatDate('2024-07-10T00:00:00Z', 'yyyy-MM-dd')); // 2024-07-10
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @see https://date-fns.org/docs/format
|
|
28
|
+
*/
|
|
29
|
+
export declare function formatDate(date: DateArg<Date> & {}, format?: string, options?: FormatOptions): string;
|
|
30
|
+
/**
|
|
31
|
+
* Get the date range (start and end) based on a given date and range type.
|
|
32
|
+
*
|
|
33
|
+
* Supports common range types like 'lastMonth', 'lastWeek', 'thisMonth', 'thisWeek', 'today', and 'yesterday'.
|
|
34
|
+
*
|
|
35
|
+
* @param {Date} date - The reference date.
|
|
36
|
+
* @param {DateRangeType} type - The range type to compute.
|
|
37
|
+
* @param {object} [options] - Optional settings.
|
|
38
|
+
* @param {boolean} [options.setEndDateToNextDayStart] - If true, set `endDate` to 00:00:00.000 of the next day.
|
|
39
|
+
* @param {Day} [options.weekStartsOn] - The start day of the week (0 = Sunday, 1 = Monday, ..., 6 = Saturday).
|
|
40
|
+
* @returns {{ startDate: Date, endDate: Date }} An object with `startDate` and `endDate`.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* import { getDateRangeFromDate } from '@kikiutils/shared/datetime';
|
|
45
|
+
*
|
|
46
|
+
* // Get the date range for last month
|
|
47
|
+
* const date = new Date('2023-07-01');
|
|
48
|
+
* console.log(getDateRangeFromDate(date, 'lastMonth'));
|
|
49
|
+
* // { startDate: 2023-06-01T00:00:00.000Z, endDate: 2023-06-30T23:59:59.999Z }
|
|
50
|
+
*
|
|
51
|
+
* // Get this week's range with Sunday as the first day
|
|
52
|
+
* console.log(getDateRangeFromDate(date, 'thisWeek', { weekStartsOn: 0 }));
|
|
53
|
+
* // { startDate: 2023-06-25T00:00:00.000Z, endDate: 2023-07-01T23:59:59.999Z }
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export declare function getDateRangeFromDate(date: Date, type: DateRangeType, options?: {
|
|
57
|
+
setEndDateToNextDayStart?: boolean;
|
|
58
|
+
weekStartsOn?: Day;
|
|
59
|
+
}): {
|
|
60
|
+
endDate: Date;
|
|
61
|
+
startDate: Date;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Returns a `Date` object set to midnight (00:00:00) of today, with an optional day offset.
|
|
65
|
+
*
|
|
66
|
+
* @param {number} [offsetDays] - Number of days to offset from today. Can be negative.
|
|
67
|
+
* @returns {Date} A `Date` object at 00:00:00 of the offset day.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```typescript
|
|
71
|
+
* import { getMidnightDateFromToday } from '@kikiutils/shared/datetime';
|
|
72
|
+
*
|
|
73
|
+
* console.log(getMidnightDateFromToday()); // today at 00:00:00
|
|
74
|
+
* console.log(getMidnightDateFromToday(3)); // 3 days from today at 00:00:00
|
|
75
|
+
* console.log(getMidnightDateFromToday(-1)); // yesterday at 00:00:00
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
export declare function getMidnightDateFromToday(offsetDays?: number): Date;
|
|
79
|
+
//# sourceMappingURL=datetime.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"datetime.d.ts","sourceRoot":"","sources":["../src/datetime.ts"],"names":[],"mappings":"AAYA,OAAO,KAAK,EACR,OAAO,EACP,GAAG,EACH,aAAa,EAChB,MAAM,UAAU,CAAC;AAElB,MAAM,MAAM,aAAa,GAAG,WAAW,GAAG,UAAU,GAAG,WAAW,GAAG,UAAU,GAAG,OAAO,GAAG,WAAW,CAAC;AAExG;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE,EAAE,MAAM,GAAE,MAA8B,EAAE,OAAO,CAAC,EAAE,aAAa,UAEnH;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,oBAAoB,CAChC,IAAI,EAAE,IAAI,EACV,IAAI,EAAE,aAAa,EACnB,OAAO,CAAC,EAAE;IACN,wBAAwB,CAAC,EAAE,OAAO,CAAC;IACnC,YAAY,CAAC,EAAE,GAAG,CAAC;CACtB;;;EAiDJ;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,wBAAwB,CAAC,UAAU,GAAE,MAAU,QAK9D"}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { format, subDays, endOfDay, startOfDay, endOfWeek, startOfWeek, endOfMonth, startOfMonth, subWeeks, subMonths } from 'date-fns';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Formats a given date, timestamp, or date string into a specified format.
|
|
5
|
+
*
|
|
6
|
+
* This function is a wrapper around `date-fns/format`.
|
|
7
|
+
*
|
|
8
|
+
* @param {DateArg<Date>} date - The input date to format. Can be a Date object, a timestamp, or a string.
|
|
9
|
+
* @param {string} [format] - The target format string.
|
|
10
|
+
* @param {FormatOptions} [options] - Optional formatting options passed to `date-fns/format`.
|
|
11
|
+
* @returns {string} The formatted date string.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* import { formatDate } from '@kikiutils/shared/datetime';
|
|
16
|
+
*
|
|
17
|
+
* // Format a Date object
|
|
18
|
+
* console.log(formatDate(new Date(), 'yyyy-MM-dd')); // 2024-07-10
|
|
19
|
+
*
|
|
20
|
+
* // Format a timestamp
|
|
21
|
+
* console.log(formatDate(1657814400000, 'yyyy-MM-dd')); // 2022-07-15
|
|
22
|
+
*
|
|
23
|
+
* // Format a date string
|
|
24
|
+
* console.log(formatDate('2024-07-10T00:00:00Z', 'yyyy-MM-dd')); // 2024-07-10
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @see https://date-fns.org/docs/format
|
|
28
|
+
*/
|
|
29
|
+
function formatDate(date, format$1 = 'yyyy-MM-dd HH:mm:ss', options) {
|
|
30
|
+
return format(date, format$1, options);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Get the date range (start and end) based on a given date and range type.
|
|
34
|
+
*
|
|
35
|
+
* Supports common range types like 'lastMonth', 'lastWeek', 'thisMonth', 'thisWeek', 'today', and 'yesterday'.
|
|
36
|
+
*
|
|
37
|
+
* @param {Date} date - The reference date.
|
|
38
|
+
* @param {DateRangeType} type - The range type to compute.
|
|
39
|
+
* @param {object} [options] - Optional settings.
|
|
40
|
+
* @param {boolean} [options.setEndDateToNextDayStart] - If true, set `endDate` to 00:00:00.000 of the next day.
|
|
41
|
+
* @param {Day} [options.weekStartsOn] - The start day of the week (0 = Sunday, 1 = Monday, ..., 6 = Saturday).
|
|
42
|
+
* @returns {{ startDate: Date, endDate: Date }} An object with `startDate` and `endDate`.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* import { getDateRangeFromDate } from '@kikiutils/shared/datetime';
|
|
47
|
+
*
|
|
48
|
+
* // Get the date range for last month
|
|
49
|
+
* const date = new Date('2023-07-01');
|
|
50
|
+
* console.log(getDateRangeFromDate(date, 'lastMonth'));
|
|
51
|
+
* // { startDate: 2023-06-01T00:00:00.000Z, endDate: 2023-06-30T23:59:59.999Z }
|
|
52
|
+
*
|
|
53
|
+
* // Get this week's range with Sunday as the first day
|
|
54
|
+
* console.log(getDateRangeFromDate(date, 'thisWeek', { weekStartsOn: 0 }));
|
|
55
|
+
* // { startDate: 2023-06-25T00:00:00.000Z, endDate: 2023-07-01T23:59:59.999Z }
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
function getDateRangeFromDate(date, type, options) {
|
|
59
|
+
let endDate;
|
|
60
|
+
let startDate;
|
|
61
|
+
switch (type) {
|
|
62
|
+
case 'lastMonth':
|
|
63
|
+
{
|
|
64
|
+
const lastMonth = subMonths(date, 1);
|
|
65
|
+
endDate = endOfMonth(lastMonth);
|
|
66
|
+
startDate = startOfMonth(lastMonth);
|
|
67
|
+
}
|
|
68
|
+
break;
|
|
69
|
+
case 'lastWeek':
|
|
70
|
+
{
|
|
71
|
+
const lastWeek = subWeeks(date, 1);
|
|
72
|
+
endDate = endOfWeek(lastWeek, { weekStartsOn: options?.weekStartsOn ?? 1 });
|
|
73
|
+
startDate = startOfWeek(lastWeek, { weekStartsOn: options?.weekStartsOn ?? 1 });
|
|
74
|
+
}
|
|
75
|
+
break;
|
|
76
|
+
case 'thisMonth':
|
|
77
|
+
endDate = endOfMonth(date);
|
|
78
|
+
startDate = startOfMonth(date);
|
|
79
|
+
break;
|
|
80
|
+
case 'thisWeek':
|
|
81
|
+
endDate = endOfWeek(date, { weekStartsOn: options?.weekStartsOn ?? 1 });
|
|
82
|
+
startDate = startOfWeek(date, { weekStartsOn: options?.weekStartsOn ?? 1 });
|
|
83
|
+
break;
|
|
84
|
+
case 'today':
|
|
85
|
+
endDate = endOfDay(date);
|
|
86
|
+
startDate = startOfDay(date);
|
|
87
|
+
break;
|
|
88
|
+
case 'yesterday':
|
|
89
|
+
{
|
|
90
|
+
const yesterday = subDays(date, 1);
|
|
91
|
+
endDate = endOfDay(yesterday);
|
|
92
|
+
startDate = startOfDay(yesterday);
|
|
93
|
+
}
|
|
94
|
+
break;
|
|
95
|
+
default: throw new Error(`Unsupported date range type: ${type}.`);
|
|
96
|
+
}
|
|
97
|
+
if (options?.setEndDateToNextDayStart)
|
|
98
|
+
endDate.setHours(24, 0, 0, 0);
|
|
99
|
+
return {
|
|
100
|
+
endDate,
|
|
101
|
+
startDate,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Returns a `Date` object set to midnight (00:00:00) of today, with an optional day offset.
|
|
106
|
+
*
|
|
107
|
+
* @param {number} [offsetDays] - Number of days to offset from today. Can be negative.
|
|
108
|
+
* @returns {Date} A `Date` object at 00:00:00 of the offset day.
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* ```typescript
|
|
112
|
+
* import { getMidnightDateFromToday } from '@kikiutils/shared/datetime';
|
|
113
|
+
*
|
|
114
|
+
* console.log(getMidnightDateFromToday()); // today at 00:00:00
|
|
115
|
+
* console.log(getMidnightDateFromToday(3)); // 3 days from today at 00:00:00
|
|
116
|
+
* console.log(getMidnightDateFromToday(-1)); // yesterday at 00:00:00
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
119
|
+
function getMidnightDateFromToday(offsetDays = 0) {
|
|
120
|
+
const date = new Date();
|
|
121
|
+
date.setDate(date.getDate() + offsetDays);
|
|
122
|
+
date.setHours(0, 0, 0, 0);
|
|
123
|
+
return date;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export { formatDate, getDateRangeFromDate, getMidnightDateFromToday };
|
|
127
|
+
//# sourceMappingURL=datetime.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"datetime.mjs","sources":["../src/datetime.ts"],"sourcesContent":["import {\n format as dateFnsFormat,\n endOfDay,\n endOfMonth,\n endOfWeek,\n startOfDay,\n startOfMonth,\n startOfWeek,\n subDays,\n subMonths,\n subWeeks,\n} from 'date-fns';\nimport type {\n DateArg,\n Day,\n FormatOptions,\n} from 'date-fns';\n\nexport type DateRangeType = 'lastMonth' | 'lastWeek' | 'thisMonth' | 'thisWeek' | 'today' | 'yesterday';\n\n/**\n * Formats a given date, timestamp, or date string into a specified format.\n *\n * This function is a wrapper around `date-fns/format`.\n *\n * @param {DateArg<Date>} date - The input date to format. Can be a Date object, a timestamp, or a string.\n * @param {string} [format] - The target format string.\n * @param {FormatOptions} [options] - Optional formatting options passed to `date-fns/format`.\n * @returns {string} The formatted date string.\n *\n * @example\n * ```typescript\n * import { formatDate } from '@kikiutils/shared/datetime';\n *\n * // Format a Date object\n * console.log(formatDate(new Date(), 'yyyy-MM-dd')); // 2024-07-10\n *\n * // Format a timestamp\n * console.log(formatDate(1657814400000, 'yyyy-MM-dd')); // 2022-07-15\n *\n * // Format a date string\n * console.log(formatDate('2024-07-10T00:00:00Z', 'yyyy-MM-dd')); // 2024-07-10\n * ```\n *\n * @see https://date-fns.org/docs/format\n */\nexport function formatDate(date: DateArg<Date> & {}, format: string = 'yyyy-MM-dd HH:mm:ss', options?: FormatOptions) {\n return dateFnsFormat(date, format, options);\n}\n\n/**\n * Get the date range (start and end) based on a given date and range type.\n *\n * Supports common range types like 'lastMonth', 'lastWeek', 'thisMonth', 'thisWeek', 'today', and 'yesterday'.\n *\n * @param {Date} date - The reference date.\n * @param {DateRangeType} type - The range type to compute.\n * @param {object} [options] - Optional settings.\n * @param {boolean} [options.setEndDateToNextDayStart] - If true, set `endDate` to 00:00:00.000 of the next day.\n * @param {Day} [options.weekStartsOn] - The start day of the week (0 = Sunday, 1 = Monday, ..., 6 = Saturday).\n * @returns {{ startDate: Date, endDate: Date }} An object with `startDate` and `endDate`.\n *\n * @example\n * ```typescript\n * import { getDateRangeFromDate } from '@kikiutils/shared/datetime';\n *\n * // Get the date range for last month\n * const date = new Date('2023-07-01');\n * console.log(getDateRangeFromDate(date, 'lastMonth'));\n * // { startDate: 2023-06-01T00:00:00.000Z, endDate: 2023-06-30T23:59:59.999Z }\n *\n * // Get this week's range with Sunday as the first day\n * console.log(getDateRangeFromDate(date, 'thisWeek', { weekStartsOn: 0 }));\n * // { startDate: 2023-06-25T00:00:00.000Z, endDate: 2023-07-01T23:59:59.999Z }\n * ```\n */\nexport function getDateRangeFromDate(\n date: Date,\n type: DateRangeType,\n options?: {\n setEndDateToNextDayStart?: boolean;\n weekStartsOn?: Day;\n },\n) {\n let endDate: Date;\n let startDate: Date;\n switch (type) {\n case 'lastMonth':\n {\n const lastMonth = subMonths(date, 1);\n endDate = endOfMonth(lastMonth);\n startDate = startOfMonth(lastMonth);\n }\n\n break;\n case 'lastWeek':\n {\n const lastWeek = subWeeks(date, 1);\n endDate = endOfWeek(lastWeek, { weekStartsOn: options?.weekStartsOn ?? 1 });\n startDate = startOfWeek(lastWeek, { weekStartsOn: options?.weekStartsOn ?? 1 });\n }\n\n break;\n case 'thisMonth':\n endDate = endOfMonth(date);\n startDate = startOfMonth(date);\n break;\n case 'thisWeek':\n endDate = endOfWeek(date, { weekStartsOn: options?.weekStartsOn ?? 1 });\n startDate = startOfWeek(date, { weekStartsOn: options?.weekStartsOn ?? 1 });\n break;\n case 'today':\n endDate = endOfDay(date);\n startDate = startOfDay(date);\n break;\n case 'yesterday':\n {\n const yesterday = subDays(date, 1);\n endDate = endOfDay(yesterday);\n startDate = startOfDay(yesterday);\n }\n\n break;\n default: throw new Error(`Unsupported date range type: ${type}.`);\n }\n\n if (options?.setEndDateToNextDayStart) endDate.setHours(24, 0, 0, 0);\n return {\n endDate,\n startDate,\n };\n}\n\n/**\n * Returns a `Date` object set to midnight (00:00:00) of today, with an optional day offset.\n *\n * @param {number} [offsetDays] - Number of days to offset from today. Can be negative.\n * @returns {Date} A `Date` object at 00:00:00 of the offset day.\n *\n * @example\n * ```typescript\n * import { getMidnightDateFromToday } from '@kikiutils/shared/datetime';\n *\n * console.log(getMidnightDateFromToday()); // today at 00:00:00\n * console.log(getMidnightDateFromToday(3)); // 3 days from today at 00:00:00\n * console.log(getMidnightDateFromToday(-1)); // yesterday at 00:00:00\n * ```\n */\nexport function getMidnightDateFromToday(offsetDays: number = 0) {\n const date = new Date();\n date.setDate(date.getDate() + offsetDays);\n date.setHours(0, 0, 0, 0);\n return date;\n}\n"],"names":["format","dateFnsFormat"],"mappings":";;AAoBA;;;;;;;;;;;;;;;;;;;;;;;;;AAyBG;AACG,SAAU,UAAU,CAAC,IAAwB,EAAEA,QAAiB,GAAA,qBAAqB,EAAE,OAAuB,EAAA;IAChH,OAAOC,MAAa,CAAC,IAAI,EAAED,QAAM,EAAE,OAAO,CAAC;AAC/C;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;AAyBG;SACa,oBAAoB,CAChC,IAAU,EACV,IAAmB,EACnB,OAGC,EAAA;AAED,IAAA,IAAI,OAAa;AACjB,IAAA,IAAI,SAAe;IACnB,QAAQ,IAAI;AACR,QAAA,KAAK,WAAW;YACZ;gBACI,MAAM,SAAS,GAAG,SAAS,CAAC,IAAI,EAAE,CAAC,CAAC;AACpC,gBAAA,OAAO,GAAG,UAAU,CAAC,SAAS,CAAC;AAC/B,gBAAA,SAAS,GAAG,YAAY,CAAC,SAAS,CAAC;;YAGvC;AACJ,QAAA,KAAK,UAAU;YACX;gBACI,MAAM,QAAQ,GAAG,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC;AAClC,gBAAA,OAAO,GAAG,SAAS,CAAC,QAAQ,EAAE,EAAE,YAAY,EAAE,OAAO,EAAE,YAAY,IAAI,CAAC,EAAE,CAAC;AAC3E,gBAAA,SAAS,GAAG,WAAW,CAAC,QAAQ,EAAE,EAAE,YAAY,EAAE,OAAO,EAAE,YAAY,IAAI,CAAC,EAAE,CAAC;;YAGnF;AACJ,QAAA,KAAK,WAAW;AACZ,YAAA,OAAO,GAAG,UAAU,CAAC,IAAI,CAAC;AAC1B,YAAA,SAAS,GAAG,YAAY,CAAC,IAAI,CAAC;YAC9B;AACJ,QAAA,KAAK,UAAU;AACX,YAAA,OAAO,GAAG,SAAS,CAAC,IAAI,EAAE,EAAE,YAAY,EAAE,OAAO,EAAE,YAAY,IAAI,CAAC,EAAE,CAAC;AACvE,YAAA,SAAS,GAAG,WAAW,CAAC,IAAI,EAAE,EAAE,YAAY,EAAE,OAAO,EAAE,YAAY,IAAI,CAAC,EAAE,CAAC;YAC3E;AACJ,QAAA,KAAK,OAAO;AACR,YAAA,OAAO,GAAG,QAAQ,CAAC,IAAI,CAAC;AACxB,YAAA,SAAS,GAAG,UAAU,CAAC,IAAI,CAAC;YAC5B;AACJ,QAAA,KAAK,WAAW;YACZ;gBACI,MAAM,SAAS,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC;AAClC,gBAAA,OAAO,GAAG,QAAQ,CAAC,SAAS,CAAC;AAC7B,gBAAA,SAAS,GAAG,UAAU,CAAC,SAAS,CAAC;;YAGrC;QACJ,SAAS,MAAM,IAAI,KAAK,CAAC,CAAgC,6BAAA,EAAA,IAAI,CAAG,CAAA,CAAA,CAAC;;IAGrE,IAAI,OAAO,EAAE,wBAAwB;QAAE,OAAO,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;IACpE,OAAO;QACH,OAAO;QACP,SAAS;KACZ;AACL;AAEA;;;;;;;;;;;;;;AAcG;AACa,SAAA,wBAAwB,CAAC,UAAA,GAAqB,CAAC,EAAA;AAC3D,IAAA,MAAM,IAAI,GAAG,IAAI,IAAI,EAAE;IACvB,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,UAAU,CAAC;IACzC,IAAI,CAAC,QAAQ,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;AACzB,IAAA,OAAO,IAAI;AACf;;;;"}
|
package/dist/enum.cjs
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Extracts the numeric values from an enumeration-like object.
|
|
5
|
+
*
|
|
6
|
+
* @param {Record<number | string, number | string>} data - The enumeration-like object to extract numeric values from.
|
|
7
|
+
* The keys can be numbers or strings, and the values can be numbers or strings.
|
|
8
|
+
* @returns {number[]} An array of numeric values extracted from the object.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import { getEnumNumberValues } from '@kikiutils/shared/enum';
|
|
13
|
+
*
|
|
14
|
+
* enum RecordType {
|
|
15
|
+
* Receive = 0,
|
|
16
|
+
* Send = 1,
|
|
17
|
+
* Unknown = 'unknown'
|
|
18
|
+
* }
|
|
19
|
+
*
|
|
20
|
+
* console.log(getEnumNumberValues(RecordType)); // [0, 1]
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
function getEnumNumberValues(data) {
|
|
24
|
+
return Object.values(data).filter((value) => typeof value === 'number');
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Extracts the string values from an enumeration-like object.
|
|
28
|
+
*
|
|
29
|
+
* @param {Record<number | string, number | string>} data - The enumeration-like object to extract string values from.
|
|
30
|
+
* The keys can be numbers or strings, and the values can be numbers or strings.
|
|
31
|
+
* @returns {string[]} An array of string values extracted from the object.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* import { getEnumStringValues } from '@kikiutils/shared/enum';
|
|
36
|
+
*
|
|
37
|
+
* enum RecordType {
|
|
38
|
+
* Receive = 0,
|
|
39
|
+
* Send = 1,
|
|
40
|
+
* Unknown = 'unknown'
|
|
41
|
+
* }
|
|
42
|
+
*
|
|
43
|
+
* console.log(getEnumStringValues(RecordType)); // ['unknown']
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
function getEnumStringValues(data) {
|
|
47
|
+
const keys = [];
|
|
48
|
+
const keysCount = {};
|
|
49
|
+
const values = [];
|
|
50
|
+
Object.entries(data).forEach(([key, value]) => {
|
|
51
|
+
keys.push(key);
|
|
52
|
+
values.push(value);
|
|
53
|
+
if (typeof value !== 'string')
|
|
54
|
+
return;
|
|
55
|
+
keysCount[key] = (keysCount[key] ?? 0) + 1;
|
|
56
|
+
keysCount[value] = (keysCount[value] ?? 0) + 1;
|
|
57
|
+
});
|
|
58
|
+
return values.filter((value) => {
|
|
59
|
+
return typeof value === 'string' && (!keys.includes(value) || (keysCount[value] && keysCount[value] > 1));
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
exports.getEnumNumberValues = getEnumNumberValues;
|
|
64
|
+
exports.getEnumStringValues = getEnumStringValues;
|
|
65
|
+
//# sourceMappingURL=enum.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"enum.cjs","sources":["../src/enum.ts"],"sourcesContent":["/**\n * Extracts the numeric values from an enumeration-like object.\n *\n * @param {Record<number | string, number | string>} data - The enumeration-like object to extract numeric values from.\n * The keys can be numbers or strings, and the values can be numbers or strings.\n * @returns {number[]} An array of numeric values extracted from the object.\n *\n * @example\n * ```typescript\n * import { getEnumNumberValues } from '@kikiutils/shared/enum';\n *\n * enum RecordType {\n * Receive = 0,\n * Send = 1,\n * Unknown = 'unknown'\n * }\n *\n * console.log(getEnumNumberValues(RecordType)); // [0, 1]\n * ```\n */\nexport function getEnumNumberValues(data: Record<number | string, number | string>) {\n return Object.values(data).filter((value) => typeof value === 'number');\n}\n\n/**\n * Extracts the string values from an enumeration-like object.\n *\n * @param {Record<number | string, number | string>} data - The enumeration-like object to extract string values from.\n * The keys can be numbers or strings, and the values can be numbers or strings.\n * @returns {string[]} An array of string values extracted from the object.\n *\n * @example\n * ```typescript\n * import { getEnumStringValues } from '@kikiutils/shared/enum';\n *\n * enum RecordType {\n * Receive = 0,\n * Send = 1,\n * Unknown = 'unknown'\n * }\n *\n * console.log(getEnumStringValues(RecordType)); // ['unknown']\n * ```\n */\nexport function getEnumStringValues(data: Record<number | string, number | string>) {\n const keys: string[] = [];\n const keysCount: Record<string, number> = {};\n const values: any[] = [];\n Object.entries(data).forEach(([key, value]) => {\n keys.push(key);\n values.push(value);\n if (typeof value !== 'string') return;\n keysCount[key] = (keysCount[key] ?? 0) + 1;\n keysCount[value] = (keysCount[value] ?? 0) + 1;\n });\n\n return values.filter((value) => {\n return typeof value === 'string' && (!keys.includes(value) || (keysCount[value] && keysCount[value] > 1));\n });\n}\n"],"names":[],"mappings":";;AAAA;;;;;;;;;;;;;;;;;;;AAmBG;AACG,SAAU,mBAAmB,CAAC,IAA8C,EAAA;AAC9E,IAAA,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,KAAK,OAAO,KAAK,KAAK,QAAQ,CAAC;AAC3E;AAEA;;;;;;;;;;;;;;;;;;;AAmBG;AACG,SAAU,mBAAmB,CAAC,IAA8C,EAAA;IAC9E,MAAM,IAAI,GAAa,EAAE;IACzB,MAAM,SAAS,GAA2B,EAAE;IAC5C,MAAM,MAAM,GAAU,EAAE;AACxB,IAAA,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,KAAI;AAC1C,QAAA,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC;AACd,QAAA,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC;QAClB,IAAI,OAAO,KAAK,KAAK,QAAQ;YAAE;AAC/B,QAAA,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC;AAC1C,QAAA,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC;AAClD,KAAC,CAAC;AAEF,IAAA,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,KAAI;AAC3B,QAAA,OAAO,OAAO,KAAK,KAAK,QAAQ,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,KAAK,SAAS,CAAC,KAAK,CAAC,IAAI,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC;AAC7G,KAAC,CAAC;AACN;;;;;"}
|
package/dist/enum.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extracts the numeric values from an enumeration-like object.
|
|
3
|
+
*
|
|
4
|
+
* @param {Record<number | string, number | string>} data - The enumeration-like object to extract numeric values from.
|
|
5
|
+
* The keys can be numbers or strings, and the values can be numbers or strings.
|
|
6
|
+
* @returns {number[]} An array of numeric values extracted from the object.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { getEnumNumberValues } from '@kikiutils/shared/enum';
|
|
11
|
+
*
|
|
12
|
+
* enum RecordType {
|
|
13
|
+
* Receive = 0,
|
|
14
|
+
* Send = 1,
|
|
15
|
+
* Unknown = 'unknown'
|
|
16
|
+
* }
|
|
17
|
+
*
|
|
18
|
+
* console.log(getEnumNumberValues(RecordType)); // [0, 1]
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export declare function getEnumNumberValues(data: Record<number | string, number | string>): number[];
|
|
22
|
+
/**
|
|
23
|
+
* Extracts the string values from an enumeration-like object.
|
|
24
|
+
*
|
|
25
|
+
* @param {Record<number | string, number | string>} data - The enumeration-like object to extract string values from.
|
|
26
|
+
* The keys can be numbers or strings, and the values can be numbers or strings.
|
|
27
|
+
* @returns {string[]} An array of string values extracted from the object.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* import { getEnumStringValues } from '@kikiutils/shared/enum';
|
|
32
|
+
*
|
|
33
|
+
* enum RecordType {
|
|
34
|
+
* Receive = 0,
|
|
35
|
+
* Send = 1,
|
|
36
|
+
* Unknown = 'unknown'
|
|
37
|
+
* }
|
|
38
|
+
*
|
|
39
|
+
* console.log(getEnumStringValues(RecordType)); // ['unknown']
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export declare function getEnumStringValues(data: Record<number | string, number | string>): any[];
|
|
43
|
+
//# sourceMappingURL=enum.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"enum.d.ts","sourceRoot":"","sources":["../src/enum.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,YAEjF;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,SAejF"}
|
package/dist/enum.mjs
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extracts the numeric values from an enumeration-like object.
|
|
3
|
+
*
|
|
4
|
+
* @param {Record<number | string, number | string>} data - The enumeration-like object to extract numeric values from.
|
|
5
|
+
* The keys can be numbers or strings, and the values can be numbers or strings.
|
|
6
|
+
* @returns {number[]} An array of numeric values extracted from the object.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { getEnumNumberValues } from '@kikiutils/shared/enum';
|
|
11
|
+
*
|
|
12
|
+
* enum RecordType {
|
|
13
|
+
* Receive = 0,
|
|
14
|
+
* Send = 1,
|
|
15
|
+
* Unknown = 'unknown'
|
|
16
|
+
* }
|
|
17
|
+
*
|
|
18
|
+
* console.log(getEnumNumberValues(RecordType)); // [0, 1]
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
function getEnumNumberValues(data) {
|
|
22
|
+
return Object.values(data).filter((value) => typeof value === 'number');
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Extracts the string values from an enumeration-like object.
|
|
26
|
+
*
|
|
27
|
+
* @param {Record<number | string, number | string>} data - The enumeration-like object to extract string values from.
|
|
28
|
+
* The keys can be numbers or strings, and the values can be numbers or strings.
|
|
29
|
+
* @returns {string[]} An array of string values extracted from the object.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* import { getEnumStringValues } from '@kikiutils/shared/enum';
|
|
34
|
+
*
|
|
35
|
+
* enum RecordType {
|
|
36
|
+
* Receive = 0,
|
|
37
|
+
* Send = 1,
|
|
38
|
+
* Unknown = 'unknown'
|
|
39
|
+
* }
|
|
40
|
+
*
|
|
41
|
+
* console.log(getEnumStringValues(RecordType)); // ['unknown']
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
function getEnumStringValues(data) {
|
|
45
|
+
const keys = [];
|
|
46
|
+
const keysCount = {};
|
|
47
|
+
const values = [];
|
|
48
|
+
Object.entries(data).forEach(([key, value]) => {
|
|
49
|
+
keys.push(key);
|
|
50
|
+
values.push(value);
|
|
51
|
+
if (typeof value !== 'string')
|
|
52
|
+
return;
|
|
53
|
+
keysCount[key] = (keysCount[key] ?? 0) + 1;
|
|
54
|
+
keysCount[value] = (keysCount[value] ?? 0) + 1;
|
|
55
|
+
});
|
|
56
|
+
return values.filter((value) => {
|
|
57
|
+
return typeof value === 'string' && (!keys.includes(value) || (keysCount[value] && keysCount[value] > 1));
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export { getEnumNumberValues, getEnumStringValues };
|
|
62
|
+
//# sourceMappingURL=enum.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"enum.mjs","sources":["../src/enum.ts"],"sourcesContent":["/**\n * Extracts the numeric values from an enumeration-like object.\n *\n * @param {Record<number | string, number | string>} data - The enumeration-like object to extract numeric values from.\n * The keys can be numbers or strings, and the values can be numbers or strings.\n * @returns {number[]} An array of numeric values extracted from the object.\n *\n * @example\n * ```typescript\n * import { getEnumNumberValues } from '@kikiutils/shared/enum';\n *\n * enum RecordType {\n * Receive = 0,\n * Send = 1,\n * Unknown = 'unknown'\n * }\n *\n * console.log(getEnumNumberValues(RecordType)); // [0, 1]\n * ```\n */\nexport function getEnumNumberValues(data: Record<number | string, number | string>) {\n return Object.values(data).filter((value) => typeof value === 'number');\n}\n\n/**\n * Extracts the string values from an enumeration-like object.\n *\n * @param {Record<number | string, number | string>} data - The enumeration-like object to extract string values from.\n * The keys can be numbers or strings, and the values can be numbers or strings.\n * @returns {string[]} An array of string values extracted from the object.\n *\n * @example\n * ```typescript\n * import { getEnumStringValues } from '@kikiutils/shared/enum';\n *\n * enum RecordType {\n * Receive = 0,\n * Send = 1,\n * Unknown = 'unknown'\n * }\n *\n * console.log(getEnumStringValues(RecordType)); // ['unknown']\n * ```\n */\nexport function getEnumStringValues(data: Record<number | string, number | string>) {\n const keys: string[] = [];\n const keysCount: Record<string, number> = {};\n const values: any[] = [];\n Object.entries(data).forEach(([key, value]) => {\n keys.push(key);\n values.push(value);\n if (typeof value !== 'string') return;\n keysCount[key] = (keysCount[key] ?? 0) + 1;\n keysCount[value] = (keysCount[value] ?? 0) + 1;\n });\n\n return values.filter((value) => {\n return typeof value === 'string' && (!keys.includes(value) || (keysCount[value] && keysCount[value] > 1));\n });\n}\n"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;AAmBG;AACG,SAAU,mBAAmB,CAAC,IAA8C,EAAA;AAC9E,IAAA,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,KAAK,OAAO,KAAK,KAAK,QAAQ,CAAC;AAC3E;AAEA;;;;;;;;;;;;;;;;;;;AAmBG;AACG,SAAU,mBAAmB,CAAC,IAA8C,EAAA;IAC9E,MAAM,IAAI,GAAa,EAAE;IACzB,MAAM,SAAS,GAA2B,EAAE;IAC5C,MAAM,MAAM,GAAU,EAAE;AACxB,IAAA,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,KAAI;AAC1C,QAAA,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC;AACd,QAAA,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC;QAClB,IAAI,OAAO,KAAK,KAAK,QAAQ;YAAE;AAC/B,QAAA,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC;AAC1C,QAAA,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC;AAClD,KAAC,CAAC;AAEF,IAAA,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,KAAI;AAC3B,QAAA,OAAO,OAAO,KAAK,KAAK,QAAQ,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,KAAK,SAAS,CAAC,KAAK,CAAC,IAAI,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC;AAC7G,KAAC,CAAC;AACN;;;;"}
|
package/dist/env.cjs
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Custom error class for handling missing environment variables.
|
|
5
|
+
*
|
|
6
|
+
* Extends the built-in `Error` class and includes the missing key.
|
|
7
|
+
*
|
|
8
|
+
* @extends {Error}
|
|
9
|
+
*/
|
|
10
|
+
class EnvironmentNotFoundError extends Error {
|
|
11
|
+
key;
|
|
12
|
+
/**
|
|
13
|
+
* Creates a new EnvironmentNotFoundError.
|
|
14
|
+
*
|
|
15
|
+
* @param {string} key - The missing environment variable key.
|
|
16
|
+
*/
|
|
17
|
+
constructor(key) {
|
|
18
|
+
super(`Missing environment variable: ${key}`);
|
|
19
|
+
this.key = key;
|
|
20
|
+
this.name = this.constructor.name;
|
|
21
|
+
Error.captureStackTrace?.(this, this.constructor);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Retrieves the value of an environment variable, or throws an error if not set.
|
|
26
|
+
*
|
|
27
|
+
* @param {string} key - The environment variable key to check.
|
|
28
|
+
* @returns {string} The value of the environment variable.
|
|
29
|
+
* @throws {EnvironmentNotFoundError} If the environment variable is not defined.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* import { checkAndGetEnvValue } from '@kikiutils/shared/env';
|
|
34
|
+
*
|
|
35
|
+
* // When the environment variable 'API_KEY' is set:
|
|
36
|
+
* console.log(checkAndGetEnvValue('API_KEY')); // value of API_KEY
|
|
37
|
+
*
|
|
38
|
+
* // When the environment variable 'API_KEY' is not set:
|
|
39
|
+
* try {
|
|
40
|
+
* const apiKey = checkAndGetEnvValue('API_KEY');
|
|
41
|
+
* } catch (error) {
|
|
42
|
+
* console.error(error); // Missing environment variable: API_KEY
|
|
43
|
+
* }
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
function checkAndGetEnvValue(key) {
|
|
47
|
+
if (!process.env[key])
|
|
48
|
+
throw new EnvironmentNotFoundError(key);
|
|
49
|
+
return process.env[key];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
exports.EnvironmentNotFoundError = EnvironmentNotFoundError;
|
|
53
|
+
exports.checkAndGetEnvValue = checkAndGetEnvValue;
|
|
54
|
+
//# sourceMappingURL=env.cjs.map
|
package/dist/env.cjs.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"env.cjs","sources":["../src/env.ts"],"sourcesContent":["/**\n * Custom error class for handling missing environment variables.\n *\n * Extends the built-in `Error` class and includes the missing key.\n *\n * @extends {Error}\n */\nexport class EnvironmentNotFoundError extends Error {\n readonly key: string;\n\n /**\n * Creates a new EnvironmentNotFoundError.\n *\n * @param {string} key - The missing environment variable key.\n */\n constructor(key: string) {\n super(`Missing environment variable: ${key}`);\n this.key = key;\n this.name = this.constructor.name;\n Error.captureStackTrace?.(this, this.constructor);\n }\n}\n\n/**\n * Retrieves the value of an environment variable, or throws an error if not set.\n *\n * @param {string} key - The environment variable key to check.\n * @returns {string} The value of the environment variable.\n * @throws {EnvironmentNotFoundError} If the environment variable is not defined.\n *\n * @example\n * ```typescript\n * import { checkAndGetEnvValue } from '@kikiutils/shared/env';\n *\n * // When the environment variable 'API_KEY' is set:\n * console.log(checkAndGetEnvValue('API_KEY')); // value of API_KEY\n *\n * // When the environment variable 'API_KEY' is not set:\n * try {\n * const apiKey = checkAndGetEnvValue('API_KEY');\n * } catch (error) {\n * console.error(error); // Missing environment variable: API_KEY\n * }\n * ```\n */\nexport function checkAndGetEnvValue(key: string): string {\n if (!process.env[key]) throw new EnvironmentNotFoundError(key);\n return process.env[key];\n}\n"],"names":[],"mappings":";;AAAA;;;;;;AAMG;AACG,MAAO,wBAAyB,SAAQ,KAAK,CAAA;AACtC,IAAA,GAAG;AAEZ;;;;AAIG;AACH,IAAA,WAAA,CAAY,GAAW,EAAA;AACnB,QAAA,KAAK,CAAC,CAAA,8BAAA,EAAiC,GAAG,CAAA,CAAE,CAAC;AAC7C,QAAA,IAAI,CAAC,GAAG,GAAG,GAAG;QACd,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI;QACjC,KAAK,CAAC,iBAAiB,GAAG,IAAI,EAAE,IAAI,CAAC,WAAW,CAAC;;AAExD;AAED;;;;;;;;;;;;;;;;;;;;;AAqBG;AACG,SAAU,mBAAmB,CAAC,GAAW,EAAA;AAC3C,IAAA,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;AAAE,QAAA,MAAM,IAAI,wBAAwB,CAAC,GAAG,CAAC;AAC9D,IAAA,OAAO,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;AAC3B;;;;;"}
|
package/dist/env.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom error class for handling missing environment variables.
|
|
3
|
+
*
|
|
4
|
+
* Extends the built-in `Error` class and includes the missing key.
|
|
5
|
+
*
|
|
6
|
+
* @extends {Error}
|
|
7
|
+
*/
|
|
8
|
+
export declare class EnvironmentNotFoundError extends Error {
|
|
9
|
+
readonly key: string;
|
|
10
|
+
/**
|
|
11
|
+
* Creates a new EnvironmentNotFoundError.
|
|
12
|
+
*
|
|
13
|
+
* @param {string} key - The missing environment variable key.
|
|
14
|
+
*/
|
|
15
|
+
constructor(key: string);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Retrieves the value of an environment variable, or throws an error if not set.
|
|
19
|
+
*
|
|
20
|
+
* @param {string} key - The environment variable key to check.
|
|
21
|
+
* @returns {string} The value of the environment variable.
|
|
22
|
+
* @throws {EnvironmentNotFoundError} If the environment variable is not defined.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* import { checkAndGetEnvValue } from '@kikiutils/shared/env';
|
|
27
|
+
*
|
|
28
|
+
* // When the environment variable 'API_KEY' is set:
|
|
29
|
+
* console.log(checkAndGetEnvValue('API_KEY')); // value of API_KEY
|
|
30
|
+
*
|
|
31
|
+
* // When the environment variable 'API_KEY' is not set:
|
|
32
|
+
* try {
|
|
33
|
+
* const apiKey = checkAndGetEnvValue('API_KEY');
|
|
34
|
+
* } catch (error) {
|
|
35
|
+
* console.error(error); // Missing environment variable: API_KEY
|
|
36
|
+
* }
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export declare function checkAndGetEnvValue(key: string): string;
|
|
40
|
+
//# sourceMappingURL=env.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"env.d.ts","sourceRoot":"","sources":["../src/env.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,qBAAa,wBAAyB,SAAQ,KAAK;IAC/C,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IAErB;;;;OAIG;gBACS,GAAG,EAAE,MAAM;CAM1B;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAGvD"}
|
package/dist/env.mjs
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom error class for handling missing environment variables.
|
|
3
|
+
*
|
|
4
|
+
* Extends the built-in `Error` class and includes the missing key.
|
|
5
|
+
*
|
|
6
|
+
* @extends {Error}
|
|
7
|
+
*/
|
|
8
|
+
class EnvironmentNotFoundError extends Error {
|
|
9
|
+
key;
|
|
10
|
+
/**
|
|
11
|
+
* Creates a new EnvironmentNotFoundError.
|
|
12
|
+
*
|
|
13
|
+
* @param {string} key - The missing environment variable key.
|
|
14
|
+
*/
|
|
15
|
+
constructor(key) {
|
|
16
|
+
super(`Missing environment variable: ${key}`);
|
|
17
|
+
this.key = key;
|
|
18
|
+
this.name = this.constructor.name;
|
|
19
|
+
Error.captureStackTrace?.(this, this.constructor);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Retrieves the value of an environment variable, or throws an error if not set.
|
|
24
|
+
*
|
|
25
|
+
* @param {string} key - The environment variable key to check.
|
|
26
|
+
* @returns {string} The value of the environment variable.
|
|
27
|
+
* @throws {EnvironmentNotFoundError} If the environment variable is not defined.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* import { checkAndGetEnvValue } from '@kikiutils/shared/env';
|
|
32
|
+
*
|
|
33
|
+
* // When the environment variable 'API_KEY' is set:
|
|
34
|
+
* console.log(checkAndGetEnvValue('API_KEY')); // value of API_KEY
|
|
35
|
+
*
|
|
36
|
+
* // When the environment variable 'API_KEY' is not set:
|
|
37
|
+
* try {
|
|
38
|
+
* const apiKey = checkAndGetEnvValue('API_KEY');
|
|
39
|
+
* } catch (error) {
|
|
40
|
+
* console.error(error); // Missing environment variable: API_KEY
|
|
41
|
+
* }
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
function checkAndGetEnvValue(key) {
|
|
45
|
+
if (!process.env[key])
|
|
46
|
+
throw new EnvironmentNotFoundError(key);
|
|
47
|
+
return process.env[key];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export { EnvironmentNotFoundError, checkAndGetEnvValue };
|
|
51
|
+
//# sourceMappingURL=env.mjs.map
|
package/dist/env.mjs.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"env.mjs","sources":["../src/env.ts"],"sourcesContent":["/**\n * Custom error class for handling missing environment variables.\n *\n * Extends the built-in `Error` class and includes the missing key.\n *\n * @extends {Error}\n */\nexport class EnvironmentNotFoundError extends Error {\n readonly key: string;\n\n /**\n * Creates a new EnvironmentNotFoundError.\n *\n * @param {string} key - The missing environment variable key.\n */\n constructor(key: string) {\n super(`Missing environment variable: ${key}`);\n this.key = key;\n this.name = this.constructor.name;\n Error.captureStackTrace?.(this, this.constructor);\n }\n}\n\n/**\n * Retrieves the value of an environment variable, or throws an error if not set.\n *\n * @param {string} key - The environment variable key to check.\n * @returns {string} The value of the environment variable.\n * @throws {EnvironmentNotFoundError} If the environment variable is not defined.\n *\n * @example\n * ```typescript\n * import { checkAndGetEnvValue } from '@kikiutils/shared/env';\n *\n * // When the environment variable 'API_KEY' is set:\n * console.log(checkAndGetEnvValue('API_KEY')); // value of API_KEY\n *\n * // When the environment variable 'API_KEY' is not set:\n * try {\n * const apiKey = checkAndGetEnvValue('API_KEY');\n * } catch (error) {\n * console.error(error); // Missing environment variable: API_KEY\n * }\n * ```\n */\nexport function checkAndGetEnvValue(key: string): string {\n if (!process.env[key]) throw new EnvironmentNotFoundError(key);\n return process.env[key];\n}\n"],"names":[],"mappings":"AAAA;;;;;;AAMG;AACG,MAAO,wBAAyB,SAAQ,KAAK,CAAA;AACtC,IAAA,GAAG;AAEZ;;;;AAIG;AACH,IAAA,WAAA,CAAY,GAAW,EAAA;AACnB,QAAA,KAAK,CAAC,CAAA,8BAAA,EAAiC,GAAG,CAAA,CAAE,CAAC;AAC7C,QAAA,IAAI,CAAC,GAAG,GAAG,GAAG;QACd,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI;QACjC,KAAK,CAAC,iBAAiB,GAAG,IAAI,EAAE,IAAI,CAAC,WAAW,CAAC;;AAExD;AAED;;;;;;;;;;;;;;;;;;;;;AAqBG;AACG,SAAU,mBAAmB,CAAC,GAAW,EAAA;AAC3C,IAAA,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;AAAE,QAAA,MAAM,IAAI,wBAAwB,CAAC,GAAG,CAAC;AAC9D,IAAA,OAAO,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;AAC3B;;;;"}
|
package/dist/general.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"general.cjs","sources":["../src/general.ts"],"sourcesContent":["/**\n * Extracts the first value from an array or returns the value itself if it's not an array.\n *\n * - If `value` is an array, returns the first element.\n * - If `value` is not an array, returns `value` directly.\n * - If the result is `null` or `undefined`, and `defaultValue` is provided, returns `defaultValue` instead.\n *\n * @template T - The type of the input value(s).\n * @template D - The type of the default value (if provided).\n *\n * @param {T | T[]} value - A single value or an array of values.\n * @param {D} [defaultValue] - A fallback value if the result is `null` or `undefined`.\n * @returns {T | D | undefined} The first value or the fallback.\n *\n * @example\n * ```typescript\n * import { extractFirstValue } from '@kikiutils/shared/general';\n *\n * console.log(extractFirstValue([1, 2, 3])); // 1\n * console.log(extractFirstValue('hello')); // hello\n * console.log(extractFirstValue([], 'default')); // default\n * console.log(extractFirstValue(undefined, 'fallback')); // fallback\n * ```\n */\nexport function extractFirstValue<T>(value: T | T[]): T | undefined;\nexport function extractFirstValue<T, D>(value: T | T[], defaultValue: D): D | NonNullable<T>;\nexport function extractFirstValue<T, D>(value: T | T[], defaultValue?: D) {\n return (Array.isArray(value) ? value[0] : value) ?? defaultValue;\n}\n"],"names":[],"mappings":";;AA0BgB,SAAA,iBAAiB,CAAO,KAAc,EAAE,YAAgB,EAAA;IACpE,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,KAAK,YAAY;AACpE;;;;"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extracts the first value from an array or returns the value itself if it's not an array.
|
|
3
|
+
*
|
|
4
|
+
* - If `value` is an array, returns the first element.
|
|
5
|
+
* - If `value` is not an array, returns `value` directly.
|
|
6
|
+
* - If the result is `null` or `undefined`, and `defaultValue` is provided, returns `defaultValue` instead.
|
|
7
|
+
*
|
|
8
|
+
* @template T - The type of the input value(s).
|
|
9
|
+
* @template D - The type of the default value (if provided).
|
|
10
|
+
*
|
|
11
|
+
* @param {T | T[]} value - A single value or an array of values.
|
|
12
|
+
* @param {D} [defaultValue] - A fallback value if the result is `null` or `undefined`.
|
|
13
|
+
* @returns {T | D | undefined} The first value or the fallback.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* import { extractFirstValue } from '@kikiutils/shared/general';
|
|
18
|
+
*
|
|
19
|
+
* console.log(extractFirstValue([1, 2, 3])); // 1
|
|
20
|
+
* console.log(extractFirstValue('hello')); // hello
|
|
21
|
+
* console.log(extractFirstValue([], 'default')); // default
|
|
22
|
+
* console.log(extractFirstValue(undefined, 'fallback')); // fallback
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export declare function extractFirstValue<T>(value: T | T[]): T | undefined;
|
|
26
|
+
export declare function extractFirstValue<T, D>(value: T | T[], defaultValue: D): D | NonNullable<T>;
|
|
27
|
+
//# sourceMappingURL=general.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"general.d.ts","sourceRoot":"","sources":["../src/general.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,GAAG,SAAS,CAAC;AACpE,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,EAAE,EAAE,YAAY,EAAE,CAAC,GAAG,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC"}
|
package/dist/general.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"general.mjs","sources":["../src/general.ts"],"sourcesContent":["/**\n * Extracts the first value from an array or returns the value itself if it's not an array.\n *\n * - If `value` is an array, returns the first element.\n * - If `value` is not an array, returns `value` directly.\n * - If the result is `null` or `undefined`, and `defaultValue` is provided, returns `defaultValue` instead.\n *\n * @template T - The type of the input value(s).\n * @template D - The type of the default value (if provided).\n *\n * @param {T | T[]} value - A single value or an array of values.\n * @param {D} [defaultValue] - A fallback value if the result is `null` or `undefined`.\n * @returns {T | D | undefined} The first value or the fallback.\n *\n * @example\n * ```typescript\n * import { extractFirstValue } from '@kikiutils/shared/general';\n *\n * console.log(extractFirstValue([1, 2, 3])); // 1\n * console.log(extractFirstValue('hello')); // hello\n * console.log(extractFirstValue([], 'default')); // default\n * console.log(extractFirstValue(undefined, 'fallback')); // fallback\n * ```\n */\nexport function extractFirstValue<T>(value: T | T[]): T | undefined;\nexport function extractFirstValue<T, D>(value: T | T[], defaultValue: D): D | NonNullable<T>;\nexport function extractFirstValue<T, D>(value: T | T[], defaultValue?: D) {\n return (Array.isArray(value) ? value[0] : value) ?? defaultValue;\n}\n"],"names":[],"mappings":"AA0BgB,SAAA,iBAAiB,CAAO,KAAc,EAAE,YAAgB,EAAA;IACpE,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,KAAK,YAAY;AACpE;;;;"}
|