fast-json-stringify
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -20,2 +20,5 @@ 'use strict' | ||
type: 'string' | ||
}, | ||
reg: { | ||
type: 'string' | ||
} | ||
@@ -29,3 +32,4 @@ } | ||
age: 32, | ||
now: new Date() | ||
now: new Date(), | ||
reg: /"([^"]|\\")*"/ | ||
})) |
16
index.js
@@ -14,2 +14,3 @@ 'use strict' | ||
${$asBoolean.toString()} | ||
${$asRegExp.toString()} | ||
` | ||
@@ -72,2 +73,4 @@ var main | ||
return '"' + str.toISOString() + '"' | ||
} else if (str instanceof RegExp) { | ||
return $asRegExp(str) | ||
} else if (typeof str !== 'string') { | ||
@@ -120,2 +123,15 @@ str = str.toString() | ||
function $asRegExp (reg) { | ||
reg = reg.source | ||
for (var i = 0, len = reg.length; i < len; i++) { | ||
if (reg[i] === '\\' || reg[i] === '"') { | ||
reg = reg.substring(0, i) + '\\' + reg.substring(i++) | ||
len += 2 | ||
} | ||
} | ||
return '"' + reg + '"' | ||
} | ||
function buildObject (schema, code, name) { | ||
@@ -122,0 +138,0 @@ code += ` |
{ | ||
"name": "fast-json-stringify", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Stringify your JSON at max speed", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -37,2 +37,5 @@ # fast-json-stringify [![Build Status](https://travis-ci.org/mcollina/fast-json-stringify.svg)](https://travis-ci.org/mcollina/fast-json-stringify) | ||
type: 'integer' | ||
}, | ||
reg: { | ||
type: 'string' | ||
} | ||
@@ -45,3 +48,4 @@ } | ||
lastName: 'Collina', | ||
age: 32 | ||
age: 32, | ||
reg: /"([^"]|\\")*"/ | ||
})) | ||
@@ -66,5 +70,12 @@ ``` | ||
And nested ones, too. | ||
`Date` instances are serialized with `toISOString()`. | ||
And nested ones, too. | ||
*Specific use cases:* | ||
| Instance | Serialized as | | ||
| -----------|---------------------------------------------| | ||
| `Date` | `string` <small>via `toISOString()`</small> | | ||
| `RegExp` | `string` | | ||
## Acknowledgements | ||
@@ -71,0 +82,0 @@ |
32
test.js
@@ -225,1 +225,33 @@ 'use strict' | ||
}) | ||
test('object with RexExp', (t) => { | ||
t.plan(3) | ||
const schema = { | ||
title: 'object with RegExp', | ||
type: 'object', | ||
properties: { | ||
reg: { | ||
type: 'string' | ||
} | ||
} | ||
} | ||
const obj = { | ||
reg: /"([^"]|\\")*"/ | ||
} | ||
const stringify = build(schema) | ||
const validate = validator(schema) | ||
const output = stringify(obj) | ||
try { | ||
JSON.parse(output) | ||
t.pass() | ||
} catch (e) { | ||
t.fail() | ||
} | ||
t.equal(obj.reg.source, new RegExp(JSON.parse(output).reg).source) | ||
t.ok(validate(JSON.parse(output)), 'valid schema') | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15863
582
85