UNPKG

5.52 kBJavaScriptView Raw
1"use strict";
2// Copyright (c) Jupyter Development Team.
3// Distributed under the terms of the Modified BSD License.
4Object.defineProperty(exports, "__esModule", { value: true });
5exports.PathExt = void 0;
6const path_1 = require("path");
7/**
8 * The namespace for path-related functions.
9 *
10 * Note that Jupyter server paths do not start with a leading slash.
11 */
12var PathExt;
13(function (PathExt) {
14 /**
15 * Join all arguments together and normalize the resulting path.
16 * Arguments must be strings. In v0.8, non-string arguments were silently ignored. In v0.10 and up, an exception is thrown.
17 *
18 * @param paths - The string paths to join.
19 */
20 function join(...paths) {
21 const path = path_1.posix.join(...paths);
22 return path === '.' ? '' : removeSlash(path);
23 }
24 PathExt.join = join;
25 /**
26 * Join all arguments together and normalize the resulting path and preserve the leading slash.
27 *
28 * @param paths - The string paths to join.
29 */
30 function joinWithLeadingSlash(...paths) {
31 const path = path_1.posix.join(...paths);
32 return path === '.' ? '' : path;
33 }
34 PathExt.joinWithLeadingSlash = joinWithLeadingSlash;
35 /**
36 * Return the last portion of a path. Similar to the Unix basename command.
37 * Often used to extract the file name from a fully qualified path.
38 *
39 * @param path - The path to evaluate.
40 *
41 * @param ext - An extension to remove from the result.
42 */
43 function basename(path, ext) {
44 return path_1.posix.basename(path, ext);
45 }
46 PathExt.basename = basename;
47 /**
48 * Get the directory name of a path, similar to the Unix dirname command.
49 * When an empty path is given, returns the root path.
50 *
51 * @param path - The file path.
52 */
53 function dirname(path) {
54 const dir = removeSlash(path_1.posix.dirname(path));
55 return dir === '.' ? '' : dir;
56 }
57 PathExt.dirname = dirname;
58 /**
59 * Get the extension of the path.
60 *
61 * @param path - The file path.
62 *
63 * @returns the extension of the file.
64 *
65 * #### Notes
66 * The extension is the string from the last occurrence of the `.`
67 * character to end of string in the last portion of the path, inclusive.
68 * If there is no `.` in the last portion of the path, or if the first
69 * character of the basename of path [[basename]] is `.`, then an
70 * empty string is returned.
71 */
72 function extname(path) {
73 return path_1.posix.extname(path);
74 }
75 PathExt.extname = extname;
76 /**
77 * Normalize a string path, reducing '..' and '.' parts.
78 * When multiple slashes are found, they're replaced by a single one; when the path contains a trailing slash, it is preserved. On Windows backslashes are used.
79 * When an empty path is given, returns the root path.
80 *
81 * @param path - The string path to normalize.
82 */
83 function normalize(path) {
84 if (path === '') {
85 return '';
86 }
87 return removeSlash(path_1.posix.normalize(path));
88 }
89 PathExt.normalize = normalize;
90 /**
91 * Resolve a sequence of paths or path segments into an absolute path.
92 * The root path in the application has no leading slash, so it is removed.
93 *
94 * @param parts - The paths to join.
95 *
96 * #### Notes
97 * The right-most parameter is considered \{to\}. Other parameters are considered an array of \{from\}.
98 *
99 * Starting from leftmost \{from\} parameter, resolves \{to\} to an absolute path.
100 *
101 * If \{to\} isn't already absolute, \{from\} arguments are prepended in right to left order, until an absolute path is found. If after using all \{from\} paths still no absolute path is found, the current working directory is used as well. The resulting path is normalized, and trailing slashes are removed unless the path gets resolved to the root directory.
102 */
103 function resolve(...parts) {
104 return removeSlash(path_1.posix.resolve(...parts));
105 }
106 PathExt.resolve = resolve;
107 /**
108 * Solve the relative path from \{from\} to \{to\}.
109 *
110 * @param from - The source path.
111 *
112 * @param to - The target path.
113 *
114 * #### Notes
115 * If from and to each resolve to the same path (after calling
116 * path.resolve() on each), a zero-length string is returned.
117 * If a zero-length string is passed as from or to, `/`
118 * will be used instead of the zero-length strings.
119 */
120 function relative(from, to) {
121 return removeSlash(path_1.posix.relative(from, to));
122 }
123 PathExt.relative = relative;
124 /**
125 * Normalize a file extension to be of the type `'.foo'`.
126 *
127 * @param extension - the file extension.
128 *
129 * #### Notes
130 * Adds a leading dot if not present and converts to lower case.
131 */
132 function normalizeExtension(extension) {
133 if (extension.length > 0 && extension.indexOf('.') !== 0) {
134 extension = `.${extension}`;
135 }
136 return extension;
137 }
138 PathExt.normalizeExtension = normalizeExtension;
139 /**
140 * Remove the leading slash from a path.
141 *
142 * @param path the path from which to remove a leading slash.
143 */
144 function removeSlash(path) {
145 if (path.indexOf('/') === 0) {
146 path = path.slice(1);
147 }
148 return path;
149 }
150 PathExt.removeSlash = removeSlash;
151})(PathExt || (exports.PathExt = PathExt = {}));
152//# sourceMappingURL=path.js.map
\No newline at end of file