serialize-to-js
Advanced tools
Comparing version 0.1.2 to 0.2.0
@@ -6,5 +6,5 @@ /* | ||
'use strict'; | ||
'use strict' | ||
var KEY = /^[a-zA-Z$_][a-zA-Z$_0-9]*$/; | ||
var KEY = /^[a-zA-Z$_][a-zA-Z$_0-9]*$/ | ||
@@ -17,6 +17,6 @@ /** | ||
function Ref (references) { | ||
this.keys = []; | ||
this.refs = []; | ||
this.key = []; | ||
this.references = references || []; | ||
this.keys = [] | ||
this.refs = [] | ||
this.key = [] | ||
this.references = references || [] | ||
} | ||
@@ -30,66 +30,65 @@ | ||
*/ | ||
Ref.wrapkey = function(key) { | ||
return (KEY.test(key) ? key : '"'+key.replace(/"/g,'\\"')+'"'); | ||
}; | ||
Ref.wrapkey = function (key) { | ||
return (KEY.test(key) ? key : '"' + key.replace(/"/g, '\\"') + '"') | ||
} | ||
Ref.prototype = { | ||
/** | ||
* push `key` to interal array | ||
* @param {String} key | ||
*/ | ||
push: function(key) { | ||
this.key.push(key); | ||
}, | ||
/** | ||
* remove the last key from internal array | ||
*/ | ||
pop: function() { | ||
this.key.pop(); | ||
}, | ||
/** | ||
* join the keys | ||
*/ | ||
join: function(key) { | ||
var out = ''; | ||
key = key || this.key; | ||
if (typeof key === 'string') { | ||
console.log('#',key) | ||
key = [ key ]; | ||
/** | ||
* push `key` to interal array | ||
* @param {String} key | ||
*/ | ||
push: function (key) { | ||
this.key.push(key) | ||
}, | ||
/** | ||
* remove the last key from internal array | ||
*/ | ||
pop: function () { | ||
this.key.pop() | ||
}, | ||
/** | ||
* join the keys | ||
*/ | ||
join: function (key) { | ||
var out = '' | ||
key = key || this.key | ||
if (typeof key === 'string') { | ||
console.log('#', key) | ||
key = [key] | ||
} | ||
} | ||
key.forEach(function(k){ | ||
if (KEY.test(k)) { | ||
out += '.' + k; | ||
} else { | ||
out += '['+ Ref.wrapkey(k) +']'; | ||
} | ||
}); | ||
return out; | ||
}, | ||
/** | ||
* check if object `source` has an already known reference. | ||
* If so then origin and source are stored in `opts.reference` | ||
* @param {Object} source - object to compare | ||
* @return {Boolean} | ||
*/ | ||
hasReference: function(source) { | ||
var idx; | ||
if (~(idx = this.refs.indexOf(source))) { | ||
this.references.push([ this.join(), this.keys[idx] ]); | ||
return true; | ||
} | ||
else { | ||
this.refs.push(source); | ||
this.keys.push(this.join()); | ||
} | ||
}, | ||
/** | ||
* get the references array | ||
* @return {Array} references array | ||
*/ | ||
getReferences: function() { | ||
return this.references; | ||
}, | ||
}; | ||
key.forEach(function (k) { | ||
if (KEY.test(k)) { | ||
out += '.' + k | ||
} else { | ||
out += '[' + Ref.wrapkey(k) + ']' | ||
} | ||
}) | ||
return out | ||
}, | ||
/** | ||
* check if object `source` has an already known reference. | ||
* If so then origin and source are stored in `opts.reference` | ||
* @param {Object} source - object to compare | ||
* @return {Boolean} | ||
*/ | ||
hasReference: function (source) { | ||
var idx | ||
if (~(idx = this.refs.indexOf(source))) { | ||
this.references.push([this.join(), this.keys[idx]]) | ||
return true | ||
} else { | ||
this.refs.push(source) | ||
this.keys.push(this.join()) | ||
} | ||
}, | ||
/** | ||
* get the references array | ||
* @return {Array} references array | ||
*/ | ||
getReferences: function () { | ||
return this.references | ||
} | ||
} | ||
module.exports = Ref; | ||
module.exports = Ref |
{ | ||
"name": "serialize-to-js", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"description": "serialize objects to javascript", | ||
"main": "index.js", | ||
"main": "lib", | ||
"engines": { | ||
@@ -14,11 +14,21 @@ "node": ">=0.8.0" | ||
"dependencies": { | ||
"js-beautify": "~1.5.5", | ||
"mergee": "~0.1.2" | ||
"js-beautify": "~1.6.2", | ||
"mergee": "~0.2.3" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^2.7.0", | ||
"eslint-config-standard": "^5.1.0", | ||
"eslint-plugin-promise": "^1.1.0", | ||
"eslint-plugin-standard": "^1.3.2", | ||
"istanbul": "^0.4.2", | ||
"mocha": "^2.4.5", | ||
"rimraf": "^2.5.2" | ||
}, | ||
"scripts": { | ||
"test": "mocha test/*.mocha.js", | ||
"cover": "istanbul cover _mocha --report lcov --report text -- -R dot --check-leaks test/*.mocha.js", | ||
"lint": "jshint --show-non-errors *.js */*.js", | ||
"doc": "jsdox -o doc index.js lib/*.js", | ||
"clean": "rm -rf doc coverage" | ||
"test": "mocha --reporter spec --check-leaks test/*.mocha.js", | ||
"cover": "istanbul cover _mocha --report lcov --report text -- --reporter dot --check-leaks test/*.mocha.js", | ||
"doc": "jsdox -o doc lib/*.js", | ||
"lint": "eslint --quiet '**/*.js'", | ||
"readme": "markedpp --githubid -i README.md -o README.md", | ||
"clean": "rimraf doc coverage node_modules" | ||
}, | ||
@@ -40,6 +50,3 @@ "repository": { | ||
}, | ||
"homepage": "https://github.com/commenthol/serialize-to-js", | ||
"devDependencies": { | ||
"mocha": "~2.2.4" | ||
} | ||
} | ||
"homepage": "https://github.com/commenthol/serialize-to-js" | ||
} |
@@ -16,2 +16,3 @@ # serialize-to-js | ||
* [serialize](#serialize) | ||
* [deserialize](#deserialize) | ||
* [serializeToModule](#serializetomodule) | ||
@@ -35,10 +36,16 @@ * [Contribution and License Agreement](#contribution-and-license-agreement) | ||
var serialize = require('serialize-to-js').serialize; | ||
var obj = { object: { | ||
regexp: /^test?$/, | ||
date: new Date(), | ||
buffer: new Buffer('data'), | ||
number: 3.1415, | ||
string: "test" } }; | ||
console.log(serialize(obj)); | ||
//> {object: {regexp: /^test?$/, date: new Date('2015-04-18T20:01:51.903Z'), buffer: new Buffer('ZGF0YQ==', 'base64'), number: 3.1415, string: 'test'}} | ||
var obj = { | ||
str: '<script>var a = 0 > 1</script>', | ||
num: 3.1415, | ||
bool: true, | ||
nil: null, | ||
undef: undefined, | ||
obj: { foo: 'bar' }, | ||
arr: [1, '2'], | ||
regexp: /^test?$/, | ||
date: new Date(), | ||
buffer: new Buffer('data'), | ||
} | ||
console.log(serialize(obj)) | ||
// > {str: "\u003Cscript\u003Evar a = 0 \u003E 1\u003C\u002Fscript\u003E", num: 3.1415, bool: true, nil: null, undef: undefined, obj: {foo: "bar"}, arr: [1, "2"], regexp: /^test?$/, date: new Date("2016-04-15T16:22:52.009Z"), buffer: new Buffer('ZGF0YQ==', 'base64')} | ||
``` | ||
@@ -72,2 +79,27 @@ | ||
### deserialize | ||
`deserialize(str)` | ||
deserialize a serialized object to javascript | ||
#### Example - serializing regex, date, buffer, ... | ||
```js | ||
var str = '{obj: {foo: "bar"}, arr: [1, "2"], regexp: /^test?$/, date: new Date("2016-04-15T16:22:52.009Z")}' | ||
var res = deserialize(str) | ||
console.log(res) | ||
//> { obj: { foo: 'bar' }, | ||
//> arr: [ 1, '2' ], | ||
//> regexp: /^test?$/, | ||
//> date: Sat Apr 16 2016 01:22:52 GMT+0900 (JST) } | ||
``` | ||
**Parameters** | ||
**str**: `String`, string containing serilaized data | ||
**Returns**: `Any`, deserialized data | ||
### serializeToModule | ||
@@ -117,3 +149,3 @@ | ||
Copyright (c) 2015 commenthol (MIT License) | ||
Copyright (c) 2016 commenthol (MIT License) | ||
@@ -123,2 +155,1 @@ See [LICENSE][] for more info. | ||
[LICENSE]: ./LICENSE | ||
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses eval() which is a dangerous function. This prevents the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses eval() which is a dangerous function. This prevents the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
151
14913
7
6
297
2
+ Addedbluebird@3.7.2(transitive)
+ Addedcommander@2.20.3(transitive)
+ Addededitorconfig@0.13.3(transitive)
+ Addedjs-beautify@1.6.14(transitive)
+ Addedlru-cache@3.2.0(transitive)
+ Addedmergee@0.2.5(transitive)
+ Addedpseudomap@1.0.2(transitive)
+ Addedsemver@5.7.2(transitive)
+ Addedsigmund@1.0.1(transitive)
- Removedjs-beautify@1.5.10(transitive)
- Removedmergee@0.1.2(transitive)
Updatedjs-beautify@~1.6.2
Updatedmergee@~0.2.3