Comparing version 0.0.5 to 0.0.6
@@ -5,95 +5,101 @@ var _ = require('lodash'); | ||
var globalOptions = { | ||
delimiter: '+' | ||
}; | ||
function Treeize(options) { | ||
var globalOptions = { | ||
delimiter: ':' | ||
}; | ||
function expand(flatData, config) { | ||
var localOptions = _.extend(globalOptions, config || {}); | ||
var translated = []; | ||
this.getOptions = function(options) { | ||
return _.clone(globalOptions); | ||
}; | ||
if (!flatData || !flatData.length) { return flatData; } | ||
this.setOptions = function(options) { | ||
_.extend(globalOptions, options); | ||
_.each(flatData, function(row, index) { | ||
var paths = []; | ||
var trails = {}; | ||
return this; | ||
}; | ||
// set up paths for processing | ||
_.each(row, function(value, fullPath) { | ||
var splitPath = fullPath.split(localOptions.delimiter); | ||
this.grow = function(flatData, config) { | ||
var localOptions = _.extend(this.getOptions(), config || {}); | ||
var translated = []; | ||
paths.push({ | ||
splitPath: splitPath.slice(0, splitPath.length - 1), | ||
fullPath: splitPath.slice(0, splitPath.length - 1).join(localOptions.delimiter), | ||
parentPath: splitPath.slice(0, splitPath.length - 2).join(localOptions.delimiter), | ||
node: splitPath.slice(splitPath.length - 2, splitPath.length - 1).join(localOptions.delimiter), | ||
attribute: _.last(splitPath), | ||
value: value, | ||
processed: false | ||
if (!flatData || !flatData.length) { return flatData; } | ||
_.each(flatData, function(row, index) { | ||
var paths = []; | ||
var trails = {}; | ||
// set up paths for processing | ||
_.each(row, function(value, fullPath) { | ||
var splitPath = fullPath.split(localOptions.delimiter); | ||
paths.push({ | ||
splitPath: splitPath.slice(0, splitPath.length - 1), | ||
fullPath: splitPath.slice(0, splitPath.length - 1).join(localOptions.delimiter), | ||
parentPath: splitPath.slice(0, splitPath.length - 2).join(localOptions.delimiter), | ||
node: splitPath.slice(splitPath.length - 2, splitPath.length - 1).join(localOptions.delimiter), | ||
attribute: _.last(splitPath), | ||
value: value, | ||
processed: false | ||
}); | ||
}); | ||
}); | ||
// sort paths to prepare for processing | ||
paths.sort(function(a, b) { | ||
if (a.splitPath.length !== b.splitPath.length) { | ||
return a.splitPath.length < b.splitPath.length ? -1 : 1; | ||
} | ||
// sort paths to prepare for processing | ||
paths.sort(function(a, b) { | ||
if (a.splitPath.length !== b.splitPath.length) { | ||
return a.splitPath.length < b.splitPath.length ? -1 : 1; | ||
} | ||
return a.parentPath < a.parentPath ? -1 : 1; | ||
}); | ||
return a.parentPath < a.parentPath ? -1 : 1; | ||
}); | ||
// proccess each unprocessed path in the row | ||
var trail = translated; | ||
// proccess each unprocessed path in the row | ||
var trail = translated; | ||
while (_.findWhere(paths, { processed: false })) { | ||
var target = _.findWhere(paths, { processed: false }); | ||
while (_.findWhere(paths, { processed: false })) { | ||
var target = _.findWhere(paths, { processed: false }); | ||
// get associated group | ||
var group = _.where(paths, { parentPath: target.parentPath, node: target.node, processed: false }); | ||
// get associated group | ||
var group = _.where(paths, { parentPath: target.parentPath, node: target.node, processed: false }); | ||
// build blueprint for current group | ||
var blueprint = {}; | ||
_.each(group, function(groupItem) { | ||
blueprint[groupItem.attribute] = groupItem.value; | ||
groupItem.processed = true; | ||
}); | ||
// build blueprint for current group | ||
var blueprint = {}; | ||
_.each(group, function(groupItem) { | ||
blueprint[groupItem.attribute] = groupItem.value; | ||
groupItem.processed = true; | ||
}); | ||
trail = trails[target.parentPath]; | ||
trail = trails[target.parentPath]; | ||
// set up first node, everythign else should have parent path | ||
if (!trail) { | ||
trail = _.findWhere(translated, blueprint); | ||
// set up first node, everythign else should have parent path | ||
if (!trail) { | ||
trail = blueprint; | ||
translated.push(blueprint); | ||
trail = _.findWhere(translated, blueprint); | ||
if (!trail) { | ||
trail = blueprint; | ||
translated.push(blueprint); | ||
} | ||
trails[target.parentPath] = trail; | ||
} | ||
trails[target.parentPath] = trail; | ||
} | ||
// trail is now at parent node, standing by for current node injection | ||
if (target.node) { // should skip root because '' resolves to false | ||
var isCollection = target.node === _.pluralize(target.node); | ||
var node = trail[target.node] = (trail[target.node] || (isCollection ? [blueprint] : blueprint)); | ||
// trail is now at parent node, standing by for current node injection | ||
if (target.node) { // should skip root because '' resolves to false | ||
var isCollection = target.node === _.pluralize(target.node); | ||
var node = trail[target.node] = (trail[target.node] || (isCollection ? [blueprint] : blueprint)); | ||
if (isCollection) { | ||
node = _.findWhere(trail[target.node], blueprint); | ||
if (!node) { | ||
node = blueprint; | ||
trail[target.node].push(node); | ||
if (isCollection) { | ||
node = _.findWhere(trail[target.node], blueprint); | ||
if (!node) { | ||
node = blueprint; | ||
trail[target.node].push(node); | ||
} | ||
} | ||
trails[target.fullPath] = node; | ||
} | ||
trails[target.fullPath] = node; | ||
} | ||
} | ||
}); | ||
}); | ||
return translated; | ||
return translated; | ||
} | ||
} | ||
exports.grow = function(rows, config) { | ||
return expand(rows, config); | ||
}; | ||
exports.set = function(config) { | ||
_.extend(globalOptions, config); | ||
}; | ||
var treeize = module.exports = new Treeize(); |
{ | ||
"name": "treeize", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "Converts tabular row data (as from SQL joins, flat JSON, etc) to deep tree graphs based on simple column naming conventions.", | ||
@@ -21,6 +21,8 @@ "main": "./lib/treeize.js", | ||
"author": "K. R. Whitley <contact@krwhitley.com> (http://krwhitley.com/)", | ||
"licenses": [{ | ||
"type": "MIT", | ||
"url" : "http://en.wikipedia.org/wiki/MIT_License" | ||
}], | ||
"licenses": [ | ||
{ | ||
"type": "MIT", | ||
"url": "http://en.wikipedia.org/wiki/MIT_License" | ||
} | ||
], | ||
"bugs": { | ||
@@ -32,3 +34,13 @@ "url": "https://github.com/kwhitley/treeize/issues" | ||
"inflection": "~1.2.6" | ||
}, | ||
"devDependencies": { | ||
"grunt": "latest", | ||
"nodeunit": "latest", | ||
"grunt-contrib-jshint": "latest", | ||
"grunt-contrib-nodeunit": "latest", | ||
"grunt-contrib-watch": "latest" | ||
}, | ||
"scripts": { | ||
"test": "grunt" | ||
} | ||
} |
@@ -8,8 +8,11 @@ treeize | ||
`npm install --save treeize` | ||
``` | ||
npm install treeize | ||
``` | ||
## Usage | ||
## API | ||
- `treeize.grow(flatData, options)` - takes your results/rows of flat associative data and returns a full object graph. | ||
- `treeize.set(options)` - sets global options for the lib. For example, to use a path delimiter of '>' instead of '+', call `treeize.set({ delimiter: '>' })` | ||
- `treeize.getOptions()` - returns global options for the lib. | ||
- `treeize.setOptions(options)` - sets global options for the lib. For example, to use a path delimiter of '>' instead of '+', call `treeize.setOptions({ delimiter: '>' })` | ||
@@ -16,0 +19,0 @@ ### Notes |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
11565
7
219
97
5
1