pathlib-js

- [Why pathlib-js](#about) - [Usage & API Links](#usage) - [The Wonderful Libraries that this wraps around](#wrappers) - [Testing](#testing) - [Authors](#authors) - [Acknowledgments](#acknowledgement)

Usage no npm install needed!

<script type="module">
  import pathlibJs from 'https://cdn.skypack.dev/pathlib-js';
</script>

README

Project logo


Object-oriented Filepath Operations in Javascript & Typescript

Status GitHub Issues License

๐Ÿ“ Table of Contents

๐Ÿค” Why pathlib-js?

If you're coming from Python, think of it this way: this library is to NodeJS's fs and path modules as Python's pathlib is to its own os module.

Avoids the hassle of string preprocessing, filepath normalization, and passing filepaths between the multitude of functions found in nodeJS filesystem and path modules.

A single class Path wraps around a filepath and exposes an API for accessing its components, globbing, watching, reading, writing, traversing a tree structure, etc.

This is not the first package to take an OOP approach to filepaths in Javascript, but hopes to be more feature-rich than others from 5-6 years ago.

Prerequisites

NodeJS v12 or greater

Installing

You know the drill

npm i pathlib-js

or

yarn add pathlib-js

๐Ÿ› ๏ธ Usage & API Links

Understanding parts of a Path

Path instances carry properties that tie in with the typically-used components of a filepath.

For example, if a particular Path instance was located in a variable example and you wished to retrieve the basename of the underlying filepath, you can simply access it by example.basename.

File Components of a Windows system

File Components of a Unix system

Defining a path

Paths can be defined from a single string or multiple strings which are resolved into a single path.

import Path from "pathlib-js"
const fp1 = new Path("/home/Documents/Yougetthepoint.json");
const fp2 = new Path("/", "home/watch_this_disappear", "../Documents/Yougetthepoint.json");
console.log(fp1.path === fp2.path);

> true

More concrete usages & API

๐Ÿงช Testing

This library is regularly updating its test suite with additional case scenarios.

At the current time, please anticipate the following conditional test failures:

  • On Windows, makeSymlink() and makeSymlinkSync() methods will fail if the environment they are used in lacks administrator priveleges (i.e. testing in VScode without starting it up with admin priveleges). This caveat has not been seen on Unix-based systems.

๐Ÿฅ‡ The Wonderful Libraries that this wraps around

This wouldn't be possible without these awesome libraries. Give them a star.

  • node-fs-extra - Enhanced filesystem operations.
  • chokidar - Robust and efficient filesystem watching.
  • fast-glob - Robust and fast globbing potential.
  • path-normalize - Workhorse function for robustly normalizing filepaths

โœ๏ธ Authors

๐ŸŽ‰ Acknowledgements

A thank you to the individuals who actually read README file from start to finish.