@catbee/utils 0.0.8-rc.3 → 1.0.1

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.
Files changed (129) hide show
  1. package/README.md +3 -2
  2. package/build/index.cjs +6912 -0
  3. package/build/index.d.ts +5284 -0
  4. package/build/index.mjs +6635 -0
  5. package/package.json +13 -292
  6. package/build/cjs/config.d.ts +0 -121
  7. package/build/cjs/config.js +0 -137
  8. package/build/cjs/index.d.ts +0 -26
  9. package/build/cjs/index.js +0 -68
  10. package/build/cjs/servers/server.builder.d.ts +0 -507
  11. package/build/cjs/servers/server.builder.js +0 -658
  12. package/build/cjs/servers/server.d.ts +0 -255
  13. package/build/cjs/servers/server.js +0 -970
  14. package/build/cjs/types/api-response.d.ts +0 -151
  15. package/build/cjs/types/api-response.js +0 -36
  16. package/build/cjs/types/index.d.ts +0 -124
  17. package/build/cjs/types/index.js +0 -25
  18. package/build/cjs/types/server.d.ts +0 -267
  19. package/build/cjs/types/server.js +0 -25
  20. package/build/cjs/utils/array.utils.d.ts +0 -167
  21. package/build/cjs/utils/array.utils.js +0 -363
  22. package/build/cjs/utils/async.utils.d.ts +0 -264
  23. package/build/cjs/utils/async.utils.js +0 -640
  24. package/build/cjs/utils/cache.utils.d.ts +0 -152
  25. package/build/cjs/utils/cache.utils.js +0 -298
  26. package/build/cjs/utils/context-store.utils.d.ts +0 -188
  27. package/build/cjs/utils/context-store.utils.js +0 -297
  28. package/build/cjs/utils/crypto.utils.d.ts +0 -159
  29. package/build/cjs/utils/crypto.utils.js +0 -295
  30. package/build/cjs/utils/date.utils.d.ts +0 -158
  31. package/build/cjs/utils/date.utils.js +0 -395
  32. package/build/cjs/utils/decorators.utils.d.ts +0 -511
  33. package/build/cjs/utils/decorators.utils.js +0 -1035
  34. package/build/cjs/utils/dir.utils.d.ts +0 -195
  35. package/build/cjs/utils/dir.utils.js +0 -500
  36. package/build/cjs/utils/env.utils.d.ts +0 -376
  37. package/build/cjs/utils/env.utils.js +0 -786
  38. package/build/cjs/utils/exception.utils.d.ts +0 -229
  39. package/build/cjs/utils/exception.utils.js +0 -408
  40. package/build/cjs/utils/fs.utils.d.ts +0 -163
  41. package/build/cjs/utils/fs.utils.js +0 -371
  42. package/build/cjs/utils/http-status-codes.d.ts +0 -265
  43. package/build/cjs/utils/http-status-codes.js +0 -297
  44. package/build/cjs/utils/id.utils.d.ts +0 -35
  45. package/build/cjs/utils/id.utils.js +0 -90
  46. package/build/cjs/utils/logger.utils.d.ts +0 -159
  47. package/build/cjs/utils/logger.utils.js +0 -350
  48. package/build/cjs/utils/middleware.utils.d.ts +0 -99
  49. package/build/cjs/utils/middleware.utils.js +0 -243
  50. package/build/cjs/utils/obj.utils.d.ts +0 -123
  51. package/build/cjs/utils/obj.utils.js +0 -428
  52. package/build/cjs/utils/performance.utils.d.ts +0 -135
  53. package/build/cjs/utils/performance.utils.js +0 -280
  54. package/build/cjs/utils/request.utils.d.ts +0 -85
  55. package/build/cjs/utils/request.utils.js +0 -199
  56. package/build/cjs/utils/response.utils.d.ts +0 -162
  57. package/build/cjs/utils/response.utils.js +0 -274
  58. package/build/cjs/utils/stream.utils.d.ts +0 -87
  59. package/build/cjs/utils/stream.utils.js +0 -217
  60. package/build/cjs/utils/string.utils.d.ts +0 -92
  61. package/build/cjs/utils/string.utils.js +0 -178
  62. package/build/cjs/utils/type.utils.d.ts +0 -89
  63. package/build/cjs/utils/type.utils.js +0 -195
  64. package/build/cjs/utils/url.utils.d.ts +0 -140
  65. package/build/cjs/utils/url.utils.js +0 -316
  66. package/build/cjs/utils/validate.utils.d.ts +0 -176
  67. package/build/cjs/utils/validate.utils.js +0 -344
  68. package/build/esm/config.d.ts +0 -121
  69. package/build/esm/config.js +0 -132
  70. package/build/esm/index.d.ts +0 -26
  71. package/build/esm/index.js +0 -49
  72. package/build/esm/servers/server.builder.d.ts +0 -507
  73. package/build/esm/servers/server.builder.js +0 -654
  74. package/build/esm/servers/server.d.ts +0 -255
  75. package/build/esm/servers/server.js +0 -963
  76. package/build/esm/types/api-response.d.ts +0 -151
  77. package/build/esm/types/api-response.js +0 -33
  78. package/build/esm/types/index.d.ts +0 -124
  79. package/build/esm/types/index.js +0 -24
  80. package/build/esm/types/server.d.ts +0 -267
  81. package/build/esm/types/server.js +0 -24
  82. package/build/esm/utils/array.utils.d.ts +0 -167
  83. package/build/esm/utils/array.utils.js +0 -344
  84. package/build/esm/utils/async.utils.d.ts +0 -264
  85. package/build/esm/utils/async.utils.js +0 -619
  86. package/build/esm/utils/cache.utils.d.ts +0 -152
  87. package/build/esm/utils/cache.utils.js +0 -294
  88. package/build/esm/utils/context-store.utils.d.ts +0 -188
  89. package/build/esm/utils/context-store.utils.js +0 -290
  90. package/build/esm/utils/crypto.utils.d.ts +0 -159
  91. package/build/esm/utils/crypto.utils.js +0 -278
  92. package/build/esm/utils/date.utils.d.ts +0 -158
  93. package/build/esm/utils/date.utils.js +0 -383
  94. package/build/esm/utils/decorators.utils.d.ts +0 -511
  95. package/build/esm/utils/decorators.utils.js +0 -1013
  96. package/build/esm/utils/dir.utils.d.ts +0 -195
  97. package/build/esm/utils/dir.utils.js +0 -476
  98. package/build/esm/utils/env.utils.d.ts +0 -376
  99. package/build/esm/utils/env.utils.js +0 -782
  100. package/build/esm/utils/exception.utils.d.ts +0 -229
  101. package/build/esm/utils/exception.utils.js +0 -382
  102. package/build/esm/utils/fs.utils.d.ts +0 -163
  103. package/build/esm/utils/fs.utils.js +0 -348
  104. package/build/esm/utils/http-status-codes.d.ts +0 -265
  105. package/build/esm/utils/http-status-codes.js +0 -294
  106. package/build/esm/utils/id.utils.d.ts +0 -35
  107. package/build/esm/utils/id.utils.js +0 -83
  108. package/build/esm/utils/logger.utils.d.ts +0 -159
  109. package/build/esm/utils/logger.utils.js +0 -304
  110. package/build/esm/utils/middleware.utils.d.ts +0 -99
  111. package/build/esm/utils/middleware.utils.js +0 -235
  112. package/build/esm/utils/obj.utils.d.ts +0 -123
  113. package/build/esm/utils/obj.utils.js +0 -412
  114. package/build/esm/utils/performance.utils.d.ts +0 -135
  115. package/build/esm/utils/performance.utils.js +0 -273
  116. package/build/esm/utils/request.utils.d.ts +0 -85
  117. package/build/esm/utils/request.utils.js +0 -190
  118. package/build/esm/utils/response.utils.d.ts +0 -162
  119. package/build/esm/utils/response.utils.js +0 -261
  120. package/build/esm/utils/stream.utils.d.ts +0 -87
  121. package/build/esm/utils/stream.utils.js +0 -209
  122. package/build/esm/utils/string.utils.d.ts +0 -92
  123. package/build/esm/utils/string.utils.js +0 -164
  124. package/build/esm/utils/type.utils.d.ts +0 -89
  125. package/build/esm/utils/type.utils.js +0 -186
  126. package/build/esm/utils/url.utils.d.ts +0 -140
  127. package/build/esm/utils/url.utils.js +0 -303
  128. package/build/esm/utils/validate.utils.d.ts +0 -176
  129. package/build/esm/utils/validate.utils.js +0 -319
@@ -1,217 +0,0 @@
1
- "use strict";
2
- /*
3
- * The MIT License
4
- *
5
- * Copyright (c) 2025 Catbee Technologies. https://catbee-utils.npm.hprasath.com/license
6
- *
7
- * Permission is hereby granted, free of charge, to any person obtaining a copy
8
- * of this software and associated documentation files (the "Software"), to deal
9
- * in the Software without restriction, including without limitation the rights
10
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
- * copies of the Software, and to permit persons to whom the Software is
12
- * furnished to do so, subject to the following conditions:
13
- *
14
- * The above copyright notice and this permission notice shall be included in all
15
- * copies or substantial portions of the Software.
16
- *
17
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
- * SOFTWARE.
24
- */
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.bufferToStream = bufferToStream;
27
- exports.streamToBuffer = streamToBuffer;
28
- exports.streamToString = streamToString;
29
- exports.createThrottleStream = createThrottleStream;
30
- exports.createBatchStream = createBatchStream;
31
- exports.createLineStream = createLineStream;
32
- const stream_1 = require("stream");
33
- /**
34
- * Convert a buffer or string to a readable stream.
35
- *
36
- * @param data - Buffer or string to convert
37
- * @returns Readable stream containing the data
38
- *
39
- * @example
40
- * ```typescript
41
- * const stream = bufferToStream(Buffer.from('Hello world'));
42
- * // or
43
- * const stream = bufferToStream('Hello world');
44
- * ```
45
- */
46
- function bufferToStream(data) {
47
- const readable = new stream_1.Readable();
48
- readable.push(data);
49
- readable.push(null); // End of stream
50
- return readable;
51
- }
52
- /**
53
- * Convert a readable stream to a buffer.
54
- *
55
- * @param stream - Readable stream to convert
56
- * @returns Promise resolving to a buffer containing all stream data
57
- *
58
- * @example
59
- * ```typescript
60
- * const buffer = await streamToBuffer(fs.createReadStream('file.txt'));
61
- * console.log(buffer.toString()); // Contents of file.txt
62
- * ```
63
- */
64
- async function streamToBuffer(stream) {
65
- return new Promise((resolve, reject) => {
66
- const chunks = [];
67
- stream.on('data', chunk => chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)));
68
- stream.on('end', () => resolve(Buffer.concat(chunks)));
69
- stream.on('error', reject);
70
- });
71
- }
72
- /**
73
- * Convert a readable stream to a string.
74
- *
75
- * @param stream - Readable stream to convert
76
- * @param encoding - Character encoding (default: 'utf8')
77
- * @returns Promise resolving to a string containing all stream data
78
- *
79
- * @example
80
- * ```typescript
81
- * const content = await streamToString(fs.createReadStream('file.txt'));
82
- * console.log(content); // Contents of file.txt as string
83
- * ```
84
- */
85
- async function streamToString(stream, encoding = 'utf8') {
86
- const buffer = await streamToBuffer(stream);
87
- return buffer.toString(encoding);
88
- }
89
- /**
90
- * Create a transform stream that limits the rate of data flow.
91
- *
92
- * @param bytesPerSecond - Maximum bytes per second
93
- * @returns Transform stream that throttles data flow
94
- */
95
- function createThrottleStream(bytesPerSecond) {
96
- let bytesSent = 0;
97
- let startTime = Date.now();
98
- return new stream_1.Transform({
99
- transform(chunk, _encoding, callback) {
100
- const elapsedSeconds = (Date.now() - startTime) / 1000;
101
- const targetBytes = bytesPerSecond * elapsedSeconds;
102
- const chunkSize = chunk.length;
103
- // If we've sent fewer bytes than our target, just send the chunk
104
- if (bytesSent + chunkSize <= targetBytes) {
105
- bytesSent += chunkSize;
106
- callback(null, chunk);
107
- return;
108
- }
109
- // Otherwise we need to throttle
110
- const delay = ((bytesSent + chunkSize) / bytesPerSecond - elapsedSeconds) * 1000;
111
- setTimeout(() => {
112
- bytesSent += chunkSize;
113
- callback(null, chunk);
114
- }, delay);
115
- }
116
- });
117
- }
118
- /**
119
- * Create a transform stream that batches data into chunks of specified size.
120
- *
121
- * @param size - Size of each batch (items for object mode, bytes for binary mode)
122
- * @param options - Stream options
123
- * @returns Transform stream that batches data
124
- *
125
- * @example
126
- * ```typescript
127
- * // Batch lines from a file into arrays of 100 lines each
128
- * createReadStream('large-file.txt')
129
- * .pipe(createLineStream())
130
- * .pipe(createBatchStream(100))
131
- * .on('data', batch => console.log(`Processing batch of ${batch.length} lines`));
132
- * ```
133
- */
134
- function createBatchStream(size, options = {}) {
135
- const objectMode = options.objectMode !== false;
136
- let batch = [];
137
- let buffers = [];
138
- let bufferSize = 0;
139
- return new stream_1.Transform({
140
- objectMode,
141
- transform(chunk, _encoding, callback) {
142
- if (objectMode) {
143
- batch.push(chunk);
144
- if (batch.length >= size) {
145
- this.push(batch);
146
- batch = [];
147
- }
148
- }
149
- else {
150
- const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
151
- buffers.push(buffer);
152
- bufferSize += buffer.length;
153
- // Emit batches of exact size
154
- let combined = Buffer.concat(buffers, bufferSize);
155
- while (combined.length >= size) {
156
- this.push(combined.slice(0, size));
157
- combined = combined.slice(size);
158
- }
159
- buffers = combined.length > 0 ? [combined] : [];
160
- bufferSize = combined.length;
161
- }
162
- callback();
163
- },
164
- flush(callback) {
165
- if (objectMode && batch.length > 0) {
166
- this.push(batch);
167
- }
168
- else if (!objectMode && buffers.length > 0 && bufferSize > 0) {
169
- let combined = Buffer.concat(buffers, bufferSize);
170
- while (combined.length >= size) {
171
- this.push(combined.slice(0, size));
172
- combined = combined.slice(size);
173
- }
174
- if (combined.length > 0) {
175
- this.push(combined);
176
- }
177
- }
178
- callback();
179
- }
180
- });
181
- }
182
- /**
183
- * Create a transform stream that splits text data by newlines.
184
- *
185
- * @param options - Options for the line stream
186
- * @returns Transform stream that emits lines
187
- *
188
- * @example
189
- * ```typescript
190
- * // Process a file line by line
191
- * createReadStream('file.txt')
192
- * .pipe(createLineStream())
193
- * .on('data', line => console.log(`Line: ${line}`));
194
- * ```
195
- */
196
- function createLineStream(options = {}) {
197
- const { encoding = 'utf8', includeNewlines = false } = options;
198
- let buffer = '';
199
- return new stream_1.Transform({
200
- objectMode: true,
201
- transform(chunk, _encoding, callback) {
202
- const str = buffer + (Buffer.isBuffer(chunk) ? chunk.toString(encoding) : chunk);
203
- const lines = str.split(/\r?\n/);
204
- buffer = lines.pop() || '';
205
- for (const line of lines) {
206
- this.push(includeNewlines ? line + '\n' : line);
207
- }
208
- callback();
209
- },
210
- flush(callback) {
211
- if (buffer) {
212
- this.push(buffer);
213
- }
214
- callback();
215
- }
216
- });
217
- }
@@ -1,92 +0,0 @@
1
- /**
2
- * Capitalizes the first character of a string.
3
- *
4
- * @param {string} str - The input string.
5
- * @returns {string} The string with the first character in uppercase.
6
- */
7
- export declare function capitalize(str: string): string;
8
- /**
9
- * Converts a string to kebab-case (e.g., "FooBar test" → "foo-bar-test").
10
- *
11
- * @param {string} str - The input string.
12
- * @returns {string} The kebab-cased string.
13
- */
14
- export declare function toKebabCase(str: string): string;
15
- /**
16
- * Converts a kebab-case or snake_case string to camelCase.
17
- *
18
- * @param {string} str - The input string.
19
- * @returns {string} The camelCased string.
20
- */
21
- export declare function toCamelCase(str: string): string;
22
- /**
23
- * Converts a string to a URL-friendly slug (lowercase, dashes, alphanumeric).
24
- *
25
- * @param {string} str - The input string.
26
- * @returns {string} The slugified string.
27
- */
28
- export declare function slugify(str: string): string;
29
- /**
30
- * Truncates a string to a specific length, appending '...' if truncated.
31
- *
32
- * @param {string} str - The input string.
33
- * @param {number} len - The maximum length.
34
- * @returns {string} The truncated string.
35
- */
36
- export declare function truncate(str: string, len: number): string;
37
- /**
38
- * Converts a string to PascalCase (e.g., "foo-bar" → "FooBar").
39
- *
40
- * @param {string} str - The input string.
41
- * @returns {string} The PascalCased string.
42
- */
43
- export declare function toPascalCase(str: string): string;
44
- /**
45
- * Converts a string to snake_case (e.g., "FooBar test" → "foo_bar_test").
46
- *
47
- * @param {string} str - The input string.
48
- * @returns {string} The snake_cased string.
49
- */
50
- export declare function toSnakeCase(str: string): string;
51
- /**
52
- * Masks a string by replacing characters with a mask character.
53
- * Useful for hiding sensitive information like credit cards or passwords.
54
- *
55
- * @param {string} str - The string to mask.
56
- * @param {number} [visibleStart=0] - Number of characters to show at start.
57
- * @param {number} [visibleEnd=0] - Number of characters to show at end.
58
- * @param {string} [maskChar="*"] - Character to use for masking.
59
- * @returns {string} The masked string.
60
- */
61
- export declare function mask(str: string, visibleStart?: number, visibleEnd?: number, maskChar?: string): string;
62
- /**
63
- * Removes all HTML tags from a string.
64
- *
65
- * @param {string} str - The HTML string to process.
66
- * @returns {string} The string with HTML tags removed.
67
- */
68
- export declare function stripHtml(str: string): string;
69
- /**
70
- * Performs case-insensitive string comparison.
71
- *
72
- * @param {string} a - First string.
73
- * @param {string} b - Second string.
74
- * @returns {boolean} True if the strings are equal ignoring case.
75
- */
76
- export declare function equalsIgnoreCase(a: string, b: string): boolean;
77
- /**
78
- * Reverses a string.
79
- *
80
- * @param {string} str - The string to reverse.
81
- * @returns {string} The reversed string.
82
- */
83
- export declare function reverse(str: string): string;
84
- /**
85
- * Counts occurrences of a substring within a string.
86
- *
87
- * @param {string} str - The source string.
88
- * @param {string} substring - The substring to count.
89
- * @param {boolean} [caseSensitive=true] - Whether to perform case-sensitive counting.
90
- * @returns {number} Number of occurrences.
91
- */
92
- export declare function countOccurrences(str: string, substring: string, caseSensitive?: boolean): number;
@@ -1,178 +0,0 @@
1
- "use strict";
2
- /*
3
- * The MIT License
4
- *
5
- * Copyright (c) 2025 Catbee Technologies. https://catbee-utils.npm.hprasath.com/license
6
- *
7
- * Permission is hereby granted, free of charge, to any person obtaining a copy
8
- * of this software and associated documentation files (the "Software"), to deal
9
- * in the Software without restriction, including without limitation the rights
10
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
- * copies of the Software, and to permit persons to whom the Software is
12
- * furnished to do so, subject to the following conditions:
13
- *
14
- * The above copyright notice and this permission notice shall be included in all
15
- * copies or substantial portions of the Software.
16
- *
17
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
- * SOFTWARE.
24
- */
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.capitalize = capitalize;
27
- exports.toKebabCase = toKebabCase;
28
- exports.toCamelCase = toCamelCase;
29
- exports.slugify = slugify;
30
- exports.truncate = truncate;
31
- exports.toPascalCase = toPascalCase;
32
- exports.toSnakeCase = toSnakeCase;
33
- exports.mask = mask;
34
- exports.stripHtml = stripHtml;
35
- exports.equalsIgnoreCase = equalsIgnoreCase;
36
- exports.reverse = reverse;
37
- exports.countOccurrences = countOccurrences;
38
- /**
39
- * Capitalizes the first character of a string.
40
- *
41
- * @param {string} str - The input string.
42
- * @returns {string} The string with the first character in uppercase.
43
- */
44
- function capitalize(str) {
45
- return str.length === 0 ? '' : str.charAt(0).toUpperCase() + str.slice(1);
46
- }
47
- /**
48
- * Converts a string to kebab-case (e.g., "FooBar test" → "foo-bar-test").
49
- *
50
- * @param {string} str - The input string.
51
- * @returns {string} The kebab-cased string.
52
- */
53
- function toKebabCase(str) {
54
- return str
55
- .replace(/([a-z])([A-Z])/g, '$1-$2') // camelCase -> camel-Case
56
- .replace(/\s+/g, '-') // spaces to dash
57
- .replace(/_+/g, '-') // underscores to dash
58
- .toLowerCase();
59
- }
60
- /**
61
- * Converts a kebab-case or snake_case string to camelCase.
62
- *
63
- * @param {string} str - The input string.
64
- * @returns {string} The camelCased string.
65
- */
66
- function toCamelCase(str) {
67
- return str.replace(/[-_](.)/g, (_, c) => c.toUpperCase());
68
- }
69
- /**
70
- * Converts a string to a URL-friendly slug (lowercase, dashes, alphanumeric).
71
- *
72
- * @param {string} str - The input string.
73
- * @returns {string} The slugified string.
74
- */
75
- function slugify(str) {
76
- return str
77
- .toLowerCase()
78
- .replace(/[^\w\s-]/g, '') // remove non-word
79
- .replace(/\s+/g, '-') // spaces to dash
80
- .replace(/-+/g, '-') // multiple dashes to one
81
- .replace(/^-+|-+$/g, ''); // trim leading/trailing dashes
82
- }
83
- /**
84
- * Truncates a string to a specific length, appending '...' if truncated.
85
- *
86
- * @param {string} str - The input string.
87
- * @param {number} len - The maximum length.
88
- * @returns {string} The truncated string.
89
- */
90
- function truncate(str, len) {
91
- return str.length > len ? str.slice(0, len) + '...' : str;
92
- }
93
- /**
94
- * Converts a string to PascalCase (e.g., "foo-bar" → "FooBar").
95
- *
96
- * @param {string} str - The input string.
97
- * @returns {string} The PascalCased string.
98
- */
99
- function toPascalCase(str) {
100
- return capitalize(toCamelCase(str));
101
- }
102
- /**
103
- * Converts a string to snake_case (e.g., "FooBar test" → "foo_bar_test").
104
- *
105
- * @param {string} str - The input string.
106
- * @returns {string} The snake_cased string.
107
- */
108
- function toSnakeCase(str) {
109
- return str
110
- .trim()
111
- .replace(/([a-z])([A-Z])/g, '$1_$2') // camelCase -> camel_Case
112
- .replace(/\s+/g, '_') // spaces to underscore
113
- .replace(/-+/g, '_') // dashes to underscore
114
- .toLowerCase();
115
- }
116
- /**
117
- * Masks a string by replacing characters with a mask character.
118
- * Useful for hiding sensitive information like credit cards or passwords.
119
- *
120
- * @param {string} str - The string to mask.
121
- * @param {number} [visibleStart=0] - Number of characters to show at start.
122
- * @param {number} [visibleEnd=0] - Number of characters to show at end.
123
- * @param {string} [maskChar="*"] - Character to use for masking.
124
- * @returns {string} The masked string.
125
- */
126
- function mask(str, visibleStart = 0, visibleEnd = 0, maskChar = '*') {
127
- if (!str || str.length === 0)
128
- return '';
129
- if (visibleStart >= str.length)
130
- return str;
131
- const start = str.slice(0, visibleStart);
132
- const end = visibleEnd > 0 ? str.slice(-visibleEnd) : '';
133
- const masked = maskChar.repeat(Math.max(0, str.length - visibleStart - visibleEnd));
134
- return start + masked + end;
135
- }
136
- /**
137
- * Removes all HTML tags from a string.
138
- *
139
- * @param {string} str - The HTML string to process.
140
- * @returns {string} The string with HTML tags removed.
141
- */
142
- function stripHtml(str) {
143
- return str.replace(/<[^>]*>/g, '');
144
- }
145
- /**
146
- * Performs case-insensitive string comparison.
147
- *
148
- * @param {string} a - First string.
149
- * @param {string} b - Second string.
150
- * @returns {boolean} True if the strings are equal ignoring case.
151
- */
152
- function equalsIgnoreCase(a, b) {
153
- return a.toLowerCase() === b.toLowerCase();
154
- }
155
- /**
156
- * Reverses a string.
157
- *
158
- * @param {string} str - The string to reverse.
159
- * @returns {string} The reversed string.
160
- */
161
- function reverse(str) {
162
- return str.split('').reverse().join('');
163
- }
164
- /**
165
- * Counts occurrences of a substring within a string.
166
- *
167
- * @param {string} str - The source string.
168
- * @param {string} substring - The substring to count.
169
- * @param {boolean} [caseSensitive=true] - Whether to perform case-sensitive counting.
170
- * @returns {number} Number of occurrences.
171
- */
172
- function countOccurrences(str, substring, caseSensitive = true) {
173
- if (!caseSensitive) {
174
- str = str.toLowerCase();
175
- substring = substring.toLowerCase();
176
- }
177
- return str.split(substring).length - 1;
178
- }
@@ -1,89 +0,0 @@
1
- /**
2
- * Check if a value is of a specific primitive type.
3
- *
4
- * @param value - Value to check
5
- * @param type - Type to check against
6
- * @returns Whether the value is of the specified type
7
- *
8
- * @example
9
- * ```typescript
10
- * isPrimitiveType('hello', 'string'); // true
11
- * isPrimitiveType(42, 'number'); // true
12
- * isPrimitiveType(true, 'boolean'); // true
13
- * isPrimitiveType(null, 'null'); // true
14
- * isPrimitiveType(undefined, 'undefined'); // true
15
- * isPrimitiveType({}, 'object'); // true
16
- * isPrimitiveType([], 'array'); // true
17
- * ```
18
- */
19
- export declare function isPrimitiveType(value: unknown, type: 'string' | 'number' | 'boolean' | 'symbol' | 'bigint' | 'function' | 'object' | 'array' | 'null' | 'undefined'): boolean;
20
- /**
21
- * Get the primitive type of a value as a string.
22
- *
23
- * @param value - Value to get the type of
24
- * @returns String representing the type
25
- *
26
- * @example
27
- * ```typescript
28
- * getTypeOf('hello'); // 'string'
29
- * getTypeOf(42); // 'number'
30
- * getTypeOf([]); // 'array'
31
- * getTypeOf(null); // 'null'
32
- * ```
33
- */
34
- export declare function getTypeOf(value: unknown): string;
35
- /**
36
- * Type guard for checking if a value is an array of a specific type.
37
- *
38
- * @param value - Value to check
39
- * @param itemTypeGuard - Function that checks if items are of the expected type
40
- * @returns True if the value is an array with items of the expected type
41
- *
42
- * @example
43
- * ```typescript
44
- * isArrayOf([1, 2, 3], (item): item is number => typeof item === 'number'); // true
45
- * isArrayOf(['a', 'b', 'c'], (item): item is string => typeof item === 'string'); // true
46
- * isArrayOf([1, '2', 3], (item): item is number => typeof item === 'number'); // false
47
- * ```
48
- */
49
- export declare function isArrayOf<T>(value: unknown, itemTypeGuard: (item: unknown) => item is T): value is T[];
50
- /**
51
- * Convert a value to a string.
52
- *
53
- * @param value - Value to convert
54
- * @param defaultValue - Default value if conversion fails
55
- * @returns String representation of the value
56
- */
57
- export declare function toStr(value: unknown, defaultValue?: string): string;
58
- /**
59
- * Convert a value to a number.
60
- *
61
- * @param value - Value to convert
62
- * @param defaultValue - Default value if conversion fails
63
- * @returns Numeric representation of the value
64
- */
65
- export declare function toNum(value: unknown, defaultValue?: number): number;
66
- /**
67
- * Convert a value to a boolean.
68
- *
69
- * @param value - Value to convert
70
- * @param defaultValue - Default value if conversion fails
71
- * @returns Boolean representation of the value
72
- */
73
- export declare function toBool(value: unknown, defaultValue?: boolean): boolean;
74
- /**
75
- * Ensure a value matches the expected type, or provide a default.
76
- *
77
- * @param value - Value to check
78
- * @param expectedType - Expected primitive type
79
- * @param defaultValue - Default value to use if type doesn't match
80
- * @returns The value if it matches the type, otherwise the default
81
- *
82
- * @example
83
- * ```typescript
84
- * ensureType(42, 'number', 0); // 42
85
- * ensureType('42', 'number', 0); // 0
86
- * ensureType(undefined, 'string', 'default'); // 'default'
87
- * ```
88
- */
89
- export declare function ensureType<T>(value: unknown, expectedType: string, defaultValue: T): T;