kslash is a collection of path utilities.
I use it as a replacement for node's path
module.
It aims to deal with file paths in a platform independent way.
Maybe it contains some tools of interest to you, even if you target only one platform.
Applies unenv
and untilde
before converting path into an absolute one.
Joins all arguments before doing so.
This is the function I use the most and it usually resolves all my problems
Normalizes the path and converts backslashes to slashes.
slash = require 'kslash'
slash.path 'C:\\Back\\Slash' ▸ C:/Back/Slash
slash.path 'C:\\Back\\Slash\\..\\To\\The\\..\\Future' ▸ C:/Back/To/Future
Windows is capable of handling paths with forward slashes, that's why all exported functions return 'slashed' paths -- except the next one -- which you can use in cases where it isn't :)
On Windows it converts
- slashes to backslashes
- first dirname to a drive if it has only one letter
slash.unslash '/c/test' ▸ C:\\test
slash.unslash 'D:/c/test' ▸ D:\\c\\test
p = '/dir/file.txt'
slash.dir(p) ▸ /dir
p = '/dir/file.txt'
slash.file(p) ▸ file.txt
p = '/dir/file.txt'
slash.base(p) ▸ file
p = '/dir/file.txt'
slash.ext(p) ▸ txt
p = '/dir/file.txt'
slash.removeExt(p) ▸ /dir/file
p = '/dir/file.txt'
slash.swapExt(p, 'md') ▸ /dir/file.md
slash.swapExt(p, '.md') ▸ /dir/file.md
slash.isRoot('C:\\') ▸ true
slash.isRoot('/') ▸ true
p = 'C:\\dir\\file.txt'
slash.removeDrive(p) ▸ /dir/file.txt
slash.home() ▸ C:/Users/kodi
p = 'C:/Users/kodi/file.txt'
slash.tilde(p) ▸ ~/file.txt
p = '~/file.txt'
slash.untilde(p) ▸ C:/Users/kodi/file.txt
Replaces $...
with matching environment variables
p = 'C:\\dir/file.txt'
slash.split(p) ▸ ['C:', 'dir', 'file']
p = 'C:\\dir/file.txt'
slash.splitExt(p) ▸ ['C:/dir/file', 'txt']
p = 'C:\\dir/file.txt'
slash.splitDrive(p) ▸ ['/dir/file.txt', 'c']
p = '/dir/file.txt:12:3'
slash.splitFileLine(p) ▸ ['/dir/file.txt', 12, 3]
p = '/dir/file.txt:12'
slash.splitFilePos(p) ▸ ['/dir/file.txt', [0,11]]
p = '/dir/file.txt:12:3'
slash.removeLinePos(p) ▸ /dir/file.txt
p = '/dir/file.txt:12:3'
slash.removeColumn(p) ▸ /dir/file.txt:12
p = '/dir/file.txt:12'
slash.joinFilePos(p, [2, 1]) ▸ /dir/file.txt:2:2
p = '/dir/file.txt'
slash.joinFileLine(p, 1, 2) ▸ /dir/file.txt:1:2
p = '/dir/file.txt'
slash.pathlist(p) ▸ ['/', '/dir', '/dir/file.txt']
p = '$HOME/dir'
slash.unenv(p) ▸ C:/Users/kodi/dir
p = 'C:/test/some/path.txt'
to ='C:/test/some/other/path'
slash.relative(p,to) ▸ ../../path.txt
Resolves
p and q and compares the results.
Encodes p for use as an URL.
p = '/dir/a # b'
slash.encode(p) ▸ /dir/a%20%23%20b
Encodes p and prefixes it with 'file://'
p = '/dir/a # b'
slash.fileUrl(p) ▸ file:///dir/a%20%23%20b
Returns a joined path of os.tmpdir and an uuid
Searches backwards in pathlist
of p for a package.json and returns the containing folder, if one is found.
Same as pkg
, just looking for .git
directory instead.
Like the unix command, creates intermediate directories if they don't exist.
Calls back with a list of info objects for items in directory p.
A small wrapper around the walkdir
package.
Returns stat of path p if it exists, null otherwise.
The callback is optional. If provided, the test is executed asynchronously and the callback will be called with the result
slash.exists p, (stat) -> if stat then # ...
The same is true for the following functions that have a callback argument:
Returns stat of path p if it is a directory, null otherwise.
Returns stat of path p if it is a file, null otherwise.
Returns true if p is writable.
Returns true if p is a textfile.
Returns content of p as an utf8 string. Returns an empty string, if p doesn't exist or isn't readable.
Returns p if p doesn't exist. Otherwise, returns a path with a number attached such that the path doesn't exist.
calls cb each time p changes. if p is a directory, cb will be called for each file change in that directory.
removes watches created with watch.
Removes leading and trailing newlines from path p.
Returns true if path.sep is '\'.
Same as the functions of the path
module but p is sanitized
and slashed
first.
All functions return an empty string or null if the provided path is an empty string, null or undefined. The same is true, if an underlying function call throws an error.
If this is too lax for your taste, or you want to debug your code, you can redefine the function slash.error
:
slash.error = (msg) -> # throw or log or something else ...