Skip to content

Commit b552e14

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

3 files changed

Lines changed: 293 additions & 52 deletions

File tree

lib/node/nodeData.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55

66
import { join } from 'path'
7-
import RegexParser from 'regex-parser'
87

98
import { Permission } from '../permissions'
109
import { NodeStatus } from './node'
@@ -163,6 +162,8 @@ export const validateData = (data: NodeData, davService: RegExp) => {
163162
/**
164163
* In case we try to create a node from deserialized data,
165164
* we need to fix date types.
165+
*
166+
* @param data The internal node data
166167
*/
167168
export const fixDates = (data: NodeData) => {
168169
if (data.mtime && typeof data.mtime === 'string') {
@@ -180,9 +181,30 @@ export const fixDates = (data: NodeData) => {
180181
}
181182
}
182183

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

0 commit comments

Comments
 (0)