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 🙏

© 2025 – Pkg Stats / Ryan Hefner

filesystem-path

v0.4.1

Published

ES6 class that provides a fluent interface over strings representing filesystem paths

Readme

FilesystemPath class

A utility class, like native Date, that provides a fluent interface over strings representing paths on a computer filesystem. Better than doing string surgery.

Does not actually touch the filesystem.

Installation and usage

Install from npm:

npm install filesystem-path

Import and use the class:

const FilesystemPath = require('filesystem-path')

let home = new FilesystemPath('/Users/tomprogers')
//> '/Users/tomprogers'

home.setFilename('.npmrc')
//> '/Users/tomprogers/.npmrc'

home.setDirectory('/Users/tomprogers/projects/filesystem-path')
//> '/Users/tomprogers/projects/filesystem-path/.npmrc'

Anatomy of a FilesystemPath

The FilesystemPath class provides methods for accessing and mutating specific portions of a path by name. The parts are:

/Users/tomprogers/projects/filesystem─path/README.md
└──────────────────────────────────────────────────┴   path

/Users/tomprogers/projects/filesystem─path/README.md
└────────────────────────────────────────┴             directory

/Users/tomprogers/projects/filesystem─path/README.md
 └───┘ └────────┘ └──────┘ └─────────────┘ └───────┴   segments (array)

/Users/tomprogers/projects/filesystem─path/README.md
 └───┘ └────────┘ └──────┘ └─────────────┴             folders (array)

/Users/tomprogers/projects/filesystem─path/README.md
                                           └───────┴   filename

/Users/tomprogers/projects/filesystem─path/README.md
                                           └────┴      basename

/Users/tomprogers/projects/filesystem─path/README.md
                                                 └─┴   ext (incl. dot)

/Users/tomprogers/projects/filesystem─path/README.md
└                                                      root (the slash)

Thus, for the example path:

| Part | Value | | --: | :-- | | root | "/" | | folders | [ "Users", "tomprogers", "projects", "filesystem─path" ] | | segments | [ "Users", "tomprogers", "projects", "filesystem─path", "README.md" ] | | directory | "/Users/tomprogers/projects/filesystem-path" | | filename | "README.md" | | basename | "README" | | ext | ".md" |

API

Construction

The FilesystemPath constructor supports a few signatures:

  • new FilesystemPath( path )

This is the easiest way, but it forces the parser to guess what the directory separator is, and whether the final segment names a directory or a file. This can produce bad results in some cases.

Here, the parser misinterprets the final path segment as a filename because the final segment has a dotted suffix that looks like a file extension, and because no hints are provided to suggest otherwise:

// `/etc/pam.d` is a directory on *nix systems that houses PAM stuff, not a file
new FilesystemPath('/etc/pam.d')
//> directory='/etc'   filename='pam.d'
  • new FilesystemPath( directory, filename, sep )

Specifies everything unambiguously, guaranteed to produce an instance that behaves correctly.

new FilesystemPath( '/Users/tomprogers/projects/filesystem-path', 'README.md', '/' )

Argument order is important, but all three arguments are optional, so all these also work:

  • new FilesystemPath( path, sep )
  • new FilesystemPath( directory, filename )
  • new FilesystemPath( filename )
  • new FilesystemPath( filename, sep )
  • new FilesystemPath( sep )

Instance methods

Every instance has methods for getting and setting the different parts of the path. Generally speaking, every getter returns a String, and every setter returns the mutated instance (to support chaining).

  • getAbsolute() : Boolean - reflects whether the path begins with separator
  • getBasename() : String - returns the basename
  • getDirectory() : String - returns the directory, without trailing slash
  • getExt() : String - returns the ext, if any, including leading dot
  • getFilename() : String - returns the filename
  • getFolders() : Array<String> - returns mutable list of directories
  • getRoot() : String - returns sep if path is absolute, empty string otherwise
  • getSegments() : Array<String> - returns read-only list of directories plus filename
  • getSep() : String - returns the sep
  • setAbsolute( newAbsolute ) : FilesystemPath - set whether the path is absolute
  • setBasename( newBasename ) : FilesystemPath - overwrite the basename
  • setDirectory( newDirectory ) : FilesystemPath - overwrite everything except the filename
  • setExt( newExt ) : FilesystemPath - overwrite the ext; leading dot optional (will be added if needed)
  • setFilename( newFilename ) : FilesystemPath - overwrite the entire filename
  • setFolders( newFolders ) : FilesystemPath - overwrite the entire list of directories
  • setRoot( newRoot ) : FilesystemPath - overwrite the leading sep
  • setSegments( newSegments ) : FilesystemPath - overwrite all directories and filename, but not the root
  • setSep( newSep ) : FilesystemPath - change the path's sep; only accepts back- and forward-slash
  • toString() : String - returns the complete path as a string