npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

treelike-helper

v1.0.3

Published

Nested data tool functions

Downloads

4

Readme

treelike-helper

NPM version

中文文档

Use

install

npm install treelike-helper

import

import { mapTreeData } from 'treelike-helper' 

API

mapTreeData

param:

  1. treelikeData(Array): treelike data
  2. mapFunc(iteratee): item => item
  3. [options={}] (Object): option
  4. [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:

  1. treelikeData(Array): treelike data
  2. filterFunc(iteratee): item => item
  3. [options={}] (Object): option
  4. [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:

  1. treelikeData(Array): treelike data
  2. filterFunc(iteratee): item => item
  3. mapFunc(iteratee): item => item
  4. [options={}] (Object): option
  5. [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:

  1. treelikeData(Array): treelike data
  2. targetKey(string): to find key
  3. [options={}] (Object): option
  4. [options.childrenKeyName = 'children'] (string) : childrenKeyName, default 'children'
  5. [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:

  1. treelikeData(Array): treelike data
  2. targetKey(string): to find key
  3. [options={}] (Object): option
  4. [options.childrenKeyName = 'children'] (string) : childrenKeyName, default 'children'
  5. [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:

  1. treelikeData(Array): treelike data
  2. targetKey(string): to find key
  3. [options={}] (Object): option
  4. [options.childrenKeyName = 'children'] (string) : childrenKeyName, default 'children'
  5. [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:

  1. treelikeData(Array): treelike data
  2. search(string): to search
  3. [options={}] (Object): option
  4. [options.childrenKeyName = 'children'] (string) : childrenKeyName, default 'children'
  5. [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:

  1. treelikeData(Array): treelike data
  2. parentKey(string|number): to add parentKey
  3. data(Object | Array): to add data or dataArray
  4. [options={}] (Object): option
  5. [options.childrenKeyName = 'children'] (string) : childrenKeyName, default 'children'
  6. [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:

  1. treelikeData(Array): treelike data
  2. targetKey(string|number): to delete key
  3. [options={}] (Object): option
  4. [options.childrenKeyName = 'children'] (string) : childrenKeyName, default 'children'
  5. [options.keyName = 'key'] (tring): keyName, default 'key'
  6. [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:

  1. treelikeData(Array): treelike data
  2. targetKey(string|number): to update key
  3. data(Object | iteratee): to update data or updateFunc
  4. [options={}] (Object): option
  5. [options.childrenKeyName = 'children'] (string) : childrenKeyName, default 'children'
  6. [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:

  1. treelikeData(Array): treelike data
  2. targetKey(string|number): target key
  3. iteratee(iteratee): updateFunc
  4. [options={}] (Object): option
  5. [options.childrenKeyName = 'children'] (string) : childrenKeyName, default 'children'
  6. [options.keyName = 'key'] (tring): keyName, default 'key'
  7. [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:

  1. treelikeData(Array): treelike data
  2. field(string|iteratee): to get field
  3. [options={}] (Object): option
  4. [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:

  1. treelikeData(Array): treelike data
  2. field(string|iteratee): to get field
  3. [options={}] (Object): option
  4. [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:

  1. treelikeData(Array): treelike data
  2. [options={}] (Object): option
  3. [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:

  1. treelikeData(Array): treelike data
  2. [options={}] (Object): option
  3. [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