@gesslar/toolkit 0.2.3 → 0.2.4
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/package.json +1 -1
- package/src/lib/Util.js +10 -0
- package/src/types/Util.d.ts +32 -0
package/package.json
CHANGED
package/src/lib/Util.js
CHANGED
|
@@ -298,4 +298,14 @@ export default class Util {
|
|
|
298
298
|
|
|
299
299
|
return closestMatch
|
|
300
300
|
}
|
|
301
|
+
|
|
302
|
+
static regexify(input, trim=true, flags=[]) {
|
|
303
|
+
return new RegExp(
|
|
304
|
+
input
|
|
305
|
+
.split("\n")
|
|
306
|
+
.map(i => trim ? i.trim() : i)
|
|
307
|
+
.filter(i => trim ? Boolean(i) : true)
|
|
308
|
+
.join("")
|
|
309
|
+
, flags?.join(""))
|
|
310
|
+
}
|
|
301
311
|
}
|
package/src/types/Util.d.ts
CHANGED
|
@@ -215,6 +215,38 @@ declare class Util {
|
|
|
215
215
|
* ```
|
|
216
216
|
*/
|
|
217
217
|
static findClosestMatch(input: string, allowedValues: string[], threshold?: number): string | null
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Creates a RegExp from a multiline string by removing line breaks and
|
|
221
|
+
* optionally trimming whitespace from each line.
|
|
222
|
+
*
|
|
223
|
+
* This utility makes complex regular expressions more readable by allowing
|
|
224
|
+
* them to be written across multiple lines with proper formatting and indentation.
|
|
225
|
+
* The resulting regex is functionally identical to writing it as a single line.
|
|
226
|
+
*
|
|
227
|
+
* @param input - Multiline string containing the regex pattern
|
|
228
|
+
* @param trim - Whether to trim whitespace from each line (default: true)
|
|
229
|
+
* @param flags - Array of regex flags to apply (default: [])
|
|
230
|
+
* @returns A new RegExp object with the processed pattern
|
|
231
|
+
*
|
|
232
|
+
* @example
|
|
233
|
+
* ```typescript
|
|
234
|
+
* const regex = Util.regexify(`
|
|
235
|
+
* \\s*\\*\\s*
|
|
236
|
+
* @(?<tag>\\w+)
|
|
237
|
+
* \\s*
|
|
238
|
+
* \\{(?<type>\\w+(?:\\|\\w+)*(?:\\*)?)\\}
|
|
239
|
+
* \\s+
|
|
240
|
+
* (?<name>\\w+)
|
|
241
|
+
* `)
|
|
242
|
+
* // Creates: /\s*\*\s*@(?<tag>\w+)\s*\{(?<type>\w+(?:\|\w+)*(?:\*)?)\}\s+(?<name>\w+)/
|
|
243
|
+
*
|
|
244
|
+
* // With flags:
|
|
245
|
+
* const globalRegex = Util.regexify(pattern, true, ['g', 'i'])
|
|
246
|
+
* // Creates regex with global and case-insensitive flags
|
|
247
|
+
* ```
|
|
248
|
+
*/
|
|
249
|
+
static regexify(input: string, trim?: boolean, flags?: string[]): RegExp
|
|
218
250
|
}
|
|
219
251
|
|
|
220
252
|
export default Util
|