Skip to content

Commit

Permalink
feat: match parser (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBuchholz committed Feb 9, 2024
1 parent 797dfe7 commit f790832
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
11 changes: 11 additions & 0 deletions reserve/src/config/parseMatch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict'

module.exports = match => {
if (match instanceof RegExp) {
return match
}
if (match && match.re) {
return new RegExp(match.re, match.flags)
}

}
23 changes: 23 additions & 0 deletions reserve/src/config/parseMatch.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict'
const { describe, it } = require('mocha')
const assert = require('assert')
const parseMatch = require('./parseMatch')

describe('config/parseMatch', () => {
it('keeps regular expression', () => {
const re = /whatever/
assert.strictEqual(parseMatch(re), re)
})

it('supports { re: "..." } syntax for configuration files', () => {
const re = parseMatch({ re: 'whatever' })
assert.ok(re instanceof RegExp)
assert.strictEqual('a whatever b'.replace(re, 'o'), 'a o b')
})

it('supports { re: "...", flags: "..." } syntax for configuration files', () => {
const re = parseMatch({ re: 'a', flags: 'g' })
assert.ok(re instanceof RegExp)
assert.strictEqual('a whatever b'.replace(re, 'o'), 'o whotever b')
})
})

0 comments on commit f790832

Please sign in to comment.