Skip to content

Commit

Permalink
update playground example
Browse files Browse the repository at this point in the history
  • Loading branch information
didavid61202 committed Apr 14, 2023
1 parent 87922c5 commit 5a5e756
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions playground/index.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import assert from 'node:assert'
import { createRegExp, exactly, digit, oneOrMore, char, wordChar } from 'magic-regexp'
import { createRegExp, exactly, maybe, digit, oneOrMore, char, wordChar } from 'magic-regexp'
/**
* change to
* import {...} from 'magic-regexp/type-level-regexp'
* to try type level RegExp match results (experimental)
*/

// Typed capture groups
const ID_RE = createRegExp(exactly('id-').and(digit.times(5).groupedAs('id')))
Expand All @@ -8,22 +13,18 @@ console.log(ID_RE, groups?.id)

// Quick-and-dirty semver
const SEMVER_RE = createRegExp(
oneOrMore(digit)
.groupedAs('major')
.and('.')
.and(oneOrMore(digit).groupedAs('minor'))
.and(exactly('.').and(oneOrMore(char).groupedAs('patch')).optionally())
oneOrMore(digit).groupedAs('major'),
'.',
oneOrMore(digit).groupedAs('minor'),
maybe('.', oneOrMore(char).groupedAs('patch'))
)
console.log(SEMVER_RE)

assert.equal(createRegExp(exactly('foo/test.js').after('bar/')).test('bar/foo/test.js'), true)

// References to previously captured groups using the group name
const TENET_RE = createRegExp(
wordChar
.groupedAs('firstChar')
.and(wordChar.groupedAs('secondChar'))
.and(oneOrMore(char))
exactly(wordChar.groupedAs('firstChar'), wordChar.groupedAs('secondChar'), oneOrMore(char))
.and.referenceTo('secondChar')
.and.referenceTo('firstChar')
)
Expand Down

0 comments on commit 5a5e756

Please sign in to comment.