@arnonsang/utils 0.0.2 → 0.0.3
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/README.md +68 -2
- package/dist/index.d.ts +5 -1
- package/dist/index.js +5 -1
- package/dist/utils/array.d.ts +84 -0
- package/dist/utils/array.js +144 -0
- package/dist/utils/date.d.ts +68 -0
- package/dist/utils/date.js +150 -0
- package/dist/utils/logger.d.ts +50 -0
- package/dist/utils/logger.js +50 -0
- package/dist/utils/number.d.ts +94 -0
- package/dist/utils/number.js +150 -0
- package/dist/utils/string.d.ts +59 -0
- package/dist/utils/string.js +108 -0
- package/package.json +15 -2
- package/index.js +0 -23
- package/index.ts +0 -2
- package/tsconfig.json +0 -10
- package/utils/chat.js +0 -29
- package/utils/chat.ts +0 -28
- package/utils/logger.js +0 -40
- package/utils/logger.ts +0 -55
- /package/dist/utils/{chat.d.ts → char.d.ts} +0 -0
- /package/dist/utils/{chat.js → char.js} +0 -0
package/README.md
CHANGED
@@ -13,7 +13,19 @@ npm install @arnonsang/utils
|
|
13
13
|
### Importing Utilities
|
14
14
|
|
15
15
|
```ts
|
16
|
-
import {
|
16
|
+
import {
|
17
|
+
logger,
|
18
|
+
charAt,
|
19
|
+
charAtFrom,
|
20
|
+
capitalize,
|
21
|
+
toCamelCase,
|
22
|
+
unique,
|
23
|
+
chunk,
|
24
|
+
formatDate,
|
25
|
+
dateDiff,
|
26
|
+
clamp,
|
27
|
+
formatBytes
|
28
|
+
} from "@arnonsang/utils";
|
17
29
|
```
|
18
30
|
|
19
31
|
### Logger Utility
|
@@ -27,13 +39,67 @@ logger.debug("Debugging info");
|
|
27
39
|
logger.table([{ name: "Alice" }, { name: "Bob" }], "User Data");
|
28
40
|
```
|
29
41
|
|
30
|
-
###
|
42
|
+
### Character Utilities
|
31
43
|
|
32
44
|
```ts
|
33
45
|
console.log(charAt(3, "upper")); // "D"
|
34
46
|
console.log(charAtFrom("hello", 1)); // "e"
|
35
47
|
```
|
36
48
|
|
49
|
+
### String Utilities
|
50
|
+
|
51
|
+
```ts
|
52
|
+
capitalize("hello world"); // "Hello world"
|
53
|
+
toCamelCase("hello world"); // "helloWorld"
|
54
|
+
toKebabCase("Hello World"); // "hello-world"
|
55
|
+
toSnakeCase("Hello World"); // "hello_world"
|
56
|
+
truncate("This is a long string", 10); // "This is..."
|
57
|
+
cleanWhitespace(" hello world "); // "hello world"
|
58
|
+
isValidEmail("test@example.com"); // true
|
59
|
+
randomString(8); // "aB3xY9pQ"
|
60
|
+
pluralize("item", 5); // "items"
|
61
|
+
```
|
62
|
+
|
63
|
+
### Array Utilities
|
64
|
+
|
65
|
+
```ts
|
66
|
+
unique([1, 2, 2, 3, 3, 4]); // [1, 2, 3, 4]
|
67
|
+
chunk([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]]
|
68
|
+
flatten([[1, 2], [3, 4]]); // [1, 2, 3, 4]
|
69
|
+
groupBy([{type: 'a'}, {type: 'b'}, {type: 'a'}], item => item.type);
|
70
|
+
shuffle([1, 2, 3, 4, 5]); // [3, 1, 5, 2, 4] (random)
|
71
|
+
sample([1, 2, 3, 4, 5]); // 3 (random)
|
72
|
+
intersection([1, 2, 3], [2, 3, 4]); // [2, 3]
|
73
|
+
sum([1, 2, 3, 4, 5]); // 15
|
74
|
+
average([1, 2, 3, 4, 5]); // 3
|
75
|
+
```
|
76
|
+
|
77
|
+
### Date Utilities
|
78
|
+
|
79
|
+
```ts
|
80
|
+
formatDate(new Date(), 'YYYY-MM-DD HH:mm:ss');
|
81
|
+
dateDiff(date1, date2, 'days'); // 5
|
82
|
+
addTime(new Date(), 5, 'days');
|
83
|
+
subtractTime(new Date(), 2, 'hours');
|
84
|
+
isToday(new Date()); // true
|
85
|
+
isPast(someDate); // true/false
|
86
|
+
getAge(new Date('1990-01-01')); // 34 (example)
|
87
|
+
```
|
88
|
+
|
89
|
+
### Number Utilities
|
90
|
+
|
91
|
+
```ts
|
92
|
+
clamp(15, 0, 10); // 10
|
93
|
+
round(3.14159, 2); // 3.14
|
94
|
+
randomBetween(1, 10); // 7.234 (random)
|
95
|
+
randomInt(1, 10); // 7 (random)
|
96
|
+
isEven(4); // true
|
97
|
+
percentage(25, 100); // 25
|
98
|
+
formatNumber(1234567); // "1,234,567"
|
99
|
+
formatBytes(1024); // "1 KB"
|
100
|
+
factorial(5); // 120
|
101
|
+
```
|
102
|
+
|
37
103
|
## Contributing
|
38
104
|
|
39
105
|
Feel free to open an issue or a pull request to contribute new utilities or improve existing ones.
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
@@ -15,6 +15,10 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
15
15
|
};
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
17
17
|
exports.logger = void 0;
|
18
|
-
__exportStar(require("./utils/
|
18
|
+
__exportStar(require("./utils/char"), exports);
|
19
|
+
__exportStar(require("./utils/string"), exports);
|
20
|
+
__exportStar(require("./utils/array"), exports);
|
21
|
+
__exportStar(require("./utils/date"), exports);
|
22
|
+
__exportStar(require("./utils/number"), exports);
|
19
23
|
var logger_1 = require("./utils/logger");
|
20
24
|
Object.defineProperty(exports, "logger", { enumerable: true, get: function () { return logger_1.default; } });
|
@@ -0,0 +1,84 @@
|
|
1
|
+
/**
|
2
|
+
* Removes duplicate values from an array
|
3
|
+
* @param arr - The array to deduplicate
|
4
|
+
* @returns Array with unique values
|
5
|
+
*/
|
6
|
+
export declare const unique: <T>(arr: T[]) => T[];
|
7
|
+
/**
|
8
|
+
* Chunks an array into smaller arrays of specified size
|
9
|
+
* @param arr - The array to chunk
|
10
|
+
* @param size - The size of each chunk
|
11
|
+
* @returns Array of chunks
|
12
|
+
*/
|
13
|
+
export declare const chunk: <T>(arr: T[], size: number) => T[][];
|
14
|
+
/**
|
15
|
+
* Flattens a nested array to a specified depth
|
16
|
+
* @param arr - The array to flatten
|
17
|
+
* @param depth - The depth to flatten (default: 1)
|
18
|
+
* @returns Flattened array
|
19
|
+
*/
|
20
|
+
export declare const flatten: <T>(arr: (T | T[])[], depth?: number) => T[];
|
21
|
+
/**
|
22
|
+
* Groups array elements by a key function
|
23
|
+
* @param arr - The array to group
|
24
|
+
* @param keyFn - Function to extract the grouping key
|
25
|
+
* @returns Object with grouped elements
|
26
|
+
*/
|
27
|
+
export declare const groupBy: <T, K extends string | number | symbol>(arr: T[], keyFn: (item: T) => K) => Record<K, T[]>;
|
28
|
+
/**
|
29
|
+
* Shuffles an array randomly
|
30
|
+
* @param arr - The array to shuffle
|
31
|
+
* @returns New shuffled array
|
32
|
+
*/
|
33
|
+
export declare const shuffle: <T>(arr: T[]) => T[];
|
34
|
+
/**
|
35
|
+
* Gets a random element from an array
|
36
|
+
* @param arr - The array to pick from
|
37
|
+
* @returns Random element or undefined if array is empty
|
38
|
+
*/
|
39
|
+
export declare const sample: <T>(arr: T[]) => T | undefined;
|
40
|
+
/**
|
41
|
+
* Gets multiple random elements from an array
|
42
|
+
* @param arr - The array to pick from
|
43
|
+
* @param count - Number of elements to pick
|
44
|
+
* @returns Array of random elements
|
45
|
+
*/
|
46
|
+
export declare const sampleSize: <T>(arr: T[], count: number) => T[];
|
47
|
+
/**
|
48
|
+
* Finds the intersection of two arrays
|
49
|
+
* @param arr1 - First array
|
50
|
+
* @param arr2 - Second array
|
51
|
+
* @returns Array of common elements
|
52
|
+
*/
|
53
|
+
export declare const intersection: <T>(arr1: T[], arr2: T[]) => T[];
|
54
|
+
/**
|
55
|
+
* Finds the difference between two arrays (elements in first but not in second)
|
56
|
+
* @param arr1 - First array
|
57
|
+
* @param arr2 - Second array
|
58
|
+
* @returns Array of different elements
|
59
|
+
*/
|
60
|
+
export declare const difference: <T>(arr1: T[], arr2: T[]) => T[];
|
61
|
+
/**
|
62
|
+
* Sums all numbers in an array
|
63
|
+
* @param arr - Array of numbers
|
64
|
+
* @returns Sum of all numbers
|
65
|
+
*/
|
66
|
+
export declare const sum: (arr: number[]) => number;
|
67
|
+
/**
|
68
|
+
* Gets the average of all numbers in an array
|
69
|
+
* @param arr - Array of numbers
|
70
|
+
* @returns Average of all numbers
|
71
|
+
*/
|
72
|
+
export declare const average: (arr: number[]) => number;
|
73
|
+
/**
|
74
|
+
* Finds the minimum value in an array
|
75
|
+
* @param arr - Array of numbers
|
76
|
+
* @returns Minimum value or undefined if array is empty
|
77
|
+
*/
|
78
|
+
export declare const min: (arr: number[]) => number | undefined;
|
79
|
+
/**
|
80
|
+
* Finds the maximum value in an array
|
81
|
+
* @param arr - Array of numbers
|
82
|
+
* @returns Maximum value or undefined if array is empty
|
83
|
+
*/
|
84
|
+
export declare const max: (arr: number[]) => number | undefined;
|
@@ -0,0 +1,144 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.max = exports.min = exports.average = exports.sum = exports.difference = exports.intersection = exports.sampleSize = exports.sample = exports.shuffle = exports.groupBy = exports.flatten = exports.chunk = exports.unique = void 0;
|
4
|
+
/**
|
5
|
+
* Removes duplicate values from an array
|
6
|
+
* @param arr - The array to deduplicate
|
7
|
+
* @returns Array with unique values
|
8
|
+
*/
|
9
|
+
const unique = (arr) => {
|
10
|
+
return [...new Set(arr)];
|
11
|
+
};
|
12
|
+
exports.unique = unique;
|
13
|
+
/**
|
14
|
+
* Chunks an array into smaller arrays of specified size
|
15
|
+
* @param arr - The array to chunk
|
16
|
+
* @param size - The size of each chunk
|
17
|
+
* @returns Array of chunks
|
18
|
+
*/
|
19
|
+
const chunk = (arr, size) => {
|
20
|
+
const chunks = [];
|
21
|
+
for (let i = 0; i < arr.length; i += size) {
|
22
|
+
chunks.push(arr.slice(i, i + size));
|
23
|
+
}
|
24
|
+
return chunks;
|
25
|
+
};
|
26
|
+
exports.chunk = chunk;
|
27
|
+
/**
|
28
|
+
* Flattens a nested array to a specified depth
|
29
|
+
* @param arr - The array to flatten
|
30
|
+
* @param depth - The depth to flatten (default: 1)
|
31
|
+
* @returns Flattened array
|
32
|
+
*/
|
33
|
+
const flatten = (arr, depth = 1) => {
|
34
|
+
return depth > 0
|
35
|
+
? arr.reduce((acc, val) => acc.concat(Array.isArray(val) ? (0, exports.flatten)(val, depth - 1) : val), [])
|
36
|
+
: arr.slice();
|
37
|
+
};
|
38
|
+
exports.flatten = flatten;
|
39
|
+
/**
|
40
|
+
* Groups array elements by a key function
|
41
|
+
* @param arr - The array to group
|
42
|
+
* @param keyFn - Function to extract the grouping key
|
43
|
+
* @returns Object with grouped elements
|
44
|
+
*/
|
45
|
+
const groupBy = (arr, keyFn) => {
|
46
|
+
return arr.reduce((groups, item) => {
|
47
|
+
const key = keyFn(item);
|
48
|
+
(groups[key] = groups[key] || []).push(item);
|
49
|
+
return groups;
|
50
|
+
}, {});
|
51
|
+
};
|
52
|
+
exports.groupBy = groupBy;
|
53
|
+
/**
|
54
|
+
* Shuffles an array randomly
|
55
|
+
* @param arr - The array to shuffle
|
56
|
+
* @returns New shuffled array
|
57
|
+
*/
|
58
|
+
const shuffle = (arr) => {
|
59
|
+
const shuffled = [...arr];
|
60
|
+
for (let i = shuffled.length - 1; i > 0; i--) {
|
61
|
+
const j = Math.floor(Math.random() * (i + 1));
|
62
|
+
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
|
63
|
+
}
|
64
|
+
return shuffled;
|
65
|
+
};
|
66
|
+
exports.shuffle = shuffle;
|
67
|
+
/**
|
68
|
+
* Gets a random element from an array
|
69
|
+
* @param arr - The array to pick from
|
70
|
+
* @returns Random element or undefined if array is empty
|
71
|
+
*/
|
72
|
+
const sample = (arr) => {
|
73
|
+
return arr[Math.floor(Math.random() * arr.length)];
|
74
|
+
};
|
75
|
+
exports.sample = sample;
|
76
|
+
/**
|
77
|
+
* Gets multiple random elements from an array
|
78
|
+
* @param arr - The array to pick from
|
79
|
+
* @param count - Number of elements to pick
|
80
|
+
* @returns Array of random elements
|
81
|
+
*/
|
82
|
+
const sampleSize = (arr, count) => {
|
83
|
+
const shuffled = (0, exports.shuffle)(arr);
|
84
|
+
return shuffled.slice(0, Math.min(count, arr.length));
|
85
|
+
};
|
86
|
+
exports.sampleSize = sampleSize;
|
87
|
+
/**
|
88
|
+
* Finds the intersection of two arrays
|
89
|
+
* @param arr1 - First array
|
90
|
+
* @param arr2 - Second array
|
91
|
+
* @returns Array of common elements
|
92
|
+
*/
|
93
|
+
const intersection = (arr1, arr2) => {
|
94
|
+
const set2 = new Set(arr2);
|
95
|
+
return arr1.filter(item => set2.has(item));
|
96
|
+
};
|
97
|
+
exports.intersection = intersection;
|
98
|
+
/**
|
99
|
+
* Finds the difference between two arrays (elements in first but not in second)
|
100
|
+
* @param arr1 - First array
|
101
|
+
* @param arr2 - Second array
|
102
|
+
* @returns Array of different elements
|
103
|
+
*/
|
104
|
+
const difference = (arr1, arr2) => {
|
105
|
+
const set2 = new Set(arr2);
|
106
|
+
return arr1.filter(item => !set2.has(item));
|
107
|
+
};
|
108
|
+
exports.difference = difference;
|
109
|
+
/**
|
110
|
+
* Sums all numbers in an array
|
111
|
+
* @param arr - Array of numbers
|
112
|
+
* @returns Sum of all numbers
|
113
|
+
*/
|
114
|
+
const sum = (arr) => {
|
115
|
+
return arr.reduce((total, num) => total + num, 0);
|
116
|
+
};
|
117
|
+
exports.sum = sum;
|
118
|
+
/**
|
119
|
+
* Gets the average of all numbers in an array
|
120
|
+
* @param arr - Array of numbers
|
121
|
+
* @returns Average of all numbers
|
122
|
+
*/
|
123
|
+
const average = (arr) => {
|
124
|
+
return arr.length > 0 ? (0, exports.sum)(arr) / arr.length : 0;
|
125
|
+
};
|
126
|
+
exports.average = average;
|
127
|
+
/**
|
128
|
+
* Finds the minimum value in an array
|
129
|
+
* @param arr - Array of numbers
|
130
|
+
* @returns Minimum value or undefined if array is empty
|
131
|
+
*/
|
132
|
+
const min = (arr) => {
|
133
|
+
return arr.length > 0 ? Math.min(...arr) : undefined;
|
134
|
+
};
|
135
|
+
exports.min = min;
|
136
|
+
/**
|
137
|
+
* Finds the maximum value in an array
|
138
|
+
* @param arr - Array of numbers
|
139
|
+
* @returns Maximum value or undefined if array is empty
|
140
|
+
*/
|
141
|
+
const max = (arr) => {
|
142
|
+
return arr.length > 0 ? Math.max(...arr) : undefined;
|
143
|
+
};
|
144
|
+
exports.max = max;
|
@@ -0,0 +1,68 @@
|
|
1
|
+
/**
|
2
|
+
* Formats a date to a readable string
|
3
|
+
* @param date - The date to format
|
4
|
+
* @param format - The format pattern (default: 'YYYY-MM-DD')
|
5
|
+
* @returns Formatted date string
|
6
|
+
*/
|
7
|
+
export declare const formatDate: (date: Date, format?: string) => string;
|
8
|
+
/**
|
9
|
+
* Gets the difference between two dates in various units
|
10
|
+
* @param date1 - First date
|
11
|
+
* @param date2 - Second date
|
12
|
+
* @param unit - Unit of measurement ('days', 'hours', 'minutes', 'seconds')
|
13
|
+
* @returns Difference in specified unit
|
14
|
+
*/
|
15
|
+
export declare const dateDiff: (date1: Date, date2: Date, unit?: "days" | "hours" | "minutes" | "seconds") => number;
|
16
|
+
/**
|
17
|
+
* Adds time to a date
|
18
|
+
* @param date - The base date
|
19
|
+
* @param amount - Amount to add
|
20
|
+
* @param unit - Unit of time ('days', 'hours', 'minutes', 'seconds')
|
21
|
+
* @returns New date with added time
|
22
|
+
*/
|
23
|
+
export declare const addTime: (date: Date, amount: number, unit: "days" | "hours" | "minutes" | "seconds") => Date;
|
24
|
+
/**
|
25
|
+
* Subtracts time from a date
|
26
|
+
* @param date - The base date
|
27
|
+
* @param amount - Amount to subtract
|
28
|
+
* @param unit - Unit of time ('days', 'hours', 'minutes', 'seconds')
|
29
|
+
* @returns New date with subtracted time
|
30
|
+
*/
|
31
|
+
export declare const subtractTime: (date: Date, amount: number, unit: "days" | "hours" | "minutes" | "seconds") => Date;
|
32
|
+
/**
|
33
|
+
* Checks if a date is in the past
|
34
|
+
* @param date - The date to check
|
35
|
+
* @returns True if date is in the past
|
36
|
+
*/
|
37
|
+
export declare const isPast: (date: Date) => boolean;
|
38
|
+
/**
|
39
|
+
* Checks if a date is in the future
|
40
|
+
* @param date - The date to check
|
41
|
+
* @returns True if date is in the future
|
42
|
+
*/
|
43
|
+
export declare const isFuture: (date: Date) => boolean;
|
44
|
+
/**
|
45
|
+
* Checks if a date is today
|
46
|
+
* @param date - The date to check
|
47
|
+
* @returns True if date is today
|
48
|
+
*/
|
49
|
+
export declare const isToday: (date: Date) => boolean;
|
50
|
+
/**
|
51
|
+
* Gets the start of day for a date
|
52
|
+
* @param date - The date
|
53
|
+
* @returns Date set to start of day (00:00:00)
|
54
|
+
*/
|
55
|
+
export declare const startOfDay: (date: Date) => Date;
|
56
|
+
/**
|
57
|
+
* Gets the end of day for a date
|
58
|
+
* @param date - The date
|
59
|
+
* @returns Date set to end of day (23:59:59.999)
|
60
|
+
*/
|
61
|
+
export declare const endOfDay: (date: Date) => Date;
|
62
|
+
/**
|
63
|
+
* Gets the age in years from a birth date
|
64
|
+
* @param birthDate - The birth date
|
65
|
+
* @param referenceDate - The reference date (default: now)
|
66
|
+
* @returns Age in years
|
67
|
+
*/
|
68
|
+
export declare const getAge: (birthDate: Date, referenceDate?: Date) => number;
|
@@ -0,0 +1,150 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.getAge = exports.endOfDay = exports.startOfDay = exports.isToday = exports.isFuture = exports.isPast = exports.subtractTime = exports.addTime = exports.dateDiff = exports.formatDate = void 0;
|
4
|
+
/**
|
5
|
+
* Formats a date to a readable string
|
6
|
+
* @param date - The date to format
|
7
|
+
* @param format - The format pattern (default: 'YYYY-MM-DD')
|
8
|
+
* @returns Formatted date string
|
9
|
+
*/
|
10
|
+
const formatDate = (date, format = 'YYYY-MM-DD') => {
|
11
|
+
const year = date.getFullYear();
|
12
|
+
const month = String(date.getMonth() + 1).padStart(2, '0');
|
13
|
+
const day = String(date.getDate()).padStart(2, '0');
|
14
|
+
const hours = String(date.getHours()).padStart(2, '0');
|
15
|
+
const minutes = String(date.getMinutes()).padStart(2, '0');
|
16
|
+
const seconds = String(date.getSeconds()).padStart(2, '0');
|
17
|
+
return format
|
18
|
+
.replace('YYYY', String(year))
|
19
|
+
.replace('MM', month)
|
20
|
+
.replace('DD', day)
|
21
|
+
.replace('HH', hours)
|
22
|
+
.replace('mm', minutes)
|
23
|
+
.replace('ss', seconds);
|
24
|
+
};
|
25
|
+
exports.formatDate = formatDate;
|
26
|
+
/**
|
27
|
+
* Gets the difference between two dates in various units
|
28
|
+
* @param date1 - First date
|
29
|
+
* @param date2 - Second date
|
30
|
+
* @param unit - Unit of measurement ('days', 'hours', 'minutes', 'seconds')
|
31
|
+
* @returns Difference in specified unit
|
32
|
+
*/
|
33
|
+
const dateDiff = (date1, date2, unit = 'days') => {
|
34
|
+
const diff = Math.abs(date1.getTime() - date2.getTime());
|
35
|
+
switch (unit) {
|
36
|
+
case 'seconds':
|
37
|
+
return Math.floor(diff / 1000);
|
38
|
+
case 'minutes':
|
39
|
+
return Math.floor(diff / (1000 * 60));
|
40
|
+
case 'hours':
|
41
|
+
return Math.floor(diff / (1000 * 60 * 60));
|
42
|
+
case 'days':
|
43
|
+
return Math.floor(diff / (1000 * 60 * 60 * 24));
|
44
|
+
default:
|
45
|
+
return diff;
|
46
|
+
}
|
47
|
+
};
|
48
|
+
exports.dateDiff = dateDiff;
|
49
|
+
/**
|
50
|
+
* Adds time to a date
|
51
|
+
* @param date - The base date
|
52
|
+
* @param amount - Amount to add
|
53
|
+
* @param unit - Unit of time ('days', 'hours', 'minutes', 'seconds')
|
54
|
+
* @returns New date with added time
|
55
|
+
*/
|
56
|
+
const addTime = (date, amount, unit) => {
|
57
|
+
const newDate = new Date(date);
|
58
|
+
switch (unit) {
|
59
|
+
case 'seconds':
|
60
|
+
newDate.setSeconds(newDate.getSeconds() + amount);
|
61
|
+
break;
|
62
|
+
case 'minutes':
|
63
|
+
newDate.setMinutes(newDate.getMinutes() + amount);
|
64
|
+
break;
|
65
|
+
case 'hours':
|
66
|
+
newDate.setHours(newDate.getHours() + amount);
|
67
|
+
break;
|
68
|
+
case 'days':
|
69
|
+
newDate.setDate(newDate.getDate() + amount);
|
70
|
+
break;
|
71
|
+
}
|
72
|
+
return newDate;
|
73
|
+
};
|
74
|
+
exports.addTime = addTime;
|
75
|
+
/**
|
76
|
+
* Subtracts time from a date
|
77
|
+
* @param date - The base date
|
78
|
+
* @param amount - Amount to subtract
|
79
|
+
* @param unit - Unit of time ('days', 'hours', 'minutes', 'seconds')
|
80
|
+
* @returns New date with subtracted time
|
81
|
+
*/
|
82
|
+
const subtractTime = (date, amount, unit) => {
|
83
|
+
return (0, exports.addTime)(date, -amount, unit);
|
84
|
+
};
|
85
|
+
exports.subtractTime = subtractTime;
|
86
|
+
/**
|
87
|
+
* Checks if a date is in the past
|
88
|
+
* @param date - The date to check
|
89
|
+
* @returns True if date is in the past
|
90
|
+
*/
|
91
|
+
const isPast = (date) => {
|
92
|
+
return date < new Date();
|
93
|
+
};
|
94
|
+
exports.isPast = isPast;
|
95
|
+
/**
|
96
|
+
* Checks if a date is in the future
|
97
|
+
* @param date - The date to check
|
98
|
+
* @returns True if date is in the future
|
99
|
+
*/
|
100
|
+
const isFuture = (date) => {
|
101
|
+
return date > new Date();
|
102
|
+
};
|
103
|
+
exports.isFuture = isFuture;
|
104
|
+
/**
|
105
|
+
* Checks if a date is today
|
106
|
+
* @param date - The date to check
|
107
|
+
* @returns True if date is today
|
108
|
+
*/
|
109
|
+
const isToday = (date) => {
|
110
|
+
const today = new Date();
|
111
|
+
return date.toDateString() === today.toDateString();
|
112
|
+
};
|
113
|
+
exports.isToday = isToday;
|
114
|
+
/**
|
115
|
+
* Gets the start of day for a date
|
116
|
+
* @param date - The date
|
117
|
+
* @returns Date set to start of day (00:00:00)
|
118
|
+
*/
|
119
|
+
const startOfDay = (date) => {
|
120
|
+
const newDate = new Date(date);
|
121
|
+
newDate.setHours(0, 0, 0, 0);
|
122
|
+
return newDate;
|
123
|
+
};
|
124
|
+
exports.startOfDay = startOfDay;
|
125
|
+
/**
|
126
|
+
* Gets the end of day for a date
|
127
|
+
* @param date - The date
|
128
|
+
* @returns Date set to end of day (23:59:59.999)
|
129
|
+
*/
|
130
|
+
const endOfDay = (date) => {
|
131
|
+
const newDate = new Date(date);
|
132
|
+
newDate.setHours(23, 59, 59, 999);
|
133
|
+
return newDate;
|
134
|
+
};
|
135
|
+
exports.endOfDay = endOfDay;
|
136
|
+
/**
|
137
|
+
* Gets the age in years from a birth date
|
138
|
+
* @param birthDate - The birth date
|
139
|
+
* @param referenceDate - The reference date (default: now)
|
140
|
+
* @returns Age in years
|
141
|
+
*/
|
142
|
+
const getAge = (birthDate, referenceDate = new Date()) => {
|
143
|
+
let age = referenceDate.getFullYear() - birthDate.getFullYear();
|
144
|
+
const monthDiff = referenceDate.getMonth() - birthDate.getMonth();
|
145
|
+
if (monthDiff < 0 || (monthDiff === 0 && referenceDate.getDate() < birthDate.getDate())) {
|
146
|
+
age--;
|
147
|
+
}
|
148
|
+
return age;
|
149
|
+
};
|
150
|
+
exports.getAge = getAge;
|
package/dist/utils/logger.d.ts
CHANGED
@@ -1,13 +1,63 @@
|
|
1
|
+
/**
|
2
|
+
* A styled console logger that only outputs in development mode
|
3
|
+
* Provides colored output with consistent formatting for different log levels
|
4
|
+
*/
|
1
5
|
declare class Logger {
|
2
6
|
private isDev;
|
7
|
+
/**
|
8
|
+
* Creates a new Logger instance
|
9
|
+
* Automatically detects development mode based on NODE_ENV
|
10
|
+
*/
|
3
11
|
constructor();
|
12
|
+
/**
|
13
|
+
* Internal method for styled console logging
|
14
|
+
* @param type - The console method to use (info, warn, error, etc.)
|
15
|
+
* @param message - The message to log
|
16
|
+
* @param color - CSS color for the log level indicator
|
17
|
+
* @param args - Additional arguments to pass to console
|
18
|
+
*/
|
4
19
|
private logMessage;
|
20
|
+
/**
|
21
|
+
* Logs a success message with green color
|
22
|
+
* @param message - The success message to log
|
23
|
+
* @param args - Additional arguments to include in the log
|
24
|
+
*/
|
5
25
|
success(message: string, ...args: unknown[]): void;
|
26
|
+
/**
|
27
|
+
* Logs an informational message with blue color
|
28
|
+
* @param message - The info message to log
|
29
|
+
* @param args - Additional arguments to include in the log
|
30
|
+
*/
|
6
31
|
info(message: string, ...args: unknown[]): void;
|
32
|
+
/**
|
33
|
+
* Logs a warning message with yellow color
|
34
|
+
* @param message - The warning message to log
|
35
|
+
* @param args - Additional arguments to include in the log
|
36
|
+
*/
|
7
37
|
warn(message: string, ...args: unknown[]): void;
|
38
|
+
/**
|
39
|
+
* Logs an error message with red color
|
40
|
+
* @param message - The error message to log
|
41
|
+
* @param args - Additional arguments to include in the log
|
42
|
+
*/
|
8
43
|
error(message: string, ...args: unknown[]): void;
|
44
|
+
/**
|
45
|
+
* Logs a debug message with green color
|
46
|
+
* @param message - The debug message to log
|
47
|
+
* @param args - Additional arguments to include in the log
|
48
|
+
*/
|
9
49
|
debug(message: string, ...args: unknown[]): void;
|
50
|
+
/**
|
51
|
+
* Logs a general message with white color
|
52
|
+
* @param message - The message to log
|
53
|
+
* @param args - Additional arguments to include in the log
|
54
|
+
*/
|
10
55
|
log(message: string, ...args: unknown[]): void;
|
56
|
+
/**
|
57
|
+
* Displays data in a table format using console.table
|
58
|
+
* @param data - The data to display in table format
|
59
|
+
* @param from - Optional label to identify the source of the data
|
60
|
+
*/
|
11
61
|
table(data: unknown, from?: string): void;
|
12
62
|
}
|
13
63
|
declare const logger: Logger;
|
package/dist/utils/logger.js
CHANGED
@@ -1,33 +1,83 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
/**
|
4
|
+
* A styled console logger that only outputs in development mode
|
5
|
+
* Provides colored output with consistent formatting for different log levels
|
6
|
+
*/
|
3
7
|
class Logger {
|
8
|
+
/**
|
9
|
+
* Creates a new Logger instance
|
10
|
+
* Automatically detects development mode based on NODE_ENV
|
11
|
+
*/
|
4
12
|
constructor() {
|
5
13
|
this.isDev = process.env.NODE_ENV === "development" || !process.env.NODE_ENV;
|
6
14
|
}
|
15
|
+
/**
|
16
|
+
* Internal method for styled console logging
|
17
|
+
* @param type - The console method to use (info, warn, error, etc.)
|
18
|
+
* @param message - The message to log
|
19
|
+
* @param color - CSS color for the log level indicator
|
20
|
+
* @param args - Additional arguments to pass to console
|
21
|
+
*/
|
7
22
|
logMessage(type, message, color, ...args) {
|
8
23
|
if (!this.isDev)
|
9
24
|
return;
|
10
25
|
const styles = `color: ${color}; font-weight: bold;`;
|
11
26
|
console[type](`%c[${type.toUpperCase()}] %c${message}`, styles, "color:white;", ...args);
|
12
27
|
}
|
28
|
+
/**
|
29
|
+
* Logs a success message with green color
|
30
|
+
* @param message - The success message to log
|
31
|
+
* @param args - Additional arguments to include in the log
|
32
|
+
*/
|
13
33
|
success(message, ...args) {
|
14
34
|
this.logMessage("info", message, "#28a745", ...args);
|
15
35
|
}
|
36
|
+
/**
|
37
|
+
* Logs an informational message with blue color
|
38
|
+
* @param message - The info message to log
|
39
|
+
* @param args - Additional arguments to include in the log
|
40
|
+
*/
|
16
41
|
info(message, ...args) {
|
17
42
|
this.logMessage("info", message, "#007bff", ...args);
|
18
43
|
}
|
44
|
+
/**
|
45
|
+
* Logs a warning message with yellow color
|
46
|
+
* @param message - The warning message to log
|
47
|
+
* @param args - Additional arguments to include in the log
|
48
|
+
*/
|
19
49
|
warn(message, ...args) {
|
20
50
|
this.logMessage("warn", message, "#ffc107", ...args);
|
21
51
|
}
|
52
|
+
/**
|
53
|
+
* Logs an error message with red color
|
54
|
+
* @param message - The error message to log
|
55
|
+
* @param args - Additional arguments to include in the log
|
56
|
+
*/
|
22
57
|
error(message, ...args) {
|
23
58
|
this.logMessage("error", message, "#dc3545", ...args);
|
24
59
|
}
|
60
|
+
/**
|
61
|
+
* Logs a debug message with green color
|
62
|
+
* @param message - The debug message to log
|
63
|
+
* @param args - Additional arguments to include in the log
|
64
|
+
*/
|
25
65
|
debug(message, ...args) {
|
26
66
|
this.logMessage("debug", message, "#28a745", ...args);
|
27
67
|
}
|
68
|
+
/**
|
69
|
+
* Logs a general message with white color
|
70
|
+
* @param message - The message to log
|
71
|
+
* @param args - Additional arguments to include in the log
|
72
|
+
*/
|
28
73
|
log(message, ...args) {
|
29
74
|
this.logMessage("log", message, "white", ...args);
|
30
75
|
}
|
76
|
+
/**
|
77
|
+
* Displays data in a table format using console.table
|
78
|
+
* @param data - The data to display in table format
|
79
|
+
* @param from - Optional label to identify the source of the data
|
80
|
+
*/
|
31
81
|
table(data, from) {
|
32
82
|
if (!this.isDev)
|
33
83
|
return;
|
@@ -0,0 +1,94 @@
|
|
1
|
+
/**
|
2
|
+
* Clamps a number between min and max values
|
3
|
+
* @param num - The number to clamp
|
4
|
+
* @param min - Minimum value
|
5
|
+
* @param max - Maximum value
|
6
|
+
* @returns Clamped number
|
7
|
+
*/
|
8
|
+
export declare const clamp: (num: number, min: number, max: number) => number;
|
9
|
+
/**
|
10
|
+
* Rounds a number to specified decimal places
|
11
|
+
* @param num - The number to round
|
12
|
+
* @param decimals - Number of decimal places
|
13
|
+
* @returns Rounded number
|
14
|
+
*/
|
15
|
+
export declare const round: (num: number, decimals?: number) => number;
|
16
|
+
/**
|
17
|
+
* Generates a random number between min and max (inclusive)
|
18
|
+
* @param min - Minimum value
|
19
|
+
* @param max - Maximum value
|
20
|
+
* @returns Random number
|
21
|
+
*/
|
22
|
+
export declare const randomBetween: (min: number, max: number) => number;
|
23
|
+
/**
|
24
|
+
* Generates a random integer between min and max (inclusive)
|
25
|
+
* @param min - Minimum value
|
26
|
+
* @param max - Maximum value
|
27
|
+
* @returns Random integer
|
28
|
+
*/
|
29
|
+
export declare const randomInt: (min: number, max: number) => number;
|
30
|
+
/**
|
31
|
+
* Checks if a number is even
|
32
|
+
* @param num - The number to check
|
33
|
+
* @returns True if even, false otherwise
|
34
|
+
*/
|
35
|
+
export declare const isEven: (num: number) => boolean;
|
36
|
+
/**
|
37
|
+
* Checks if a number is odd
|
38
|
+
* @param num - The number to check
|
39
|
+
* @returns True if odd, false otherwise
|
40
|
+
*/
|
41
|
+
export declare const isOdd: (num: number) => boolean;
|
42
|
+
/**
|
43
|
+
* Converts degrees to radians
|
44
|
+
* @param degrees - Angle in degrees
|
45
|
+
* @returns Angle in radians
|
46
|
+
*/
|
47
|
+
export declare const toRadians: (degrees: number) => number;
|
48
|
+
/**
|
49
|
+
* Converts radians to degrees
|
50
|
+
* @param radians - Angle in radians
|
51
|
+
* @returns Angle in degrees
|
52
|
+
*/
|
53
|
+
export declare const toDegrees: (radians: number) => number;
|
54
|
+
/**
|
55
|
+
* Calculates the percentage of a value relative to a total
|
56
|
+
* @param value - The value
|
57
|
+
* @param total - The total
|
58
|
+
* @returns Percentage (0-100)
|
59
|
+
*/
|
60
|
+
export declare const percentage: (value: number, total: number) => number;
|
61
|
+
/**
|
62
|
+
* Formats a number with thousand separators
|
63
|
+
* @param num - The number to format
|
64
|
+
* @param separator - The separator to use (default: ',')
|
65
|
+
* @returns Formatted number string
|
66
|
+
*/
|
67
|
+
export declare const formatNumber: (num: number, separator?: string) => string;
|
68
|
+
/**
|
69
|
+
* Converts bytes to human readable format
|
70
|
+
* @param bytes - Number of bytes
|
71
|
+
* @param decimals - Number of decimal places (default: 2)
|
72
|
+
* @returns Human readable string
|
73
|
+
*/
|
74
|
+
export declare const formatBytes: (bytes: number, decimals?: number) => string;
|
75
|
+
/**
|
76
|
+
* Calculates the factorial of a number
|
77
|
+
* @param n - The number
|
78
|
+
* @returns Factorial of n
|
79
|
+
*/
|
80
|
+
export declare const factorial: (n: number) => number;
|
81
|
+
/**
|
82
|
+
* Calculates the greatest common divisor of two numbers
|
83
|
+
* @param a - First number
|
84
|
+
* @param b - Second number
|
85
|
+
* @returns GCD of a and b
|
86
|
+
*/
|
87
|
+
export declare const gcd: (a: number, b: number) => number;
|
88
|
+
/**
|
89
|
+
* Calculates the least common multiple of two numbers
|
90
|
+
* @param a - First number
|
91
|
+
* @param b - Second number
|
92
|
+
* @returns LCM of a and b
|
93
|
+
*/
|
94
|
+
export declare const lcm: (a: number, b: number) => number;
|
@@ -0,0 +1,150 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.lcm = exports.gcd = exports.factorial = exports.formatBytes = exports.formatNumber = exports.percentage = exports.toDegrees = exports.toRadians = exports.isOdd = exports.isEven = exports.randomInt = exports.randomBetween = exports.round = exports.clamp = void 0;
|
4
|
+
/**
|
5
|
+
* Clamps a number between min and max values
|
6
|
+
* @param num - The number to clamp
|
7
|
+
* @param min - Minimum value
|
8
|
+
* @param max - Maximum value
|
9
|
+
* @returns Clamped number
|
10
|
+
*/
|
11
|
+
const clamp = (num, min, max) => {
|
12
|
+
return Math.min(Math.max(num, min), max);
|
13
|
+
};
|
14
|
+
exports.clamp = clamp;
|
15
|
+
/**
|
16
|
+
* Rounds a number to specified decimal places
|
17
|
+
* @param num - The number to round
|
18
|
+
* @param decimals - Number of decimal places
|
19
|
+
* @returns Rounded number
|
20
|
+
*/
|
21
|
+
const round = (num, decimals = 0) => {
|
22
|
+
const factor = Math.pow(10, decimals);
|
23
|
+
return Math.round(num * factor) / factor;
|
24
|
+
};
|
25
|
+
exports.round = round;
|
26
|
+
/**
|
27
|
+
* Generates a random number between min and max (inclusive)
|
28
|
+
* @param min - Minimum value
|
29
|
+
* @param max - Maximum value
|
30
|
+
* @returns Random number
|
31
|
+
*/
|
32
|
+
const randomBetween = (min, max) => {
|
33
|
+
return Math.random() * (max - min) + min;
|
34
|
+
};
|
35
|
+
exports.randomBetween = randomBetween;
|
36
|
+
/**
|
37
|
+
* Generates a random integer between min and max (inclusive)
|
38
|
+
* @param min - Minimum value
|
39
|
+
* @param max - Maximum value
|
40
|
+
* @returns Random integer
|
41
|
+
*/
|
42
|
+
const randomInt = (min, max) => {
|
43
|
+
return Math.floor(Math.random() * (max - min + 1)) + min;
|
44
|
+
};
|
45
|
+
exports.randomInt = randomInt;
|
46
|
+
/**
|
47
|
+
* Checks if a number is even
|
48
|
+
* @param num - The number to check
|
49
|
+
* @returns True if even, false otherwise
|
50
|
+
*/
|
51
|
+
const isEven = (num) => {
|
52
|
+
return num % 2 === 0;
|
53
|
+
};
|
54
|
+
exports.isEven = isEven;
|
55
|
+
/**
|
56
|
+
* Checks if a number is odd
|
57
|
+
* @param num - The number to check
|
58
|
+
* @returns True if odd, false otherwise
|
59
|
+
*/
|
60
|
+
const isOdd = (num) => {
|
61
|
+
return num % 2 !== 0;
|
62
|
+
};
|
63
|
+
exports.isOdd = isOdd;
|
64
|
+
/**
|
65
|
+
* Converts degrees to radians
|
66
|
+
* @param degrees - Angle in degrees
|
67
|
+
* @returns Angle in radians
|
68
|
+
*/
|
69
|
+
const toRadians = (degrees) => {
|
70
|
+
return degrees * (Math.PI / 180);
|
71
|
+
};
|
72
|
+
exports.toRadians = toRadians;
|
73
|
+
/**
|
74
|
+
* Converts radians to degrees
|
75
|
+
* @param radians - Angle in radians
|
76
|
+
* @returns Angle in degrees
|
77
|
+
*/
|
78
|
+
const toDegrees = (radians) => {
|
79
|
+
return radians * (180 / Math.PI);
|
80
|
+
};
|
81
|
+
exports.toDegrees = toDegrees;
|
82
|
+
/**
|
83
|
+
* Calculates the percentage of a value relative to a total
|
84
|
+
* @param value - The value
|
85
|
+
* @param total - The total
|
86
|
+
* @returns Percentage (0-100)
|
87
|
+
*/
|
88
|
+
const percentage = (value, total) => {
|
89
|
+
return total === 0 ? 0 : (value / total) * 100;
|
90
|
+
};
|
91
|
+
exports.percentage = percentage;
|
92
|
+
/**
|
93
|
+
* Formats a number with thousand separators
|
94
|
+
* @param num - The number to format
|
95
|
+
* @param separator - The separator to use (default: ',')
|
96
|
+
* @returns Formatted number string
|
97
|
+
*/
|
98
|
+
const formatNumber = (num, separator = ',') => {
|
99
|
+
return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, separator);
|
100
|
+
};
|
101
|
+
exports.formatNumber = formatNumber;
|
102
|
+
/**
|
103
|
+
* Converts bytes to human readable format
|
104
|
+
* @param bytes - Number of bytes
|
105
|
+
* @param decimals - Number of decimal places (default: 2)
|
106
|
+
* @returns Human readable string
|
107
|
+
*/
|
108
|
+
const formatBytes = (bytes, decimals = 2) => {
|
109
|
+
if (bytes === 0)
|
110
|
+
return '0 Bytes';
|
111
|
+
const k = 1024;
|
112
|
+
const dm = decimals < 0 ? 0 : decimals;
|
113
|
+
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
114
|
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
115
|
+
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
|
116
|
+
};
|
117
|
+
exports.formatBytes = formatBytes;
|
118
|
+
/**
|
119
|
+
* Calculates the factorial of a number
|
120
|
+
* @param n - The number
|
121
|
+
* @returns Factorial of n
|
122
|
+
*/
|
123
|
+
const factorial = (n) => {
|
124
|
+
if (n < 0)
|
125
|
+
return NaN;
|
126
|
+
if (n === 0 || n === 1)
|
127
|
+
return 1;
|
128
|
+
return n * (0, exports.factorial)(n - 1);
|
129
|
+
};
|
130
|
+
exports.factorial = factorial;
|
131
|
+
/**
|
132
|
+
* Calculates the greatest common divisor of two numbers
|
133
|
+
* @param a - First number
|
134
|
+
* @param b - Second number
|
135
|
+
* @returns GCD of a and b
|
136
|
+
*/
|
137
|
+
const gcd = (a, b) => {
|
138
|
+
return b === 0 ? a : (0, exports.gcd)(b, a % b);
|
139
|
+
};
|
140
|
+
exports.gcd = gcd;
|
141
|
+
/**
|
142
|
+
* Calculates the least common multiple of two numbers
|
143
|
+
* @param a - First number
|
144
|
+
* @param b - Second number
|
145
|
+
* @returns LCM of a and b
|
146
|
+
*/
|
147
|
+
const lcm = (a, b) => {
|
148
|
+
return Math.abs(a * b) / (0, exports.gcd)(a, b);
|
149
|
+
};
|
150
|
+
exports.lcm = lcm;
|
@@ -0,0 +1,59 @@
|
|
1
|
+
/**
|
2
|
+
* Capitalizes the first letter of a string
|
3
|
+
* @param str - The string to capitalize
|
4
|
+
* @returns The string with first letter capitalized
|
5
|
+
*/
|
6
|
+
export declare const capitalize: (str: string) => string;
|
7
|
+
/**
|
8
|
+
* Converts a string to camelCase
|
9
|
+
* @param str - The string to convert
|
10
|
+
* @returns The camelCase string
|
11
|
+
*/
|
12
|
+
export declare const toCamelCase: (str: string) => string;
|
13
|
+
/**
|
14
|
+
* Converts a string to kebab-case
|
15
|
+
* @param str - The string to convert
|
16
|
+
* @returns The kebab-case string
|
17
|
+
*/
|
18
|
+
export declare const toKebabCase: (str: string) => string;
|
19
|
+
/**
|
20
|
+
* Converts a string to snake_case
|
21
|
+
* @param str - The string to convert
|
22
|
+
* @returns The snake_case string
|
23
|
+
*/
|
24
|
+
export declare const toSnakeCase: (str: string) => string;
|
25
|
+
/**
|
26
|
+
* Truncates a string to a specified length and adds ellipsis
|
27
|
+
* @param str - The string to truncate
|
28
|
+
* @param length - The maximum length
|
29
|
+
* @param suffix - The suffix to add (default: '...')
|
30
|
+
* @returns The truncated string
|
31
|
+
*/
|
32
|
+
export declare const truncate: (str: string, length: number, suffix?: string) => string;
|
33
|
+
/**
|
34
|
+
* Removes leading and trailing whitespace and reduces multiple spaces to single space
|
35
|
+
* @param str - The string to clean
|
36
|
+
* @returns The cleaned string
|
37
|
+
*/
|
38
|
+
export declare const cleanWhitespace: (str: string) => string;
|
39
|
+
/**
|
40
|
+
* Checks if a string is a valid email address
|
41
|
+
* @param email - The email string to validate
|
42
|
+
* @returns True if valid email, false otherwise
|
43
|
+
*/
|
44
|
+
export declare const isValidEmail: (email: string) => boolean;
|
45
|
+
/**
|
46
|
+
* Generates a random string of specified length
|
47
|
+
* @param length - The length of the string to generate
|
48
|
+
* @param chars - The characters to use (default: alphanumeric)
|
49
|
+
* @returns A random string
|
50
|
+
*/
|
51
|
+
export declare const randomString: (length: number, chars?: string) => string;
|
52
|
+
/**
|
53
|
+
* Pluralizes a word based on count
|
54
|
+
* @param word - The word to pluralize
|
55
|
+
* @param count - The count to check
|
56
|
+
* @param plural - Custom plural form (optional)
|
57
|
+
* @returns The singular or plural form
|
58
|
+
*/
|
59
|
+
export declare const pluralize: (word: string, count: number, plural?: string) => string;
|
@@ -0,0 +1,108 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.pluralize = exports.randomString = exports.isValidEmail = exports.cleanWhitespace = exports.truncate = exports.toSnakeCase = exports.toKebabCase = exports.toCamelCase = exports.capitalize = void 0;
|
4
|
+
/**
|
5
|
+
* Capitalizes the first letter of a string
|
6
|
+
* @param str - The string to capitalize
|
7
|
+
* @returns The string with first letter capitalized
|
8
|
+
*/
|
9
|
+
const capitalize = (str) => {
|
10
|
+
if (!str)
|
11
|
+
return str;
|
12
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
13
|
+
};
|
14
|
+
exports.capitalize = capitalize;
|
15
|
+
/**
|
16
|
+
* Converts a string to camelCase
|
17
|
+
* @param str - The string to convert
|
18
|
+
* @returns The camelCase string
|
19
|
+
*/
|
20
|
+
const toCamelCase = (str) => {
|
21
|
+
return str
|
22
|
+
.replace(/[-_\s]+(.)?/g, (_, c) => c ? c.toUpperCase() : '')
|
23
|
+
.replace(/^[A-Z]/, (c) => c.toLowerCase());
|
24
|
+
};
|
25
|
+
exports.toCamelCase = toCamelCase;
|
26
|
+
/**
|
27
|
+
* Converts a string to kebab-case
|
28
|
+
* @param str - The string to convert
|
29
|
+
* @returns The kebab-case string
|
30
|
+
*/
|
31
|
+
const toKebabCase = (str) => {
|
32
|
+
return str
|
33
|
+
.replace(/([a-z0-9])([A-Z])/g, '$1-$2')
|
34
|
+
.replace(/\s+/g, '-')
|
35
|
+
.toLowerCase();
|
36
|
+
};
|
37
|
+
exports.toKebabCase = toKebabCase;
|
38
|
+
/**
|
39
|
+
* Converts a string to snake_case
|
40
|
+
* @param str - The string to convert
|
41
|
+
* @returns The snake_case string
|
42
|
+
*/
|
43
|
+
const toSnakeCase = (str) => {
|
44
|
+
return str
|
45
|
+
.replace(/([a-z0-9])([A-Z])/g, '$1_$2')
|
46
|
+
.replace(/\s+/g, '_')
|
47
|
+
.toLowerCase();
|
48
|
+
};
|
49
|
+
exports.toSnakeCase = toSnakeCase;
|
50
|
+
/**
|
51
|
+
* Truncates a string to a specified length and adds ellipsis
|
52
|
+
* @param str - The string to truncate
|
53
|
+
* @param length - The maximum length
|
54
|
+
* @param suffix - The suffix to add (default: '...')
|
55
|
+
* @returns The truncated string
|
56
|
+
*/
|
57
|
+
const truncate = (str, length, suffix = '...') => {
|
58
|
+
if (str.length <= length)
|
59
|
+
return str;
|
60
|
+
return str.substring(0, length - suffix.length) + suffix;
|
61
|
+
};
|
62
|
+
exports.truncate = truncate;
|
63
|
+
/**
|
64
|
+
* Removes leading and trailing whitespace and reduces multiple spaces to single space
|
65
|
+
* @param str - The string to clean
|
66
|
+
* @returns The cleaned string
|
67
|
+
*/
|
68
|
+
const cleanWhitespace = (str) => {
|
69
|
+
return str.trim().replace(/\s+/g, ' ');
|
70
|
+
};
|
71
|
+
exports.cleanWhitespace = cleanWhitespace;
|
72
|
+
/**
|
73
|
+
* Checks if a string is a valid email address
|
74
|
+
* @param email - The email string to validate
|
75
|
+
* @returns True if valid email, false otherwise
|
76
|
+
*/
|
77
|
+
const isValidEmail = (email) => {
|
78
|
+
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
79
|
+
return emailRegex.test(email);
|
80
|
+
};
|
81
|
+
exports.isValidEmail = isValidEmail;
|
82
|
+
/**
|
83
|
+
* Generates a random string of specified length
|
84
|
+
* @param length - The length of the string to generate
|
85
|
+
* @param chars - The characters to use (default: alphanumeric)
|
86
|
+
* @returns A random string
|
87
|
+
*/
|
88
|
+
const randomString = (length, chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') => {
|
89
|
+
let result = '';
|
90
|
+
for (let i = 0; i < length; i++) {
|
91
|
+
result += chars.charAt(Math.floor(Math.random() * chars.length));
|
92
|
+
}
|
93
|
+
return result;
|
94
|
+
};
|
95
|
+
exports.randomString = randomString;
|
96
|
+
/**
|
97
|
+
* Pluralizes a word based on count
|
98
|
+
* @param word - The word to pluralize
|
99
|
+
* @param count - The count to check
|
100
|
+
* @param plural - Custom plural form (optional)
|
101
|
+
* @returns The singular or plural form
|
102
|
+
*/
|
103
|
+
const pluralize = (word, count, plural) => {
|
104
|
+
if (count === 1)
|
105
|
+
return word;
|
106
|
+
return plural || word + 's';
|
107
|
+
};
|
108
|
+
exports.pluralize = pluralize;
|
package/package.json
CHANGED
@@ -1,19 +1,32 @@
|
|
1
1
|
{
|
2
2
|
"name": "@arnonsang/utils",
|
3
|
-
"version": "0.0.
|
3
|
+
"version": "0.0.3",
|
4
4
|
"description": "A collection of utility functions",
|
5
5
|
"main": "dist/index.js",
|
6
6
|
"types": "dist/index.d.ts",
|
7
7
|
"license": "MIT",
|
8
8
|
"author": "arnonsang",
|
9
|
+
"repository": {
|
10
|
+
"type": "git",
|
11
|
+
"url": "https://github.com/arnonsang/utils.git"
|
12
|
+
},
|
13
|
+
"homepage": "https://github.com/arnonsang/utils#readme",
|
14
|
+
"bugs": {
|
15
|
+
"url": "https://github.com/arnonsang/utils/issues"
|
16
|
+
},
|
9
17
|
"scripts": {
|
10
18
|
"prepare": "tsc",
|
11
19
|
"build": "tsc",
|
12
20
|
"patch": "npm version patch && npm publish --access public ",
|
13
|
-
"test": "
|
21
|
+
"test": "jest",
|
22
|
+
"test:watch": "jest --watch",
|
23
|
+
"test:coverage": "jest --coverage"
|
14
24
|
},
|
15
25
|
"devDependencies": {
|
26
|
+
"@types/jest": "^30.0.0",
|
16
27
|
"@types/node": "^22.13.1",
|
28
|
+
"jest": "^30.0.3",
|
29
|
+
"ts-jest": "^29.4.0",
|
17
30
|
"typescript": "^5.7.3"
|
18
31
|
}
|
19
32
|
}
|
package/index.js
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
-
if (k2 === undefined) k2 = k;
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
-
}
|
8
|
-
Object.defineProperty(o, k2, desc);
|
9
|
-
}) : (function(o, m, k, k2) {
|
10
|
-
if (k2 === undefined) k2 = k;
|
11
|
-
o[k2] = m[k];
|
12
|
-
}));
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
|
-
};
|
16
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
17
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
18
|
-
};
|
19
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
20
|
-
exports.logger = void 0;
|
21
|
-
__exportStar(require("./utils/chat"), exports);
|
22
|
-
var logger_1 = require("./utils/logger");
|
23
|
-
Object.defineProperty(exports, "logger", { enumerable: true, get: function () { return __importDefault(logger_1).default; } });
|
package/index.ts
DELETED
package/tsconfig.json
DELETED
package/utils/chat.js
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.charAtFrom = exports.charAt = void 0;
|
4
|
-
/**
|
5
|
-
* Returns the character in the alphabet at the given index (0-based).
|
6
|
-
* @param index index of the character in the alphabet
|
7
|
-
* @param type type of character to return (lowercase or uppercase)
|
8
|
-
* @returns character at the given index
|
9
|
-
*/
|
10
|
-
const charAt = (index, type = "lower") => {
|
11
|
-
const charset = "abcdefghijklmnopqrstuvwxyz";
|
12
|
-
if (index < 0 || index >= charset.length) {
|
13
|
-
return "";
|
14
|
-
}
|
15
|
-
return type === "upper"
|
16
|
-
? charset.charAt(index).toUpperCase()
|
17
|
-
: charset.charAt(index);
|
18
|
-
};
|
19
|
-
exports.charAt = charAt;
|
20
|
-
/**
|
21
|
-
* Returns the character at the given index in the string.
|
22
|
-
* @param str input string
|
23
|
-
* @param index index of the character to return
|
24
|
-
* @returns character at the given index
|
25
|
-
*/
|
26
|
-
const charAtFrom = (str, index) => {
|
27
|
-
return str.charAt(index);
|
28
|
-
};
|
29
|
-
exports.charAtFrom = charAtFrom;
|
package/utils/chat.ts
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* Returns the character in the alphabet at the given index (0-based).
|
3
|
-
* @param index index of the character in the alphabet
|
4
|
-
* @param type type of character to return (lowercase or uppercase)
|
5
|
-
* @returns character at the given index
|
6
|
-
*/
|
7
|
-
const charAt = (index: number, type: "lower" | "upper" = "lower") => {
|
8
|
-
const charset = "abcdefghijklmnopqrstuvwxyz";
|
9
|
-
|
10
|
-
if (index < 0 || index >= charset.length) {
|
11
|
-
return "";
|
12
|
-
}
|
13
|
-
return type === "upper"
|
14
|
-
? charset.charAt(index).toUpperCase()
|
15
|
-
: charset.charAt(index);
|
16
|
-
};
|
17
|
-
|
18
|
-
/**
|
19
|
-
* Returns the character at the given index in the string.
|
20
|
-
* @param str input string
|
21
|
-
* @param index index of the character to return
|
22
|
-
* @returns character at the given index
|
23
|
-
*/
|
24
|
-
const charAtFrom = (str: string, index: number) => {
|
25
|
-
return str.charAt(index);
|
26
|
-
};
|
27
|
-
|
28
|
-
export { charAt, charAtFrom };
|
package/utils/logger.js
DELETED
@@ -1,40 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
class Logger {
|
4
|
-
constructor() {
|
5
|
-
this.isDev = process.env.NODE_ENV === "development" || !process.env.NODE_ENV;
|
6
|
-
}
|
7
|
-
logMessage(type, message, color, ...args) {
|
8
|
-
if (!this.isDev)
|
9
|
-
return;
|
10
|
-
const styles = `color: ${color}; font-weight: bold;`;
|
11
|
-
console[type](`%c[${type.toUpperCase()}] %c${message}`, styles, "color:white;", ...args);
|
12
|
-
}
|
13
|
-
success(message, ...args) {
|
14
|
-
this.logMessage("info", message, "#28a745", ...args);
|
15
|
-
}
|
16
|
-
info(message, ...args) {
|
17
|
-
this.logMessage("info", message, "#007bff", ...args);
|
18
|
-
}
|
19
|
-
warn(message, ...args) {
|
20
|
-
this.logMessage("warn", message, "#ffc107", ...args);
|
21
|
-
}
|
22
|
-
error(message, ...args) {
|
23
|
-
this.logMessage("error", message, "#dc3545", ...args);
|
24
|
-
}
|
25
|
-
debug(message, ...args) {
|
26
|
-
this.logMessage("debug", message, "#28a745", ...args);
|
27
|
-
}
|
28
|
-
log(message, ...args) {
|
29
|
-
this.logMessage("log", message, "white", ...args);
|
30
|
-
}
|
31
|
-
table(data, from) {
|
32
|
-
if (!this.isDev)
|
33
|
-
return;
|
34
|
-
console.log(`%c[DATA TABLE] %c${from || ""}`, "color: #007bff; font-weight: bold;", "color:white;");
|
35
|
-
console.table(data);
|
36
|
-
}
|
37
|
-
}
|
38
|
-
// Export an instance
|
39
|
-
const logger = new Logger();
|
40
|
-
exports.default = logger;
|
package/utils/logger.ts
DELETED
@@ -1,55 +0,0 @@
|
|
1
|
-
type LogType = "info" | "warn" | "error" | "debug" | "log" | "table";
|
2
|
-
|
3
|
-
class Logger {
|
4
|
-
private isDev: boolean;
|
5
|
-
|
6
|
-
constructor() {
|
7
|
-
this.isDev = process.env.NODE_ENV === "development" || !process.env.NODE_ENV;
|
8
|
-
}
|
9
|
-
|
10
|
-
private logMessage(
|
11
|
-
type: LogType,
|
12
|
-
message: string,
|
13
|
-
color: string,
|
14
|
-
...args: unknown[]
|
15
|
-
): void {
|
16
|
-
if (!this.isDev) return;
|
17
|
-
|
18
|
-
const styles = `color: ${color}; font-weight: bold;`;
|
19
|
-
console[type](`%c[${type.toUpperCase()}] %c${message}`, styles, "color:white;", ...args);
|
20
|
-
}
|
21
|
-
|
22
|
-
success(message: string, ...args: unknown[]): void {
|
23
|
-
this.logMessage("info", message, "#28a745", ...args);
|
24
|
-
}
|
25
|
-
|
26
|
-
info(message: string, ...args: unknown[]): void {
|
27
|
-
this.logMessage("info", message, "#007bff", ...args);
|
28
|
-
}
|
29
|
-
|
30
|
-
warn(message: string, ...args: unknown[]): void {
|
31
|
-
this.logMessage("warn", message, "#ffc107", ...args);
|
32
|
-
}
|
33
|
-
|
34
|
-
error(message: string, ...args: unknown[]): void {
|
35
|
-
this.logMessage("error", message, "#dc3545", ...args);
|
36
|
-
}
|
37
|
-
|
38
|
-
debug(message: string, ...args: unknown[]): void {
|
39
|
-
this.logMessage("debug", message, "#28a745", ...args);
|
40
|
-
}
|
41
|
-
|
42
|
-
log(message: string, ...args: unknown[]): void {
|
43
|
-
this.logMessage("log", message, "white", ...args);
|
44
|
-
}
|
45
|
-
|
46
|
-
table(data: unknown, from?: string): void {
|
47
|
-
if (!this.isDev) return;
|
48
|
-
console.log(`%c[DATA TABLE] %c${from || ""}`, "color: #007bff; font-weight: bold;", "color:white;");
|
49
|
-
console.table(data);
|
50
|
-
}
|
51
|
-
}
|
52
|
-
|
53
|
-
// Export an instance
|
54
|
-
const logger = new Logger();
|
55
|
-
export default logger;
|
File without changes
|
File without changes
|