1.0.3 • Published 4 years ago

paths2tree v1.0.3

Weekly downloads
23
License
MIT
Repository
github
Last release
4 years ago

paths2tree

Support me on Patreon Buy me a book PayPal Ask me anything Version Downloads Get help on Codementor

Convert a list of paths into a tree.

:cloud: Installation

# Using npm
npm install --save paths2tree

# Using yarn
yarn add paths2tree

:clipboard: Example

const paths2tree = require("paths2tree")

console.log(paths2tree([
    "index.html",
    "README.md",
    "assets/css/style.css",
    "assets/js/index.css",
    "images/background/1.png",
    "images/background/2.png"
]))
// {
//     "path": "",
//     "name": "",
//     "children": [{
//         "path": "/README.md",
//         "name": "README.md",
//         "children": []
//     }, {
//         "path": "/assets",
//         "name": "assets",
//         "children": [{
//             "path": "/assets/css",
//             "name": "css",
//             "children": [{
//                 "path": "/assets/css/style.css",
//                 "name": "style.css",
//                 "children": []
//             }]
//         }, {
//             "path": "/assets/js",
//             "name": "js",
//             "children": [{
//                 "path": "/assets/js/index.css",
//                 "name": "index.css",
//                 "children": []
//             }]
//         }]
//     }, {
//         "path": "/images",
//         "name": "images",
//         "children": [{
//             "path": "/images/background",
//             "name": "background",
//             "children": [{
//                 "path": "/images/background/1.png",
//                 "name": "1.png",
//                 "children": []
//             }, {
//                 "path": "/images/background/2.png",
//                 "name": "2.png",
//                 "children": []
//             }]
//         }]
//     }, {
//         "path": "/index.html",
//         "name": "index.html",
//         "children": []
//     }]
// }

// Custom separator, also with a custom handler called on node creation
let i = 0
console.log(paths2tree([
    "a",
    "a|b|c|d|e|f",
], "|", node => {
    node.id = ++i
}))
// {
//     "path": "",
//     "name": "",
//     "children": [{
//         "path": "|a",
//         "name": "a",
//         "children": [{
//             "path": "|a|b",
//             "name": "b",
//             "children": [{
//                 "path": "|a|b|c",
//                 "name": "c",
//                 "children": [{
//                     "path": "|a|b|c|d",
//                     "name": "d",
//                     "children": [{
//                         "path": "|a|b|c|d|e",
//                         "name": "e",
//                         "children": [{
//                             "path": "|a|b|c|d|e|f",
//                             "name": "f",
//                             "children": [],
//                             "id": 7
//                         }],
//                         "id": 6
//                     }],
//                     "id": 5
//                 }],
//                 "id": 4
//             }],
//             "id": 3
//         }],
//         "id": 2
//     }],
//     "id": 1
// }

:question: Get Help

There are few ways to get help:

  1. Please post questions on Stack Overflow. You can open issues with questions, as long you add a link to your Stack Overflow question.
  2. For bug reports and feature requests, open issues. :bug:
  3. For direct and quick help, you can use Codementor. :rocket:

:memo: Documentation

paths2tree(paths, delimiter, onNode)

Convert a list of paths into a tree.

Params

  • Array paths: An array of paths.
  • String delimiter: The delimiter. Default: /.
  • Function onNode: An optional function called when the node is created.

Return

  • Object The path tree.

:yum: How to contribute

Have an idea? Found a bug? See how to contribute.

:sparkling_heart: Support my projects

I open-source almost everything I can, and I try to reply to everyone needing help using these projects. Obviously, this takes time. You can integrate and use these projects in your applications for free! You can even change the source code and redistribute (even resell it).

However, if you get some profit from this or just want to encourage me to continue creating stuff, there are few ways you can do it:

  • Starring and sharing the projects you like :rocket:
  • Buy me a book—I love books! I will remember you after years if you buy me one. :grin: :book:
  • PayPal—You can make one-time donations via PayPal. I'll probably buy a coffee tea. :tea:
  • Support me on Patreon—Set up a recurring monthly donation and you will get interesting news about what I'm doing (things that I don't share with everyone).
  • Bitcoin—You can send me bitcoins at this address (or scanning the code below): 1P9BRsmazNQcuyTxEqveUsnf5CERdq35V6

    npm.io

Thanks! :heart:

:scroll: License

MIT © Ionică Bizău