From b7edee7a9e62285f2120a4402a96845b068cb34b Mon Sep 17 00:00:00 2001 From: delvedor Date: Mon, 8 Aug 2016 00:14:15 +0200 Subject: [PATCH 1/3] Added regex stringify --- index.js | 22 ++++++++++++++++++++++ test.js | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/index.js b/index.js index 0ab8fb21..448fed3f 100644 --- a/index.js +++ b/index.js @@ -11,6 +11,7 @@ function build (schema) { ${$asNumber.toString()} ${$asNull.toString()} ${$asBoolean.toString()} + ${$asRegExp.toString()} ` var main @@ -36,6 +37,9 @@ function build (schema) { main = '$main' code = buildArray(schema, code, main) break + case 'RegExp': + main = $asRegExp.name + break default: throw new Error(`${schema.type} unsupported`) } @@ -115,6 +119,19 @@ function $asStringSmall (str) { return '"' + result + '"' } +function $asRegExp (reg) { + reg = reg instanceof RegExp ? reg.source : reg + + 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) { @@ -223,6 +240,11 @@ function nested (laterCode, name, key, schema) { json += ${funcName}(obj${key}) ` break + case 'RegExp': + code += ` + json += $asRegExp(obj${key}) + ` + break default: throw new Error(`${type} unsupported`) } diff --git a/test.js b/test.js index c3c7c701..f2f71f27 100644 --- a/test.js +++ b/test.js @@ -222,3 +222,38 @@ buildTest({ }, { readonly: true }) + +test('object with RexExp', (t) => { + t.plan(3) + + const schema = { + title: 'object with RegExp', + type: 'object', + properties: { + reg: { + type: 'RegExp' + }, + streg: { + type: 'RegExp' + } + } + } + + const obj = { + reg: /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/, + streg: '^(([^<>()\\[\\]\\\\.,;:\\s@"]+(\\.[^<>()\\[\\]\\\\.,;:\\s@"]+)*)|(".+"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$' + } + + const stringify = build(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.equal(obj.streg, JSON.parse(output).streg) +}) From f78eb9707f5de87cfc098c5a7ffd2057a590b285 Mon Sep 17 00:00:00 2001 From: delvedor Date: Mon, 8 Aug 2016 18:33:06 +0200 Subject: [PATCH 2/3] Parse regex objects to strings --- example.js | 6 +++++- index.js | 12 +++--------- test.js | 11 ++++------- 3 files changed, 12 insertions(+), 17 deletions(-) diff --git a/example.js b/example.js index d5549224..c9455e86 100644 --- a/example.js +++ b/example.js @@ -17,6 +17,9 @@ const stringify = fastJson({ }, now: { type: 'string' + }, + reg: { + type: 'string' } } }) @@ -25,5 +28,6 @@ console.log(stringify({ firstName: 'Matteo', lastName: 'Collina', age: 32, - now: new Date() + now: new Date(), + reg: /"([^"]|\\")*"/ })) diff --git a/index.js b/index.js index 448fed3f..20ff97ce 100644 --- a/index.js +++ b/index.js @@ -37,9 +37,6 @@ function build (schema) { main = '$main' code = buildArray(schema, code, main) break - case 'RegExp': - main = $asRegExp.name - break default: throw new Error(`${schema.type} unsupported`) } @@ -72,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() } @@ -120,7 +119,7 @@ function $asStringSmall (str) { } function $asRegExp (reg) { - reg = reg instanceof RegExp ? reg.source : reg + reg = reg.source for (var i = 0, len = reg.length; i < len; i++) { if (reg[i] === '\\' || reg[i] === '"') { @@ -240,11 +239,6 @@ function nested (laterCode, name, key, schema) { json += ${funcName}(obj${key}) ` break - case 'RegExp': - code += ` - json += $asRegExp(obj${key}) - ` - break default: throw new Error(`${type} unsupported`) } diff --git a/test.js b/test.js index f2f71f27..8e39c72a 100644 --- a/test.js +++ b/test.js @@ -231,20 +231,17 @@ test('object with RexExp', (t) => { type: 'object', properties: { reg: { - type: 'RegExp' - }, - streg: { - type: 'RegExp' + type: 'string' } } } const obj = { - reg: /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/, - streg: '^(([^<>()\\[\\]\\\\.,;:\\s@"]+(\\.[^<>()\\[\\]\\\\.,;:\\s@"]+)*)|(".+"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$' + reg: /"([^"]|\\")*"/ } const stringify = build(schema) + const validate = validator(schema) const output = stringify(obj) try { @@ -255,5 +252,5 @@ test('object with RexExp', (t) => { } t.equal(obj.reg.source, new RegExp(JSON.parse(output).reg).source) - t.equal(obj.streg, JSON.parse(output).streg) + t.ok(validate(JSON.parse(output)), 'valid schema') }) From 4f1b725d0128d73b234c29511e25d8aad729db38 Mon Sep 17 00:00:00 2001 From: delvedor Date: Tue, 9 Aug 2016 19:24:16 +0200 Subject: [PATCH 3/3] Updated README.md --- README.md | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 84828f11..b546342c 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,9 @@ const stringify = fastJson({ age: { description: 'Age in years', type: 'integer' + }, + reg: { + type: 'string' } } }) @@ -41,7 +44,8 @@ const stringify = fastJson({ console.log(stringify({ firstName: 'Matteo', lastName: 'Collina', - age: 32 + age: 32, + reg: /"([^"]|\\")*"/ })) ``` @@ -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` via `toISOString()` | +| `RegExp` | `string` | + ## Acknowledgements