Installations
npm install nice-path
Developer Guide
Typescript
Yes
Module System
CommonJS
Node Version
18.18.2
NPM Version
9.8.1
Score
72.8
Supply Chain
98.9
Quality
81.4
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (100%)
Developer
suchipi
Download Statistics
Total Downloads
878
Last Day
1
Last Week
8
Last Month
105
Last Year
451
GitHub Statistics
12 Stars
16 Commits
3 Watching
1 Branches
1 Contributors
Bundle Size
3.05 kB
Minified
1.21 kB
Minified + Gzipped
Package Meta Information
Latest Version
3.0.0
Package Id
nice-path@3.0.0
Unpacked Size
35.07 kB
Size
8.23 kB
File Count
7
NPM Version
9.8.1
Node Version
18.18.2
Publised On
05 Jan 2025
Total Downloads
Cumulative downloads
Total Downloads
878
Last day
0%
1
Compared to previous day
Last week
166.7%
8
Compared to previous week
Last month
238.7%
105
Compared to previous month
Last year
226.8%
451
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
4
nice-path
nice-path
provides a class that represents a filesystem path (POSIX-style or Win32-style), which has various nice methods on it that make it easy to work with. It can be used as a replacement for the Node.js path
builtin module, where you pass around Path objects and stringify them before use, rather than passing around strings.
nice-path
's code is derived from yavascript.
Example
1import { Path } from "nice-path"; 2 3const here = new Path(__dirname); 4console.log(here); 5// Path { 6// segments: ["", "home", "suchipi", "Code", "nice-path"], 7// separator: "/" 8// } 9 10here.toString(); // "/home/suchipi/Code/nice-path" 11here.isAbsolute(); // true 12 13const readme = here.concat("README.md"); 14console.log(readme); 15// Path { 16// segments: [ 17// "", 18// "home", 19// "suchipi", 20// "Code", 21// "nice-path", 22// "README.md" 23// ], 24// separator: "/" 25// } 26readme.basename(); // "README.md" 27readme.extname(); // ".md" 28readme.dirname(); // Path object with same contents as 'here' 29 30// normalize resolves . and .. components 31const homeDir = here.concat("../../.").normalize(); 32console.log(homeDir); 33// Path { 34// segments: [ 35// "", 36// "home", 37// "suchipi" 38// ], 39// separator: "/" 40// } 41 42here.relativeTo(homeDir).toString(); // "./Code/nice-path" 43readme.relativeTo(homeDir).toString(); // "./Code/nice-path/README.txt" 44 45// There are also several other methods which aren't in this example.
API Documentation
Overview
This package has one named export: "Path", which is a class.
The "Path" class has the following instance properties:
- segments (Array of string)
- separator (string)
and the following instance methods:
- normalize
- concat
- isAbsolute
- clone
- relativeTo
- toString
- basename
- extname
- dirname
- startsWith
- endsWith
- indexOf
- includes
- replace
- replaceAll
- replaceLast
and the following static methods:
- splitToSegments
- detectSeparator
- normalize
- isAbsolute
- fromRaw
Details
Path (class)
An object that represents a filesystem path. It has the following constructor signature:
1class Path { 2 constructor(...inputs: Array<string | Path | Array<string | Path>>); 3}
You can pass in zero or more arguments to the constructor, where each argument can be either a string, a Path object, or an array of strings and Path objects.
When multiple strings/Paths are provided to the constructor, they will be concatenated together in order, left-to-right.
The resulting object has two properties: segments
, which is an array of strings containing all the non-slash portions of the Path, and separator
, which is the slash string that those portions would have between them if this path were represented as a string.
segments: Array<string>
(instance property of Path)
Each Path
object has a segments
property, which is an array of strings containing all the non-slash portions of the Path.
For example, given a path object myPath = new Path("a/b/c/d")
, myPath.segments
is an array of strings ["a", "b", "c", "d"]
.
You may freely mutate this array in order to add or remove segments, but I recommend you instead use instance methods on the Path object, which take a "separator-aware" approach to looking at path segments.
POSIX-style absolute paths start with a leading slash character, like "/a/b/c". A Path object representing that path, (ie new Path("/a/b/c")
) would have the following path segments:
1["", "a", "b", "c"]
That empty string in the first position represents the "left side" of the first slash. When you use .toString()
on that Path object, it will become "/a/b/c" again, as expected.
Windows UNC paths have two empty strings at the beginning of the Array.
separator: string
(instance property of Path)
Each Path
object has a separator
property, which is the slash string that those portions would have between them if this path were represented as a string. It's always either /
(forward slash) or \
(backward slash). If you change this property, the result of calling .toString()
on the Path object will change:
1// The initial value of separator is inferred from the input path: 2const myPath = new Path("hi/there/friend"); 3console.log(myPath.separator); // prints / 4 5// Using toString() joins the path segments using the separator: 6console.log(myPath.toString()); // prints hi/there/friend 7 8// If you change the separator... (note: this is a single backslash character. It has to be "escaped" with another one, which is why there are two.) 9myPath.separator = "\\"; 10 11// Then toString() uses the new separator instead: 12console.log(myPath.toString()); // prints hi\there\friend
The initial value of the separator
property is inferred by searching the input string(s) the Path was constructed with for a slash character and using the first one found. If none is found, a forward slash (/
) is used.
normalize(): Path
(instance method of Path)
The normalize
method returns a new Path with all non-leading .
and ..
segments resolved.
1const myPath = new Path("/some/where/../why/over/./here"); 2const resolved = myPath.normalize(); 3console.log(resolved.toString()); // /some/why/over/here
If you want to evaluate a relative path (a path with leading .
or ..
segments) using a base directory, you can concatenate and then normalize them:
1const baseDir = new Path("/home/me"); 2const relativeDir = new Path("./somewhere/something/../blue"); 3const resolved = baseDir.concat(relativeDir).normalize(); 4console.log(resolved.toString()); // /home/me/something/blue
concat(...others): Path
(instance method of Path)
The concat
method creates a new Path by appending additional path segments onto the end of the target Path's segments. The additional path segments are passed to the concat method as either strings, Paths, or Arrays of strings and Paths.
The new Path will use the separator from the target Path.
1const pathOne = new Path("a/one"); 2const withStuffAdded = pathOne.concat( 3 "two", 4 "three/four", 5 new Path("yeah\\yes"), 6); 7 8console.log(withStuffAdded.toString()); 9// "a/one/two/three/four/yeah/yes"
isAbsolute(): boolean
(instance method of Path)
The isAbsolute
method returns whether the target Path is an absolute path; that is, whether it starts with either /
, \
, or a drive letter (ie C:
).
clone(): Path
(instance method of Path)
The clone
method makes a second Path object containing the same segments and separator as the target.
Note that although the new Path has the same segments as the target Path, it doesn't use the same Array instance.
relativeTo(dir, options?): Path
(instance method of Path)
The relativeTo
method expresses the target path as a relative path, relative to the dir
argument.
The options
argument, if present, should be an object with the property "noLeadingDot", which is a boolean. The noLeadingDot option controls whether the resulting relative path has a leading .
segment or not. If this option isn't provided, the leading dot will be present. Note that if the resulting path starts with "..", that will always be present, regardless of the option.
toString(): string
(instance method of Path)
The toString
method returns a string representation of the target Path by joining its segments using its separator.
basename(): string
(instance method of Path)
The basename
method returns the final segment string of the target Path. If that Path has no segments, the empty string is returned.
extname(options?): string
(instance method of Path)
The extname
method returns the trailing extension of the target Path. Pass { full: true }
as the "options" argument to get a compound extension like ".d.ts", rather than the final extension (like ".ts").
If the Path doesn't have a trailing extension, the empty string (""
) is returned.
dirname(): Path
(instance method of Path)
The dirname
method returns a new Path containing all of the segments in the target Path except for the last one; ie. the path to the directory that contains the target path.
startsWith(value): boolean
(instance method of Path)
The startsWith
method returns whether the target Path starts with the provided value (string, Path, or Array of string/Path), by comparing one path segment at a time, left-to-right.
The starting segment(s) of the target Path must exactly match the segment(s) in the provided value.
This means that, given two Paths A and B:
A: Path { /home/user/.config }
B: Path { /home/user/.config2 }
Path B does not start with Path A, because ".config" !== ".config2"
.
endsWith(value): boolean
(instance method of Path)
The endsWith
method returns whether the target Path ends with the provided value (string, Path, or Array of string/Path), by comparing one path segment at a time, right-to-left.
The ending segment(s) of the target Path must exactly match the segment(s) in the provided value.
This means that, given two Paths A and B:
A: Path { /home/1user/.config }
B: Path { user/.config }
Path A does not end with Path B, because "1user" !== "user"
.
indexOf(value, fromIndex?): number;
(instance method of Path)
The indexOf
method returns the path segment index (number) at which value
(string, Path, or Array of string/Path) appears in the target Path, or -1
if it doesn't appear.
If the provided value argument contains more than one path segment, the returned index will refer to the location of the value's first path segment.
The optional argument fromIndex
can be provided to specify which index into the target Path's segments to begin the search at. If not provided, the search starts at the beginning (index 0).
includes(value, fromIndex?): boolean;
(instance method of Path)
The includes
method returns a boolean indicating whether value
(string, Path, or Array of string/Path) appears in the target Path.
The optional argument fromIndex
can be provided to specify which index into the target Path's segments to begin the search at. If not provided, the search starts at the beginning (index 0).
replace(value, replacement): Path;
(instance method of Path)
The replace
method returns a new Path
object wherein the first occurrence of value
(string, Path, or Array of string/Path) in the target Path has been replaced with replacement
(string, Path, or Array of string/Path).
Note that only the first match is replaced; to replace multiple occurrences, use replaceAll
.
If value
is not present in the target Path, a clone of said Path is returned.
Tip: To "replace with nothing", pass an empty array as the replacement.
replaceAll(value, replacement): Path;
(instance method of Path)
The replaceAll
method returns a new Path
object wherein all occurrences of value
(string, Path, or Array of string/Path) in the target Path have been replaced with replacement
(string, Path, or Array of string/Path).
If you want to only replace the first occurrence, use replace
instead.
If value
is not present in the target Path, a clone of said Path is returned.
Tip: To "replace with nothing", pass an empty array as the replacement.
replaceLast(replacement): Path;
(instance method of Path)
The replaceLast
method returns a copy of the target Path, but with its final segment replaced with replacement
(string, Path, or Array of string/Path).
This method is most commonly used to modify the final (filename) part of a path.
If the target Path has no segments, the returned Path will contain exactly the segments from replacement
.
Path.splitToSegments(inputParts): Array<string>
(static method on Path)
Splits one or more path strings into an array of path segments. Used internally by the Path constructor.
Path.detectSeparator(input, fallback)
(static method on Path)
Searches input (a string or Array of strings) for a path separator character (either forward slash or backslash), and returns it. If none is found, returns fallback
.
Path.normalize(...inputs): Path
(static method on Path)
Concatenates the input path(s) and then resolves all non-leading .
and ..
segments. Shortcut for new Path(...inputs).normalize()
.
Path.isAbsolute(path)
: boolean (static method on Path)
Return whether the path
argument (string or Path) is an absolute path; that is, whether it starts with either /
, \
, or a drive letter (ie C:
).
Path.fromRaw(segments, separator): Path
(static method on Path)
Creates a new Path object using the provided segments and separator.
License
MIT
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
1 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-vg6x-rcgg-rjx6
Reason
2 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 1
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
Reason
Found 0/16 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Score
3
/10
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to nice-path
path-nice
The elegant and handy alternative to path, fs and glob
@nomemo/nice-import-sorting
A Prettier plugin that sorts import statements within JS, JSX, TS, and TSX files using the import names instead of the module/path names.
gentle-path-truncate
A package which nicely truncates path strings.
epr
A tool for making node require paths nicer