Socket
Socket
Sign inDemoInstall

json-cycle

Package Overview
Dependencies
0
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.1.0 to 1.2.0

130

cycle.js

@@ -25,22 +25,20 @@ 'use strict';

module.exports = {
function decycle(object) {
decycle: function decycle(object) {
var objects = [], // Keep a reference to each unique object or array
paths = []; // Keep the path to each unique object or array
var objects = [], // Keep a reference to each unique object or array
paths = []; // Keep the path to each unique object or array
return (function derez(value, path) {
return (function derez(value, path) {
// The derez recurses through the object, producing the deep copy.
var i, // The loop counter
name, // Property name
nu; // The new object or array
var i, // The loop counter
name, // Property name
nu; // The new object or array
var _value = value && value.toJSON instanceof Function ? value.toJSON() : value;
var _value = value && value.toJSON instanceof Function ? value.toJSON() : value;
// typeof null === 'object', so go on if this value is really an object but not
// one of the weird builtin objects.
if (typeof _value === 'object' && _value !== null) {
if (typeof _value === 'object' && _value !== null) {

@@ -51,43 +49,43 @@ // If the value is an object or array, look to see if we have already

for (i = 0; i < objects.length; i += 1) {
if (objects[i] === _value) {
return {$ref: paths[i]};
}
for (i = 0; i < objects.length; i += 1) {
if (objects[i] === _value) {
return {$ref: paths[i]};
}
}
// Otherwise, accumulate the unique value and its path.
objects.push(_value);
paths.push(path);
objects.push(_value);
paths.push(path);
// If it is an array, replicate the array.
if (Object.prototype.toString.apply(_value) === '[object Array]') {
nu = [];
for (i = 0; i < _value.length; i += 1) {
nu[i] = derez(_value[i], path + '[' + i + ']');
}
} else {
if (Object.prototype.toString.apply(_value) === '[object Array]') {
nu = [];
for (i = 0; i < _value.length; i += 1) {
nu[i] = derez(_value[i], path + '[' + i + ']');
}
} else {
// If it is an object, replicate the object.
nu = {};
for (name in _value) {
if (Object.prototype.hasOwnProperty.call(_value, name)) {
nu[name] = derez(_value[name],
path + '[' + JSON.stringify(name) + ']');
}
nu = {};
for (name in _value) {
if (Object.prototype.hasOwnProperty.call(_value, name)) {
nu[name] = derez(_value[name],
path + '[' + JSON.stringify(name) + ']');
}
}
return nu;
}
return _value;
}(object, '$'));
},
return nu;
}
return _value;
}(object, '$'));
}
retrocycle: function retrocycle($) {
function retrocycle($) {
var px = /^\$(?:\[(?:\d+|\"(?:[^\\\"\u0000-\u001f]|\\([\\\"\/bfnrt]|u[0-9a-zA-Z]{4}))*\")\])*$/;
var px = /^\$(?:\[(?:\d+|\"(?:[^\\\"\u0000-\u001f]|\\([\\\"\/bfnrt]|u[0-9a-zA-Z]{4}))*\")\])*$/;
(function rez(value) {
(function rez(value) {

@@ -99,12 +97,25 @@ // The rez function walks recursively through the object looking for $ref

var i, item, name, path;
var i, item, name, path;
if (value && typeof value === 'object') {
if (Object.prototype.toString.apply(value) === '[object Array]') {
for (i = 0; i < value.length; i += 1) {
item = value[i];
if (item && typeof item === 'object') {
if (value && typeof value === 'object') {
if (Object.prototype.toString.apply(value) === '[object Array]') {
for (i = 0; i < value.length; i += 1) {
item = value[i];
if (item && typeof item === 'object') {
path = item.$ref;
if (typeof path === 'string' && px.test(path)) {
value[i] = eval(path);
} else {
rez(item);
}
}
}
} else {
for (name in value) {
if (typeof value[name] === 'object') {
item = value[name];
if (item) {
path = item.$ref;
if (typeof path === 'string' && px.test(path)) {
value[i] = eval(path);
value[name] = eval(path);
} else {

@@ -115,21 +126,20 @@ rez(item);

}
} else {
for (name in value) {
if (typeof value[name] === 'object') {
item = value[name];
if (item) {
path = item.$ref;
if (typeof path === 'string' && px.test(path)) {
value[name] = eval(path);
} else {
rez(item);
}
}
}
}
}
}
}($));
return $;
}
}
}($));
return $;
}
module.exports = {
stringify: function stringifyJC(object) {
return JSON.stringify(decycle(object))
},
parse: function parseJC($) {
return retrocycle(JSON.parse($))
},
decycle: decycle,
retrocycle: retrocycle,
};
{
"name": "json-cycle",
"version": "1.1.0",
"version": "1.2.0",
"description": "Encode/decode circular structures for converting to and from JSON.",

@@ -5,0 +5,0 @@ "keywords": [

@@ -13,4 +13,2 @@ # json-cycle

[![NPM][npm-image]][npm-url]
Utilities provide ability to encode/decode circular structures for converting to and from JSON.

@@ -30,3 +28,3 @@

This package contains two functions, decycle and retrocycle,
This package contains four functions, decycle, retrocycle, stringify and parse,
which make it possible to encode cyclical structures and convert them to JSON, and

@@ -36,6 +34,4 @@ then recover them. This is a capability that is not provided by ES5. JSONPath

## Warnings
> Note: If you stringify javascript structure and then parse it back in some cases you can get not the same javascript structure. For instance, if it contains Date object you get String form of it.
If you stringify javascript structure and then parse it back in some cases you can get not the same javascript structure. For instance, if it contains Date object you get String form of it.
## Methods

@@ -45,2 +41,4 @@

- [retrocycle](#retrocycle)
- [stringify](#stringify)
- [parse](#parse)

@@ -85,3 +83,3 @@ ### decycle(object)

**Note** The eval function is used to locate the values described by a PATH. The
> Note: The eval function is used to locate the values described by a PATH. The
root object is kept in a $ variable. A regular expression is used to

@@ -110,9 +108,15 @@ assure that the PATH is extremely well formed. The regexp contains nested

### stringify(object)
It equals to `JSON.stringify(decycle(object))`
### parse(object)
It equals to `retrocycle(JSON.parse(object))`
## License
MIT &copy; 2015 Valery Barysok, Douglas Crockford
MIT &copy; 2015-... Valery Barysok, Douglas Crockford
[npm-version-image]: https://img.shields.io/npm/v/json-cycle.svg?style=flat-square
[npm-downloads-image]: https://img.shields.io/npm/dm/json-cycle.svg?style=flat-square
[npm-image]: https://nodei.co/npm/json-cycle.png?downloads=true&downloadRank=true&stars=true
[npm-url]: https://npmjs.org/package/json-cycle

@@ -119,0 +123,0 @@ [travis-image]: https://img.shields.io/travis/valery-barysok/json-cycle/master.svg?style=flat-square

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