Socket
Socket
Sign inDemoInstall

serialize-to-js

Package Overview
Dependencies
16
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.1.2 to 0.2.0

lib/index.js

133

lib/reference.js

@@ -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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc