Skip to content

Transform flat path arrays into nested directory trees.

Notifications You must be signed in to change notification settings

LucaOttvn/path-mapper-json

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

path-mapper-json

A small library to turn an array of paths like the one below:

const paths = ["Coding", "Coding/JS", "Coding/JS/Enum.ts", "Coding/JS/ForLoop.md", "Coding/TS", "Security", "Security/Easy/Example1.ts", "Security/Medium"];

...into a JSON structure made like this:

[
    {
        "name": "Coding",
        "type": "tree",
        "children": [
            {
                "name": "JS",
                "type": "tree",
                "children": [
                    {
                        "name": "Enum.ts",
                        "type": "blob",
                        "children": []
                    },
                    {
                        "name": "ForLoop.md",
                        "type": "blob",
                        "children": []
                    }
                ]
            },
            {
                "name": "TS",
                "type": "tree",
                "children": []
            }
        ]
    },
    {
        "name": "Security",
        "type": "tree",
        "children": [
            {
                "name": "Easy",
                "type": "tree",
                "children": [
                    {
                        "name": "Example1.ts",
                        "type": "blob",
                        "children": []
                    }
                ]
            },
            {
                "name": "Medium",
                "type": "tree",
                "children": []
            }
        ]
    }
]

Installation

npm install path-mapper-json

Usage

import { buildTree } from 'path-mapper-json';

const paths = ["Coding", "Coding/JS", "Coding/JS/Enum.ts", "Coding/JS/ForLoop.md", "Coding/TS"];
const tree = buildTree(paths);

console.log(tree);

MIT © LucaOttvn

About

Transform flat path arrays into nested directory trees.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published