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

treeify-paths

v2.0.1

Published

list of file names becomes a tree of file descriptions

Downloads

1,176

Readme

treeify-paths

NPM version npm

Provide a list of file names:

  • blog/all.html
  • blog/2036/overflows.html

And recieve a directory-like tree:

  • blog
    • all.html
    • 2036
      • overflows.html

Use Cases

Useful when converting a list of file names into a nested UL/LI tree. Nice for site maps, etc.

Installation:

Install it with NPM:

npm install --save treeify-paths
import treeifyPaths from "treeify-paths";

If you are not using NPM, install the library by downloading the source file and including it in your project:

curl -o treeify-paths.js "https://raw.githubusercontent.com/khtdr/treeify-paths/master/treeify-paths.js"
let treeify_paths = require("./treeify-paths").default;

Usage:

This module provides a function treeifyPaths that takes a list of file names and returns a directory-like tree.

the following script:

import treeifyPaths from 'treeify-paths';
console.log(JSON.stringify(treeifyPaths([
  'about.html',
  'careers',
  'careers/job-1.html',
  'careers/job-2.html',
  'to/some/page.aspx',
]), null, 3);

produces the following output:

{
  "path": "",
  "name": "",
  "children": [
    {
      "path": "about.html",
      "name": "about.html",
      "children": []
    },
    {
      "path": "careers",
      "name": "careers",
      "children": [
        {
          "path": "careers/job-1.html",
          "name": "job-1.html",
          "children": []
        },
        {
          "path": "careers/job-2.html",
          "name": "job-2.html",
          "children": []
        }
      ]
    },
    {
      "path": "to",
      "name": "",
      "children": [
        {
          "path": "to/some",
          "name": "",
          "children": [
            {
              "path": "to/some/page.aspx",
              "name": "page.aspx",
              "children": []
            }
          ]
        }
      ]
    }
  ]
}

Controlling the behavior

You may pass in a list of path/context pairs. The context is available in the result.

import treeifyPaths from 'treeify-paths';
console.log(JSON.stringify(treeifyPaths([
  ['about.html', {admin:false}],
  ['careers', {admin:false}],
  ['careers/job-1.html', {admin:false}],
  ['careers/job-2.html', {admin:false}],
  ['to/some/page.aspx', {admin:true}],
]), null, 3);

The context is available on the result:

{
  "path": "about.html",
  "name": "about.html",
  "children": [],
  "ctx": { "admin": false }
}

By default:

  • sorting is case-sensitive
  • directories and files are treated the same

There are options for overridding this:

import treeifyPaths from "treeify-paths";
treeifyPaths([], {
  caseInsensitive: true, // default false
  directoriesFirst: true, // default false
  directoriesLast: true, // default false
});

Testing

The mocha tests have many examples

> npm run test

  treeifyPaths([...arguments])
    arguments: none
      ✔ should return an empty object
    arguments: empty list
      ✔ should return an empty object
    arguments: list with a single file
      ✔ should return a single file
      ✔ should ignore leading slashes
      ✔ should return with nested children
    arguments: multiple file names
      ✔ should return with nested children
      ✔ should ignore perimeter slashes and empty or redundant entries
      ✔ should alphabetize
      ✔ should alphabetize case-sensitve
      ✔ should not respect directories
      ✔ should ignore duplicates
      ✔ should be able to target directories

  12 passing (7ms)