Skip to content

Commit

Permalink
feat(action-code): implement concat of two regex
Browse files Browse the repository at this point in the history
BREAKING CHANGE: action as RegExp can not have any flag anymore.
  • Loading branch information
EdJoPaTo committed Sep 17, 2018
1 parent 7ab0302 commit 19cb546
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
17 changes: 7 additions & 10 deletions action-code.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
class ActionCode {
constructor(actionCode) {
if (actionCode instanceof RegExp) {
if (actionCode.flags.replace('i', '') !== '') {
throw new Error('flags exept i are not supported')
if (actionCode.flags !== '') {
throw new Error('RegExp flags are not supported')
}
console.assert(actionCode.flags === '', 'using ActionCode with RegExp Flags is depricated')
if (actionCode.source.startsWith('^') || actionCode.source.endsWith('$')) {
throw new Error('begin or end anchors are not supported (^, $)')
}
Expand Down Expand Up @@ -47,17 +46,15 @@ class ActionCode {
if (action instanceof ActionCode) {
action = action.code
}
if (this.code instanceof RegExp) {
throw new TypeError('concat to an RegExp is currently not supported. Open an Issue for it.')
}
if (this.code === 'main') {
return new ActionCode(action)
}
if (action instanceof RegExp) {
const source = this.code + ':' + action.source
return new ActionCode(new RegExp(source, action.flags))
if (typeof this.code === 'string' && typeof action === 'string') {
return new ActionCode(this.code + ':' + action)
}
return new ActionCode(this.code + ':' + action)
const prefix = this.code.source || this.code
action = action.source || action
return new ActionCode(new RegExp(prefix + ':' + action))
}

parent() {
Expand Down
14 changes: 10 additions & 4 deletions action-code.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,29 @@ test('concat an ActionCode string', t => {
})

test('concat an ActionCode regex', t => {
t.deepEqual(new ActionCode('b').concat(new ActionCode(/(.+)/i)).get(), /^b:(.+)$/i)
t.deepEqual(new ActionCode('b').concat(new ActionCode(/(.+)/)).get(), /^b:(.+)$/)
})

test('regex', t => {
t.deepEqual(new ActionCode(/(.+)/).get(), /^(.+)$/)
t.deepEqual(new ActionCode(/(.+)/i).get(), /^(.+)$/i)
})

test('concat string with regex', t => {
t.deepEqual(new ActionCode('b').concat(/(.+)/).get(), /^b:(.+)$/)
t.deepEqual(new ActionCode('b').concat(/(.+)/i).get(), /^b:(.+)$/i)
})

test('concat regex with string', t => {
t.deepEqual(new ActionCode(/foo/).concat('bar').get(), /^foo:bar$/)
})

test('concat regex with regex', t => {
t.deepEqual(new ActionCode(/foo/).concat(/bar/).get(), /^foo:bar$/)
})

test('regex fail flags', t => {
t.throws(() => new ActionCode(/42/g), /flags/)
t.throws(() => new ActionCode(/42/gi), /flags/)
t.notThrows(() => new ActionCode(/42/i), /flags/)
t.throws(() => new ActionCode(/42/i), /flags/)
})

test('regex fail anchors', t => {
Expand Down

0 comments on commit 19cb546

Please sign in to comment.