UNPKG

hierarchy-js

Version:

Elegant and lightweight library for working with data structures

43 lines (34 loc) 1.28 kB
const { createCopy } = require('../services/createCopy') const { id, childrenKey, parentId, mergeOptionsBeforeCreateHierarchy } = require('./options') const hasParent = (parentId, items) => { return items.some((item) => id(item) === parentId) } const hasChildren = (item) => { const key = childrenKey() return !!(item && item[key] && item[key].length) } const getParents = (items = []) => { return items.filter((item) => id(item) && !hasParent(parentId(item), items)) } const getChildren = (child, items) => { const childId = id(child) return childId ? items.filter((item) => parentId(item) === childId) : [] } const getParentChildren = (parent) => { const key = childrenKey() return Array.isArray(parent[key]) ? parent[key].slice() : [] } const mergeChildren = (parent, children) => { if (children) { const parentChildren = getParentChildren(parent) parent[childrenKey()] = parentChildren.concat(children) } return parent } const createHierarchy = (method) => (array, options) => { if (array && array.length) { const OPTIONS = mergeOptionsBeforeCreateHierarchy(options) return method(createCopy(array), null, OPTIONS) } } module.exports = { getParents, getChildren, mergeChildren, hasChildren, childrenKey, createHierarchy }