File tree Expand file tree Collapse file tree 1 file changed +31
-1
lines changed Expand file tree Collapse file tree 1 file changed +31
-1
lines changed Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ function build (schema, options) {
15
15
`
16
16
code += `
17
17
${ $asString . toString ( ) }
18
+ ${ $asStringSmall . toString ( ) }
18
19
${ $asNumber . toString ( ) }
19
20
${ $asNull . toString ( ) }
20
21
${ $asBoolean . toString ( ) }
@@ -83,7 +84,36 @@ function $asString (str) {
83
84
str = str . toString ( )
84
85
}
85
86
86
- return JSON . stringify ( str )
87
+ if ( str . length < 42 ) {
88
+ return $asStringSmall ( str )
89
+ } else {
90
+ return JSON . stringify ( str )
91
+ }
92
+ }
93
+
94
+ // magically escape strings for json
95
+ // relying on their charCodeAt
96
+ // everything below 32 needs JSON.stringify()
97
+ // 34 and 92 happens all the time, so we
98
+ // have a fast case for them
99
+ function $asStringSmall ( str ) {
100
+ var result = ''
101
+ var last = 0
102
+ var l = str . length
103
+ var point = 255
104
+ for ( var i = 0 ; i < l && point >= 32 ; i ++ ) {
105
+ point = str . charCodeAt ( i )
106
+ if ( point === 34 || point === 92 ) {
107
+ result += str . slice ( last , i ) + '\\' + str [ i ]
108
+ last = i + 1
109
+ }
110
+ }
111
+ if ( last === 0 ) {
112
+ result = str
113
+ } else {
114
+ result += str . slice ( last )
115
+ }
116
+ return point < 32 ? JSON . stringify ( str ) : '"' + result + '"'
87
117
}
88
118
89
119
function addPatternProperties ( schema , externalSchema ) {
You can’t perform that action at this time.
0 commit comments