JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 4061
  • Score
    100M100P100Q146859F
  • License MIT

Convert a hierarchy from flat to nested representation.

Package Exports

  • flat-to-nested

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (flat-to-nested) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

flat-to-nested

Convert a hierarchy from flat to nested representation.

Build Status

Example

var FlatToNested, flatToNested, flat;

FlatToNested = require('flat-to-nested');
flatToNested = new FlatToNested( /* can take a config object to use other property names */ );

flat = [
    {id: 111, parent: 11},
    {id: 11, parent: 1},
    {id: 12, parent: 1},
    {id: 1}
];

var nested = flatToNested.convert(flat);
console.log(nested);

//	{
//		id: 1,
//		children: [
//			{
//				id: 11,
//				children: [
//					{
//						id: 111
//					}
//				]
//			},
//			{
//				id: 12
//			}
//		]
//	}

Configuration

The constructor accepts an optional object with some or all of these properties:

flatToNested = new FlatToNested({
    // The name of the property with the node id in the flat representation
    id: 'id',
    // The name of the property with the parent node id in the flat representation
    parent: 'parent',
    // The name of the property that will hold the children nodes in the nested representation
    children: 'children'
}});

Contributing

Setup

Fork this repository and run npm install on the project root folder to make sure you have all project dependencies installed.

Code Linting

Run npm run lint

This will check both source and tests for code correctness and style compliance.

Running Tests

Run npm test