UNPKG

4.13 kBJavaScriptView Raw
1export var Branch;
2(function (Branch) {
3 Branch["Public"] = "public";
4 Branch["Pretty"] = "p";
5 Branch["Private"] = "private";
6 Branch["PrivateLog"] = "privateLog";
7 Branch["Version"] = "version";
8})(Branch || (Branch = {}));
9export var Kind;
10(function (Kind) {
11 Kind["Directory"] = "directory";
12 Kind["File"] = "file";
13})(Kind || (Kind = {}));
14// CREATION
15/**
16 * Utility function to create a `DirectoryPath`
17 */
18export function directory(...args) {
19 if (args.some(p => p.includes("/"))) {
20 throw new Error("Forward slashes `/` are not allowed");
21 }
22 return { directory: args };
23}
24/**
25 * Utility function to create a `FilePath`
26 */
27export function file(...args) {
28 if (args.some(p => p.includes("/"))) {
29 throw new Error("Forward slashes `/` are not allowed");
30 }
31 return { file: args };
32}
33/**
34 * Utility function to create a root `DirectoryPath`
35 */
36export function root() {
37 return { directory: [] };
38}
39// POSIX
40/**
41 * Transform a string into a `DistinctivePath`.
42 *
43 * Directories should have the format `path/to/dir/` and
44 * files should have the format `path/to/file`.
45 *
46 * Leading forward slashes are removed too, so you can pass absolute paths.
47 */
48export function fromPosix(path) {
49 const split = path.replace(/^\/+/, "").split("/");
50 if (path.endsWith("/"))
51 return { directory: split.slice(0, -1) };
52 else if (path === "")
53 return root();
54 return { file: split };
55}
56/**
57 * Transform a `DistinctivePath` into a string.
58 *
59 * Directories will have the format `path/to/dir/` and
60 * files will have the format `path/to/file`.
61 */
62export function toPosix(path, { absolute } = { absolute: false }) {
63 const prefix = absolute ? "/" : "";
64 const joinedPath = unwrap(path).join("/");
65 if (isDirectory(path))
66 return prefix + joinedPath + (joinedPath.length ? "/" : "");
67 return prefix + joinedPath;
68}
69// 🛠
70/**
71 * Combine two `DistinctivePath`s.
72 */
73export function combine(a, b) {
74 return map(p => unwrap(a).concat(p), b);
75}
76/**
77 * Is this `DistinctivePath` of the given `Branch`?
78 */
79export function isBranch(branch, path) {
80 return unwrap(path)[0] === branch;
81}
82/**
83 * Is this `DistinctivePath` a directory?
84 */
85export function isDirectory(path) {
86 return !!path.directory;
87}
88/**
89 * Is this `DistinctivePath` a file?
90 */
91export function isFile(path) {
92 return !!path.file;
93}
94/**
95 * Is this `DirectoryPath` a root directory?
96 */
97export function isRootDirectory(path) {
98 return path.directory.length === 0;
99}
100/**
101 * Check if two `DistinctivePath` have the same `Branch`.
102 */
103export function isSameBranch(a, b) {
104 return unwrap(a)[0] === unwrap(b)[0];
105}
106/**
107 * Check if two `DistinctivePath` are of the same kind.
108 */
109export function isSameKind(a, b) {
110 if (isDirectory(a) && isDirectory(b))
111 return true;
112 else if (isFile(a) && isFile(b))
113 return true;
114 else
115 return false;
116}
117/**
118 * What `Kind` of path are we dealing with?
119 */
120export function kind(path) {
121 if (isDirectory(path))
122 return Kind.Directory;
123 return Kind.File;
124}
125/**
126 * Map a `DistinctivePath`.
127 */
128export function map(fn, path) {
129 if (isDirectory(path))
130 return { directory: fn(path.directory) };
131 else if (isFile(path))
132 return { file: fn(path.file) };
133 return path;
134}
135/**
136 * Get the parent directory of a `DistinctivePath`.
137 */
138export function parent(path) {
139 return isDirectory(path) && isRootDirectory(path)
140 ? null
141 : directory(...unwrap(path).slice(0, -1));
142}
143/**
144 * Remove the `Branch` of a `DistinctivePath` (ie. the top-level directory)
145 */
146export function removeBranch(path) {
147 return map(p => isDirectory(path) || p.length > 1 ? p.slice(1) : p, path);
148}
149/**
150 * Unwrap a `DistinctivePath`.
151 */
152export function unwrap(path) {
153 if (isDirectory(path)) {
154 return path.directory;
155 }
156 else if (isFile(path)) {
157 return path.file;
158 }
159 return [];
160}
161// ⚗️
162/**
163 * Render a raw `Path` to a string for logging purposes.
164 */
165export function log(path) {
166 return `[ ${path.join(", ")} ]`;
167}
168//# sourceMappingURL=path.js.map
\No newline at end of file