@flex-development/pathe
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

pathe

github release npm codecov license conventional commits typescript vitest yarn

Universal drop-in replacement for node:path

Contents

What is this?

This package is a universal drop-in replacement for Node.js' path module.

It enforces consistency between POSIX and Windows operating systems and also provides additional utilities for adding, changing, formatting, and removing file extensions.

When should I use this?

For historical reasons, Windows operating systems used backslashes (\) to separate path components. This was in stark contrast to POSIX operating systems, which used forwardslashes (/). Even though Windows operating systems now support both separators, there are still discrepancies between operating systems when using Node.js' path module:

The default operation of the node:path module varies based on the operating system on which a Node.js application is running. Specifically, when running on a Windows operating system, the node:path module will assume that Windows-style paths are being used. – Windows vs. POSIX

This package enforces consistency between operating systems by ensuring all paths meet POSIX standards. Forwardslashes (/) will be used to separate path components; semicolons (;) will be used to delimit paths. With full support for both drive paths and UNC paths as well, platform-specific modules like node:path/posix and node:path/win32 are no longer needed.

To achieve consistent results when working with Windows file paths on any operating system, use path.win32. To achieve consistent results when working with POSIX file paths on any operating system, use path.posix.

To achieve consistent results when working with Windows file paths on any operating system, use pathe. To achieve consistent results when working with POSIX file paths on any operating system, use pathe. 😊

Install

This package is ESM only.

yarn add @flex-development/pathe

From Git:

yarn add @flex-development/pathe@flex-development/pathe
See Git - Protocols | Yarn  for details on requesting a specific branch, commit, or tag.

API

This package exports the following identifiers:

The default export is pathe.

Core

See node:path for Node.js documentation.

basename(path: string, suffix?: string): string

  • path: Path to evaluate
  • suffix?: Suffix to remove from result

Returns the last portion of a path, similar to the Unix basename command.

Trailing directory separators are ignored.

import { basename } from '@flex-development/pathe'

console.debug(basename('/tmp/file.html'))                // 'file.html'
console.debug(basename('/tmp/file.html', '.html'))       // 'file'
console.debug(basename('C:\\temp\\file.html'))           // 'file.html'
console.debug(basename('C:\\temp\\file.HTML', '.html'))  // 'file.HTML'

Source: src/lib/basename.ts

delimiter

Path delimiter.

import { delimiter } from '@flex-development/pathe'

console.debug(delimiter)  // ':'

Source: src/lib/delimiter.ts

dirname(path: string): string

  • path: Path to evaluate

Returns the directory name of a path, similar to the Unix dirname command.

Trailing directory separators are ignored.

import { dirname } from '@flex-development/pathe'

console.debug(dirname('/users/user/projects/pathe/README.md'))  // 'src'

Source: src/lib/dirname.ts

extname(path: string): EmptyString | Ext

  • path: Path to evaluate

Returns the extension of a path, from the last occurrence of the . (dot) character to end of the string in the last portion of the path.

If there is no . in the last portion of the path, or if there are no . characters other than the first character of the path's basename, an empty string will be returned.

import { extname } from '@flex-development/pathe'

console.debug(extname('index.html'))  // '.html'
console.debug(extname('index.d.ts'))  // '.ts'
console.debug(extname('.index.md'))   // '.md'
console.debug(extname('index.'))      // '.'
console.debug(extname('.index'))      // ''
console.debug(extname('index'))       // ''

Source: src/lib/extname.ts

format(pathObject: FormatInputPathObject): string

  • pathObject: Object to evaluate

Returns a path string from an object — the opposite of parse.

When adding properties to pathObject, there are combinations where one property has priority over another:

  • pathObject.root is ignored if pathObject.dir is provided
  • pathObject.ext is ignored if pathObject.base exists
  • pathObject.name is ignored if pathObject.base exists
import { format, sep } from '@flex-development/pathe'

console.debug(format({ base: 'file.txt', dir: '/user/dir', root: '/noop' }))  // '/user/dir/file.txt'
console.debug(format({ base: 'file.txt', ext: 'noop', root: sep }))           // '/file.txt'
console.debug(format({ ext: '.txt', name: 'file', root: sep }))               // '/file.txt'
console.debug(format({ ext: 'txt', name: 'file', root: sep }))                // '/file.txt'

Source: src/lib/format.ts

isAbsolute(path: string): boolean

  • path: Path to evaluate

Determines if path is an absolute path.

If the given path is a zero-length string, false will be returned.

import { isAbsolute } from '@flex-development/pathe'

console.debug(isAbsolute(''))                                // false
console.debug(isAbsolute('.'))                               // false
console.debug(isAbsolute('C:'))                              // false
console.debug(isAbsolute('loader.mjs'))                      // false
console.debug(isAbsolute('/user/dir/file.txt'))              // true
console.debug(isAbsolute('C:\\temp\\file.html'))             // true
console.debug(isAbsolute('\\\\host\\share\\dir\\file.txt'))  // true

Source: src/lib/is-absolute.ts

join(...paths: string[]): string

  • ...paths: Path segment sequence

Joins all given path segments together using a path separator as the delimiter, then normalizes the resulting path.

Zero-length path segments are ignored. If the joined path string is a zero-length string then '.' will be returned, representing the current working directory.

import { join } from '@flex-development/pathe'

console.debug(join('', '', ''))                        // '.'
console.debug(join('/user', 'dir', 'file.txt', '..'))  // '/user/dir/file.txt'

Source: src/lib/join.ts

normalize(path: string): string

  • path: Path to evaluate

Normalizes the given path, resolving '..' and '.' segments.

When multiple, sequential path segment separation characters are found (e.g. / on POSIX and either \\ or / on Windows), they are replaced by a single instance of a POSIX-compliant separator. Trailing separators are preserved.

If the path is a zero-length string, '.' is returned, representing the current working directory.

import { normalize } from '@flex-development/pathe'

console.debug(normalize('/user/dir//file.txt/..'))          // '/user/dir/file.txt'
console.debug(normalize('C:\\temp\\\\foo\\bar\\..\\'))      // 'C:/temp/foo/'
console.debug(normalize('C:////temp\\\\/\\/\\/foo/bar'))    // 'C:/temp/foo/bar'
console.debug(normalize('\\\\host\\share\\dir\\file.txt'))  // '//host/share/dir/file.txt'

Source: src/lib/normalize.ts

parse(path: string): ParsedPath

  • path: Path to evaluate

Returns an object whose properties represent significant elements of the given path. Trailing directory separators are ignored.

Note: Unlike node:path, parse(path).dir === dirname(path) when path is a non-empty string. See nodejs/node#18655 for details.

import { parse } from '@flex-development/pathe'

console.debug(parse('/dir/file.txt'))      // { base: 'file.txt', dir: '/dir', ext: '.txt', name: 'file', root: '/'}
console.debug(parse('C:\\dir\\file.txt'))  // { base: 'file.txt', dir: 'C:/dir', ext: '.txt', name: 'file', root: 'C:/'}

Source: src/lib/parse.ts

relative(from: string, to: string): string

  • path: Start path
  • to: Destination path

Returns the relative path from from to to based on the current working directory.

If from and to resolve to the same path (after calling resolve on each), a zero-length string will be returned.

If a zero-length string is passed as from or to, the current working directory will be used instead of the zero-length strings.

import { relative } from '@flex-development/pathe'

console.debug(relative('/src/index.ts', '/src/lib/index.ts'))  // '../index.ts'

Source: src/lib/relative.ts

resolve(...paths: string[]): string

  • ...paths: Path segment sequence

Resolves a sequence of paths or path segments into an absolute path.

The given sequence of paths is processed from right to left, with each subsequent path prepended until an absolute path is constructed.

For instance, given the sequence of path segments: /foo, /bar, baz, calling resolve('/foo', '/bar', 'baz') would return /bar/baz because 'baz' is not an absolute path but '/bar' + '/' + 'baz' is.

If, after processing all given path segments, an absolute path has not yet been generated, the current working directory is used.

The resulting path is normalized and trailing separators are removed unless the path is resolved to the root directory.

Zero-length path segments are ignored.

If no path segments are passed, the absolute path of the current working directory will be returned.

import { resolve } from '@flex-development/pathe'

console.debug(resolve('/foo/bar', './baz'))       // '/foo/bar/baz'
console.debug(resolve('/foo/bar', '/tmp/file/'))  // '/tmp/file'

Source: src/lib/resolve.ts

sep

Path segment separator.

Also known as a "directory separator".

import { sep } from '@flex-development/pathe'

console.debug(sep)  // '/'

Source: src/lib/sep.ts

toNamespacedPath(path: string): string

  • path: Path to evaluate

Returns an equivalent namespace-prefixed path for the given path.

If path is not a drive path or UNC path, path will be returned without modifications.

import { toNamespacedPath } from '@flex-development/pathe'

console.debug(toNamespacedPath('C:\\file.txt'))                    // '//?/C:/file.txt'
console.debug(toNamespacedPath('\\\\host\\share\\dir\\file.txt'))  // '//?/UNC/host/share/dir/file.txt'
console.debug(toNamespacedPath('/projects/pathe/loader.mjs'))      // '/users/projects/pathe/loader.mjs'

Source: src/lib/to-namespaced-path.ts

Utilities

addExt(path: string, ext?: Nullable<string>): string

  • path: Path to evaluate
  • ext?: File extension to add

Appends a file extension to the given path if and only if the path does not already have that exact file extension.

Does nothing if a file extension is not provided.

import { addExt } from '@flex-development/pathe'

console.debug(addExt('file'))                // 'file'
console.debug(addExt('file', 'mjs'))         // 'file.mjs'
console.debug(addExt('file', '.mjs'))        // 'file.mjs'
console.debug(addExt('file.d.mts', '.mts'))  // 'file.d.mts'

Source: src/utils/add-ext.ts

changeExt(path: string, ext?: Nullable<string>): string

  • path: Path to evaluate
  • ext?: New file extension

Changes the file extension of the given path.

Does nothing if a file extension isn't provided. If the file extension is an empty string, however, the path's file extension will be removed.

import { changeExt } from '@flex-development/pathe'

console.debug(changeExt('file'))                // 'file'
console.debug(changeExt('file.mjs', ''))        // 'file'
console.debug(changeExt('file', 'mjs'))         // 'file.mjs'
console.debug(changeExt('file', '.mjs'))        // 'file.mjs'
console.debug(changeExt('file.mts', '.d.mts'))  // 'file.d.mts'

Source: src/utils/change-ext.ts

defaultExt(path: string, ext?: Nullable<string>, ignore?: string[]): string

  • path: Path to evaluate
  • ext?: Default file extension
  • ignore?: File extensions to ignore if found

Appends a file extension to the given path if and only if the path does not already have an extension. Force adding an extension can be accomplished by passing an array of extensions to ignore.

Does nothing if a file extension is not provided.

import { defaultExt } from '@flex-development/pathe'

console.debug(defaultExt('file'))                    // 'file'
console.debug(defaultExt('file', 'mjs'))             // 'file.mjs'
console.debug(defaultExt('file', '.mjs'))            // 'file.mjs'
console.debug(defaultExt('file.mjs', '.mjs'))        // 'file.mjs'
console.debug(defaultExt('file.d', '.mts', ['.d']))  // 'file.d.mts'

Source: src/utils/default-ext.ts

formatExt(ext?: string): EmptyString | Ext

  • ext: File extension to format

Formats a file extension.

This includes:

  • Prepending a . (dot) character if not already present

Does nothing if a file extension is not provided.

import { formatExt } from '@flex-development/pathe'

console.debug(formatExt())         // ''
console.debug(formatExt(''))       // ''
console.debug(formatExt('.ts'))    // '.ts'
console.debug(formatExt('mjs'))    // '.mjs'
console.debug(formatExt('d.mts'))  // '.d.mts'

Source: src/utils/format-ext.ts

removeExt(path: string, ext?: Nullable<string>): string

  • path: Path to evaluate
  • ext?: File extension to remove

Removes a file extension from the given path.

Does nothing if path does not end with the provided file extension, or if a file extension isn't provided.

import { removeExt } from '@flex-development/pathe'

console.debug(removeExt('file'))                // 'file'
console.debug(removeExt('file.mjs', 'mjs'))     // 'file'
console.debug(removeExt('file.mjs', '.mjs'))    // 'file'
console.debug(removeExt('file.d.mts', '.mjs'))  // 'file.d.mts'

Source: src/utils/remove-ext.ts

Types

This package is fully typed with TypeScript. It exports the following definitions:

Contribute

See CONTRIBUTING.md.

Package Sidebar

Install

npm i @flex-development/pathe

Weekly Downloads

4

Version

2.0.0

License

BSD-3-Clause

Unpacked Size

229 kB

Total Files

146

Last publish

Collaborators

  • unicornware