@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,209 +0,0 @@
1
- /*
2
- * The MIT License
3
- *
4
- * Copyright (c) 2025 Catbee Technologies. https://catbee-utils.npm.hprasath.com/license
5
- *
6
- * Permission is hereby granted, free of charge, to any person obtaining a copy
7
- * of this software and associated documentation files (the "Software"), to deal
8
- * in the Software without restriction, including without limitation the rights
9
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
- * copies of the Software, and to permit persons to whom the Software is
11
- * furnished to do so, subject to the following conditions:
12
- *
13
- * The above copyright notice and this permission notice shall be included in all
14
- * copies or substantial portions of the Software.
15
- *
16
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
- * SOFTWARE.
23
- */
24
- import { Readable, Transform } from 'stream';
25
- /**
26
- * Convert a buffer or string to a readable stream.
27
- *
28
- * @param data - Buffer or string to convert
29
- * @returns Readable stream containing the data
30
- *
31
- * @example
32
- * ```typescript
33
- * const stream = bufferToStream(Buffer.from('Hello world'));
34
- * // or
35
- * const stream = bufferToStream('Hello world');
36
- * ```
37
- */
38
- export function bufferToStream(data) {
39
- const readable = new Readable();
40
- readable.push(data);
41
- readable.push(null); // End of stream
42
- return readable;
43
- }
44
- /**
45
- * Convert a readable stream to a buffer.
46
- *
47
- * @param stream - Readable stream to convert
48
- * @returns Promise resolving to a buffer containing all stream data
49
- *
50
- * @example
51
- * ```typescript
52
- * const buffer = await streamToBuffer(fs.createReadStream('file.txt'));
53
- * console.log(buffer.toString()); // Contents of file.txt
54
- * ```
55
- */
56
- export async function streamToBuffer(stream) {
57
- return new Promise((resolve, reject) => {
58
- const chunks = [];
59
- stream.on('data', chunk => chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)));
60
- stream.on('end', () => resolve(Buffer.concat(chunks)));
61
- stream.on('error', reject);
62
- });
63
- }
64
- /**
65
- * Convert a readable stream to a string.
66
- *
67
- * @param stream - Readable stream to convert
68
- * @param encoding - Character encoding (default: 'utf8')
69
- * @returns Promise resolving to a string containing all stream data
70
- *
71
- * @example
72
- * ```typescript
73
- * const content = await streamToString(fs.createReadStream('file.txt'));
74
- * console.log(content); // Contents of file.txt as string
75
- * ```
76
- */
77
- export async function streamToString(stream, encoding = 'utf8') {
78
- const buffer = await streamToBuffer(stream);
79
- return buffer.toString(encoding);
80
- }
81
- /**
82
- * Create a transform stream that limits the rate of data flow.
83
- *
84
- * @param bytesPerSecond - Maximum bytes per second
85
- * @returns Transform stream that throttles data flow
86
- */
87
- export function createThrottleStream(bytesPerSecond) {
88
- let bytesSent = 0;
89
- let startTime = Date.now();
90
- return new Transform({
91
- transform(chunk, _encoding, callback) {
92
- const elapsedSeconds = (Date.now() - startTime) / 1000;
93
- const targetBytes = bytesPerSecond * elapsedSeconds;
94
- const chunkSize = chunk.length;
95
- // If we've sent fewer bytes than our target, just send the chunk
96
- if (bytesSent + chunkSize <= targetBytes) {
97
- bytesSent += chunkSize;
98
- callback(null, chunk);
99
- return;
100
- }
101
- // Otherwise we need to throttle
102
- const delay = ((bytesSent + chunkSize) / bytesPerSecond - elapsedSeconds) * 1000;
103
- setTimeout(() => {
104
- bytesSent += chunkSize;
105
- callback(null, chunk);
106
- }, delay);
107
- }
108
- });
109
- }
110
- /**
111
- * Create a transform stream that batches data into chunks of specified size.
112
- *
113
- * @param size - Size of each batch (items for object mode, bytes for binary mode)
114
- * @param options - Stream options
115
- * @returns Transform stream that batches data
116
- *
117
- * @example
118
- * ```typescript
119
- * // Batch lines from a file into arrays of 100 lines each
120
- * createReadStream('large-file.txt')
121
- * .pipe(createLineStream())
122
- * .pipe(createBatchStream(100))
123
- * .on('data', batch => console.log(`Processing batch of ${batch.length} lines`));
124
- * ```
125
- */
126
- export function createBatchStream(size, options = {}) {
127
- const objectMode = options.objectMode !== false;
128
- let batch = [];
129
- let buffers = [];
130
- let bufferSize = 0;
131
- return new Transform({
132
- objectMode,
133
- transform(chunk, _encoding, callback) {
134
- if (objectMode) {
135
- batch.push(chunk);
136
- if (batch.length >= size) {
137
- this.push(batch);
138
- batch = [];
139
- }
140
- }
141
- else {
142
- const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
143
- buffers.push(buffer);
144
- bufferSize += buffer.length;
145
- // Emit batches of exact size
146
- let combined = Buffer.concat(buffers, bufferSize);
147
- while (combined.length >= size) {
148
- this.push(combined.slice(0, size));
149
- combined = combined.slice(size);
150
- }
151
- buffers = combined.length > 0 ? [combined] : [];
152
- bufferSize = combined.length;
153
- }
154
- callback();
155
- },
156
- flush(callback) {
157
- if (objectMode && batch.length > 0) {
158
- this.push(batch);
159
- }
160
- else if (!objectMode && buffers.length > 0 && bufferSize > 0) {
161
- let combined = Buffer.concat(buffers, bufferSize);
162
- while (combined.length >= size) {
163
- this.push(combined.slice(0, size));
164
- combined = combined.slice(size);
165
- }
166
- if (combined.length > 0) {
167
- this.push(combined);
168
- }
169
- }
170
- callback();
171
- }
172
- });
173
- }
174
- /**
175
- * Create a transform stream that splits text data by newlines.
176
- *
177
- * @param options - Options for the line stream
178
- * @returns Transform stream that emits lines
179
- *
180
- * @example
181
- * ```typescript
182
- * // Process a file line by line
183
- * createReadStream('file.txt')
184
- * .pipe(createLineStream())
185
- * .on('data', line => console.log(`Line: ${line}`));
186
- * ```
187
- */
188
- export function createLineStream(options = {}) {
189
- const { encoding = 'utf8', includeNewlines = false } = options;
190
- let buffer = '';
191
- return new Transform({
192
- objectMode: true,
193
- transform(chunk, _encoding, callback) {
194
- const str = buffer + (Buffer.isBuffer(chunk) ? chunk.toString(encoding) : chunk);
195
- const lines = str.split(/\r?\n/);
196
- buffer = lines.pop() || '';
197
- for (const line of lines) {
198
- this.push(includeNewlines ? line + '\n' : line);
199
- }
200
- callback();
201
- },
202
- flush(callback) {
203
- if (buffer) {
204
- this.push(buffer);
205
- }
206
- callback();
207
- }
208
- });
209
- }
@@ -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,164 +0,0 @@
1
- /*
2
- * The MIT License
3
- *
4
- * Copyright (c) 2025 Catbee Technologies. https://catbee-utils.npm.hprasath.com/license
5
- *
6
- * Permission is hereby granted, free of charge, to any person obtaining a copy
7
- * of this software and associated documentation files (the "Software"), to deal
8
- * in the Software without restriction, including without limitation the rights
9
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
- * copies of the Software, and to permit persons to whom the Software is
11
- * furnished to do so, subject to the following conditions:
12
- *
13
- * The above copyright notice and this permission notice shall be included in all
14
- * copies or substantial portions of the Software.
15
- *
16
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
- * SOFTWARE.
23
- */
24
- /**
25
- * Capitalizes the first character of a string.
26
- *
27
- * @param {string} str - The input string.
28
- * @returns {string} The string with the first character in uppercase.
29
- */
30
- export function capitalize(str) {
31
- return str.length === 0 ? '' : str.charAt(0).toUpperCase() + str.slice(1);
32
- }
33
- /**
34
- * Converts a string to kebab-case (e.g., "FooBar test" → "foo-bar-test").
35
- *
36
- * @param {string} str - The input string.
37
- * @returns {string} The kebab-cased string.
38
- */
39
- export function toKebabCase(str) {
40
- return str
41
- .replace(/([a-z])([A-Z])/g, '$1-$2') // camelCase -> camel-Case
42
- .replace(/\s+/g, '-') // spaces to dash
43
- .replace(/_+/g, '-') // underscores to dash
44
- .toLowerCase();
45
- }
46
- /**
47
- * Converts a kebab-case or snake_case string to camelCase.
48
- *
49
- * @param {string} str - The input string.
50
- * @returns {string} The camelCased string.
51
- */
52
- export function toCamelCase(str) {
53
- return str.replace(/[-_](.)/g, (_, c) => c.toUpperCase());
54
- }
55
- /**
56
- * Converts a string to a URL-friendly slug (lowercase, dashes, alphanumeric).
57
- *
58
- * @param {string} str - The input string.
59
- * @returns {string} The slugified string.
60
- */
61
- export function slugify(str) {
62
- return str
63
- .toLowerCase()
64
- .replace(/[^\w\s-]/g, '') // remove non-word
65
- .replace(/\s+/g, '-') // spaces to dash
66
- .replace(/-+/g, '-') // multiple dashes to one
67
- .replace(/^-+|-+$/g, ''); // trim leading/trailing dashes
68
- }
69
- /**
70
- * Truncates a string to a specific length, appending '...' if truncated.
71
- *
72
- * @param {string} str - The input string.
73
- * @param {number} len - The maximum length.
74
- * @returns {string} The truncated string.
75
- */
76
- export function truncate(str, len) {
77
- return str.length > len ? str.slice(0, len) + '...' : str;
78
- }
79
- /**
80
- * Converts a string to PascalCase (e.g., "foo-bar" → "FooBar").
81
- *
82
- * @param {string} str - The input string.
83
- * @returns {string} The PascalCased string.
84
- */
85
- export function toPascalCase(str) {
86
- return capitalize(toCamelCase(str));
87
- }
88
- /**
89
- * Converts a string to snake_case (e.g., "FooBar test" → "foo_bar_test").
90
- *
91
- * @param {string} str - The input string.
92
- * @returns {string} The snake_cased string.
93
- */
94
- export function toSnakeCase(str) {
95
- return str
96
- .trim()
97
- .replace(/([a-z])([A-Z])/g, '$1_$2') // camelCase -> camel_Case
98
- .replace(/\s+/g, '_') // spaces to underscore
99
- .replace(/-+/g, '_') // dashes to underscore
100
- .toLowerCase();
101
- }
102
- /**
103
- * Masks a string by replacing characters with a mask character.
104
- * Useful for hiding sensitive information like credit cards or passwords.
105
- *
106
- * @param {string} str - The string to mask.
107
- * @param {number} [visibleStart=0] - Number of characters to show at start.
108
- * @param {number} [visibleEnd=0] - Number of characters to show at end.
109
- * @param {string} [maskChar="*"] - Character to use for masking.
110
- * @returns {string} The masked string.
111
- */
112
- export function mask(str, visibleStart = 0, visibleEnd = 0, maskChar = '*') {
113
- if (!str || str.length === 0)
114
- return '';
115
- if (visibleStart >= str.length)
116
- return str;
117
- const start = str.slice(0, visibleStart);
118
- const end = visibleEnd > 0 ? str.slice(-visibleEnd) : '';
119
- const masked = maskChar.repeat(Math.max(0, str.length - visibleStart - visibleEnd));
120
- return start + masked + end;
121
- }
122
- /**
123
- * Removes all HTML tags from a string.
124
- *
125
- * @param {string} str - The HTML string to process.
126
- * @returns {string} The string with HTML tags removed.
127
- */
128
- export function stripHtml(str) {
129
- return str.replace(/<[^>]*>/g, '');
130
- }
131
- /**
132
- * Performs case-insensitive string comparison.
133
- *
134
- * @param {string} a - First string.
135
- * @param {string} b - Second string.
136
- * @returns {boolean} True if the strings are equal ignoring case.
137
- */
138
- export function equalsIgnoreCase(a, b) {
139
- return a.toLowerCase() === b.toLowerCase();
140
- }
141
- /**
142
- * Reverses a string.
143
- *
144
- * @param {string} str - The string to reverse.
145
- * @returns {string} The reversed string.
146
- */
147
- export function reverse(str) {
148
- return str.split('').reverse().join('');
149
- }
150
- /**
151
- * Counts occurrences of a substring within a string.
152
- *
153
- * @param {string} str - The source string.
154
- * @param {string} substring - The substring to count.
155
- * @param {boolean} [caseSensitive=true] - Whether to perform case-sensitive counting.
156
- * @returns {number} Number of occurrences.
157
- */
158
- export function countOccurrences(str, substring, caseSensitive = true) {
159
- if (!caseSensitive) {
160
- str = str.toLowerCase();
161
- substring = substring.toLowerCase();
162
- }
163
- return str.split(substring).length - 1;
164
- }
@@ -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;