Skip to content

Commit

Permalink
fix(action-code): get parent of regex
Browse files Browse the repository at this point in the history
  • Loading branch information
EdJoPaTo committed Sep 20, 2018
1 parent 51443fb commit dbb123c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
12 changes: 10 additions & 2 deletions action-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,19 @@ class ActionCode {
}

parent() {
const parts = this.code.split(':')
const isRegex = this.code instanceof RegExp
const content = isRegex ? this.code.source : this.code
const parts = content.split(':')
// Remove current
parts.pop()
const parent = parts.join(':')
return new ActionCode(parent || 'main')
let newCode
if (parent) {
newCode = isRegex ? new RegExp(parent) : parent
} else {
newCode = 'main'
}
return new ActionCode(newCode)
}
}

Expand Down
5 changes: 5 additions & 0 deletions action-code.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ test('regex', t => {
t.deepEqual(new ActionCode(/(.+)/).get(), /^(.+)$/)
})

test('regex parent', t => {
t.deepEqual(new ActionCode(/b:(.+)/).parent().get(), /^b$/)
t.deepEqual(new ActionCode(/b-(.+)/).parent().get(), 'main')
})

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

0 comments on commit dbb123c

Please sign in to comment.