Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,18 @@ const stringify = fastJson({
age: {
description: 'Age in years',
type: 'integer'
},
reg: {
type: 'string'
}
}
})

console.log(stringify({
firstName: 'Matteo',
lastName: 'Collina',
age: 32
age: 32,
reg: /"([^"]|\\")*"/
}))
```

Expand All @@ -61,8 +65,15 @@ Supported types:
* `'boolean'`
* `'null'`

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

Expand Down
6 changes: 5 additions & 1 deletion example.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ const stringify = fastJson({
},
now: {
type: 'string'
},
reg: {
type: 'string'
}
}
})
Expand All @@ -25,5 +28,6 @@ console.log(stringify({
firstName: 'Matteo',
lastName: 'Collina',
age: 32,
now: new Date()
now: new Date(),
reg: /"([^"]|\\")*"/
}))
16 changes: 16 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function build (schema) {
${$asNumber.toString()}
${$asNull.toString()}
${$asBoolean.toString()}
${$asRegExp.toString()}
`
var main

Expand Down Expand Up @@ -68,6 +69,8 @@ function $asBoolean (bool) {
function $asString (str) {
if (str instanceof Date) {
return '"' + str.toISOString() + '"'
} else if (str instanceof RegExp) {
return $asRegExp(str)
} else if (typeof str !== 'string') {
str = str.toString()
}
Expand Down Expand Up @@ -115,6 +118,19 @@ function $asStringSmall (str) {
return '"' + result + '"'
}

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) {
code += `
function ${name} (obj) {
Expand Down
32 changes: 32 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,35 @@ buildTest({
}, {
readonly: true
})

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')
})