serialize-javascript
Advanced tools
Comparing version 1.3.0 to 1.4.0
32
index.js
@@ -9,7 +9,5 @@ /* | ||
var isRegExp = require('util').isRegExp; | ||
// Generate an internal UID to make the regexp pattern harder to guess. | ||
var UID = Math.floor(Math.random() * 0x10000000000).toString(16); | ||
var PLACE_HOLDER_REGEXP = new RegExp('"@__(F|R)-' + UID + '-(\\d+)__@"', 'g'); | ||
var PLACE_HOLDER_REGEXP = new RegExp('"@__(F|R|D)-' + UID + '-(\\d+)__@"', 'g'); | ||
@@ -43,2 +41,3 @@ var IS_NATIVE_CODE_REGEXP = /\{\s*\[native code\]\s*\}/g; | ||
var regexps = []; | ||
var dates = []; | ||
@@ -52,14 +51,19 @@ // Returns placeholders for functions and regexps (identified by index) | ||
var type = typeof value; | ||
// If the value is an object w/ a toJSON method, toJSON is called before | ||
// the replacer runs, so we use this[key] to get the non-toJSONed value. | ||
var origValue = this[key]; | ||
var type = typeof origValue; | ||
if (type === 'object') { | ||
if (isRegExp(value)) { | ||
return '@__R-' + UID + '-' + (regexps.push(value) - 1) + '__@'; | ||
if(origValue instanceof RegExp) { | ||
return '@__R-' + UID + '-' + (regexps.push(origValue) - 1) + '__@'; | ||
} | ||
return value; | ||
if(origValue instanceof Date) { | ||
return '@__D-' + UID + '-' + (dates.push(origValue) - 1) + '__@'; | ||
} | ||
} | ||
if (type === 'function') { | ||
return '@__F-' + UID + '-' + (functions.push(value) - 1) + '__@'; | ||
return '@__F-' + UID + '-' + (functions.push(origValue) - 1) + '__@'; | ||
} | ||
@@ -91,10 +95,14 @@ | ||
if (functions.length === 0 && regexps.length === 0) { | ||
if (functions.length === 0 && regexps.length === 0 && dates.length === 0) { | ||
return str; | ||
} | ||
// Replaces all occurrences of function and regexp placeholders in the JSON | ||
// string with their string representations. If the original value can not | ||
// be found, then `undefined` is used. | ||
// Replaces all occurrences of function, regexp and date placeholders in the | ||
// JSON string with their string representations. If the original value can | ||
// not be found, then `undefined` is used. | ||
return str.replace(PLACE_HOLDER_REGEXP, function (match, type, valueIndex) { | ||
if (type === 'D') { | ||
return "new Date(\"" + dates[valueIndex].toISOString() + "\")"; | ||
} | ||
if (type === 'R') { | ||
@@ -101,0 +109,0 @@ return regexps[valueIndex].toString(); |
{ | ||
"name": "serialize-javascript", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "Serialize JavaScript to a superset of JSON that includes regular expressions and functions.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
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. | ||
@@ -14,3 +14,3 @@ [![npm Version][npm-badge]][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. | ||
@@ -40,2 +40,3 @@ 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 `<script>` element. **HTML charaters and JavaScript line terminators are escaped automatically.** | ||
undef: undefined, | ||
date: new Date("Thu, 28 Apr 2016 22:02:17 GMT"), | ||
@@ -50,3 +51,3 @@ fn: function echo(arg) { return arg; }, | ||
```js | ||
'{"str":"string","num":0,"obj":{"foo":"foo"},"arr":[1,2,3],"bool":true,"nil":null,"fn":function echo(arg) { return arg; },"re":/([^\\s]+)/g}' | ||
'{"str":"string","num":0,"obj":{"foo":"foo"},"arr":[1,2,3],"bool":true,"nil":null,date:new Date("2016-04-28T22:02:17.156Z"),"fn":function echo(arg) { return arg; },"re":/([^\\s]+)/g}' | ||
``` | ||
@@ -94,2 +95,14 @@ | ||
## Deserializing | ||
For some use cases you might also need to deserialize the string. This is explicitely not part of this module. However, you can easily write it yourself: | ||
```js | ||
function deserialize(serializedJavascript){ | ||
return eval('(' + serializedJavascript + ')'); | ||
} | ||
``` | ||
**Note:** Don't forget the parentheses around the serialized javascript, as the opening bracket `{` will be considered to be the start of a body. | ||
## License | ||
@@ -96,0 +109,0 @@ |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
10985
93
119
1