Skip to content

Commit 11804e1

Browse files
committed
chore: add prettier config and format utils
1 parent c9877b3 commit 11804e1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+2548
-3167
lines changed

.prettierrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"singleQuote": true,
3+
"arrowParens": "avoid",
4+
"printWidth": 120,
5+
"bracketSpacing": true,
6+
"trailingComma": "none"
7+
}

src/config.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
1-
import { Env } from "./utils/env.utils";
1+
import { Env } from './utils/env.utils';
22

3-
type LogLevel =
4-
| "fatal"
5-
| "error"
6-
| "warn"
7-
| "info"
8-
| "debug"
9-
| "trace"
10-
| "silent";
3+
type LogLevel = 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace' | 'silent';
114

125
/**
136
* Application runtime configuration loaded from environment variables.
@@ -17,30 +10,30 @@ export const Config = {
1710
/**
1811
* Logging level (e.g., 'info', 'debug', 'warn', 'error').
1912
*/
20-
level: Env.get("LOGGER_LEVEL", "info") as LogLevel,
13+
level: Env.get('LOGGER_LEVEL', 'info') as LogLevel,
2114

2215
/**
2316
* Name of the logger instance (defaults to npm package name).
2417
*/
25-
name: Env.get("LOGGER_NAME", Env.get("npm_package_name", "@catbee/utils")),
18+
name: Env.get('LOGGER_NAME', Env.get('npm_package_name', '@catbee/utils')),
2619

2720
/**
2821
* Whether to use ISO 8601 timestamps in logs.
2922
*/
30-
isoTimestamp: Env.getBoolean("LOGGER_ISO_TIMESTAMP", false),
23+
isoTimestamp: Env.getBoolean('LOGGER_ISO_TIMESTAMP', false)
3124
},
3225

3326
Http: {
3427
/**
3528
* Timeout for HTTP requests in milliseconds
3629
*/
37-
timeout: Env.getNumber("HTTP_TIMEOUT", 30000),
30+
timeout: Env.getNumber('HTTP_TIMEOUT', 30000)
3831
},
3932

4033
Cache: {
4134
/**
4235
* Default TTL (time to live) for cache entries in seconds
4336
*/
44-
defaultTtl: Env.getNumber("CACHE_DEFAULT_TTL", 300),
45-
},
37+
defaultTtl: Env.getNumber('CACHE_DEFAULT_TTL', 300)
38+
}
4639
};

src/index.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
export * from "./utils/array.utils";
2-
export * from "./utils/async.utils";
3-
export * from "./utils/cache.utils";
4-
export * from "./utils/context-store.utils";
5-
export * from "./utils/crypto.utils";
6-
export * from "./utils/decorators.utils";
7-
export * from "./utils/dir.utils";
8-
export * from "./utils/env.utils";
9-
export * from "./utils/exception.utils";
10-
export * from "./utils/fs.utils";
11-
export * from "./utils/http-status-codes";
12-
export * from "./utils/id.utils";
13-
export * from "./utils/logger.utils";
14-
export * from "./utils/middleware.utils";
15-
export * from "./utils/obj.utils";
16-
export * from "./utils/response.utils";
17-
export * from "./utils/string.utils";
18-
export * from "./utils/url.utils";
19-
export * from "./utils/validate.utils";
1+
export * from './utils/array.utils';
2+
export * from './utils/async.utils';
3+
export * from './utils/cache.utils';
4+
export * from './utils/context-store.utils';
5+
export * from './utils/crypto.utils';
6+
export * from './utils/decorators.utils';
7+
export * from './utils/dir.utils';
8+
export * from './utils/env.utils';
9+
export * from './utils/exception.utils';
10+
export * from './utils/fs.utils';
11+
export * from './utils/http-status-codes';
12+
export * from './utils/id.utils';
13+
export * from './utils/logger.utils';
14+
export * from './utils/middleware.utils';
15+
export * from './utils/obj.utils';
16+
export * from './utils/response.utils';
17+
export * from './utils/string.utils';
18+
export * from './utils/url.utils';
19+
export * from './utils/validate.utils';
2020

21-
export * from "./types/api-response";
21+
export * from './types/api-response';

src/types/api-response.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export interface Pagination<T = any> {
4444
sortBy: string;
4545

4646
/** Sort order: ascending or descending. */
47-
sortOrder: "asc" | "desc";
47+
sortOrder: 'asc' | 'desc';
4848
};
4949
}
5050

@@ -58,7 +58,7 @@ export type PaginationResponse<T = any> = Pagination<T>;
5858
* Error response structure with additional metadata.
5959
* Used for providing richer error information to clients.
6060
*/
61-
export interface ApiErrorResponse extends Omit<ApiResponse<never>, "data"> {
61+
export interface ApiErrorResponse extends Omit<ApiResponse<never>, 'data'> {
6262
/** Error always true for error responses */
6363
error: true;
6464

src/utils/array.utils.ts

Lines changed: 32 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getValueByPath } from "./obj.utils";
1+
import { getValueByPath } from './obj.utils';
22

33
/**
44
* Splits an array into chunks of the specified size.
@@ -11,13 +11,10 @@ import { getValueByPath } from "./obj.utils";
1111
* @throws {Error} If chunk size is not a positive integer.
1212
*/
1313
export function chunk<T>(array: T[], size: number): T[][] {
14-
if (!Array.isArray(array)) throw new TypeError("Expected an array");
14+
if (!Array.isArray(array)) throw new TypeError('Expected an array');
1515
if (!array.length) return [];
16-
if (!Number.isInteger(size) || size <= 0)
17-
throw new Error("Chunk size must be a positive integer");
18-
return Array.from({ length: Math.ceil(array.length / size) }, (_, i) =>
19-
array.slice(i * size, i * size + size),
20-
);
16+
if (!Number.isInteger(size) || size <= 0) throw new Error('Chunk size must be a positive integer');
17+
return Array.from({ length: Math.ceil(array.length / size) }, (_, i) => array.slice(i * size, i * size + size));
2118
}
2219

2320
/**
@@ -33,7 +30,7 @@ export function unique<T>(array: T[], keyFn?: (item: T) => unknown): T[] {
3330
if (!Array.isArray(array) || array.length === 0) return [];
3431
if (!keyFn) return Array.from(new Set(array));
3532
const seen = new Set<unknown>();
36-
return array.filter((item) => {
33+
return array.filter(item => {
3734
const key = keyFn(item);
3835
if (seen.has(key)) return false;
3936
seen.add(key);
@@ -90,27 +87,21 @@ export function random<T>(array: T[]): T | undefined {
9087
* @returns {Record<string | number | symbol, T[]>} An object mapping group keys to item arrays.
9188
*/
9289
export function groupBy<T>(array: T[], key: keyof T): Record<string, T[]>;
93-
export function groupBy<T, K extends string | number | symbol>(
94-
array: T[],
95-
keyFn: (item: T) => K,
96-
): Record<K, T[]>;
90+
export function groupBy<T, K extends string | number | symbol>(array: T[], keyFn: (item: T) => K): Record<K, T[]>;
9791
export function groupBy<T>(
9892
array: T[],
99-
keyOrFn: keyof T | ((item: T) => string | number | symbol),
93+
keyOrFn: keyof T | ((item: T) => string | number | symbol)
10094
): Record<string | number | symbol, T[]> {
10195
if (!Array.isArray(array) || array.length === 0) return {};
102-
const keyFn =
103-
typeof keyOrFn === "function"
104-
? keyOrFn
105-
: (item: T) => item[keyOrFn as keyof T];
96+
const keyFn = typeof keyOrFn === 'function' ? keyOrFn : (item: T) => item[keyOrFn as keyof T];
10697
return array.reduce(
10798
(acc, item) => {
10899
const key = keyFn(item) as string | number | symbol;
109100
if (!acc[key]) acc[key] = [];
110101
acc[key].push(item);
111102
return acc;
112103
},
113-
{} as Record<string | number | symbol, T[]>,
104+
{} as Record<string | number | symbol, T[]>
114105
);
115106
}
116107
/* eslint-enable no-redeclare */
@@ -124,7 +115,7 @@ export function groupBy<T>(
124115
* @throws {TypeError} If array is not an array.
125116
*/
126117
export function shuffle<T>(array: T[]): T[] {
127-
if (!Array.isArray(array)) throw new TypeError("Expected an array");
118+
if (!Array.isArray(array)) throw new TypeError('Expected an array');
128119
const copy = array.slice();
129120
for (let i = copy.length - 1; i > 0; i--) {
130121
const j = Math.floor(Math.random() * (i + 1));
@@ -144,7 +135,7 @@ export function shuffle<T>(array: T[]): T[] {
144135
*/
145136
export function pluck<T, K extends keyof T>(array: T[], key: K): T[K][] {
146137
if (!Array.isArray(array)) return [];
147-
return array.map((item) => item[key]);
138+
return array.map(item => item[key]);
148139
}
149140

150141
/**
@@ -158,7 +149,7 @@ export function pluck<T, K extends keyof T>(array: T[], key: K): T[K][] {
158149
export function difference<T>(a: T[], b: T[]): T[] {
159150
if (!Array.isArray(a) || !Array.isArray(b)) return [];
160151
const setB = new Set(b);
161-
return a.filter((item) => !setB.has(item));
152+
return a.filter(item => !setB.has(item));
162153
}
163154

164155
/**
@@ -172,7 +163,7 @@ export function difference<T>(a: T[], b: T[]): T[] {
172163
export function intersect<T>(a: T[], b: T[]): T[] {
173164
if (!Array.isArray(a) || !Array.isArray(b)) return [];
174165
const setB = new Set(b);
175-
return a.filter((item) => setB.has(item));
166+
return a.filter(item => setB.has(item));
176167
}
177168

178169
/**
@@ -186,27 +177,20 @@ export function intersect<T>(a: T[], b: T[]): T[] {
186177
* @returns {T[]} A new sorted array.
187178
* @throws {TypeError} If array is not an array.
188179
*/
189-
export function mergeSort<T>(
190-
array: T[],
191-
key: string | ((item: T) => any),
192-
direction: "asc" | "desc" = "asc",
193-
): T[] {
194-
if (!Array.isArray(array)) throw new TypeError("Expected array");
180+
export function mergeSort<T>(array: T[], key: string | ((item: T) => any), direction: 'asc' | 'desc' = 'asc'): T[] {
181+
if (!Array.isArray(array)) throw new TypeError('Expected array');
195182
if (array.length <= 1) return array.slice();
196183

197-
const keyFn =
198-
typeof key === "function"
199-
? key
200-
: (item: T) => getValueByPath(item as object, key);
184+
const keyFn = typeof key === 'function' ? key : (item: T) => getValueByPath(item as object, key);
201185

202186
const compare = (a: T, b: T) => {
203187
const aVal = keyFn(a);
204188
const bVal = keyFn(b);
205189
// Sorts undefined/null last for "asc", first for "desc"
206190
if (aVal === bVal) return 0;
207-
if (aVal == null) return direction === "asc" ? 1 : -1;
208-
if (bVal == null) return direction === "asc" ? -1 : 1;
209-
return direction === "asc" ? (aVal < bVal ? -1 : 1) : aVal > bVal ? -1 : 1;
191+
if (aVal == null) return direction === 'asc' ? 1 : -1;
192+
if (bVal == null) return direction === 'asc' ? -1 : 1;
193+
return direction === 'asc' ? (aVal < bVal ? -1 : 1) : aVal > bVal ? -1 : 1;
210194
};
211195

212196
const merge = (left: T[], right: T[]): T[] => {
@@ -240,15 +224,15 @@ export function mergeSort<T>(
240224
*/
241225
export function zip<T>(...arrays: T[][]): T[][] {
242226
if (arrays.length === 0) return [];
243-
if (arrays.some((arr) => !Array.isArray(arr))) {
244-
throw new TypeError("All arguments must be arrays");
227+
if (arrays.some(arr => !Array.isArray(arr))) {
228+
throw new TypeError('All arguments must be arrays');
245229
}
246230

247-
const minLength = Math.min(...arrays.map((arr) => arr.length));
231+
const minLength = Math.min(...arrays.map(arr => arr.length));
248232
const result: T[][] = [];
249233

250234
for (let i = 0; i < minLength; i++) {
251-
result.push(arrays.map((arr) => arr[i]));
235+
result.push(arrays.map(arr => arr[i]));
252236
}
253237

254238
return result;
@@ -262,19 +246,14 @@ export function zip<T>(...arrays: T[][]): T[][] {
262246
* @param {(item: T, index: number, array: T[]) => boolean} predicate - Function to test each element.
263247
* @returns {[T[], T[]]} A tuple of two arrays: [matched, unmatched].
264248
*/
265-
export function partition<T>(
266-
array: T[],
267-
predicate: (item: T, index: number, array: T[]) => boolean,
268-
): [T[], T[]] {
249+
export function partition<T>(array: T[], predicate: (item: T, index: number, array: T[]) => boolean): [T[], T[]] {
269250
if (!Array.isArray(array)) return [[], []];
270251

271252
return array.reduce(
272253
([pass, fail], item, index) => {
273-
return predicate(item, index, array)
274-
? [[...pass, item], fail]
275-
: [pass, [...fail, item]];
254+
return predicate(item, index, array) ? [[...pass, item], fail] : [pass, [...fail, item]];
276255
},
277-
[[] as T[], [] as T[]],
256+
[[] as T[], [] as T[]]
278257
);
279258
}
280259

@@ -287,14 +266,10 @@ export function partition<T>(
287266
* @returns {number[]} Array of numbers in range.
288267
*/
289268
export function range(start: number, end: number, step: number = 1): number[] {
290-
if (
291-
!Number.isFinite(start) ||
292-
!Number.isFinite(end) ||
293-
!Number.isFinite(step)
294-
) {
295-
throw new TypeError("Arguments must be finite numbers");
269+
if (!Number.isFinite(start) || !Number.isFinite(end) || !Number.isFinite(step)) {
270+
throw new TypeError('Arguments must be finite numbers');
296271
}
297-
if (step === 0) throw new Error("Step cannot be zero");
272+
if (step === 0) throw new Error('Step cannot be zero');
298273

299274
const isAscending = step > 0;
300275
if ((isAscending && start >= end) || (!isAscending && start <= end)) {
@@ -332,10 +307,7 @@ export function take<T>(array: T[], n: number = 1): T[] {
332307
* @param {(item: T, index: number) => boolean} predicate - Function to test each element.
333308
* @returns {T[]} New array with taken elements.
334309
*/
335-
export function takeWhile<T>(
336-
array: T[],
337-
predicate: (item: T, index: number) => boolean,
338-
): T[] {
310+
export function takeWhile<T>(array: T[], predicate: (item: T, index: number) => boolean): T[] {
339311
if (!Array.isArray(array)) return [];
340312

341313
const result: T[] = [];
@@ -368,10 +340,7 @@ export function compact<T>(array: T[]): NonNullable<T>[] {
368340
* @param {(item: T) => string | number | symbol} keyFn - Function to generate count key.
369341
* @returns {Record<string, number>} Object with counts by key.
370342
*/
371-
export function countBy<T>(
372-
array: T[],
373-
keyFn: (item: T) => string | number | symbol,
374-
): Record<string, number> {
343+
export function countBy<T>(array: T[], keyFn: (item: T) => string | number | symbol): Record<string, number> {
375344
if (!Array.isArray(array)) return {};
376345

377346
return array.reduce(
@@ -380,6 +349,6 @@ export function countBy<T>(
380349
acc[key] = (acc[key] || 0) + 1;
381350
return acc;
382351
},
383-
{} as Record<string, number>,
352+
{} as Record<string, number>
384353
);
385354
}

0 commit comments

Comments
 (0)