8000 GitHub - BonneVoyager/nested-objects-util at ff5418ff7204e420e65295c38d93b22c47655bc4
[go: up one dir, main page]

Skip to content

BonneVoyager/nested-objects-util

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nested-objects-util

A module to work with huge nested objects having circular references.

It was implemented to filter out some values from huge nested objects with circular references.

It's designed to work both on nodejs and browser.

Installation

npm install --save nested-objects-util

Usage

const nestedObjectsUtil = require('nested-objects-util');

nestedObjectsUtil.flatten(Object: object) returns {Object}

Flatten object by keys composed from own nested properties.

nestedObjectsUtil.flatten({
  keyA: {
    keyB: {
      keyC: 'value'
    },
    keyD: 'value2'
  } 
})

returns:

{
  "keyA.keyB.keyC": "value",
  "keyA.keyD": "value2"
}

nestedObjectsUtil.unflatten(Object: object) returns {Object}

Unflatten object by keys composed from own nested properties.

nestedObjectsUtil.unflatten({
  'keyA.keyB.keyC': 'value',
  'keyA.keyD': 'value2'
});

returns:

{
  "keyA": {
    "keyB": {
      "keyC": "value"
    },
    "keyD": "value2"
    }
  }

nestedObjectsUtil.accessProperty([String | Array]: keys, Object: parent = global | window) returns {Various}

Access object's nested property.

var nestedObject = {
  keyA: {
    keyB: {
      keyC: 'value'
    }
  }
};
nestedObjectsUtil.accessProperty('keyA.keyB.keyC', nestedObject);

returns:

"value"

nestedObjectsUtil.discardCircular(Object: object) returns {Object}

Discard circular references (to avoid "Converting circular structure to JSON" error).

var a = {
  b: 1
};
a.c = a;
nestedObjectsUtil.discardCircular(a)

returns:

{
  "b": 1,
  "c": "~"
}

nestedObjectsUtil.filterValue(Object: object, Various: query, Boolean: flattenFlag = false) returns {Object}

Filter a nested object by value (with strict comparison performed).

var a = {
  b: {
    c: 'str',
    d: 'str2'
  },
  e: 'str',
  f: {
    g: {
      h: 'str',
      i: 'str3'
    },
    j: 'str4'
  }
};
a.k = a.b;
nestedObjectsUtil.filterValue(a, 'str');

returns:

{
  "b": {
    "c": "str"
  },
  "e": "str",
  "f": {
    "g": {
      "h": "str"
    }
  }
}

or

nestedObjectsUtil.filterValue(a, 'str', true);

returns:

{
  "b.c": "str",
  "e": "str",
  "f.g.h": "str"
}

nestedObjectsUtil.downloadStringified(Object: object, Number: space = 2) returns {[String | undefined]}

On browser with HTML5 download API: stringify, format and download the object.

Else: return stringified text.

var a = {
  b: 1,
  c: {
    d: 2,
    e: 2
  }
};
a.f = a;
a.g = a.f;
var obj = nestedObjectsUtil.discardCircular(a);
nestedObjectsUtil.downloadStringified(obj);

returns:

{
  "b": 1,
  "c": {
    "d": 2,
    "e": 2
  },
  "f": "~",
  "g": "~"
}

Example browser usage

Filter out 'abcd' value from the flattened object and download stringified json via HTML5 API with:

const object = NestedObjectsUtil.filterValue(App.SomeHugeObject, 'abcd', true);
NestedObjectsUtil.downloadStringified(object);

Test

npm run test

License

MIT

0