OLD | NEW |
1 A comprehensive, cross-platform path manipulation library for Dart. | 1 A comprehensive, cross-platform path manipulation library for Dart. |
2 | 2 |
3 The pathos library provides common operations for manipulating file paths: | 3 The pathos library provides common operations for manipulating file paths: |
4 joining, splitting, normalizing, etc. | 4 joining, splitting, normalizing, etc. |
5 | 5 |
6 We've tried very hard to make this library do the "right" thing on whatever | 6 We've tried very hard to make this library do the "right" thing on whatever |
7 platform you run it on. When you use the top-level functions, it will assume | 7 platform you run it on. When you use the top-level functions, it will assume the |
8 the host OS's path style and work with that. If you want to specifically work | 8 current platform's path style and work with that. If you want to specifically |
9 with paths of a specific style, you can construct a `path.Builder` for that | 9 work with paths of a specific style, you can construct a `path.Builder` for that |
10 style. | 10 style. |
11 | 11 |
12 ## Using | 12 ## Using |
13 | 13 |
14 The path library was designed to be imported with a prefix, though you don't | 14 The path library was designed to be imported with a prefix, though you don't |
15 have to if you don't want to: | 15 have to if you don't want to: |
16 | 16 |
17 import 'package:pathos/path.dart' as path; | 17 import 'package:pathos/path.dart' as path; |
18 | 18 |
19 ## Top-level functions | 19 ## Top-level functions |
20 | 20 |
21 The most common way to use the library is through the top-level functions. | 21 The most common way to use the library is through the top-level functions. |
22 These manipulate path strings based on your current working directory and the | 22 These manipulate path strings based on your current working directory and the |
23 path style (POSIX or Windows) of the host operating system. | 23 path style (POSIX, Windows, or URLs) of the host platform. |
24 | 24 |
25 ### String get current | 25 ### String get current |
26 | 26 |
27 Gets the path to the current working directory. | 27 Gets the path to the current working directory. In the browser, this means the |
| 28 current URL. When using dart2js, this currently returns `.` due to technical |
| 29 constraints. In the future, it will return the current URL. |
28 | 30 |
29 ### String get separator | 31 ### String get separator |
30 | 32 |
31 Gets the path separator for the current platform. On Mac and Linux, this | 33 Gets the path separator for the current platform. On Mac, Linux, and the |
32 is `/`. On Windows, it's `\`. | 34 browser, this is `/`. On Windows, it's `\`. |
33 | 35 |
34 ### String absolute(String path) | 36 ### String absolute(String path) |
35 | 37 |
36 Converts [path] to an absolute path by resolving it relative to the current | 38 Converts [path] to an absolute path by resolving it relative to the current |
37 working directory. If [path] is already an absolute path, just returns it. | 39 working directory. If [path] is already an absolute path, just returns it. |
38 | 40 |
39 path.absolute('foo/bar.txt'); // -> /your/current/dir/foo/bar.txt | 41 path.absolute('foo/bar.txt'); // -> /your/current/dir/foo/bar.txt |
40 | 42 |
41 ### String basename(String path) | 43 ### String basename(String path) |
42 | 44 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 relative. | 95 relative. |
94 | 96 |
95 // Unix | 97 // Unix |
96 path.rootPrefix('path/to/foo'); // -> '' | 98 path.rootPrefix('path/to/foo'); // -> '' |
97 path.rootPrefix('/path/to/foo'); // -> '/' | 99 path.rootPrefix('/path/to/foo'); // -> '/' |
98 | 100 |
99 // Windows | 101 // Windows |
100 path.rootPrefix(r'path\to\foo'); // -> '' | 102 path.rootPrefix(r'path\to\foo'); // -> '' |
101 path.rootPrefix(r'C:\path\to\foo'); // -> r'C:\' | 103 path.rootPrefix(r'C:\path\to\foo'); // -> r'C:\' |
102 | 104 |
| 105 // URL |
| 106 path.rootPrefix('path/to/foo'); // -> '' |
| 107 path.rootPrefix('http://dartlang.org/path/to/foo'); |
| 108 // -> 'http://dartlang.org' |
| 109 |
103 ### bool isAbsolute(String path) | 110 ### bool isAbsolute(String path) |
104 | 111 |
105 Returns `true` if [path] is an absolute path and `false` if it is a | 112 Returns `true` if [path] is an absolute path and `false` if it is a relative |
106 relative path. On POSIX systems, absolute paths start with a `/` (forward | 113 path. On POSIX systems, absolute paths start with a `/` (forward slash). On |
107 slash). On Windows, an absolute path starts with `\\`, or a drive letter | 114 Windows, an absolute path starts with `\\`, or a drive letter followed by `:/` |
108 followed by `:/` or `:\`. | 115 or `:\`. For URLs, absolute paths either start with a protocol and optional |
| 116 hostname (e.g. `http://dartlang.org`, `file://`) or with a `/`. |
| 117 |
| 118 URLs that start with `/` are known as "root-relative", since they're relative to |
| 119 the root of the current URL. Since root-relative paths are still absolute in |
| 120 every other sense, [isAbsolute] will return true for them. They can be detected |
| 121 using [isRootRelative]. |
109 | 122 |
110 ### bool isRelative(String path) | 123 ### bool isRelative(String path) |
111 | 124 |
112 Returns `true` if [path] is a relative path and `false` if it is absolute. | 125 Returns `true` if [path] is a relative path and `false` if it is absolute. |
113 On POSIX systems, absolute paths start with a `/` (forward slash). On | 126 On POSIX systems, absolute paths start with a `/` (forward slash). On |
114 Windows, an absolute path starts with `\\`, or a drive letter followed by | 127 Windows, an absolute path starts with `\\`, or a drive letter followed by |
115 `:/` or `:\`. | 128 `:/` or `:\`. |
116 | 129 |
| 130 ### bool isRootRelative(String path) |
| 131 |
| 132 Returns `true` if [path] is a root-relative path and `false` if it's not. URLs |
| 133 that start with `/` are known as "root-relative", since they're relative to the |
| 134 root of the current URL. Since root-relative paths are still absolute in every |
| 135 other sense, [isAbsolute] will return true for them. They can be detected using |
| 136 [isRootRelative]. |
| 137 |
| 138 No POSIX and Windows paths are root-relative. |
| 139 |
117 ### String join(String part1, [String part2, String part3, ...]) | 140 ### String join(String part1, [String part2, String part3, ...]) |
118 | 141 |
119 Joins the given path parts into a single path using the current platform's | 142 Joins the given path parts into a single path using the current platform's |
120 [separator]. Example: | 143 [separator]. Example: |
121 | 144 |
122 path.join('path', 'to', 'foo'); // -> 'path/to/foo' | 145 path.join('path', 'to', 'foo'); // -> 'path/to/foo' |
123 | 146 |
124 If any part ends in a path separator, then a redundant separator will not | 147 If any part ends in a path separator, then a redundant separator will not |
125 be added: | 148 be added: |
126 | 149 |
(...skipping 15 matching lines...) Expand all Loading... |
142 | 165 |
143 If [path] is absolute, the root directory will be the first element in the | 166 If [path] is absolute, the root directory will be the first element in the |
144 array. Example: | 167 array. Example: |
145 | 168 |
146 // Unix | 169 // Unix |
147 path.split('/path/to/foo'); // -> ['/', 'path', 'to', 'foo'] | 170 path.split('/path/to/foo'); // -> ['/', 'path', 'to', 'foo'] |
148 | 171 |
149 // Windows | 172 // Windows |
150 path.split(r'C:\path\to\foo'); // -> [r'C:\', 'path', 'to', 'foo'] | 173 path.split(r'C:\path\to\foo'); // -> [r'C:\', 'path', 'to', 'foo'] |
151 | 174 |
| 175 // Browser |
| 176 path.split('http://dartlang.org/path/to/foo'); |
| 177 // -> ['http://dartlang.org', 'path', 'to', 'foo'] |
| 178 |
152 ### String normalize(String path) | 179 ### String normalize(String path) |
153 | 180 |
154 Normalizes [path], simplifying it by handling `..`, and `.`, and | 181 Normalizes [path], simplifying it by handling `..`, and `.`, and |
155 removing redundant path separators whenever possible. | 182 removing redundant path separators whenever possible. |
156 | 183 |
157 path.normalize('path/./to/..//file.text'); // -> 'path/file.txt' | 184 path.normalize('path/./to/..//file.text'); // -> 'path/file.txt' |
158 String normalize(String path) => _builder.normalize(path); | 185 String normalize(String path) => _builder.normalize(path); |
159 | 186 |
160 ### String relative(String path, {String from}) | 187 ### String relative(String path, {String from}) |
161 | 188 |
162 Attempts to convert [path] to an equivalent relative path from the current | 189 Attempts to convert [path] to an equivalent relative path from the current |
163 directory. | 190 directory. |
164 | 191 |
165 // Given current directory is /root/path: | 192 // Given current directory is /root/path: |
166 path.relative('/root/path/a/b.dart'); // -> 'a/b.dart' | 193 path.relative('/root/path/a/b.dart'); // -> 'a/b.dart' |
167 path.relative('/root/other.dart'); // -> '../other.dart' | 194 path.relative('/root/other.dart'); // -> '../other.dart' |
168 | 195 |
169 If the [from] argument is passed, [path] is made relative to that instead. | 196 If the [from] argument is passed, [path] is made relative to that instead. |
170 | 197 |
171 path.relative('/root/path/a/b.dart', | 198 path.relative('/root/path/a/b.dart', |
172 from: '/root/path'); // -> 'a/b.dart' | 199 from: '/root/path'); // -> 'a/b.dart' |
173 path.relative('/root/other.dart', | 200 path.relative('/root/other.dart', |
174 from: '/root/path'); // -> '../other.dart' | 201 from: '/root/path'); // -> '../other.dart' |
175 | 202 |
176 Since there is no relative path from one drive letter to another on Windows, | 203 Since there is no relative path from one drive letter to another on Windows, |
177 this will return an absolute path in that case. | 204 this will return an absolute path in that case. |
178 | 205 |
| 206 // Windows |
179 path.relative(r'D:\other', from: r'C:\home'); // -> 'D:\other' | 207 path.relative(r'D:\other', from: r'C:\home'); // -> 'D:\other' |
180 | 208 |
| 209 // URL |
| 210 path.relative('http://dartlang.org', from: 'http://pub.dartlang.org'); |
| 211 // -> 'http://dartlang.org' |
| 212 |
181 ### String withoutExtension(String path) | 213 ### String withoutExtension(String path) |
182 | 214 |
183 Removes a trailing extension from the last part of [path]. | 215 Removes a trailing extension from the last part of [path]. |
184 | 216 |
185 withoutExtension('path/to/foo.dart'); // -> 'path/to/foo' | 217 withoutExtension('path/to/foo.dart'); // -> 'path/to/foo' |
186 | 218 |
187 ## The path.Builder class | 219 ## The path.Builder class |
188 | 220 |
189 In addition to the functions, path exposes a `path.Builder` class. This lets | 221 In addition to the functions, path exposes a `path.Builder` class. This lets |
190 you configure the root directory and path style that paths are built using | 222 you configure the root directory and path style that paths are built using |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 relative. | 259 relative. |
228 | 260 |
229 // Unix | 261 // Unix |
230 builder.rootPrefix('path/to/foo'); // -> '' | 262 builder.rootPrefix('path/to/foo'); // -> '' |
231 builder.rootPrefix('/path/to/foo'); // -> '/' | 263 builder.rootPrefix('/path/to/foo'); // -> '/' |
232 | 264 |
233 // Windows | 265 // Windows |
234 builder.rootPrefix(r'path\to\foo'); // -> '' | 266 builder.rootPrefix(r'path\to\foo'); // -> '' |
235 builder.rootPrefix(r'C:\path\to\foo'); // -> r'C:\' | 267 builder.rootPrefix(r'C:\path\to\foo'); // -> r'C:\' |
236 | 268 |
| 269 // URL |
| 270 builder.rootPrefix('path/to/foo'); // -> '' |
| 271 builder.rootPrefix('http://dartlang.org/path/to/foo'); |
| 272 // -> 'http://dartlang.org' |
| 273 |
237 ### String resolve(String part1, [String part2, String part3, ...]) | 274 ### String resolve(String part1, [String part2, String part3, ...]) |
238 | 275 |
239 Creates a new path by appending the given path parts to the [root]. | 276 Creates a new path by appending the given path parts to the [root]. |
240 Equivalent to [join()] with [root] as the first argument. Example: | 277 Equivalent to [join()] with [root] as the first argument. Example: |
241 | 278 |
242 var builder = new Builder(root: 'root'); | 279 var builder = new Builder(root: 'root'); |
243 builder.resolve('path', 'to', 'foo'); // -> 'root/path/to/foo' | 280 builder.resolve('path', 'to', 'foo'); // -> 'root/path/to/foo' |
244 | 281 |
245 ## The path.Style class | 282 ## The path.Style class |
246 | 283 |
247 The path library can work with two different "flavors" of path: POSIX and | 284 The path library can work with three different "flavors" of path: POSIX, |
248 Windows. The differences between these are encapsulated by the `path.Style` | 285 Windows, and URLs. The differences between these are encapsulated by the |
249 enum class. There are two instances of it: | 286 `path.Style` enum class. There are three instances of it: |
250 | 287 |
251 ### path.Style.posix | 288 ### path.Style.posix |
252 | 289 |
253 POSIX-style paths use "/" (forward slash) as separators. Absolute paths | 290 POSIX-style paths use "/" (forward slash) as separators. Absolute paths |
254 start with "/". Used by UNIX, Linux, Mac OS X, and others. | 291 start with "/". Used by UNIX, Linux, Mac OS X, and others. |
255 | 292 |
256 ### path.Style.windows | 293 ### path.Style.windows |
257 | 294 |
258 Windows paths use "\" (backslash) as separators. Absolute paths start with | 295 Windows paths use "\" (backslash) as separators. Absolute paths start with |
259 a drive letter followed by a colon (example, "C:") or two backslashes | 296 a drive letter followed by a colon (example, "C:") or two backslashes |
260 ("\\") for UNC paths. | 297 ("\\") for UNC paths. |
261 | 298 |
| 299 ### path.Style.url |
| 300 |
| 301 URLs aren't filesystem paths, but they're supported by Pathos to make it easier |
| 302 to manipulate URL paths in the browser. |
| 303 |
| 304 URLs use "/" (forward slash) as separators. Absolute paths either start with a |
| 305 protocol and optional hostname (e.g. `http://dartlang.org`, `file://`) or with |
| 306 "/". |
| 307 |
262 ## FAQ | 308 ## FAQ |
263 | 309 |
264 ### Where can I use this? | 310 ### Where can I use this? |
265 | 311 |
266 Currently, Dart has no way of encapsulating configuration-specific code. | 312 Pathos runs on the Dart VM and in the browser under both dart2js and Dartium. |
267 Ideally, this library would be able to import dart:io when that's available or | 313 Under dart2js, it currently returns "." as the current working directory, while |
268 dart:html when that is. That would let it seamlessly work on both. | 314 under Dartium it returns the current URL. |
269 | |
270 Until then, this only works on the standalone VM. It's API is not coupled to | |
271 dart:io, but it uses it internally to determine the current working directory. | |
272 | 315 |
273 ### Why doesn't this make paths first-class objects? | 316 ### Why doesn't this make paths first-class objects? |
274 | 317 |
275 When you have path *objects*, then every API that takes a path has to decide if | 318 When you have path *objects*, then every API that takes a path has to decide if |
276 it accepts strings, path objects, or both. | 319 it accepts strings, path objects, or both. |
277 | 320 |
278 * Accepting strings is the most convenient, but then it seems weird to have | 321 * Accepting strings is the most convenient, but then it seems weird to have |
279 these path objects that aren't actually accepted by anything that needs a | 322 these path objects that aren't actually accepted by anything that needs a |
280 path. Once you've created a path, you have to always call `.toString()` on | 323 path. Once you've created a path, you have to always call `.toString()` on |
281 it before you can do anything useful with it. | 324 it before you can do anything useful with it. |
(...skipping 21 matching lines...) Expand all Loading... |
303 * It can accurately tell if a path is absolute based on drive-letters or UNC | 346 * It can accurately tell if a path is absolute based on drive-letters or UNC |
304 prefix. | 347 prefix. |
305 | 348 |
306 * It understands that "/foo" is not an absolute path on Windows. | 349 * It understands that "/foo" is not an absolute path on Windows. |
307 | 350 |
308 * It knows that "C:\foo\one.txt" and "c:/foo\two.txt" are two files in the | 351 * It knows that "C:\foo\one.txt" and "c:/foo\two.txt" are two files in the |
309 same directory. | 352 same directory. |
310 | 353 |
311 If you find a problem, surprise or something that's unclear, please don't | 354 If you find a problem, surprise or something that's unclear, please don't |
312 hesitate to [file a bug](http://dartbug.com/new) and let us know. | 355 hesitate to [file a bug](http://dartbug.com/new) and let us know. |
OLD | NEW |