treelike-helper
v1.0.3
Published
Nested data tool functions
Downloads
4
Readme
treelike-helper
Use
install
npm install treelike-helper
import
import { mapTreeData } from 'treelike-helper'
API
mapTreeData
param:
- treelikeData(Array): treelike data
- mapFunc(iteratee): item => item
- [options={}] (Object): option
- [options.childrenKeyName = 'children'] (string) : childrenKeyName, default 'children'
return:
treelikeData(Array): treelike data
eg:
const treeData = [
{ id: '1', title: '1' },
{ id: '2', title: '2', children: [{ id: '2-1', title: '2-1' }] },
]
mapTreeData(treeData, item => {
if (item.id === '2-1') {
item.isLeaf = true
}
return {
...item,
name: item.title,
key: item.id,
};
});
=>
[
{ id: '1', title: '1', key: '1', name: '1' },
{
id: '2',
title: '2',
key: '2',
name: '2',
children: [{ id: '2-1', title: '2-1', key: '2-1', name: '2-1', isLeaf: true }],
},
]
filterTreeData
param:
- treelikeData(Array): treelike data
- filterFunc(iteratee): item => item
- [options={}] (Object): option
- [options.childrenKeyName = 'children'] (string) : childrenKeyName, default 'children'
return:
treelikeData(Array): treelike data
eg:
const treeData = [
{ id: '1', title: '1', hasPermission: true },
{ id: '2', title: '2' },
{
id: '3',
title: '3',
hasPermission: true,
children: [
{ id: '3-1', title: '3-1' },
{ id: '3-2', title: '3-2', hasPermission: true },
{ id: '3-3', title: '3-3', hasPermission: false },
],
},
{
id: '4',
title: '4',
hasPermission: false,
children: [
{ id: '4-1', title: '4-1' },
{ id: '4-2', title: '4-2', hasPermission: true },
],
},
]
filterTreeData(treeData, item => item.hasPermission);
=>
[
{ id: '1', title: '1', hasPermission: true },
{
id: '3',
title: '3',
hasPermission: true,
children: [{ id: '3-2', title: '3-2', hasPermission: true }],
},
]
mapFilterTreeData
param:
- treelikeData(Array): treelike data
- filterFunc(iteratee): item => item
- mapFunc(iteratee): item => item
- [options={}] (Object): option
- [options.childrenKeyName = 'children'] (string) : childrenKeyName, default 'children'
return:
treelikeData(Array): treelike data
eg:
const treeData = [
{ id: '1', title: '1', hasPermission: true },
{ id: '2', title: '2' },
{
id: '3',
title: '3',
hasPermission: true,
children: [
{ id: '3-1', title: '3-1' },
{ id: '3-2', title: '3-2', hasPermission: true },
{ id: '3-3', title: '3-3', hasPermission: false },
],
},
]
mapFilterTreeData(treeData,
item => item.hasPermission),
item => ({ ...item, subTitle: 'already filter data' }
);
=>
[
{
id: '1',
title: '1',
hasPermission: true,
subTitle: 'already filter data',
},
{
id: '3',
title: '3',
hasPermission: true,
subTitle: 'already filter data',
children: [
{
id: '3-2',
title: '3-2',
hasPermission: true,
subTitle: 'already filter data',
},
],
},
]
findKeyPath
param:
- treelikeData(Array): treelike data
- targetKey(string): to find key
- [options={}] (Object): option
- [options.childrenKeyName = 'children'] (string) : childrenKeyName, default 'children'
- [options.keyName = 'key'] (tring): keyName, default 'key'
return:
path(Array): key path
eg:
findKeyPath([{ key: '1', children: [{ key: '1-1' }] }], '1-1')
=> ['0', 'children', '0']
findData
param:
- treelikeData(Array): treelike data
- targetKey(string): to find key
- [options={}] (Object): option
- [options.childrenKeyName = 'children'] (string) : childrenKeyName, default 'children'
- [options.keyName = 'key'] (tring): keyName, default 'key'
return:
data: tree node item
eg:
findData([{ key: '1', children: [{ key: '1-1' }] }], '1-1')
=> { key: '1-1' }
findParentData
param:
- treelikeData(Array): treelike data
- targetKey(string): to find key
- [options={}] (Object): option
- [options.childrenKeyName = 'children'] (string) : childrenKeyName, default 'children'
- [options.keyName = 'key'] (tring): keyName, default 'key'
return:
data (Object | null): parent data
eg:
const treeData = [
{
key: '1',
title: '1',
children: [
{
key: '1-1',
title: '1-1',
children: [{ key: '1-1-1', title: '1-1-1' }],
},
],
},
];
findParentData(treeData, '1'); // => null
findParentData(treeData, '1-1').title; // => '1'
findParentData(treeData, '1-1-1').key; // => '1-1'
findSearchData
param:
- treelikeData(Array): treelike data
- search(string): to search
- [options={}] (Object): option
- [options.childrenKeyName = 'children'] (string) : childrenKeyName, default 'children'
- [options.searchKeyName = 'title'] (string): searchKeyName, default 'title'
return:
newSearchTreelikeData(Array): treelike data
eg:
const treeData = [
{ key: '1', title: 'layer1' },
{
key: '2',
title: '2',
children: [
{ key: '2-1', title: '2-1' },
{
key: '2-2',
title: '2-2',
children: [{ key: '2-2-1', title: '2-2-1' }],
},
{
key: '2-2-2',
title: '2-2-2',
},
],
},
];
findSearchData(treeData, 'lay')
// [
// { key: '1', title: 'layer1' },
// ]
findSearchData(treeData, '2-2-1')
// [
// {
// key: '2',
// title: '2',
// children: [
// {
// key: '2-2',
// title: '2-2',
// children: [{ key: '2-2-1', title: '2-2-1' }],
// },
// ],
// },
// ]
findSearchData(treeData, '2-2')
// [
// {
// key: '2',
// title: '2',
// children: [
// {
// key: '2-2',
// title: '2-2',
// children: [{ key: '2-2-1', title: '2-2-1' }],
// },
// {
// key: '2-2-2',
// title: '2-2-2',
// },
// ],
// },
// ]
addData
param:
- treelikeData(Array): treelike data
- parentKey(string|number): to add parentKey
- data(Object | Array): to add data or dataArray
- [options={}] (Object): option
- [options.childrenKeyName = 'children'] (string) : childrenKeyName, default 'children'
- [options.keyName = 'key'] (tring): keyName, default 'key'
return:
newTreelikeData(Array): treelike data
eg:
const treeData = [
{ key: '1', children: [{ key: '1-1', children: [{ key: '1-1-1' }] }] },
];
const newTreeData = addData(treeData, '1-1', { key: '1-1-2' });
// newTreeData: [{ key: '1', children: [{ key: '1-1', children: [{ key: '1-1-1' }, { key: '1-1-2' }] }] }]
const newTreeData2 = addData(treeData, '1-1', [
{ key: '1-1-2' },
{ key: '1-1-3' },
]);
// newTreeData2: [{ key: '1', children: [{ key: '1-1', children: [{ key: '1-1-1' }, { key: '1-1-2' }, { key: '1-1-3' }] }] }]
deleteData
param:
- treelikeData(Array): treelike data
- targetKey(string|number): to delete key
- [options={}] (Object): option
- [options.childrenKeyName = 'children'] (string) : childrenKeyName, default 'children'
- [options.keyName = 'key'] (tring): keyName, default 'key'
- [options.deleteEmptyParent = false] (boolean) : when parent array empty, should delete children
return:
newTreelikeData(Array): treelike data
eg:
const treeData = [
{ key: 1 },
{ key: 2, children: [{ key: 22, children: [{ key: 33 }] }] },
];
deleteData(treeData, 2) // => [{ key: 1 }]
deleteData(treeData, 22, { deleteEmptyParent: true }) // => [{key:1}, {key:2}]
updateData
param:
- treelikeData(Array): treelike data
- targetKey(string|number): to update key
- data(Object | iteratee): to update data or updateFunc
- [options={}] (Object): option
- [options.childrenKeyName = 'children'] (string) : childrenKeyName, default 'children'
- [options.keyName = 'key'] (tring): keyName, default 'key'
return:
newTreelikeData(Array): treelike data
eg:
const treeData = [
{ key: '1', title: '1', children: [{ key: '1-1', title: '1-1' }] },
];
updateData(treeData, '1-1', { title: 'update 1-1' });
// or
updateData(treeData, '1-1', item => ({ ...item, title: 'update 1-1' }));
updateThroughData
param:
- treelikeData(Array): treelike data
- targetKey(string|number): target key
- iteratee(iteratee): updateFunc
- [options={}] (Object): option
- [options.childrenKeyName = 'children'] (string) : childrenKeyName, default 'children'
- [options.keyName = 'key'] (tring): keyName, default 'key'
- [options.includeSelf = false] (boolean) : this update should include target key item
return:
newTreelikeData(Array): treelike data
eg:
const treeData = [
{ key: '1', children: [{ key: '1-1', children: [{ key: '1-1-1' }] }] },
];
const newTreeData1 = updateThroughData(treeData, '1-1-1', item => {
return {
...item,
title: item.key,
};
});
treeData[0].title // undefined
newTreeData1[0].title // '1'
newTreeData1[0].children[0].title // '1-1'
newTreeData1[0].children[0].children[0].title // undefined
const newTreeData2 = updateThroughData(
treeData,
'1-1-1',
item => {
return {
...item,
title: item.key,
};
},
{ includeSelf: true }
);
newTreeData2[0].children[0].children[0].title // '1-1-1'
getFieldValues
param:
- treelikeData(Array): treelike data
- field(string|iteratee): to get field
- [options={}] (Object): option
- [options.childrenKeyName = 'children'] (string) : childrenKeyName, default 'children'
return
values(Array)
eg:
const treeData = [
{ key: '1', title: '1' },
{ key: '1.1', title: '1' },
{
key: '2',
title: '2',
children: [
{ key: '2-1', title: '2-1', , expand: true },
{
key: '2-2',
title: '2-2',
children: [{ key: '2-2-1', title: '2-2-1', expand: true }],
},
{ key: '2-2-2', title: '2-2-2' },
],
},
];
getFieldValues(treeData, 'title') // ['1', '1','2', '2-1', '2-2', '2-2-1', '2-2-2']
getFieldValues(treeData, item => item.title) // ['1', '1','2', '2-1', '2-2', '2-2-1', '2-2-2']
getFieldValues(treeData, item => {
if (item.expand) {
return item;
} else {
return null;
}
}).filter(item => !!item)
// =>
[
{ key: '2-1', title: '2-1', expand: true },
{ key: '2-2-1', title: '2-2-1', expand: true },
]
getFieldValueSet
param:
- treelikeData(Array): treelike data
- field(string|iteratee): to get field
- [options={}] (Object): option
- [options.childrenKeyName = 'children'] (string) : childrenKeyName, default 'children'
return
values(Array)
eg:
const treeData = [
{ key: '1', title: '1' },
{ key: '1.1', title: '1' },
{
key: '2',
title: '2',
children: [
{ key: '2-1', title: '2-1' },
{
key: '2-2',
title: '2-2',
children: [{ key: '2-2-1', title: '2-2-1' }],
},
{ key: '2-2-2', title: '2-2-2' },
],
},
];
getFieldValueSet(treeData, 'title') // new Set(['1', '2', '2-1', '2-2', '2-2-1', '2-2-2'])
getFieldValueSet(treeData, item => item.title) // new Set(['1', '2', '2-1', '2-2', '2-2-1', '2-2-2'])
calculateLeafCount
param:
- treelikeData(Array): treelike data
- [options={}] (Object): option
- [options.childrenKeyName = 'children'] (string) : childrenKeyName, default 'children'
return:
count(number): leaf count
eg:
const treeData = [
{ key: '1', children: [{ key: '1-1' }, { key: '1-2' }] },
{ key: '2' },
{
key: '3',
children: [
{
key: '3-1',
children: [{ key: '3-1-1' }, { key: '3-1-2' }, { key: '3-1-3' }],
},
],
},
]
const count = calculateLeafCount(treeData)
// => 6
countNestedLayers
param:
- treelikeData(Array): treelike data
- [options={}] (Object): option
- [options.childrenKeyName = 'children'] (string) : childrenKeyName, default 'children'
return:
layer(number): data layer
eg:
countNestedLayers([]) // 0
countNestedLayers([{ key: '1', title: '1' }]) // 1
countNestedLayers([{ key: '1', children: [{ key: '1-1' }] }]) // 2
LICENSE
MIT