Comparing version 0.1.0 to 0.2.0
@@ -0,1 +1,18 @@ | ||
"use strict"; | ||
/** | ||
* The purpose of flatten/unflatten is serialize objects containing | ||
* cyclic references so that they can be ferried between execution | ||
* contexts, e.g. iframes, web workers, other windows | ||
*/ | ||
/** | ||
* Converts the root object to an array of smaller objects where | ||
* each object in the array contains properties that are either | ||
* basic data types or objects of the form { id: number } to | ||
* reference other objects/arrays in the original tree. | ||
* | ||
* @param {Object} root | ||
* @returns {Array} | ||
*/ | ||
function deconstruct(root) { | ||
@@ -10,10 +27,16 @@ if (root === null || root === undefined) { | ||
var result = Object.create(null); | ||
if (originalObjects.indexOf(obj) === -1) { | ||
originalObjects[index] = obj; | ||
descontructedObjects[index] = result; | ||
index++; | ||
} | ||
return Object.keys(obj).reduce(function (acc, key) { | ||
var val = obj[key]; | ||
if (typeof (val) === "object") { | ||
if (val == null) { | ||
acc[key] = val; | ||
} else if (typeof val === "object") { | ||
if (originalObjects.indexOf(val) === -1) { | ||
@@ -26,16 +49,23 @@ traverse(val); | ||
}; | ||
} | ||
else if (typeof (val) === "function") { | ||
} else if (typeof val === "function") { | ||
acc[key] = "[function]"; | ||
} | ||
else { | ||
} else { | ||
acc[key] = val; | ||
} | ||
return acc; | ||
}, result); | ||
}; | ||
traverse(root); | ||
return descontructedObjects; | ||
} | ||
exports.deconstruct = deconstruct; | ||
/** | ||
* Converts an array produced by flatten back to an object with the | ||
* same structure and data as the original root object passed into | ||
* flatten. | ||
* | ||
* @param {Array} descontructedObjects | ||
*/ | ||
function reconstruct(descontructedObjects) { | ||
@@ -46,20 +76,29 @@ if (descontructedObjects === null || descontructedObjects === undefined) { | ||
var originalObjects = []; | ||
var recurse = function (obj, id, isArray) { | ||
var recurse = function (obj, id) { | ||
var isArray = arguments[2] === undefined ? false : arguments[2]; | ||
var result = isArray ? [] : Object.create(null); | ||
originalObjects[id] = result; | ||
return Object.keys(obj).reduce(function (acc, key) { | ||
var val = obj[key]; | ||
if (typeof (val) === "object") { | ||
if (val == null) { | ||
acc[key] = val; | ||
} else if (typeof val === "object") { | ||
var id = val.id; | ||
var isArray = val.isArray; | ||
acc[key] = originalObjects[id] || recurse(descontructedObjects[id], id, isArray); | ||
} | ||
else { | ||
} else { | ||
acc[key] = val; | ||
} | ||
return acc; | ||
}, result); | ||
}; | ||
return recurse(descontructedObjects[0], 0); | ||
return recurse(descontructedObjects[0], 0); // root object's id is always 0 | ||
} | ||
exports.reconstruct = reconstruct; | ||
exports.deconstruct = deconstruct; | ||
exports.reconstruct = reconstruct; |
{ | ||
"name": "gehry", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "deconstruct (and reconstruct) objects", | ||
"main": "index.js", | ||
"main": "./lib/gehry.js", | ||
"directories": { | ||
@@ -10,3 +10,3 @@ "test": "test" | ||
"scripts": { | ||
"test": "./node_modules/.bin/mocha-phantomjs ./test/runner.html" | ||
"test": "mocha test" | ||
}, | ||
@@ -24,8 +24,6 @@ "repository": { | ||
"devDependencies": { | ||
"browserify": "^8.0.3", | ||
"expect.js": "^0.3.1", | ||
"mocha": "^2.0.1", | ||
"mocha-phantomjs": "^3.5.1", | ||
"phantomjs": "^1.9.12", | ||
"tsbuild": "git://github.com/kevinb7/tsbuild" | ||
"mocha": "^2.0.1" | ||
} | ||
} |
@@ -11,5 +11,3 @@ [![Build Status](https://travis-ci.org/kevinb7/gehry.svg)](https://travis-ci.org/kevinb7/gehry) | ||
Originally it was called "flatten" but there were too many projects with a similar name. Then it was called | ||
"teleport", but that wasn't very accurate because it doesn't transport anything. Finally, I settled on "gehry" | ||
after deconstructivist architect Frank Gehry. | ||
It's named after after the deconstructivist architect Frank Gehry. | ||
@@ -16,0 +14,0 @@ ## Features ## |
/*global describe, it, beforeEach, afterEach */ | ||
var expect = require('expect.js'); | ||
var gehry = require('../lib/gehry'); | ||
@@ -90,2 +92,12 @@ describe("gehry.js", function () { | ||
}); | ||
it('should handle objects with null/undefined values', function() { | ||
var flat = gehry.deconstruct({ | ||
empty: null, | ||
nothing: undefined | ||
}); | ||
var unflat = gehry.reconstruct(flat); | ||
expect(unflat.empty).to.be(null); | ||
expect(unflat.nothing).to.be(undefined); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
3
0
49370
19
169
21
1