kwhitley / treeize

Converts row data (in JSON/associative array format) to tree structure based on column naming conventions.
MIT License
189 stars 24 forks source link

Removing/reducing empty structures #4

Closed EvanK closed 10 years ago

EvanK commented 10 years ago

It'd be useful to have an option to remove/reduce empty deep structures. For example, after running a particular query through treeize.grow, I might end up with:

[
  {
    "items": [
      {
        "foo": null,
        "bar": null,
        "baz": null,
        "bob": null
      }
    ],
    "group": {
      "lorem": null,
      "ipsum": null,
      "dolor": null,
      "sit": null,
      "amet": null
    }
  }
]

One could, say, remove objects with entirely null properties, and reduce arrays of empty objects to an empty array (the latter so that something expected to be iterable still would be, without needing an additional empty check in whatever is consuming your data feed):

[
  {
    "items": []
  }
]
kwhitley commented 10 years ago

There a case study/real world example for this? Certainly not opposed to adding functionality, but curious when this may arise! :8ball:

EvanK commented 10 years ago

Any SQL query that results in a lot of null values, in my use case a complex left join:

new db('foo')
  .select('id')
  .join('bar', function() {
    this
      .on('foo.id','=','bar.foreign_key_1')
      .orOn('foo.id','=','bar.foreign_key_2');
  }, 'left');
  .column(
    'bar.foreign_key_1',
    'bar.foreign_key_2',
    'bar.name',
    'bar.something',
    'bar.something_else'
  );

This would return something like:

+--------+-------------------+-------------------+----------+---------------+--------------------+
| foo.id | bar.foreign_key_1 | bar.foreign_key_2 | bar.name | bar.something | bar.something_else |
+--------+-------------------+-------------------+----------+---------------+--------------------+
| 123    | NULL              | 123               | alpha    | NULL          | NULL               |
+--------+-------------------+-------------------+----------+---------------+--------------------+
kwhitley commented 10 years ago

Good point @EvanK!

kwhitley commented 10 years ago

@EvanK This should all be addressed by default in v2.0.0+ in case you are still using this...

EvanK commented 10 years ago

awesome