Skip to content

Commit d65d2ef

Browse files
authored
Fixes 101 (#102)
* Fixes #101 * Updated test * Removed slow down
1 parent ec7fcf6 commit d65d2ef

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,16 +207,19 @@ function $asString (str) {
207207
function $asStringSmall (str) {
208208
var result = ''
209209
var last = 0
210+
var found = false
210211
var l = str.length
211212
var point = 255
212213
for (var i = 0; i < l && point >= 32; i++) {
213214
point = str.charCodeAt(i)
214215
if (point === 34 || point === 92) {
215216
result += str.slice(last, i) + '\\'
216217
last = i
218+
found = true
217219
}
218220
}
219-
if (last === 0) {
221+
222+
if (!found) {
220223
result = str
221224
} else {
222225
result += str.slice(last)

test/basic.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,3 +328,19 @@ test('patternProperties - throw on unknown type', (t) => {
328328
t.ok(err)
329329
}
330330
})
331+
332+
test('render a single quote as JSON', (t) => {
333+
t.plan(2)
334+
335+
const schema = {
336+
type: 'string'
337+
}
338+
const toStringify = '" double quote'
339+
340+
const validate = validator(schema)
341+
const stringify = build(schema)
342+
const output = stringify(toStringify)
343+
344+
t.equal(output, JSON.stringify(toStringify))
345+
t.ok(validate(JSON.parse(output)), 'valid schema')
346+
})

0 commit comments

Comments
 (0)