npm install cascade-helper --save
- subKey (string): key name of represent sub object
- valueKey (string): key name of represent value
const cascadeHelper = new CascadeHelper([(subKey = 'children')], [(valueKey = 'value')])
cascadeHelper.deepFlatten(cascades[, options])
Array<Cascade>
KEY |
TYPE |
DEFAULT |
labels |
Array<string> |
[] |
itemSeparator |
string |
'-' |
endLevel |
nubmer |
|
const cascades = [
{ name: '0.0', value: '0.0', children: [{ name: '1.0', value: '1.0' }, { name: '1.1', value: '1.1' }] },
{ name: '0.1', value: '0.1', children: [{ name: '1.0', value: '1.0' }, { name: '1.1', value: '1.1' }] },
]
const results = cascadeHelper.deepFlatten(cascades, { labels: ['name'] })
console.log(results)
/****
[
{ strs: { name: '0.0-1.0' }, cascade: { name: '1.0', value: '1.0' }, path: '[0].children[0]' },
{ strs: { name: '0.0-1.1' }, cascade: { name: '1.1', value: '1.1' }, path: '[0].children[1]' },
{ strs: { name: '0.1-1.0' }, cascade: { name: '1.0', value: '1.0' }, path: '[1].children[0]' },
{ strs: { name: '0.1-1.1' }, cascade: { name: '1.1', value: '1.1' }, path: '[1].children[1]' },
]
****/
cascadeHelper.deepForEach(cascades, callback[, options])
Array<Cascade>
(cascade, level, index) => void
ARG |
TYPE |
cascade |
Cascade |
level |
number |
index |
number |
KEY |
TYPE |
DEFAULT |
startLevel |
number |
0 |
endLevel |
number |
|
const cascades = [
{ name: '0.0', value: '0.0', children: [{ name: '1.0', value: '1.0' }, { name: '1.1', value: '1.1' }] },
{ name: '0.1', value: '0.1', children: [{ name: '1.0', value: '1.0' }, { name: '1.1', value: '1.1' }] },
]
cascadeHelper.deepForEach(cascades, (cascade, level, index) => {
cascade.name = `modify-${level}-${index}`
})
console.log(cascades)
/****
[
{ name: 'modify-0-0', value: '0.0', children: [{ name: 'modify-1-0', value: '1.0' }, { name: 'modify-1-1', value: '1.1'}] },
{ name: 'modify-0-1', value: '0.1', children: [{ name: 'modify-1-0', value: '1.0' }, { name: 'modify-1-1', value: '1.1'}] },
]
****/
cascadeHelper.deepMap(cascades, callback[, options])
Array<Cascade>
(cascade, level, index, path, parent) => void
ARG |
TYPE |
cascade |
Cascade |
level |
number |
index |
number |
path |
string |
parent |
Cascade |
KEY |
TYPE |
DEFAULT |
startLevel |
number |
0 |
const cascades = [
{ name: '0.0', value: '0.0', children: [{ name: '1.0', value: '1.0' }, { name: '1.1', value: '1.1' }] },
{ name: '0.1', value: '0.1', children: [{ name: '1.0', value: '1.0' }, { name: '1.1', value: '1.1' }] },
]
const results = cascadeHelper.deepMap(cascades, (cascade, level, index, path) => {
return { ...cascade, name: `modify-${currentLevel}-${currentIndex}`, path }
})
console.log(results)
/****
[
{ name: 'modify-0-0', value: '0.0', path: '[0]', children: [{ name: 'modify-1-0', value: '1.0', path: '[0].children[0]' }, { name: 'modify-1-1', value: '1.1', path: '[0].children[1]'}] },
{ name: 'modify-0-1', value: '0.1', path: '[1]', children: [{ name: 'modify-1-0', value: '1.0', path: '[1].children[0]' }, { name: 'modify-1-1', value: '1.1', path: '[1].children[1]'}] },
]
****/
cascadeHelper.getLevelCascades(cascades, values, level)
Array<Cascade>
const cascades = [
{ name: '0.0', value: '0.0', children: [{ name: '1.0', value: '1.0' }, { name: '1.1', value: '1.1' }] },
{ name: '0.1', value: '0.1', children: [{ name: '1.0', value: '1.0' }, { name: '1.1', value: '1.1' }] },
]
const current = cascadeHelper.getLevelCascades(cascades, { level0: '0.0', level1: '1.1' }, 1)
console.log(current)
/****
{
cascades: [{ name: '1.0', value: '1.0' }, { name: '1.1', value: '1.1' }],
path: '[0].children',
parent: {
cascade: cascades[0],
index: 0,
level: 0,
},
}
****/
cascadeHelper.initValues(cascades, level[, index])
cascadeHelper.deepFill(cascades[, options])
cascadeHelper.parse(str, callback[, options])
string
(key, valueKey, level, index) => Cascade
ARG |
TYPE |
key |
string |
valueKey |
string |
level |
number |
index |
number |
KEY |
TYPE |
DEFAULT |
itemSeparator |
string |
'-' |
levelSeparator |
string |
'\n' |
const str = '1-1\n1-2\n2-1\n1-1-1'
const results = cascadeHelper.parse(str, (key, valueKey, level, index) => {
return { name: key, [valueKey]: `${level}.${index}`, other: 'other' }
})
console.log(results)
/****
[
{
name: '1',
value: '0.0',
other: 'other',
children: [
{ name: '1', value: '1.0', other: 'other', children: [{ name: '1', value: '2.0', other: 'other' }] },
{ name: '2', value: '1.1', other: 'other' },
],
},
{ name: '2', value: '0.1', other: 'other', children: [{ name: '1', value: '1.0', other: 'other' }] },
]
****/
cascadeHelper.stringify(cascades, label[, options])