Skip to content

Commit

Permalink
Merge pull request #97 from leifg/implement-functions-01
Browse files Browse the repository at this point in the history
Implement REGEX
  • Loading branch information
leifg committed Apr 14, 2016
2 parents ca3b95f + bd80a85 commit d92f675
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/functionDispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ const functionValidations = {
len: validateNumOfParams(1),
mid: validateNumOfParams(3),
right: validateNumOfParams(2),
trim: validateNumOfParams(1)
trim: validateNumOfParams(1),
regex: validateNumOfParams(3)
}
7 changes: 7 additions & 0 deletions src/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,10 @@ export const sf$trim = (text) => {
export const sf$upper = (text, locale) => {
return buildLiteralFromJs(text.value.toUpperCase())
}

// Advanced Functions

export const sf$regex = (text, regexText) => {
let r = new RegExp(`^${regexText.value}$`)
return buildLiteralFromJs(r.exec(text.value) != null)
}
28 changes: 28 additions & 0 deletions test/functions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -556,3 +556,31 @@ describe('upper', () => {
expect(functions.sf$upper(buildLiteralFromJs('mycompany.com'))).to.deep.eq(buildLiteralFromJs('MYCOMPANY.COM'))
})
})

// Advanced Functions

describe('regex', () => {
it('returns true for match', () => {
let text = buildLiteralFromJs('999-99-9999')
let regexText = buildLiteralFromJs('[0-9]{3}-[0-9]{2}-[0-9]{4}')
expect(functions.sf$regex(text, regexText)).to.deep.eq(buildLiteralFromJs(true))
})

it ('returns false for non-match', () => {
let text = buildLiteralFromJs('something else')
let regexText = buildLiteralFromJs('[0-9]{3}-[0-9]{2}-[0-9]{4}')
expect(functions.sf$regex(text, regexText)).to.deep.eq(buildLiteralFromJs(false))
})

it('matches complete string', () => {
let text = buildLiteralFromJs('According to Marc Benioff, the social enterprise increases customer success.')
let regexText = buildLiteralFromJs('.*Marc Benioff.*')
expect(functions.sf$regex(text, regexText)).to.deep.eq(buildLiteralFromJs(true))
})

it('does not match partial string', () => {
let text = buildLiteralFromJs('According to Marc Benioff, the social enterprise increases customer success.')
let regexText = buildLiteralFromJs('Marc Benioff')
expect(functions.sf$regex(text, regexText)).to.deep.eq(buildLiteralFromJs(false))
})
})

0 comments on commit d92f675

Please sign in to comment.