Skip to content

Commit 649a47c

Browse files
authored
Merge pull request #12 from jacob-alford/main
feat(regex): add global and multiline flags
2 parents 4d37c78 + 12ae5ea commit 649a47c

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

src/regex.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,12 @@ const regexStringFromPattern: (pattern: Pattern) => string = (pattern) => {
110110
export const regexFromPattern = (
111111
pattern: Pattern,
112112
caseInsensitive = false,
113+
global = false,
114+
multiline = false,
113115
): RegExp =>
114116
new RegExp(
115-
`^(${regexStringFromPattern(pattern)})$`,
116-
caseInsensitive ? 'i' : '',
117+
`${global ? '' : '^('}${regexStringFromPattern(pattern)}${
118+
global ? '' : ')$'
119+
}`,
120+
`${global ? 'g' : ''}${caseInsensitive ? 'i' : ''}${multiline ? 'm' : ''}`,
117121
)

test/regex.test.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,34 @@ describe('kuvio', () => {
3232
k.or(k.atLeast(2)(k.exactString('bar'))),
3333
)
3434

35+
const testPattern =
36+
"(((foo){5,9}z+?y+)?.*.*?.*?[^a-z]{3}[0-4A#-'Q-T\\x1f-\\x2d\\x5e-\\x7f\\xff-\\u0100])|(bar){2,}"
37+
3538
it('can create RegExps', () => {
3639
const actual = k.regexFromPattern(pattern)
3740

38-
expect(actual.source).toEqual(
39-
"^((((foo){5,9}z+?y+)?.*.*?.*?[^a-z]{3}[0-4A#-'Q-T\\x1f-\\x2d\\x5e-\\x7f\\xff-\\u0100])|(bar){2,})$",
40-
)
41+
expect(actual.source).toEqual(`^(${testPattern})$`)
4142
expect(actual.flags).toEqual('')
4243
})
4344

4445
it('can create case-insensitive RegExps', () => {
4546
const actual = k.regexFromPattern(pattern, true)
4647

47-
expect(actual.source).toEqual(
48-
"^((((foo){5,9}z+?y+)?.*.*?.*?[^a-z]{3}[0-4A#-'Q-T\\x1f-\\x2d\\x5e-\\x7f\\xff-\\u0100])|(bar){2,})$",
49-
)
48+
expect(actual.source).toEqual(`^(${testPattern})$`)
5049
expect(actual.flags).toEqual('i')
5150
})
51+
52+
it('can create global RegExps', () => {
53+
const actual = k.regexFromPattern(pattern, false, true)
54+
55+
expect(actual.source).toEqual(testPattern)
56+
expect(actual.flags).toEqual('g')
57+
})
58+
59+
it('can create multiline RegExps', () => {
60+
const actual = k.regexFromPattern(pattern, false, false, true)
61+
62+
expect(actual.source).toEqual(`^(${testPattern})$`)
63+
expect(actual.flags).toEqual('m')
64+
})
5265
})

0 commit comments

Comments
 (0)