Skip to content

Commit

Permalink
#121 - replace only the end occurrence of a forward slash along with …
Browse files Browse the repository at this point in the history
…any possible flags
  • Loading branch information
svile committed Apr 27, 2021
1 parent c1f4f7d commit 290123b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/StringSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ const StringSchema = (
pattern = pattern
.toString()
.substr(1)
.replace(`/${flags}`, '')
.replace(new RegExp(`/${flags}$`), '')
}

return setAttribute({ schema, ...options }, ['pattern', pattern, 'string'])
Expand Down
22 changes: 17 additions & 5 deletions src/StringSchema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,34 @@ describe('StringSchema', () => {
it('as a string', () => {
expect(
StringSchema()
.pattern('.*')
.pattern('\\/.*\\/')
.valueOf()
).toEqual({
type: 'string',
pattern: '.*',
pattern: '\\/.*\\/',
})
})
it('as a regex', () => {
it('as a regex without flags', () => {
const prop = 'prop'
expect(
StringSchema()
.pattern(/.*/gi)
.pattern(/\/.*\//)
.valueOf()
).toEqual({
type: 'string',
pattern: '.*',
pattern: '\\/.*\\/',
})
})

it('as a regex with flags', () => {
const prop = 'prop'
expect(
StringSchema()
.pattern(/\/.*\//gi)
.valueOf()
).toEqual({
type: 'string',
pattern: '\\/.*\\/',
})
})

Expand Down

0 comments on commit 290123b

Please sign in to comment.