summaryrefslogtreecommitdiff
path: root/lib/epub.js/src/utils/path.js
blob: 6a060cb146438f69df053f8084cd4e74043bbaec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import path from "path-webpack";

/**
 * Creates a Path object for parsing and manipulation of a path strings
 *
 * Uses a polyfill for Nodejs path: https://nodejs.org/api/path.html
 * @param	{string} pathString	a url string (relative or absolute)
 * @class
 */
class Path {
	constructor(pathString) {
		var protocol;
		var parsed;

		protocol = pathString.indexOf("://");
		if (protocol > -1) {
			pathString = new URL(pathString).pathname;
		}

		parsed = this.parse(pathString);

		this.path = pathString;

		if (this.isDirectory(pathString)) {
			this.directory = pathString;
		} else {
			this.directory = parsed.dir + "/";
		}

		this.filename = parsed.base;
		this.extension = parsed.ext.slice(1);

	}

	/**
	 * Parse the path: https://nodejs.org/api/path.html#path_path_parse_path
	 * @param	{string} what
	 * @returns {object}
	 */
	parse (what) {
		return path.parse(what);
	}

	/**
	 * @param	{string} what
	 * @returns {boolean}
	 */
	isAbsolute (what) {
		return path.isAbsolute(what || this.path);
	}

	/**
	 * Check if path ends with a directory
	 * @param	{string} what
	 * @returns {boolean}
	 */
	isDirectory (what) {
		return (what.charAt(what.length-1) === "/");
	}

	/**
	 * Resolve a path against the directory of the Path
	 *
	 * https://nodejs.org/api/path.html#path_path_resolve_paths
	 * @param	{string} what
	 * @returns {string} resolved
	 */
	resolve (what) {
		return path.resolve(this.directory, what);
	}

	/**
	 * Resolve a path relative to the directory of the Path
	 *
	 * https://nodejs.org/api/path.html#path_path_relative_from_to
	 * @param	{string} what
	 * @returns {string} relative
	 */
	relative (what) {
		var isAbsolute = what && (what.indexOf("://") > -1);

		if (isAbsolute) {
			return what;
		}

		return path.relative(this.directory, what);
	}

	splitPath(filename) {
		return this.splitPathRe.exec(filename).slice(1);
	}

	/**
	 * Return the path string
	 * @returns {string} path
	 */
	toString () {
		return this.path;
	}
}

export default Path;