Skip to content

Commit c349f7b

Browse files
author
K. R. Whitley
committed
Merge pull request #30 from kwhitley/feature/v2.0.1
v2.0.1
2 parents ea0d265 + 22103d7 commit c349f7b

File tree

3 files changed

+33
-24
lines changed

3 files changed

+33
-24
lines changed

README.md

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Treeize.js
22

3-
[![Build Status via Travis CI](https://travis-ci.org/kwhitley/treeize.svg?branch=feature%2Fmulti-format)](https://travis-ci.org/kwhitley/treeize)
3+
[![Build Status via Travis CI](https://travis-ci.org/kwhitley/treeize.svg)](https://travis-ci.org/kwhitley/treeize)
44

55
Converts row data (in JSON/associative array format or flat array format) to object/tree structure based on simple column naming conventions.
66

@@ -108,12 +108,14 @@ people.getData() == [
108108
##### 1. get/set options (optional)
109109

110110
- [`options([options])`](#options) - getter/setter for options
111+
- [`getOptions()`](#getOptions) - returns options
111112
- [`setOptions(options)`](#setOptions) - merges new `[options]` with existing
112113
- [`resetOptions()`](#resetOptions) - resets options to defaults
113114

114115
##### 2a. set data signature manually if needed (optional)
115116

116117
- [`signature([row], [options])`](#signature) - getter/setter for signature definitions
118+
- [`getSignature()`](#getSignature) - returns currently defined signature
117119
- [`setSignature(row, [options])`](#setSignature) - sets signature using a specific row of data/headers (preserves signature between data sets if uniformity option is enabled)
118120
- [`clearSignature()`](#clearSignature) - clear signature between data sets (only needed when previously defined a uniform signature via `setSignature`)
119121

@@ -127,8 +129,6 @@ people.getData() == [
127129

128130
##### * misc/internal methods
129131

130-
- [`getOptions()`](#getOptions) - returns options
131-
- [`getSignature()`](#getSignature) - returns currently defined signature
132132
- [`getStats()`](#getStats) - returns object with growth statistics
133133
- [`toString()`](#toString) - uses `util` to return data in visually formatted object graph
134134
- [`log(arg1, arg2, arg3)`](#log) - console.log output of `arg1..n` when `log` option is set to `true`
@@ -461,7 +461,7 @@ In this example, we'll take our dump (as if from a CSV or SQL result) - and name
461461
group by movies (as if for an `/api/movies`).
462462

463463
```js
464-
var movieDump = [
464+
var movieData = [
465465
{
466466
'title': 'The Prestige',
467467
'director': 'Christopher Nolan',
@@ -497,7 +497,7 @@ var movieDump = [
497497
var Treeize = require('treeize');
498498
var movies = new Treeize();
499499

500-
movies.grow(movieDump);
500+
movies.grow(movieData);
501501

502502
/*
503503
@@ -595,7 +595,7 @@ var moviesDump = [
595595
var Treeize = require('treeize');
596596
var actors = new Treeize();
597597

598-
actors.grow(movieDump);
598+
actors.grow(moviesData);
599599

600600
/*
601601
@@ -651,3 +651,10 @@ actors.grow(movieDump);
651651
652652
*/
653653
```
654+
655+
# Changelog
656+
657+
### 2.0.1
658+
659+
- minor README modifications
660+
- performance tuning... ~400% performance boost over 2.0.0

lib/treeize.js

+17-17
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function Treeize(options) {
4747
}
4848

4949
Treeize.prototype.log = function() {
50-
if (this.options().log) {
50+
if (this._options.log) {
5151
console.log.apply(this, arguments);
5252
}
5353

@@ -79,21 +79,21 @@ Treeize.prototype.signature = function(row, options, auto) {
7979

8080
var nodes = this.data.signature.nodes = [];
8181
var isArray = _.isArray(row);
82-
var opt = merge(this.options(), options || {});
82+
var opt = merge(this._options, options || {});
8383

8484
this.data.signature.type = isArray ? 'array' : 'object';
8585

8686
_.each(row, function(value, key) {
8787
// set up attribute
8888
var attr = {}
8989

90-
attr.key = typeof key === 'number' ? key : key.replace(/^[\*\-\+]|[\*\-\+]$/g,'');
90+
attr.key = typeof key === 'number' ? key : key;//.replace(/^[\*\-\+]|[\*\-\+]$/g,'');
9191
attr.fullPath = isArray ? value : key;
9292
attr.split = attr.fullPath.split(opt.input.delimiter);
93-
attr.path = _.initial(attr.split).join(opt.input.delimiter);//.replace(/^[\*\-\+]|[\*\-\+]$/g,'');
94-
attr.parent = _.initial(attr.split, 2).join(opt.input.delimiter).replace(/^[\*\-\+]|[\*\-\+]$/g,'');
93+
attr.path = attr.split.slice(0,attr.split.length-1).join(opt.input.delimiter);
94+
attr.parent = attr.split.slice(0,attr.split.length-2).join(opt.input.delimiter);//.replace(/^[\*\-\+]|[\*\-\+]$/g,'');
9595
attr.node = attr.split[attr.split.length - 2];
96-
attr.attr = _.last(attr.split);
96+
attr.attr = attr.split[attr.split.length - 1];
9797

9898
if (attr.attr.match(/\*/gi)) {
9999
attr.attr = attr.attr.replace(/[\*]/gi,'');
@@ -123,7 +123,7 @@ Treeize.prototype.signature = function(row, options, auto) {
123123

124124
node.name = attr.node;
125125
node.depth = attr.split.length - 1;
126-
node.parent = _.initial(attr.split, 2).join(opt.input.delimiter);
126+
node.parent = attr.split.slice(0, attr.split.length - 2).join(opt.input.delimiter);
127127
node.attributes.push({ name: attr.attr, key: attr.key });
128128
if (attr.pk) {
129129
this.log('adding node to blueprint');
@@ -133,7 +133,7 @@ Treeize.prototype.signature = function(row, options, auto) {
133133
}, this);
134134

135135
// backfill blueprint when not specifically defined
136-
_.each(nodes, function(node) {
136+
nodes.forEach(function(node) {
137137
if (!node.blueprint.length) {
138138
node.blueprint = node.attributes;
139139
}
@@ -170,7 +170,7 @@ Treeize.prototype.clearSignature = function() {
170170

171171

172172
Treeize.prototype.grow = function(data, options) {
173-
var opt = merge(this.options(), options || {});
173+
var opt = merge(this._options, options || {});
174174

175175
// chain past if no data to grow
176176
if (!data || !_.isArray(data) || !data.length) {
@@ -198,7 +198,7 @@ Treeize.prototype.grow = function(data, options) {
198198
data = [];
199199

200200
// copy data without original signature row before processing
201-
_.each(originalData, function(row, index) {
201+
originalData.forEach(function(row, index) {
202202
if (index > 0) {
203203
data.push(row);
204204
}
@@ -215,7 +215,7 @@ Treeize.prototype.grow = function(data, options) {
215215
this.stats.sources++;
216216
var t1 = (new Date()).getTime();
217217

218-
_.each(data, function(row) {
218+
data.forEach(function(row) {
219219
var trails = {}; // LUT for trails (find parent of new node in trails path)
220220
var trail = root = this.data.tree; // OPTIMIZATION: do we need to reset this trail for each row?
221221
this.log('CURRENT TRAIL STATUS>', trail);
@@ -248,20 +248,20 @@ Treeize.prototype.grow = function(data, options) {
248248
}, this);
249249
}
250250

251-
_.each(this.signature().nodes, function(node) {
251+
this.signature().nodes.forEach(function(node) {
252252
this.log('PROCESSING NODE>', node);
253253
var blueprint = {};
254254
var blueprintExtended = {};
255255

256256
// create blueprint for locating existing nodes
257-
_.each(node.blueprint, function(attribute) {
257+
node.blueprint.forEach(function(attribute) {
258258
var key = (node.path ? (node.path + ':') : '') + attribute.name;
259259
blueprint[attribute.name] = row[attribute.key];
260260
this.log('creating attribute "' + attribute.name + '" within blueprint', row[attribute.key]);
261261
}, this);
262262

263263
// create full node signature for insertion/updating
264-
_.each(node.attributes, function(attribute) {
264+
node.attributes.forEach(function(attribute) {
265265
var key = (node.path ? (node.path + ':') : '') + attribute.name;
266266
var value = row[attribute.key];
267267

@@ -338,7 +338,7 @@ Treeize.prototype.grow = function(data, options) {
338338
this.log('..attempting path location for "' + pathAttempt + '"');
339339

340340
//infinite loop kickout
341-
if (segmentsStripped > 5) break;
341+
if (segmentsStripped > 15) break;
342342
}
343343
this.log('path FOUND for location for "' + pathAttempt + '" after removing ' + segmentsStripped + ' segments');
344344

@@ -369,7 +369,7 @@ Treeize.prototype.grow = function(data, options) {
369369
// node attribute exists, set path for next pass
370370
// TODO: extend trail??
371371
this.log('object at node "' + node.name + '" exists as "' + trail[node.name] + '", skipping insertion and adding trail');
372-
if (_.isObject(trail[node.name])) {
372+
if (typeof trail[node.name] === 'object') {
373373
trail[node.name] = merge(trail[node.name], blueprintExtended);
374374
}
375375
this.log('trail[node.name] updated to "' + trail[node.name]);
@@ -410,7 +410,7 @@ Treeize.prototype.options = function(options) {
410410
};
411411

412412
Treeize.prototype.getOptions = function() {
413-
return this.options();
413+
return this._options;
414414
};
415415

416416
Treeize.prototype.setOptions = function(options) {

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "treeize",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"description": "Converts tabular row data (as from SQL joins, flat JSON, etc) to deep object graphs based on simple column naming conventions - without the use of an ORM or models.",
55
"main": "./lib/treeize.js",
66
"repository": {
@@ -10,6 +10,8 @@
1010
"keywords": [
1111
"JSON",
1212
"SQL",
13+
"CSV",
14+
"excel",
1315
"tree",
1416
"object",
1517
"graph",

0 commit comments

Comments
 (0)