Skip to content

Commit

Permalink
Merge pull request #16 from mcollina/regex-escape
Browse files Browse the repository at this point in the history
Correctly escape regex string
  • Loading branch information
delvedor authored Dec 4, 2016
2 parents 60fe6ad + 9c3db0b commit 58558f1
Showing 1 changed file with 1 addition and 15 deletions.
16 changes: 1 addition & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ function build (schema, options) {
${$asNumber.toString()}
${$asNull.toString()}
${$asBoolean.toString()}
${$asRegExp.toString()}
`
var main

Expand Down Expand Up @@ -79,27 +78,14 @@ function $asString (str) {
if (str instanceof Date) {
return '"' + str.toISOString() + '"'
} else if (str instanceof RegExp) {
return $asRegExp(str)
str = str.source
} else if (typeof str !== 'string') {
str = str.toString()
}

return JSON.stringify(str)
}

function $asRegExp (reg) {
reg = reg.source

for (var i = 0, len = reg.length; i < len; i++) {
if (reg[i] === '\\' || reg[i] === '"') {
reg = reg.substring(0, i) + '\\' + reg.substring(i++)
len += 2
}
}

return '"' + reg + '"'
}

function addPatternProperties (schema, externalSchema) {
var pp = schema.patternProperties
let code = `
Expand Down

0 comments on commit 58558f1

Please sign in to comment.