From 4c6eb793de5a9866dfb9ea1ac44f67f5631d1eb8 Mon Sep 17 00:00:00 2001 From: Fabian Jakobs Date: Thu, 29 Oct 2015 15:13:31 +0000 Subject: [PATCH 1/2] extract into module --- node_modules/c9/json-with-re.js | 28 ++++++++++++++++++++++++++++ node_modules/c9/json-with-re_test.js | 27 +++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 node_modules/c9/json-with-re.js create mode 100644 node_modules/c9/json-with-re_test.js diff --git a/node_modules/c9/json-with-re.js b/node_modules/c9/json-with-re.js new file mode 100644 index 00000000..0f748bc1 --- /dev/null +++ b/node_modules/c9/json-with-re.js @@ -0,0 +1,28 @@ +/** + * JSON (de-)serializer with support for encosing regular expressions + */ +"use strict"; + +exports.replacer = function(key, value) { + if (value instanceof RegExp) + return ("__REGEXP " + value.toString()); + else + return value; +}; + +exports.reviver = function(key, value) { + if ((value || "").toString().indexOf("__REGEXP ") == 0) { + var m = value.split("__REGEXP ")[1].match(/\/(.*)\/(.*)?/); + return new RegExp(m[1], m[2] || ""); + } + else + return value; +}; + +exports.stringify = function(value, space) { + return JSON.stringify(value, exports.replacer, space); +}; + +exports.parse = function(rext) { + return JSON.parse(rext, exports.reviver); +}; \ No newline at end of file diff --git a/node_modules/c9/json-with-re_test.js b/node_modules/c9/json-with-re_test.js new file mode 100644 index 00000000..5ccb3ecb --- /dev/null +++ b/node_modules/c9/json-with-re_test.js @@ -0,0 +1,27 @@ +/*global describe it before after beforeEach afterEach define*/ +"use strict"; +"use server"; +"use mocha"; + +require("c9/inline-mocha")(module); + +var assert = require("assert-diff"); +var reJSON = require("./json-with-re"); + +describe(__filename, function(){ + it("should encode regular expressions", function() { + assert.deepEqual(reJSON.stringify({ foo: /foo/ }), '{"foo":"__REGEXP /foo/"}'); + assert.deepEqual(reJSON.stringify({ foo: /foo\//gi }), "{\"foo\":\"__REGEXP /foo\\\\//gi\"}"); + }); + it("should decode regular expressions", function() { + assert.deepEqual(reJSON.parse('{"foo":"__REGEXP /foo/"}'), { foo: /foo/ }); + assert.deepEqual(reJSON.parse("{\"foo\":\"__REGEXP /foo\\\\//gi\"}"), { foo: /foo\//gi }); + }); + it("should deal with null values", function() { + var o = { + foo: null, + bar: /dd/ + }; + assert.deepEqual(reJSON.parse(reJSON.stringify(o)), o); + }); +}); From 0c280207fbf8c3b33cf0486afea6a3324e42c7dc Mon Sep 17 00:00:00 2001 From: Fabian Jakobs Date: Thu, 29 Oct 2015 15:18:16 +0000 Subject: [PATCH 2/2] cleanup --- node_modules/c9/json-with-re.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/node_modules/c9/json-with-re.js b/node_modules/c9/json-with-re.js index 0f748bc1..e8f475db 100644 --- a/node_modules/c9/json-with-re.js +++ b/node_modules/c9/json-with-re.js @@ -11,9 +11,9 @@ exports.replacer = function(key, value) { }; exports.reviver = function(key, value) { - if ((value || "").toString().indexOf("__REGEXP ") == 0) { - var m = value.split("__REGEXP ")[1].match(/\/(.*)\/(.*)?/); - return new RegExp(m[1], m[2] || ""); + if ((value + "").indexOf("__REGEXP ") == 0) { + var m = value.match(/__REGEXP \/(.*)\/(.*)?/); + return new RegExp(m[1], m[2]); } else return value;