Pathz
Functional toolkit for file/dir paths. Drop-in path
replacement.
Originally built for a static site generator where path transformations are common, but use-cases
are really unlimited.
Features
- High-level path manipulations
- Curried composable API
- Crossplatform: works in NodeJS and all modern browsers
- Extensive test suite
Peer dependencies:
@paqmind/ramdax
(temp. until basic Ramda)
Dependencies:
path
(Server)path-webpack
(Browser)
Usage
$ npm install pathz
let P = // defaults to either POSIX or WIN32let P_posix = // POSIX (P.sep is / etc.)let P_win32 = // WIN32 (P.sep is \ etc.) // The following snippets also use shortcuts for:let R = let PP =
Browser
Usage hint. Webpack's path-browserify
dependency (polyfill of path
) is very old and severily broken.
It doesn't even provide a parse
function! If you want to import path
directly, be sure to
substitute it with another package. Add the following to your webpack.config.js
:
resolve: alias: path: "path-webpack"
This can fix other libraries naively depending on path
btw. Other bundlers, like Browserify
or Rollup, should support such replacement. You can use this approach to keep import P from "path"
or import P from pathz/lib/browser
instead. There's no support for Win-32 separators in Browser a.t.m.
Motivation
format
/ parse
provide limited, low-level, mutable API
1. Original pathz
respects trailing separators as possible directory indicator
2. console // "/foo"console // "/foo/bar/" console // "bar"console // ""
pathz
respects "relativeness" and "absoluteness" of paths
3. console // "/bar/foo.txt" (+)console // "bar/foo.txt" (-) naive console // "/bar/foo.txt" (+)console // "/foo.txt/bar" (-) naive
pathz
is composition friendly
4. let R = let src = "content/team/about.md"let dst = Rsrc console // "public/team/about/index.html" // corresponding to "/team/about/" URL
pathz
is like CRUD for path fragments
5. // GETconsole // "foo"console // "bar" // UPDATEconsole // "/qux/bar/baz.txt"console // "/foo/qux/baz.txt" // DELETEconsole // "/bar/baz.txt"console // "/foo/baz.txt" // ...
pathz
provides extra utils
6. R "foo/bar/11.1/2.1" "foo/bar/2.1/10.1" "foo/bar/10.1/2.1" "foo/bar/2.1/2.1" // [ 'foo/bar/2.1/2.1',// 'foo/bar/2.1/10.1',// 'foo/bar/10.1/2.1',// 'foo/bar/11.1/2.1' ]
API
dir :: String -> String
Pdir"foo/bar/baz.txt" // "foo/bar/" (P.dirname + "/")Pdir"foo/bar/" // "foo/bar/" (identity)Pdir"/foo/bar/baz.txt" // "/foo/bar/" (P.dirname + "/")Pdir"/foo/bar/" // "/foo/bar/" (identity)
splitDirs :: String -> [String]
P // ["foo", "bar"]P // ["foo", "bar"]
base :: String -> String
P // "baz.txt" (P.basename)P // "" (zero)
name :: String -> String
Pname"whatever/index.html" // "index"Pname"whatever/.gitignore" // ".gitignore"
ext :: String -> String
P // ".html"P // ""
leftDir :: String -> String
P // "foo"P // ""P // "foo"
rightDir :: String -> String
P // "bar"P // ""P // "foo"
leftDirs :: Number -> String -> String
P == PleftDirP // "foo/bar"P // "foo/bar/baz"P // "foo/bar/baz"
rightDirs :: Number -> String -> String
P == PrightDirP // "bar/baz"P // "foo/bar/baz"P // "foo/bar/baz"
addLeftDir :: String -> String -> String
P // "foo/bar/index.html"P // "foo/"
addRightDir :: String -> String -> String
P // "foo/bar/index.html"P // "foo/"
withLeftDir :: String -> String -> String
P // "qux/bar/index.html"P // "qux/index.html"P // "qux/"
withRightDir :: String -> String -> String
P // "foo/qux/index.html"P // "qux/index.html"P // "qux/"
dropLeftDir :: String -> String
P // "foo/bar/index.html"P // "bar/"
dropRightDir :: String -> String
P // "foo/index.html"P // "index.html"
withDir :: String -> String -> String
P // "bar/index.html"P // "bar/"
withBase :: String -> String -> String
P // "foo/index"P // "index"
withName :: String -> String
P // "foo/index.html"P // "index"
withExt :: String -> String
P // "foo/index.html"P // ".html"
dropBase :: String -> String
P // "foo/bar/"P // ""
dropExt :: String -> String
P // "foo/bar/index"P // ".gitignore"
padNumeric :: Number -> String
P // "x"P // "0001"
padName :: Number -> String
P // "01.01.foo.js"
padPath :: Number -> String
P // "01.folder/file.01.md"
Original API
delimiter
: low-level, import directlyposix
: low-level, import directlysep
: low-level, import directlywin32
: low-level, import directlybasename
: wrapped, useP.base
insteaddirname
: wrapped, useP.dir
insteadextname
: wrapped, useP.ext
insteadformat
: wrapped, useP
helpers insteadparse
: wrapped, useP
helpers insteadisAbsolute
: reexportedjoin
: reexportednormalize
: reexportedrelative
: reexportedresolve
: reexported