Debian Codegraph @

closure-library/closure/goog/string/path.js

raw
1 // Copyright 2010 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /**
16 * @fileoverview Utilities for dealing with POSIX path strings. Based on
17 * Python's os.path and posixpath.
18 * @author [email protected] (Nathan Naze)
19 */
20
21 goog.provide('goog.string.path');
22
23 goog.require('goog.array');
24 goog.require('goog.string');
25
26
27 /**
28 * Returns the final component of a pathname.
29 * See http://docs.python.org/library/os.path.html#os.path.basename
30 * @param {string} path A pathname.
31 * @return {string} path The final component of a pathname, i.e. everything
32 * after the final slash.
33 */
34 goog.string.path.baseName = function(path) {
35 var i = path.lastIndexOf('/') + 1;
36 return path.slice(i);
37 };
38
39
40 /**
41 * Alias to goog.string.path.baseName.
42 * @param {string} path A pathname.
43 * @return {string} path The final component of a pathname.
44 * @deprecated Use goog.string.path.baseName.
45 */
46 goog.string.path.basename = goog.string.path.baseName;
47
48
49 /**
50 * Returns the directory component of a pathname.
51 * See http://docs.python.org/library/os.path.html#os.path.dirname
52 * @param {string} path A pathname.
53 * @return {string} The directory component of a pathname, i.e. everything
54 * leading up to the final slash.
55 */
56 goog.string.path.dirname = function(path) {
57 var i = path.lastIndexOf('/') + 1;
58 var head = path.slice(0, i);
59 // If the path isn't all forward slashes, trim the trailing slashes.
60 if (!/^\/+$/.test(head)) {
61 head = head.replace(/\/+$/, '');
62 }
63 return head;
64 };
65
66
67 /**
68 * Extracts the extension part of a pathname.
69 * @param {string} path The path name to process.
70 * @return {string} The extension if any, otherwise the empty string.
71 */
72 goog.string.path.extension = function(path) {
73 var separator = '.';
74 // Combining all adjacent periods in the basename to a single period.
75 var baseName = goog.string.path.baseName(path).replace(/\.+/g, separator);
76 var separatorIndex = baseName.lastIndexOf(separator);
77 return separatorIndex <= 0 ? '' : baseName.substr(separatorIndex + 1);
78 };
79
80
81 /**
82 * Joins one or more path components (e.g. 'foo/' and 'bar' make 'foo/bar').
83 * An absolute component will discard all previous component.
84 * See http://docs.python.org/library/os.path.html#os.path.join
85 * @param {...string} var_args One of more path components.
86 * @return {string} The path components joined.
87 */
88 goog.string.path.join = function(var_args) {
89 var path = arguments[0];
90
91 for (var i = 1; i < arguments.length; i++) {
92 var arg = arguments[i];
93 if (goog.string.startsWith(arg, '/')) {
94 path = arg;
95 } else if (path == '' || goog.string.endsWith(path, '/')) {
96 path += arg;
97 } else {
98 path += '/' + arg;
99 }
100 }
101
102 return path;
103 };
104
105
106 /**
107 * Normalizes a pathname by collapsing duplicate separators, parent directory
108 * references ('..'), and current directory references ('.').
109 * See http://docs.python.org/library/os.path.html#os.path.normpath
110 * @param {string} path One or more path components.
111 * @return {string} The path after normalization.
112 */
113 goog.string.path.normalizePath = function(path) {
114 if (path == '') {
115 return '.';
116 }
117
118 var initialSlashes = '';
119 // POSIX will keep two slashes, but three or more will be collapsed to one.
120 if (goog.string.startsWith(path, '/')) {
121 initialSlashes = '/';
122 if (goog.string.startsWith(path, '//') &&
123 !goog.string.startsWith(path, '///')) {
124 initialSlashes = '//';
125 }
126 }
127
128 var parts = path.split('/');
129 var newParts = [];
130
131 for (var i = 0; i < parts.length; i++) {
132 var part = parts[i];
133
134 // '' and '.' don't change the directory, ignore.
135 if (part == '' || part == '.') {
136 continue;
137 }
138
139 // A '..' should pop a directory unless this is not an absolute path and
140 // we're at the root, or we've travelled upwards relatively in the last
141 // iteration.
142 if (part != '..' || (!initialSlashes && !newParts.length) ||
143 goog.array.peek(newParts) == '..') {
144 newParts.push(part);
145 } else {
146 newParts.pop();
147 }
148 }
149
150 var returnPath = initialSlashes + newParts.join('/');
151 return returnPath || '.';
152 };
153
154
155 /**
156 * Splits a pathname into "dirname" and "baseName" components, where "baseName"
157 * is everything after the final slash. Either part may return an empty string.
158 * See http://docs.python.org/library/os.path.html#os.path.split
159 * @param {string} path A pathname.
160 * @return {!Array<string>} An array of [dirname, basename].
161 */
162 goog.string.path.split = function(path) {
163 var head = goog.string.path.dirname(path);
164 var tail = goog.string.path.baseName(path);
165 return [head, tail];
166 };
167
168 // TODO(nnaze): Implement other useful functions from os.path