From 084172577e492a673652c1c1d5eebbce04f980e1 Mon Sep 17 00:00:00 2001 From: Moshe Atlow Date: Sun, 21 Aug 2022 01:13:56 +0300 Subject: [PATCH 1/2] path: add `fromURL` --- doc/api/path.md | 33 +++++++++++++++++++++++++++++ lib/path.js | 13 ++++++++++++ test/parallel/test-path-from-url.js | 29 +++++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 test/parallel/test-path-from-url.js diff --git a/doc/api/path.md b/doc/api/path.md index 0bbb5b80c57ef3..dc2801e7cc3a88 100644 --- a/doc/api/path.md +++ b/doc/api/path.md @@ -267,6 +267,38 @@ path.format({ // Returns: 'C:\\path\\dir\\file.txt' ``` +## `path.fromURL(pathOrUrl)` + + + +* `pathOrUrl` {string|URL}. +* Returns: {string} + +Converts a [`URL`][] instance to a path string, +returns `pathOrUrl` if it is already a string. + +A [`TypeError`][] is thrown if `pathOrUrl` is a +[`URL`][] with a schema other than `file://`. + +```js +path.fromURL(new URL('file:///Users/node/dev')); +// Returns: '/Users/node/dev' + +path.fromURL('file:///Users/node/dev'); +// Returns: 'file:///Users/node/dev' + +path.fromURL(new URL('file:///c:/foo.txt')); +// Returns On Windows: 'c:\\foo.txt' + +path.fromURL('index/path'); +// Returns: 'index/path' + +path.fromURL(new URL('http://example.com')); +// Throws 'TypeError [ERR_INVALID_URL_SCHEME]: The URL must be of scheme file' +``` + ## `path.isAbsolute(path)`