A toolkit for path
npm i path-copilot
path-copilot - v1.0.0-Beta
▸ isAbsolute(path
): boolean
Determine whether the given path string is an absolute path.
Since
1.0.0
Example
isAbsolute('/') // true
isAbsolute('/path') // true
isAbsolute('C:\\path') // true
isAbsolute('\\path') // true
isAbsolute('https://example.com') // true
isAbsolute('ftp://example.com') // true
isAbsolute('relative/path') // false
Name | Type | Description |
---|---|---|
path |
string |
The path string to be determined. |
boolean
True if it's an absolute path, false otherwise.
▸ join(...paths
): string
Combines an array of path segments into a single path string, with forward slashes (/) as separators.
Since
1.0.0
Example
const path = join('foo', 'bar', 'baz'); // path == 'foo/bar/baz'
const path2 = join('foo/', '/bar/', '/baz/'); // path2 == 'foo/bar/baz'
Name | Type | Description |
---|---|---|
...paths |
string [] |
An array of path segments to join. |
string
A combined path string.
▸ normalize(path
): string
Normalize a file system path by removing any unnecessary "." and ".." segments and resolving any directory separators to match the host operating system.
Example
const path = '/users/john/../jane/./documents/';
const normalizedPath = normalize(path); // "/users/jane/documents/"
Since
1.0.0
Name | Type | Description |
---|---|---|
path |
string |
The file system path to be normalized. |
string
The normalized path.
▸ parseUrl(url
): Object
Parses the query and hash parameters of the specified URL string.
Example
// Basic usage
const url = 'http://example.com/path/to/page?a=1&b=2#section1';
const { query, hash } = parseUrl(url);
console.log(query); // { a: '1', b: '2' }
console.log(hash); // { section1: '' }
// Query and hash without value
const url2 = 'http://example.com/path/to/page?a&b#c';
const { query: query2, hash: hash2 } = parseUrl(url2);
console.log(query2); // { a: '', b: '' }
console.log(hash2); // { c: '' }
// Query with array value
const url3 = 'http://example.com/path/to/page?color=red&color=green&color=blue';
const { query: query3, hash: hash3 } = parseUrl(url3);
console.log(query3); // { color: ['red', 'green', 'blue'] }
console.log(hash3); // {}
Since
1.0.0
Name | Type | Description |
---|---|---|
url |
string |
The URL string to parse. |
Object
An object containing the query and hash parameters of the URL.
Name | Type |
---|---|
hash |
Record <string , string | string []> |
query |
Record <string , string | string []> |