Skip to content

Commit 6db307b

Browse files
committed
feat: replace regex-parser
Signed-off-by: skjnldsv <skjnldsv@protonmail.com>
1 parent 9a11f2a commit 6db307b

3 files changed

Lines changed: 287 additions & 51 deletions

File tree

lib/node/nodeData.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ export const validateData = (data: NodeData, davService: RegExp) => {
163163
/**
164164
* In case we try to create a node from deserialized data,
165165
* we need to fix date types.
166+
* @param data
166167
*/
167168
export const fixDates = (data: NodeData) => {
168169
if (data.mtime && typeof data.mtime === 'string') {
@@ -181,8 +182,24 @@ export const fixDates = (data: NodeData) => {
181182
}
182183

183184
export const fixRegExp = (pattern: string | RegExp): RegExp => {
184-
if (typeof pattern === 'string') {
185-
return RegexParser(pattern)
185+
if (pattern instanceof RegExp) {
186+
return pattern
186187
}
187-
return pattern
188+
189+
// Extract the pattern and flags if it's a string
190+
// Pulled from https://www.npmjs.com/package/regex-parser
191+
const matches = pattern.match(/(\/?)(.+)\1([a-z]*)/i)
192+
193+
// If there's no match, throw an error
194+
if (!matches) {
195+
throw new Error('Invalid regular expression format.')
196+
}
197+
198+
// Filter valid flags: 'g', 'i', 'm', 's', 'u', and 'y'
199+
const validFlags = Array.from(new Set(matches[3]))
200+
.filter((flag) => 'gimsuy'.includes(flag))
201+
.join('')
202+
203+
// Create the regular expression
204+
return new RegExp(matches[2], validFlags)
188205
}

0 commit comments

Comments
 (0)