From ba11f280b04b347f5f5adcc1d4e8584b05bf269b Mon Sep 17 00:00:00 2001 From: Ethan Resnick Date: Sun, 26 Mar 2017 00:06:17 -0400 Subject: [PATCH 1/2] Support dates We detect dates simply by doing an instanceof check on raw value being serialized. We're also now looking at the typeof the *rawValue* not the toJSON()ed value when determining whether a value is a function or a RegExp. This should allow functions and regexs to be serialized properly even if a Function.prototype.toJSON or a RegExp.prototype.toJSON has been added for some reason. Finally, for consistency and because util.isRegExp has been deprecated, we also update our logic for detecting RegExps to use the same strategy as for Dates. --- README.md | 7 ++++--- index.js | 32 ++++++++++++++++++++------------ test/unit/serialize.js | 20 ++++++++++++++++++++ 3 files changed, 44 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 9c0081b..aeb9661 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ Serialize JavaScript ==================== -Serialize JavaScript to a _superset_ of JSON that includes regular expressions and functions. +Serialize JavaScript to a _superset_ of JSON that includes regular expressions, dates and functions. [![npm Version][npm-badge]][npm] [![Dependency Status][david-badge]][david] @@ -11,7 +11,7 @@ Serialize JavaScript to a _superset_ of JSON that includes regular expressions a The code in this package began its life as an internal module to [express-state][]. To expand its usefulness, it now lives as `serialize-javascript` — an independent package on npm. -You're probably wondering: **What about `JSON.stringify()`!?** We've found that sometimes we need to serialize JavaScript **functions** and **regexps**. A great example is a web app that uses client-side URL routing where the route definitions are regexps that need to be shared from the server to the client. +You're probably wondering: **What about `JSON.stringify()`!?** We've found that sometimes we need to serialize JavaScript **functions**, **regexps** or **dates**. A great example is a web app that uses client-side URL routing where the route definitions are regexps that need to be shared from the server to the client. The string returned from this package's single export function is literal JavaScript which can be saved to a `.js` file, or be embedded into an HTML document by making the content of a `')).to.equal('"\\u003C\\u002Fscript\\u003E"'); From 3a13d54fd8d5e3cb1b49e76847cdc427384db112 Mon Sep 17 00:00:00 2001 From: Ethan Resnick Date: Sun, 26 Mar 2017 00:06:51 -0400 Subject: [PATCH 2/2] Test using isJSON and space options together --- test/unit/serialize.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/unit/serialize.js b/test/unit/serialize.js index 361c88b..33d79c3 100644 --- a/test/unit/serialize.js +++ b/test/unit/serialize.js @@ -217,6 +217,7 @@ describe('serialize( obj )', function () { expect(serialize(fn, {isJSON: false})).to.equal('function fn() { return true; }'); expect(serialize(fn, {isJSON: true})).to.equal('undefined'); + expect(serialize([1], {isJSON: true, space: 2})).to.equal('[\n 1\n]'); }); });