File tree Expand file tree Collapse file tree 4 files changed +67
-4
lines changed Expand file tree Collapse file tree 4 files changed +67
-4
lines changed Original file line number Diff line number Diff line change @@ -34,14 +34,18 @@ const stringify = fastJson({
34
34
age: {
35
35
description: ' Age in years' ,
36
36
type: ' integer'
37
+ },
38
+ reg: {
39
+ type: ' string'
37
40
}
38
41
}
39
42
})
40
43
41
44
console .log (stringify ({
42
45
firstName: ' Matteo' ,
43
46
lastName: ' Collina' ,
44
- age: 32
47
+ age: 32 ,
48
+ reg: / "([^ "] | \\ ")* "/
45
49
}))
46
50
```
47
51
@@ -61,8 +65,15 @@ Supported types:
61
65
* ` 'boolean' `
62
66
* ` 'null' `
63
67
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
+
66
77
67
78
## Acknowledgements
68
79
Original file line number Diff line number Diff line change @@ -17,6 +17,9 @@ const stringify = fastJson({
17
17
} ,
18
18
now : {
19
19
type : 'string'
20
+ } ,
21
+ reg : {
22
+ type : 'string'
20
23
}
21
24
}
22
25
} )
@@ -25,5 +28,6 @@ console.log(stringify({
25
28
firstName : 'Matteo' ,
26
29
lastName : 'Collina' ,
27
30
age : 32 ,
28
- now : new Date ( )
31
+ now : new Date ( ) ,
32
+ reg : / " ( [ ^ " ] | \\ " ) * " /
29
33
} ) )
Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ function build (schema) {
11
11
${ $asNumber . toString ( ) }
12
12
${ $asNull . toString ( ) }
13
13
${ $asBoolean . toString ( ) }
14
+ ${ $asRegExp . toString ( ) }
14
15
`
15
16
var main
16
17
@@ -68,6 +69,8 @@ function $asBoolean (bool) {
68
69
function $asString ( str ) {
69
70
if ( str instanceof Date ) {
70
71
return '"' + str . toISOString ( ) + '"'
72
+ } else if ( str instanceof RegExp ) {
73
+ return $asRegExp ( str )
71
74
} else if ( typeof str !== 'string' ) {
72
75
str = str . toString ( )
73
76
}
@@ -115,6 +118,19 @@ function $asStringSmall (str) {
115
118
return '"' + result + '"'
116
119
}
117
120
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
+
118
134
function buildObject ( schema , code , name ) {
119
135
code += `
120
136
function ${ name } (obj) {
Original file line number Diff line number Diff line change @@ -222,3 +222,35 @@ buildTest({
222
222
} , {
223
223
readonly : true
224
224
} )
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
+ } )
You can’t perform that action at this time.
0 commit comments