Skip to content

Commit 791f427

Browse files
authored
Merge pull request #1 from delvedor/regex-proposal
Regex stringify proposal
2 parents 895646c + 4f1b725 commit 791f427

File tree

4 files changed

+67
-4
lines changed

4 files changed

+67
-4
lines changed

README.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,18 @@ const stringify = fastJson({
3434
age: {
3535
description: 'Age in years',
3636
type: 'integer'
37+
},
38+
reg: {
39+
type: 'string'
3740
}
3841
}
3942
})
4043

4144
console.log(stringify({
4245
firstName: 'Matteo',
4346
lastName: 'Collina',
44-
age: 32
47+
age: 32,
48+
reg: /"([^"]|\\")*"/
4549
}))
4650
```
4751

@@ -61,8 +65,15 @@ Supported types:
6165
* `'boolean'`
6266
* `'null'`
6367

64-
And nested ones, too.
65-
`Date` instances are serialized with `toISOString()`.
68+
And nested ones, too.
69+
70+
*Specific use cases:*
71+
72+
| Instance | Serialized as |
73+
| -----------|---------------------------------------------|
74+
| `Date` | `string` <small>via `toISOString()`</small> |
75+
| `RegExp` | `string` |
76+
6677

6778
## Acknowledgements
6879

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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ function build (schema) {
1111
${$asNumber.toString()}
1212
${$asNull.toString()}
1313
${$asBoolean.toString()}
14+
${$asRegExp.toString()}
1415
`
1516
var main
1617

@@ -68,6 +69,8 @@ function $asBoolean (bool) {
6869
function $asString (str) {
6970
if (str instanceof Date) {
7071
return '"' + str.toISOString() + '"'
72+
} else if (str instanceof RegExp) {
73+
return $asRegExp(str)
7174
} else if (typeof str !== 'string') {
7275
str = str.toString()
7376
}
@@ -115,6 +118,19 @@ function $asStringSmall (str) {
115118
return '"' + result + '"'
116119
}
117120

121+
function $asRegExp (reg) {
122+
reg = reg.source
123+
124+
for (var i = 0, len = reg.length; i < len; i++) {
125+
if (reg[i] === '\\' || reg[i] === '"') {
126+
reg = reg.substring(0, i) + '\\' + reg.substring(i++)
127+
len += 2
128+
}
129+
}
130+
131+
return '"' + reg + '"'
132+
}
133+
118134
function buildObject (schema, code, name) {
119135
code += `
120136
function ${name} (obj) {

test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,35 @@ buildTest({
222222
}, {
223223
readonly: true
224224
})
225+
226+
test('object with RexExp', (t) => {
227+
t.plan(3)
228+
229+
const schema = {
230+
title: 'object with RegExp',
231+
type: 'object',
232+
properties: {
233+
reg: {
234+
type: 'string'
235+
}
236+
}
237+
}
238+
239+
const obj = {
240+
reg: /"([^"]|\\")*"/
241+
}
242+
243+
const stringify = build(schema)
244+
const validate = validator(schema)
245+
const output = stringify(obj)
246+
247+
try {
248+
JSON.parse(output)
249+
t.pass()
250+
} catch (e) {
251+
t.fail()
252+
}
253+
254+
t.equal(obj.reg.source, new RegExp(JSON.parse(output).reg).source)
255+
t.ok(validate(JSON.parse(output)), 'valid schema')
256+
})

0 commit comments

Comments
 (0)