Skip to content

Commit f78eb97

Browse files
committed
Parse regex objects to strings
1 parent b7edee7 commit f78eb97

File tree

3 files changed

+12
-17
lines changed

3 files changed

+12
-17
lines changed

example.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ const stringify = fastJson({
1717
},
1818
now: {
1919
type: 'string'
20+
},
21+
reg: {
22+
type: 'string'
2023
}
2124
}
2225
})
@@ -25,5 +28,6 @@ console.log(stringify({
2528
firstName: 'Matteo',
2629
lastName: 'Collina',
2730
age: 32,
28-
now: new Date()
31+
now: new Date(),
32+
reg: /"([^"]|\\")*"/
2933
}))

index.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ function build (schema) {
3737
main = '$main'
3838
code = buildArray(schema, code, main)
3939
break
40-
case 'RegExp':
41-
main = $asRegExp.name
42-
break
4340
default:
4441
throw new Error(`${schema.type} unsupported`)
4542
}
@@ -72,6 +69,8 @@ function $asBoolean (bool) {
7269
function $asString (str) {
7370
if (str instanceof Date) {
7471
return '"' + str.toISOString() + '"'
72+
} else if (str instanceof RegExp) {
73+
return $asRegExp(str)
7574
} else if (typeof str !== 'string') {
7675
str = str.toString()
7776
}
@@ -120,7 +119,7 @@ function $asStringSmall (str) {
120119
}
121120

122121
function $asRegExp (reg) {
123-
reg = reg instanceof RegExp ? reg.source : reg
122+
reg = reg.source
124123

125124
for (var i = 0, len = reg.length; i < len; i++) {
126125
if (reg[i] === '\\' || reg[i] === '"') {
@@ -240,11 +239,6 @@ function nested (laterCode, name, key, schema) {
240239
json += ${funcName}(obj${key})
241240
`
242241
break
243-
case 'RegExp':
244-
code += `
245-
json += $asRegExp(obj${key})
246-
`
247-
break
248242
default:
249243
throw new Error(`${type} unsupported`)
250244
}

test.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -231,20 +231,17 @@ test('object with RexExp', (t) => {
231231
type: 'object',
232232
properties: {
233233
reg: {
234-
type: 'RegExp'
235-
},
236-
streg: {
237-
type: 'RegExp'
234+
type: 'string'
238235
}
239236
}
240237
}
241238

242239
const obj = {
243-
reg: /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
244-
streg: '^(([^<>()\\[\\]\\\\.,;:\\s@"]+(\\.[^<>()\\[\\]\\\\.,;:\\s@"]+)*)|(".+"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$'
240+
reg: /"([^"]|\\")*"/
245241
}
246242

247243
const stringify = build(schema)
244+
const validate = validator(schema)
248245
const output = stringify(obj)
249246

250247
try {
@@ -255,5 +252,5 @@ test('object with RexExp', (t) => {
255252
}
256253

257254
t.equal(obj.reg.source, new RegExp(JSON.parse(output).reg).source)
258-
t.equal(obj.streg, JSON.parse(output).streg)
255+
t.ok(validate(JSON.parse(output)), 'valid schema')
259256
})

0 commit comments

Comments
 (0)