forked from badges/shields
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pattern-helpers.ts
34 lines (32 loc) · 1 KB
/
pattern-helpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { parse } from 'path-to-regexp'
// Given a patternToRegex `pattern` with multiple-choice options like
// `foo|bar|baz`, return an array with the options. If it can't be described
// as multiple-choice options, return `undefined`.
const basicChars = /^[A-za-z0-9-]+$/
export function patternToOptions(pattern: string): string[] | undefined {
const split = pattern.split('|')
if (split.some(part => !part.match(basicChars))) {
return undefined
} else {
return split
}
}
// Removes regexp for named parameters.
export function removeRegexpFromPattern(pattern: string): string {
const tokens = parse(pattern)
const simplePattern = tokens
.map(token => {
if (typeof token === 'string') {
return token
} else {
const { prefix, modifier, name, pattern } = token
if (typeof name === 'number') {
return `${prefix}(${pattern})`
} else {
return `${prefix}:${name}${modifier}`
}
}
})
.join('')
return simplePattern
}