@catbee/utils 2.0.0-next.0 → 2.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 +1 -1
- package/README.md +102 -52
- package/array/index.cjs +215 -74
- package/array/index.d.ts +345 -2
- package/array/index.mjs +201 -74
- package/async/index.cjs +116 -39
- package/async/index.d.ts +292 -2
- package/async/index.mjs +116 -40
- package/cache/index.cjs +2 -2
- package/cache/index.d.ts +156 -2
- package/cache/index.mjs +3 -3
- package/config/index.cjs +80 -66
- package/config/index.d.ts +65 -3
- package/config/index.mjs +77 -65
- package/context-store/index.cjs +2 -3
- package/context-store/index.d.ts +193 -2
- package/context-store/index.mjs +2 -3
- package/crypto/index.cjs +55 -5
- package/crypto/index.d.ts +225 -2
- package/crypto/index.mjs +52 -7
- package/date/index.cjs +676 -2
- package/date/index.d.ts +676 -2
- package/date/index.mjs +665 -3
- package/decorator/index.cjs +2172 -0
- package/{decorators/decorators.utils.d.ts → decorator/index.d.ts} +58 -54
- package/decorator/index.mjs +2131 -0
- package/{dir → directory}/index.cjs +5 -4
- package/{dir/dir.utils.d.ts → directory/index.d.ts} +24 -21
- package/{dir → directory}/index.mjs +5 -4
- package/env/index.cjs +100 -68
- package/env/index.d.ts +391 -2
- package/env/index.mjs +100 -68
- package/exception/index.cjs +1 -1
- package/exception/index.d.ts +233 -2
- package/exception/index.mjs +1 -1
- package/fs/index.cjs +71 -37
- package/fs/index.d.ts +206 -2
- package/fs/index.mjs +65 -35
- package/http-status-codes/index.cjs +1 -1
- package/http-status-codes/index.d.ts +268 -2
- package/http-status-codes/index.mjs +1 -1
- package/id/index.cjs +1 -1
- package/id/index.d.ts +38 -2
- package/id/index.mjs +1 -1
- package/index.cjs +13 -13
- package/index.d.ts +5 -5
- package/index.mjs +5 -5
- package/logger/index.cjs +13 -15
- package/logger/index.d.ts +190 -2
- package/logger/index.mjs +14 -16
- package/middleware/index.cjs +1 -1
- package/middleware/index.d.ts +104 -2
- package/middleware/index.mjs +1 -1
- package/object/index.cjs +379 -0
- package/{obj/obj.utils.d.ts → object/index.d.ts} +73 -33
- package/object/index.mjs +360 -0
- package/package.json +41 -23
- package/performance/index.cjs +4 -4
- package/performance/index.d.ts +139 -2
- package/performance/index.mjs +4 -4
- package/request/index.cjs +37 -25
- package/request/index.d.ts +242 -3
- package/request/index.mjs +37 -25
- package/response/index.cjs +1 -1
- package/response/index.d.ts +319 -3
- package/response/index.mjs +1 -1
- package/server/index.cjs +249 -146
- package/server/index.d.ts +866 -5
- package/server/index.mjs +248 -144
- package/stream/index.cjs +1 -1
- package/stream/index.d.ts +91 -2
- package/stream/index.mjs +1 -1
- package/string/index.cjs +34 -1
- package/string/index.d.ts +146 -2
- package/string/index.mjs +31 -2
- package/type/index.cjs +19 -2
- package/type/index.d.ts +144 -2
- package/type/index.mjs +17 -3
- package/types/index.cjs +1 -1
- package/types/index.d.ts +775 -5
- package/types/index.mjs +1 -1
- package/url/index.cjs +63 -5
- package/url/index.d.ts +200 -2
- package/url/index.mjs +59 -6
- package/{validate → validation}/index.cjs +91 -44
- package/{validate/validate.utils.d.ts → validation/index.d.ts} +33 -24
- package/{validate → validation}/index.mjs +87 -44
- package/array/array.utils.d.ts +0 -191
- package/async/async.utils.d.ts +0 -296
- package/cache/cache.utils.d.ts +0 -176
- package/config/config.d.ts +0 -57
- package/context-store/context-store.utils.d.ts +0 -212
- package/crypto/crypto.utils.d.ts +0 -183
- package/date/date.utils.d.ts +0 -190
- package/decorators/index.cjs +0 -913
- package/decorators/index.d.ts +0 -25
- package/decorators/index.mjs +0 -872
- package/dir/index.d.ts +0 -25
- package/env/env.utils.d.ts +0 -400
- package/exception/exception.utils.d.ts +0 -253
- package/fs/fs.utils.d.ts +0 -196
- package/http-status-codes/http-status-codes.d.ts +0 -289
- package/id/id.utils.d.ts +0 -59
- package/logger/logger.utils.d.ts +0 -210
- package/middleware/middleware.utils.d.ts +0 -123
- package/obj/index.cjs +0 -317
- package/obj/index.d.ts +0 -25
- package/obj/index.mjs +0 -301
- package/performance/performance.utils.d.ts +0 -159
- package/request/request.utils.d.ts +0 -109
- package/response/response.utils.d.ts +0 -186
- package/server/server.builder.d.ts +0 -531
- package/server/server.d.ts +0 -303
- package/stream/stream.utils.d.ts +0 -111
- package/string/string.utils.d.ts +0 -124
- package/type/type.utils.d.ts +0 -129
- package/types/api-response.d.ts +0 -175
- package/types/common.d.ts +0 -148
- package/types/config.d.ts +0 -88
- package/types/server.d.ts +0 -291
- package/url/url.utils.d.ts +0 -164
- package/validate/index.d.ts +0 -25
package/stream/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* The MIT License
|
|
3
3
|
*
|
|
4
|
-
* Copyright (c)
|
|
4
|
+
* Copyright (c) 2026 Catbee Technologies. https://catbee.in/license
|
|
5
5
|
*
|
|
6
6
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
7
|
* of this software and associated documentation files (the "Software"), to deal
|
|
@@ -22,4 +22,93 @@
|
|
|
22
22
|
* SOFTWARE.
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
import { Readable, Transform } from 'node:stream';
|
|
26
|
+
import { BufferEncoding } from '@catbee/utils/crypto';
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Convert a buffer or string to a readable stream.
|
|
30
|
+
*
|
|
31
|
+
* @param data - Buffer or string to convert
|
|
32
|
+
* @returns Readable stream containing the data
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* const stream = bufferToStream(Buffer.from('Hello world'));
|
|
37
|
+
* // or
|
|
38
|
+
* const stream = bufferToStream('Hello world');
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
declare function bufferToStream(data: Buffer | string): Readable;
|
|
42
|
+
/**
|
|
43
|
+
* Convert a readable stream to a buffer.
|
|
44
|
+
*
|
|
45
|
+
* @param stream - Readable stream to convert
|
|
46
|
+
* @returns Promise resolving to a buffer containing all stream data
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* const buffer = await streamToBuffer(fs.createReadStream('file.txt'));
|
|
51
|
+
* console.log(buffer.toString()); // Contents of file.txt
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
declare function streamToBuffer(stream: Readable): Promise<Buffer>;
|
|
55
|
+
/**
|
|
56
|
+
* Convert a readable stream to a string.
|
|
57
|
+
*
|
|
58
|
+
* @param stream - Readable stream to convert
|
|
59
|
+
* @param encoding - Character encoding (default: 'utf8')
|
|
60
|
+
* @returns Promise resolving to a string containing all stream data
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* const content = await streamToString(fs.createReadStream('file.txt'));
|
|
65
|
+
* console.log(content); // Contents of file.txt as string
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
declare function streamToString(stream: Readable, encoding?: BufferEncoding): Promise<string>;
|
|
69
|
+
/**
|
|
70
|
+
* Create a transform stream that limits the rate of data flow.
|
|
71
|
+
*
|
|
72
|
+
* @param bytesPerSecond - Maximum bytes per second
|
|
73
|
+
* @returns Transform stream that throttles data flow
|
|
74
|
+
*/
|
|
75
|
+
declare function createThrottleStream(bytesPerSecond: number): Transform;
|
|
76
|
+
/**
|
|
77
|
+
* Create a transform stream that batches data into chunks of specified size.
|
|
78
|
+
*
|
|
79
|
+
* @param size - Size of each batch (items for object mode, bytes for binary mode)
|
|
80
|
+
* @param options - Stream options
|
|
81
|
+
* @returns Transform stream that batches data
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```typescript
|
|
85
|
+
* // Batch lines from a file into arrays of 100 lines each
|
|
86
|
+
* createReadStream('large-file.txt')
|
|
87
|
+
* .pipe(createLineStream())
|
|
88
|
+
* .pipe(createBatchStream(100))
|
|
89
|
+
* .on('data', batch => console.log(`Processing batch of ${batch.length} lines`));
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
declare function createBatchStream(size: number, options?: {
|
|
93
|
+
objectMode?: boolean;
|
|
94
|
+
}): Transform;
|
|
95
|
+
/**
|
|
96
|
+
* Create a transform stream that splits text data by newlines.
|
|
97
|
+
*
|
|
98
|
+
* @param options - Options for the line stream
|
|
99
|
+
* @returns Transform stream that emits lines
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```typescript
|
|
103
|
+
* // Process a file line by line
|
|
104
|
+
* createReadStream('file.txt')
|
|
105
|
+
* .pipe(createLineStream())
|
|
106
|
+
* .on('data', line => console.log(`Line: ${line}`));
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
declare function createLineStream(options?: {
|
|
110
|
+
encoding?: BufferEncoding;
|
|
111
|
+
includeNewlines?: boolean;
|
|
112
|
+
}): Transform;
|
|
113
|
+
|
|
114
|
+
export { bufferToStream, createBatchStream, createLineStream, createThrottleStream, streamToBuffer, streamToString };
|
package/stream/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* The MIT License
|
|
3
3
|
*
|
|
4
|
-
* Copyright (c)
|
|
4
|
+
* Copyright (c) 2026 Catbee Technologies. https://catbee.in/license
|
|
5
5
|
*
|
|
6
6
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
7
|
* of this software and associated documentation files (the "Software"), to deal
|
package/string/index.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* The MIT License
|
|
3
3
|
*
|
|
4
|
-
* Copyright (c)
|
|
4
|
+
* Copyright (c) 2026 Catbee Technologies. https://catbee.in/license
|
|
5
5
|
*
|
|
6
6
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
7
|
* of this software and associated documentation files (the "Software"), to deal
|
|
@@ -93,10 +93,42 @@ function toTitleCase(str) {
|
|
|
93
93
|
return str.split(/(\s+)/).map((part) => part.trim().length === 0 ? part : part.charAt(0).toUpperCase() + part.slice(1).toLowerCase()).join("");
|
|
94
94
|
}
|
|
95
95
|
__name(toTitleCase, "toTitleCase");
|
|
96
|
+
function escapeRegex(str) {
|
|
97
|
+
if (typeof str !== "string") throw new TypeError("Expected a string");
|
|
98
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
99
|
+
}
|
|
100
|
+
__name(escapeRegex, "escapeRegex");
|
|
101
|
+
function unescapeHtml(str) {
|
|
102
|
+
if (typeof str !== "string") return "";
|
|
103
|
+
const entities = {
|
|
104
|
+
"&": "&",
|
|
105
|
+
"<": "<",
|
|
106
|
+
">": ">",
|
|
107
|
+
""": '"',
|
|
108
|
+
"'": "'",
|
|
109
|
+
"'": "'"
|
|
110
|
+
};
|
|
111
|
+
return str.replace(/&(?:amp|lt|gt|quot|#39|#x27);/g, (match) => entities[match] || match);
|
|
112
|
+
}
|
|
113
|
+
__name(unescapeHtml, "unescapeHtml");
|
|
114
|
+
function isBlank(str) {
|
|
115
|
+
return typeof str === "string" && str.trim().length === 0;
|
|
116
|
+
}
|
|
117
|
+
__name(isBlank, "isBlank");
|
|
118
|
+
function ellipsis(str, maxLength, suffix = "...") {
|
|
119
|
+
if (typeof str !== "string" || str.length <= maxLength) return str;
|
|
120
|
+
const truncated = str.slice(0, maxLength - suffix.length);
|
|
121
|
+
const lastSpace = truncated.lastIndexOf(" ");
|
|
122
|
+
return (lastSpace > 0 ? truncated.slice(0, lastSpace) : truncated) + suffix;
|
|
123
|
+
}
|
|
124
|
+
__name(ellipsis, "ellipsis");
|
|
96
125
|
|
|
97
126
|
exports.capitalize = capitalize;
|
|
98
127
|
exports.countOccurrences = countOccurrences;
|
|
128
|
+
exports.ellipsis = ellipsis;
|
|
99
129
|
exports.equalsIgnoreCase = equalsIgnoreCase;
|
|
130
|
+
exports.escapeRegex = escapeRegex;
|
|
131
|
+
exports.isBlank = isBlank;
|
|
100
132
|
exports.mask = mask;
|
|
101
133
|
exports.reverse = reverse;
|
|
102
134
|
exports.slugify = slugify;
|
|
@@ -107,3 +139,4 @@ exports.toPascalCase = toPascalCase;
|
|
|
107
139
|
exports.toSnakeCase = toSnakeCase;
|
|
108
140
|
exports.toTitleCase = toTitleCase;
|
|
109
141
|
exports.truncate = truncate;
|
|
142
|
+
exports.unescapeHtml = unescapeHtml;
|
package/string/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* The MIT License
|
|
3
3
|
*
|
|
4
|
-
* Copyright (c)
|
|
4
|
+
* Copyright (c) 2026 Catbee Technologies. https://catbee.in/license
|
|
5
5
|
*
|
|
6
6
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
7
|
* of this software and associated documentation files (the "Software"), to deal
|
|
@@ -22,4 +22,148 @@
|
|
|
22
22
|
* SOFTWARE.
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
/**
|
|
26
|
+
* Capitalizes the first character of a string.
|
|
27
|
+
*
|
|
28
|
+
* @param {string} str - The input string.
|
|
29
|
+
* @returns {string} The string with the first character in uppercase.
|
|
30
|
+
*/
|
|
31
|
+
declare function capitalize(str: string): string;
|
|
32
|
+
/**
|
|
33
|
+
* Converts a string to kebab-case (e.g., "FooBar test" -> "foo-bar-test").
|
|
34
|
+
*
|
|
35
|
+
* @param {string} str - The input string.
|
|
36
|
+
* @returns {string} The kebab-cased string.
|
|
37
|
+
*/
|
|
38
|
+
declare function toKebabCase(str: string): string;
|
|
39
|
+
/**
|
|
40
|
+
* Converts a kebab-case or snake_case string to camelCase.
|
|
41
|
+
*
|
|
42
|
+
* @param {string} str - The input string.
|
|
43
|
+
* @returns {string} The camelCased string.
|
|
44
|
+
*/
|
|
45
|
+
declare function toCamelCase(str: string): string;
|
|
46
|
+
/**
|
|
47
|
+
* Converts a string to a URL-friendly slug (lowercase, dashes, alphanumeric).
|
|
48
|
+
*
|
|
49
|
+
* @param {string} str - The input string.
|
|
50
|
+
* @returns {string} The slugified string.
|
|
51
|
+
*/
|
|
52
|
+
declare function slugify(str: string): string;
|
|
53
|
+
/**
|
|
54
|
+
* Truncates a string to a specific length, appending '...' if truncated.
|
|
55
|
+
*
|
|
56
|
+
* @param {string} str - The input string.
|
|
57
|
+
* @param {number} len - The maximum length.
|
|
58
|
+
* @returns {string} The truncated string.
|
|
59
|
+
*/
|
|
60
|
+
declare function truncate(str: string, len: number): string;
|
|
61
|
+
/**
|
|
62
|
+
* Converts a string to PascalCase (e.g., "foo-bar" -> "FooBar").
|
|
63
|
+
*
|
|
64
|
+
* @param {string} str - The input string.
|
|
65
|
+
* @returns {string} The PascalCased string.
|
|
66
|
+
*/
|
|
67
|
+
declare function toPascalCase(str: string): string;
|
|
68
|
+
/**
|
|
69
|
+
* Converts a string to snake_case (e.g., "FooBar test" -> "foo_bar_test").
|
|
70
|
+
*
|
|
71
|
+
* @param {string} str - The input string.
|
|
72
|
+
* @returns {string} The snake_cased string.
|
|
73
|
+
*/
|
|
74
|
+
declare function toSnakeCase(str: string): string;
|
|
75
|
+
/**
|
|
76
|
+
* Masks a string by replacing characters with a mask character.
|
|
77
|
+
* Useful for hiding sensitive information like credit cards or passwords.
|
|
78
|
+
*
|
|
79
|
+
* @param {string} str - The string to mask.
|
|
80
|
+
* @param {number} [visibleStart=0] - Number of characters to show at start.
|
|
81
|
+
* @param {number} [visibleEnd=0] - Number of characters to show at end.
|
|
82
|
+
* @param {string} [maskChar="*"] - Character to use for masking.
|
|
83
|
+
* @returns {string} The masked string.
|
|
84
|
+
*/
|
|
85
|
+
declare function mask(str: string, visibleStart?: number, visibleEnd?: number, maskChar?: string): string;
|
|
86
|
+
/**
|
|
87
|
+
* Removes all HTML tags from a string.
|
|
88
|
+
*
|
|
89
|
+
* @param {string} str - The HTML string to process.
|
|
90
|
+
* @returns {string} The string with HTML tags removed.
|
|
91
|
+
*/
|
|
92
|
+
declare function stripHtml(str: string): string;
|
|
93
|
+
/**
|
|
94
|
+
* Performs case-insensitive string comparison.
|
|
95
|
+
*
|
|
96
|
+
* @param {string} a - First string.
|
|
97
|
+
* @param {string} b - Second string.
|
|
98
|
+
* @returns {boolean} True if the strings are equal ignoring case.
|
|
99
|
+
*/
|
|
100
|
+
declare function equalsIgnoreCase(a: string, b: string): boolean;
|
|
101
|
+
/**
|
|
102
|
+
* Reverses a string.
|
|
103
|
+
*
|
|
104
|
+
* @param {string} str - The string to reverse.
|
|
105
|
+
* @returns {string} The reversed string.
|
|
106
|
+
*/
|
|
107
|
+
declare function reverse(str: string): string;
|
|
108
|
+
/**
|
|
109
|
+
* Counts occurrences of a substring within a string.
|
|
110
|
+
*
|
|
111
|
+
* @param {string} str - The source string.
|
|
112
|
+
* @param {string} substring - The substring to count.
|
|
113
|
+
* @param {boolean} [caseSensitive=true] - Whether to perform case-sensitive counting.
|
|
114
|
+
* @returns {number} Number of occurrences.
|
|
115
|
+
*/
|
|
116
|
+
declare function countOccurrences(str: string, substring: string, caseSensitive?: boolean): number;
|
|
117
|
+
/**
|
|
118
|
+
* Convert a string to Title Case (each word capitalized).
|
|
119
|
+
* Preserves existing spacing and punctuation between words.
|
|
120
|
+
*
|
|
121
|
+
* @param str - Input string
|
|
122
|
+
* @returns Title-cased string
|
|
123
|
+
*/
|
|
124
|
+
declare function toTitleCase(str: string): string;
|
|
125
|
+
/**
|
|
126
|
+
* Escapes special regex characters in a string.
|
|
127
|
+
*
|
|
128
|
+
* @param {string} str - The input string.
|
|
129
|
+
* @returns {string} String with regex characters escaped.
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* escapeRegex('Hello (world)'); // 'Hello \\(world\\)'
|
|
133
|
+
*/
|
|
134
|
+
declare function escapeRegex(str: string): string;
|
|
135
|
+
/**
|
|
136
|
+
* Unescapes HTML entities in a string.
|
|
137
|
+
*
|
|
138
|
+
* @param {string} str - The HTML string.
|
|
139
|
+
* @returns {string} String with HTML entities unescaped.
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* unescapeHtml('<div>Hello</div>'); // '<div>Hello</div>'
|
|
143
|
+
*/
|
|
144
|
+
declare function unescapeHtml(str: string): string;
|
|
145
|
+
/**
|
|
146
|
+
* Checks if a string is blank (empty or only whitespace).
|
|
147
|
+
*
|
|
148
|
+
* @param {string} str - The input string.
|
|
149
|
+
* @returns {boolean} True if blank.
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* isBlank(' '); // true
|
|
153
|
+
* isBlank('hello'); // false
|
|
154
|
+
*/
|
|
155
|
+
declare function isBlank(str: string): boolean;
|
|
156
|
+
/**
|
|
157
|
+
* Truncates a string with an ellipsis, ensuring word boundaries.
|
|
158
|
+
*
|
|
159
|
+
* @param {string} str - The input string.
|
|
160
|
+
* @param {number} maxLength - Maximum length.
|
|
161
|
+
* @param {string} [suffix='...'] - Suffix to append.
|
|
162
|
+
* @returns {string} Truncated string.
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* ellipsis('The quick brown fox', 10); // 'The quick...'
|
|
166
|
+
*/
|
|
167
|
+
declare function ellipsis(str: string, maxLength: number, suffix?: string): string;
|
|
168
|
+
|
|
169
|
+
export { capitalize, countOccurrences, ellipsis, equalsIgnoreCase, escapeRegex, isBlank, mask, reverse, slugify, stripHtml, toCamelCase, toKebabCase, toPascalCase, toSnakeCase, toTitleCase, truncate, unescapeHtml };
|
package/string/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* The MIT License
|
|
3
3
|
*
|
|
4
|
-
* Copyright (c)
|
|
4
|
+
* Copyright (c) 2026 Catbee Technologies. https://catbee.in/license
|
|
5
5
|
*
|
|
6
6
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
7
|
* of this software and associated documentation files (the "Software"), to deal
|
|
@@ -91,5 +91,34 @@ function toTitleCase(str) {
|
|
|
91
91
|
return str.split(/(\s+)/).map((part) => part.trim().length === 0 ? part : part.charAt(0).toUpperCase() + part.slice(1).toLowerCase()).join("");
|
|
92
92
|
}
|
|
93
93
|
__name(toTitleCase, "toTitleCase");
|
|
94
|
+
function escapeRegex(str) {
|
|
95
|
+
if (typeof str !== "string") throw new TypeError("Expected a string");
|
|
96
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
97
|
+
}
|
|
98
|
+
__name(escapeRegex, "escapeRegex");
|
|
99
|
+
function unescapeHtml(str) {
|
|
100
|
+
if (typeof str !== "string") return "";
|
|
101
|
+
const entities = {
|
|
102
|
+
"&": "&",
|
|
103
|
+
"<": "<",
|
|
104
|
+
">": ">",
|
|
105
|
+
""": '"',
|
|
106
|
+
"'": "'",
|
|
107
|
+
"'": "'"
|
|
108
|
+
};
|
|
109
|
+
return str.replace(/&(?:amp|lt|gt|quot|#39|#x27);/g, (match) => entities[match] || match);
|
|
110
|
+
}
|
|
111
|
+
__name(unescapeHtml, "unescapeHtml");
|
|
112
|
+
function isBlank(str) {
|
|
113
|
+
return typeof str === "string" && str.trim().length === 0;
|
|
114
|
+
}
|
|
115
|
+
__name(isBlank, "isBlank");
|
|
116
|
+
function ellipsis(str, maxLength, suffix = "...") {
|
|
117
|
+
if (typeof str !== "string" || str.length <= maxLength) return str;
|
|
118
|
+
const truncated = str.slice(0, maxLength - suffix.length);
|
|
119
|
+
const lastSpace = truncated.lastIndexOf(" ");
|
|
120
|
+
return (lastSpace > 0 ? truncated.slice(0, lastSpace) : truncated) + suffix;
|
|
121
|
+
}
|
|
122
|
+
__name(ellipsis, "ellipsis");
|
|
94
123
|
|
|
95
|
-
export { capitalize, countOccurrences, equalsIgnoreCase, mask, reverse, slugify, stripHtml, toCamelCase, toKebabCase, toPascalCase, toSnakeCase, toTitleCase, truncate };
|
|
124
|
+
export { capitalize, countOccurrences, ellipsis, equalsIgnoreCase, escapeRegex, isBlank, mask, reverse, slugify, stripHtml, toCamelCase, toKebabCase, toPascalCase, toSnakeCase, toTitleCase, truncate, unescapeHtml };
|
package/type/index.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* The MIT License
|
|
3
3
|
*
|
|
4
|
-
* Copyright (c)
|
|
4
|
+
* Copyright (c) 2026 Catbee Technologies. https://catbee.in/license
|
|
5
5
|
*
|
|
6
6
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
7
|
* of this software and associated documentation files (the "Software"), to deal
|
|
@@ -73,7 +73,7 @@ function toNum(value, defaultValue = 0) {
|
|
|
73
73
|
if (typeof value === "number") return value;
|
|
74
74
|
try {
|
|
75
75
|
const num = Number(value);
|
|
76
|
-
return isNaN(num) ? defaultValue : num;
|
|
76
|
+
return Number.isNaN(num) ? defaultValue : num;
|
|
77
77
|
} catch {
|
|
78
78
|
return defaultValue;
|
|
79
79
|
}
|
|
@@ -117,12 +117,29 @@ function isEmpty(value) {
|
|
|
117
117
|
return false;
|
|
118
118
|
}
|
|
119
119
|
__name(isEmpty, "isEmpty");
|
|
120
|
+
function isIterable(value) {
|
|
121
|
+
return value !== null && value !== void 0 && typeof value[Symbol.iterator] === "function";
|
|
122
|
+
}
|
|
123
|
+
__name(isIterable, "isIterable");
|
|
124
|
+
function isAsyncIterable(value) {
|
|
125
|
+
return value !== null && value !== void 0 && typeof value[Symbol.asyncIterator] === "function";
|
|
126
|
+
}
|
|
127
|
+
__name(isAsyncIterable, "isAsyncIterable");
|
|
128
|
+
function assertType(value, guard, message) {
|
|
129
|
+
if (!guard(value)) {
|
|
130
|
+
throw new TypeError(message || `Type assertion failed`);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
__name(assertType, "assertType");
|
|
120
134
|
|
|
135
|
+
exports.assertType = assertType;
|
|
121
136
|
exports.ensureType = ensureType;
|
|
122
137
|
exports.getTypeOf = getTypeOf;
|
|
123
138
|
exports.isArrayOf = isArrayOf;
|
|
139
|
+
exports.isAsyncIterable = isAsyncIterable;
|
|
124
140
|
exports.isDefined = isDefined;
|
|
125
141
|
exports.isEmpty = isEmpty;
|
|
142
|
+
exports.isIterable = isIterable;
|
|
126
143
|
exports.isPrimitiveType = isPrimitiveType;
|
|
127
144
|
exports.toBool = toBool;
|
|
128
145
|
exports.toNum = toNum;
|
package/type/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* The MIT License
|
|
3
3
|
*
|
|
4
|
-
* Copyright (c)
|
|
4
|
+
* Copyright (c) 2026 Catbee Technologies. https://catbee.in/license
|
|
5
5
|
*
|
|
6
6
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
7
|
* of this software and associated documentation files (the "Software"), to deal
|
|
@@ -22,4 +22,146 @@
|
|
|
22
22
|
* SOFTWARE.
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
/**
|
|
26
|
+
* Check if a value is of a specific primitive type.
|
|
27
|
+
*
|
|
28
|
+
* @param value - Value to check
|
|
29
|
+
* @param type - Type to check against
|
|
30
|
+
* @returns Whether the value is of the specified type
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* isPrimitiveType('hello', 'string'); // true
|
|
35
|
+
* isPrimitiveType(42, 'number'); // true
|
|
36
|
+
* isPrimitiveType(true, 'boolean'); // true
|
|
37
|
+
* isPrimitiveType(null, 'null'); // true
|
|
38
|
+
* isPrimitiveType(undefined, 'undefined'); // true
|
|
39
|
+
* isPrimitiveType({}, 'object'); // true
|
|
40
|
+
* isPrimitiveType([], 'array'); // true
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
declare function isPrimitiveType(value: unknown, type: 'string' | 'number' | 'boolean' | 'symbol' | 'bigint' | 'function' | 'object' | 'array' | 'null' | 'undefined'): boolean;
|
|
44
|
+
/**
|
|
45
|
+
* Get the primitive type of a value as a string.
|
|
46
|
+
*
|
|
47
|
+
* @param value - Value to get the type of
|
|
48
|
+
* @returns String representing the type
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```typescript
|
|
52
|
+
* getTypeOf('hello'); // 'string'
|
|
53
|
+
* getTypeOf(42); // 'number'
|
|
54
|
+
* getTypeOf([]); // 'array'
|
|
55
|
+
* getTypeOf(null); // 'null'
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
declare function getTypeOf(value: unknown): string;
|
|
59
|
+
/**
|
|
60
|
+
* Type guard for checking if a value is an array of a specific type.
|
|
61
|
+
*
|
|
62
|
+
* @param value - Value to check
|
|
63
|
+
* @param itemTypeGuard - Function that checks if items are of the expected type
|
|
64
|
+
* @returns True if the value is an array with items of the expected type
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```typescript
|
|
68
|
+
* isArrayOf([1, 2, 3], (item): item is number => typeof item === 'number'); // true
|
|
69
|
+
* isArrayOf(['a', 'b', 'c'], (item): item is string => typeof item === 'string'); // true
|
|
70
|
+
* isArrayOf([1, '2', 3], (item): item is number => typeof item === 'number'); // false
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
declare function isArrayOf<T>(value: unknown, itemTypeGuard: (item: unknown) => item is T): value is T[];
|
|
74
|
+
/**
|
|
75
|
+
* Convert a value to a string.
|
|
76
|
+
*
|
|
77
|
+
* @param value - Value to convert
|
|
78
|
+
* @param defaultValue - Default value if conversion fails
|
|
79
|
+
* @returns String representation of the value
|
|
80
|
+
*/
|
|
81
|
+
declare function toStr(value: unknown, defaultValue?: string): string;
|
|
82
|
+
/**
|
|
83
|
+
* Convert a value to a number.
|
|
84
|
+
*
|
|
85
|
+
* @param value - Value to convert
|
|
86
|
+
* @param defaultValue - Default value if conversion fails
|
|
87
|
+
* @returns Numeric representation of the value
|
|
88
|
+
*/
|
|
89
|
+
declare function toNum(value: unknown, defaultValue?: number): number;
|
|
90
|
+
/**
|
|
91
|
+
* Convert a value to a boolean.
|
|
92
|
+
*
|
|
93
|
+
* @param value - Value to convert
|
|
94
|
+
* @param defaultValue - Default value if conversion fails
|
|
95
|
+
* @returns Boolean representation of the value
|
|
96
|
+
*/
|
|
97
|
+
declare function toBool(value: unknown, defaultValue?: boolean): boolean;
|
|
98
|
+
/**
|
|
99
|
+
* Ensure a value matches the expected type, or provide a default.
|
|
100
|
+
*
|
|
101
|
+
* @param value - Value to check
|
|
102
|
+
* @param expectedType - Expected primitive type
|
|
103
|
+
* @param defaultValue - Default value to use if type doesn't match
|
|
104
|
+
* @returns The value if it matches the type, otherwise the default
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```typescript
|
|
108
|
+
* ensureType(42, 'number', 0); // 42
|
|
109
|
+
* ensureType('42', 'number', 0); // 0
|
|
110
|
+
* ensureType(undefined, 'string', 'default'); // 'default'
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
declare function ensureType<T>(value: unknown, expectedType: string, defaultValue: T): T;
|
|
114
|
+
/**
|
|
115
|
+
* Check whether a value is neither null nor undefined.
|
|
116
|
+
* Useful in filter chains and guards.
|
|
117
|
+
*
|
|
118
|
+
* @param value - Value to check
|
|
119
|
+
* @returns True when value !== null && value !== undefined
|
|
120
|
+
*/
|
|
121
|
+
declare function isDefined<T>(value: T | null | undefined): value is T;
|
|
122
|
+
/**
|
|
123
|
+
* Check whether a value is empty.
|
|
124
|
+
* Supports strings, arrays, maps, sets and plain objects.
|
|
125
|
+
*
|
|
126
|
+
* @param value - Value to inspect
|
|
127
|
+
* @returns True when value is considered empty
|
|
128
|
+
*/
|
|
129
|
+
declare function isEmpty(value: any): boolean;
|
|
130
|
+
/**
|
|
131
|
+
* Type guard to check if a value is iterable.
|
|
132
|
+
*
|
|
133
|
+
* @param {unknown} value - Value to check.
|
|
134
|
+
* @returns {boolean} True if value is iterable.
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* isIterable([1, 2, 3]); // true
|
|
138
|
+
* isIterable('hello'); // true
|
|
139
|
+
* isIterable(new Set([1, 2])); // true
|
|
140
|
+
*/
|
|
141
|
+
declare function isIterable<T = any>(value: unknown): value is Iterable<T>;
|
|
142
|
+
/**
|
|
143
|
+
* Type guard to check if a value is an async iterable.
|
|
144
|
+
*
|
|
145
|
+
* @param {unknown} value - Value to check.
|
|
146
|
+
* @returns {boolean} True if value is async iterable.
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
* async function* gen() { yield 1; }
|
|
150
|
+
* isAsyncIterable(gen()); // true
|
|
151
|
+
*/
|
|
152
|
+
declare function isAsyncIterable<T = any>(value: unknown): value is AsyncIterable<T>;
|
|
153
|
+
/**
|
|
154
|
+
* Asserts that a value is of a specific type, throwing an error if not.
|
|
155
|
+
*
|
|
156
|
+
* @template T
|
|
157
|
+
* @param {unknown} value - Value to assert.
|
|
158
|
+
* @param {(value: unknown) => value is T} guard - Type guard function.
|
|
159
|
+
* @param {string} [message] - Error message if assertion fails.
|
|
160
|
+
* @returns {asserts value is T}
|
|
161
|
+
*
|
|
162
|
+
* @example
|
|
163
|
+
* assertType(value, (v): v is string => typeof v === 'string', 'Expected string');
|
|
164
|
+
*/
|
|
165
|
+
declare function assertType<T>(value: unknown, guard: (value: unknown) => value is T, message?: string): asserts value is T;
|
|
166
|
+
|
|
167
|
+
export { assertType, ensureType, getTypeOf, isArrayOf, isAsyncIterable, isDefined, isEmpty, isIterable, isPrimitiveType, toBool, toNum, toStr };
|
package/type/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* The MIT License
|
|
3
3
|
*
|
|
4
|
-
* Copyright (c)
|
|
4
|
+
* Copyright (c) 2026 Catbee Technologies. https://catbee.in/license
|
|
5
5
|
*
|
|
6
6
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
7
|
* of this software and associated documentation files (the "Software"), to deal
|
|
@@ -71,7 +71,7 @@ function toNum(value, defaultValue = 0) {
|
|
|
71
71
|
if (typeof value === "number") return value;
|
|
72
72
|
try {
|
|
73
73
|
const num = Number(value);
|
|
74
|
-
return isNaN(num) ? defaultValue : num;
|
|
74
|
+
return Number.isNaN(num) ? defaultValue : num;
|
|
75
75
|
} catch {
|
|
76
76
|
return defaultValue;
|
|
77
77
|
}
|
|
@@ -115,5 +115,19 @@ function isEmpty(value) {
|
|
|
115
115
|
return false;
|
|
116
116
|
}
|
|
117
117
|
__name(isEmpty, "isEmpty");
|
|
118
|
+
function isIterable(value) {
|
|
119
|
+
return value !== null && value !== void 0 && typeof value[Symbol.iterator] === "function";
|
|
120
|
+
}
|
|
121
|
+
__name(isIterable, "isIterable");
|
|
122
|
+
function isAsyncIterable(value) {
|
|
123
|
+
return value !== null && value !== void 0 && typeof value[Symbol.asyncIterator] === "function";
|
|
124
|
+
}
|
|
125
|
+
__name(isAsyncIterable, "isAsyncIterable");
|
|
126
|
+
function assertType(value, guard, message) {
|
|
127
|
+
if (!guard(value)) {
|
|
128
|
+
throw new TypeError(message || `Type assertion failed`);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
__name(assertType, "assertType");
|
|
118
132
|
|
|
119
|
-
export { ensureType, getTypeOf, isArrayOf, isDefined, isEmpty, isPrimitiveType, toBool, toNum, toStr };
|
|
133
|
+
export { assertType, ensureType, getTypeOf, isArrayOf, isAsyncIterable, isDefined, isEmpty, isIterable, isPrimitiveType, toBool, toNum, toStr };
|
package/types/index.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* The MIT License
|
|
3
3
|
*
|
|
4
|
-
* Copyright (c)
|
|
4
|
+
* Copyright (c) 2026 Catbee Technologies. https://catbee.in/license
|
|
5
5
|
*
|
|
6
6
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
7
|
* of this software and associated documentation files (the "Software"), to deal
|