| 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 path package provides common operations for manipulating file paths: | 3 The path package provides common operations for manipulating 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 the | 7 platform you run it on, including in the browser. When you use the top-level |
| 8 current platform's path style and work with that. If you want to specifically | 8 functions, it will assume the current platform's path style and work with |
| 9 work with paths of a specific style, you can construct a `path.Builder` for that | 9 that. If you want to explicitly work with paths of a specific style, you can |
| 10 style. | 10 construct a `path.Builder` for that 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:path/path.dart' as path; // TODO(bob): ??? | 17 import 'package:path/path.dart' as path; |
| 18 | |
| 19 ## Top-level functions | |
| 20 | 18 |
| 21 The most common way to use the library is through the top-level functions. | 19 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 | 20 These manipulate path strings based on your current working directory and |
| 23 path style (POSIX, Windows, or URLs) of the host platform. | 21 the path style (POSIX, Windows, or URLs) of the host platform. For example: |
| 24 | 22 |
| 25 ### String get current | 23 path.join("directory", "file.txt"); |
| 26 | 24 |
| 27 Gets the path to the current working directory. In the browser, this means the | 25 This calls the top-level [join] function to join "directory" and |
| 28 current URL. When using dart2js, this currently returns `.` due to technical | 26 "file.txt" using the current platform's directory separator. |
| 29 constraints. In the future, it will return the current URL. | |
| 30 | 27 |
| 31 ### String get separator | 28 If you want to work with paths for a specific platform regardless of the |
| 29 underlying platform that the program is running on, you can create a |
| 30 [Builder] and give it an explicit [Style]: |
| 32 | 31 |
| 33 Gets the path separator for the current platform. On Mac, Linux, and the | 32 var builder = new path.Builder(style: Style.windows); |
| 34 browser, this is `/`. On Windows, it's `\`. | 33 builder.join("directory", "file.txt"); |
| 35 | 34 |
| 36 ### String absolute(String path) | 35 This will join "directory" and "file.txt" using the Windows path separator, |
| 37 | 36 even when the program is run on a POSIX machine. |
| 38 Converts [path] to an absolute path by resolving it relative to the current | |
| 39 working directory. If [path] is already an absolute path, just returns it. | |
| 40 | |
| 41 path.absolute('foo/bar.txt'); // -> /your/current/dir/foo/bar.txt | |
| 42 | |
| 43 ### String basename(String path) | |
| 44 | |
| 45 Gets the part of [path] after the last separator. | |
| 46 | |
| 47 path.basename('path/to/foo.dart'); // -> 'foo.dart' | |
| 48 path.basename('path/to'); // -> 'to' | |
| 49 | |
| 50 Trailing separators are ignored. | |
| 51 | |
| 52 builder.basename('path/to/'); // -> 'to' | |
| 53 | |
| 54 ### String basenameWithoutExtension(String path) | |
| 55 | |
| 56 Gets the part of [path] after the last separator, and without any trailing | |
| 57 file extension. | |
| 58 | |
| 59 path.basenameWithoutExtension('path/to/foo.dart'); // -> 'foo' | |
| 60 | |
| 61 Trailing separators are ignored. | |
| 62 | |
| 63 builder.basenameWithoutExtension('path/to/foo.dart/'); // -> 'foo' | |
| 64 | |
| 65 ### String dirname(String path) | |
| 66 | |
| 67 Gets the part of [path] before the last separator. | |
| 68 | |
| 69 path.dirname('path/to/foo.dart'); // -> 'path/to' | |
| 70 path.dirname('path/to'); // -> 'to' | |
| 71 | |
| 72 Trailing separators are ignored. | |
| 73 | |
| 74 builder.dirname('path/to/'); // -> 'path' | |
| 75 | |
| 76 If an absolute path contains no directories, only a root, then the root | |
| 77 is returned. | |
| 78 | |
| 79 path.dirname('/'); // -> '/' (posix) | |
| 80 path.dirname('c:\'); // -> 'c:\' (windows) | |
| 81 | |
| 82 If a relative path has no directories, then '.' is returned. | |
| 83 path.dirname('foo'); // -> '.' | |
| 84 path.dirname(''); // -> '.' | |
| 85 | |
| 86 ### String extension(String path) | |
| 87 | |
| 88 Gets the file extension of [path]: the portion of [basename] from the last | |
| 89 `.` to the end (including the `.` itself). | |
| 90 | |
| 91 path.extension('path/to/foo.dart'); // -> '.dart' | |
| 92 path.extension('path/to/foo'); // -> '' | |
| 93 path.extension('path.to/foo'); // -> '' | |
| 94 path.extension('path/to/foo.dart.js'); // -> '.js' | |
| 95 | |
| 96 If the file name starts with a `.`, then that is not considered the | |
| 97 extension: | |
| 98 | |
| 99 path.extension('~/.bashrc'); // -> '' | |
| 100 path.extension('~/.notes.txt'); // -> '.txt' | |
| 101 | |
| 102 ### String rootPrefix(String path) | |
| 103 | |
| 104 Returns the root of [path], if it's absolute, or the empty string if it's | |
| 105 relative. | |
| 106 | |
| 107 // Unix | |
| 108 path.rootPrefix('path/to/foo'); // -> '' | |
| 109 path.rootPrefix('/path/to/foo'); // -> '/' | |
| 110 | |
| 111 // Windows | |
| 112 path.rootPrefix(r'path\to\foo'); // -> '' | |
| 113 path.rootPrefix(r'C:\path\to\foo'); // -> r'C:\' | |
| 114 | |
| 115 // URL | |
| 116 path.rootPrefix('path/to/foo'); // -> '' | |
| 117 path.rootPrefix('http://dartlang.org/path/to/foo'); | |
| 118 // -> 'http://dartlang.org' | |
| 119 | |
| 120 ### bool isAbsolute(String path) | |
| 121 | |
| 122 Returns `true` if [path] is an absolute path and `false` if it is a relative | |
| 123 path. On POSIX systems, absolute paths start with a `/` (forward slash). On | |
| 124 Windows, an absolute path starts with `\\`, or a drive letter followed by `:/` | |
| 125 or `:\`. For URLs, absolute paths either start with a protocol and optional | |
| 126 hostname (e.g. `http://dartlang.org`, `file://`) or with a `/`. | |
| 127 | |
| 128 URLs that start with `/` are known as "root-relative", since they're relative to | |
| 129 the root of the current URL. Since root-relative paths are still absolute in | |
| 130 every other sense, [isAbsolute] will return true for them. They can be detected | |
| 131 using [isRootRelative]. | |
| 132 | |
| 133 ### bool isRelative(String path) | |
| 134 | |
| 135 Returns `true` if [path] is a relative path and `false` if it is absolute. | |
| 136 On POSIX systems, absolute paths start with a `/` (forward slash). On | |
| 137 Windows, an absolute path starts with `\\`, or a drive letter followed by | |
| 138 `:/` or `:\`. | |
| 139 | |
| 140 ### bool isRootRelative(String path) | |
| 141 | |
| 142 Returns `true` if [path] is a root-relative path and `false` if it's not. URLs | |
| 143 that start with `/` are known as "root-relative", since they're relative to the | |
| 144 root of the current URL. Since root-relative paths are still absolute in every | |
| 145 other sense, [isAbsolute] will return true for them. They can be detected using | |
| 146 [isRootRelative]. | |
| 147 | |
| 148 No POSIX and Windows paths are root-relative. | |
| 149 | |
| 150 ### String join(String part1, [String part2, String part3, ...]) | |
| 151 | |
| 152 Joins the given path parts into a single path using the current platform's | |
| 153 [separator]. Example: | |
| 154 | |
| 155 path.join('path', 'to', 'foo'); // -> 'path/to/foo' | |
| 156 | |
| 157 If any part ends in a path separator, then a redundant separator will not | |
| 158 be added: | |
| 159 | |
| 160 path.join('path/', 'to', 'foo'); // -> 'path/to/foo | |
| 161 | |
| 162 If a part is an absolute path, then anything before that will be ignored: | |
| 163 | |
| 164 path.join('path', '/to', 'foo'); // -> '/to/foo' | |
| 165 | |
| 166 ### List<String> split(String path) | |
| 167 | |
| 168 Splits [path] into its components using the current platform's [separator]. | |
| 169 | |
| 170 path.split('path/to/foo'); // -> ['path', 'to', 'foo'] | |
| 171 | |
| 172 The path will *not* be normalized before splitting. | |
| 173 | |
| 174 path.split('path/../foo'); // -> ['path', '..', 'foo'] | |
| 175 | |
| 176 If [path] is absolute, the root directory will be the first element in the | |
| 177 array. Example: | |
| 178 | |
| 179 // Unix | |
| 180 path.split('/path/to/foo'); // -> ['/', 'path', 'to', 'foo'] | |
| 181 | |
| 182 // Windows | |
| 183 path.split(r'C:\path\to\foo'); // -> [r'C:\', 'path', 'to', 'foo'] | |
| 184 | |
| 185 // Browser | |
| 186 path.split('http://dartlang.org/path/to/foo'); | |
| 187 // -> ['http://dartlang.org', 'path', 'to', 'foo'] | |
| 188 | |
| 189 ### String normalize(String path) | |
| 190 | |
| 191 Normalizes [path], simplifying it by handling `..`, and `.`, and | |
| 192 removing redundant path separators whenever possible. | |
| 193 | |
| 194 path.normalize('path/./to/..//file.text'); // -> 'path/file.txt' | |
| 195 String normalize(String path) => _builder.normalize(path); | |
| 196 | |
| 197 ### String relative(String path, {String from}) | |
| 198 | |
| 199 Attempts to convert [path] to an equivalent relative path from the current | |
| 200 directory. | |
| 201 | |
| 202 // Given current directory is /root/path: | |
| 203 path.relative('/root/path/a/b.dart'); // -> 'a/b.dart' | |
| 204 path.relative('/root/other.dart'); // -> '../other.dart' | |
| 205 | |
| 206 If the [from] argument is passed, [path] is made relative to that instead. | |
| 207 | |
| 208 path.relative('/root/path/a/b.dart', | |
| 209 from: '/root/path'); // -> 'a/b.dart' | |
| 210 path.relative('/root/other.dart', | |
| 211 from: '/root/path'); // -> '../other.dart' | |
| 212 | |
| 213 If [path] and/or [from] are relative paths, they are assumed to be relative | |
| 214 to the current directory. | |
| 215 | |
| 216 Since there is no relative path from one drive letter to another on Windows, | |
| 217 this will return an absolute path in that case. | |
| 218 | |
| 219 // Windows | |
| 220 path.relative(r'D:\other', from: r'C:\home'); // -> 'D:\other' | |
| 221 | |
| 222 // URL | |
| 223 path.relative('http://dartlang.org', from: 'http://pub.dartlang.org'); | |
| 224 // -> 'http://dartlang.org' | |
| 225 | |
| 226 ### String withoutExtension(String path) | |
| 227 | |
| 228 Removes a trailing extension from the last part of [path]. | |
| 229 | |
| 230 withoutExtension('path/to/foo.dart'); // -> 'path/to/foo' | |
| 231 | |
| 232 ### String fromUri(Uri uri) | |
| 233 | |
| 234 Returns the path represented by [uri]. For POSIX and Windows styles, [uri] must | |
| 235 be a `file:` URI. For the URL style, this will just convert [uri] to a string. | |
| 236 | |
| 237 // POSIX | |
| 238 path.fromUri(Uri.parse('file:///path/to/foo')) | |
| 239 // -> '/path/to/foo' | |
| 240 | |
| 241 // Windows | |
| 242 path.fromUri(Uri.parse('file:///C:/path/to/foo')) | |
| 243 // -> r'C:\path\to\foo' | |
| 244 | |
| 245 // URL | |
| 246 path.fromUri(Uri.parse('http://dartlang.org/path/to/foo')) | |
| 247 // -> 'http://dartlang.org/path/to/foo' | |
| 248 | |
| 249 ### Uri toUri(String path) | |
| 250 | |
| 251 Returns the URI that represents [path]. For POSIX and Windows styles, this will | |
| 252 return a `file:` URI. For the URL style, this will just convert [path] to a | |
| 253 [Uri]. | |
| 254 | |
| 255 This will always convert relative paths to absolute ones before converting | |
| 256 to a URI. | |
| 257 | |
| 258 // POSIX | |
| 259 path.toUri('/path/to/foo') | |
| 260 // -> Uri.parse('file:///path/to/foo') | |
| 261 | |
| 262 // Windows | |
| 263 path.toUri(r'C:\path\to\foo') | |
| 264 // -> Uri.parse('file:///C:/path/to/foo') | |
| 265 | |
| 266 // URL | |
| 267 path.toUri('http://dartlang.org/path/to/foo') | |
| 268 // -> Uri.parse('http://dartlang.org/path/to/foo') | |
| 269 | |
| 270 ## The path.Builder class | |
| 271 | |
| 272 In addition to the functions, path exposes a `path.Builder` class. This lets | |
| 273 you configure the root directory and path style that paths are built using | |
| 274 explicitly instead of assuming the current working directory and host OS's path | |
| 275 style. | |
| 276 | |
| 277 You won't often use this, but it can be useful if you do a lot of path | |
| 278 manipulation relative to some root directory. | |
| 279 | |
| 280 var builder = new path.Builder(root: '/other/root'); | |
| 281 builder.relative('/other/root/foo.txt'); // -> 'foo.txt' | |
| 282 | |
| 283 It exposes the same methods and getters as the top-level functions, with the | |
| 284 addition of: | |
| 285 | |
| 286 ### new Builder({Style style, String root}) | |
| 287 | |
| 288 Creates a new path builder for the given style and root directory. | |
| 289 | |
| 290 If [style] is omitted, it uses the host operating system's path style. If | |
| 291 [root] is omitted, it defaults to the current working directory. If [root] | |
| 292 is relative, it is considered relative to the current working directory. | |
| 293 | |
| 294 ### Style style | |
| 295 | |
| 296 The style of path that this builder works with. | |
| 297 | |
| 298 ### String root | |
| 299 | |
| 300 The root directory that relative paths will be relative to. | |
| 301 | |
| 302 ### String get separator | |
| 303 | |
| 304 Gets the path separator for the builder's [style]. On Mac and Linux, | |
| 305 this is `/`. On Windows, it's `\`. | |
| 306 | |
| 307 ### String rootPrefix(String path) | |
| 308 | |
| 309 Returns the root of [path], if it's absolute, or an empty string if it's | |
| 310 relative. | |
| 311 | |
| 312 // Unix | |
| 313 builder.rootPrefix('path/to/foo'); // -> '' | |
| 314 builder.rootPrefix('/path/to/foo'); // -> '/' | |
| 315 | |
| 316 // Windows | |
| 317 builder.rootPrefix(r'path\to\foo'); // -> '' | |
| 318 builder.rootPrefix(r'C:\path\to\foo'); // -> r'C:\' | |
| 319 | |
| 320 // URL | |
| 321 builder.rootPrefix('path/to/foo'); // -> '' | |
| 322 builder.rootPrefix('http://dartlang.org/path/to/foo'); | |
| 323 // -> 'http://dartlang.org' | |
| 324 | |
| 325 ### String resolve(String part1, [String part2, String part3, ...]) | |
| 326 | |
| 327 Creates a new path by appending the given path parts to the [root]. | |
| 328 Equivalent to [join()] with [root] as the first argument. Example: | |
| 329 | |
| 330 var builder = new Builder(root: 'root'); | |
| 331 builder.resolve('path', 'to', 'foo'); // -> 'root/path/to/foo' | |
| 332 | |
| 333 ## The path.Style class | |
| 334 | |
| 335 The path library can work with three different "flavors" of path: POSIX, | |
| 336 Windows, and URLs. The differences between these are encapsulated by the | |
| 337 `path.Style` enum class. There are three instances of it: | |
| 338 | |
| 339 ### path.Style.posix | |
| 340 | |
| 341 POSIX-style paths use "/" (forward slash) as separators. Absolute paths | |
| 342 start with "/". Used by UNIX, Linux, Mac OS X, and others. | |
| 343 | |
| 344 ### path.Style.windows | |
| 345 | |
| 346 Windows paths use "\" (backslash) as separators. Absolute paths start with | |
| 347 a drive letter followed by a colon (example, "C:") or two backslashes | |
| 348 ("\\") for UNC paths. | |
| 349 | |
| 350 ### path.Style.url | |
| 351 | |
| 352 URLs aren't filesystem paths, but they're supported by Pathos to make it easier | |
| 353 to manipulate URL paths in the browser. | |
| 354 | |
| 355 URLs use "/" (forward slash) as separators. Absolute paths either start with a | |
| 356 protocol and optional hostname (e.g. `http://dartlang.org`, `file://`) or with | |
| 357 "/". | |
| 358 | 37 |
| 359 ## FAQ | 38 ## FAQ |
| 360 | 39 |
| 361 ### Where can I use this? | 40 ### Where can I use this? |
| 362 | 41 |
| 363 Pathos runs on the Dart VM and in the browser under both dart2js and Dartium. | 42 Pathos runs on the Dart VM and in the browser under both dart2js and Dartium. |
| 364 Under dart2js, it currently returns "." as the current working directory, while | 43 Under dart2js, it currently returns "." as the current working directory, while |
| 365 under Dartium it returns the current URL. | 44 under Dartium it returns the current URL. |
| 366 | 45 |
| 367 ### Why doesn't this make paths first-class objects? | 46 ### Why doesn't this make paths first-class objects? |
| 368 | 47 |
| 369 When you have path *objects*, then every API that takes a path has to decide if | 48 When you have path *objects*, then every API that takes a path has to decide if |
| 370 it accepts strings, path objects, or both. | 49 it accepts strings, path objects, or both. |
| 371 | 50 |
| 372 * Accepting strings is the most convenient, but then it seems weird to have | 51 * Accepting strings is the most convenient, but then it seems weird to have |
| 373 these path objects that aren't actually accepted by anything that needs a | 52 these path objects that aren't actually accepted by anything that needs a |
| 374 path. Once you've created a path, you have to always call `.toString()` on | 53 path. Once you've created a path, you have to always call `.toString()` on |
| 375 it before you can do anything useful with it. | 54 it before you can do anything useful with it. |
| 376 | 55 |
| 377 * Requiring objects forces users to wrap path strings in these objects, which | 56 * Requiring objects forces users to wrap path strings in these objects, which |
| 378 is tedious. It also means coupling that API to whatever library defines this | 57 is tedious. It also means coupling that API to whatever library defines this |
| 379 path class. If there are multiple "path" libraries that each define their | 58 path class. If there are multiple "path" libraries that each define their |
| 380 own path types, then any library that works with paths has to pick which one | 59 own path types, then any library that works with paths has to pick which one |
| 381 it uses. | 60 it uses. |
| 382 | 61 |
| 383 * Taking both means you can't type your API. That defeats the purpose of | 62 * Taking both means you can't type your API. That defeats the purpose of |
| 384 having a path type: why have a type if your APIs can't annotate that they | 63 having a path type: why have a type if your APIs can't annotate that they |
| 385 use it? | 64 expect it? |
| 386 | 65 |
| 387 Given that, we've decided this library should simply treat paths as strings. | 66 Given that, we've decided this library should simply treat paths as strings. |
| 388 | 67 |
| 389 ### How cross-platform is this? | 68 ### How cross-platform is this? |
| 390 | 69 |
| 391 We believe this library handles most of the corner cases of Windows paths | 70 We believe this library handles most of the corner cases of Windows paths |
| 392 (POSIX paths are generally pretty straightforward): | 71 (POSIX paths are generally pretty straightforward): |
| 393 | 72 |
| 394 * It understands that *both* "/" and "\" are valid path separators, not just | 73 * It understands that *both* "/" and "\" are valid path separators, not just |
| 395 "\". | 74 "\". |
| 396 | 75 |
| 397 * It can accurately tell if a path is absolute based on drive-letters or UNC | 76 * It can accurately tell if a path is absolute based on drive-letters or UNC |
| 398 prefix. | 77 prefix. |
| 399 | 78 |
| 400 * It understands that "/foo" is not an absolute path on Windows. | 79 * It understands that "/foo" is not an absolute path on Windows. |
| 401 | 80 |
| 402 * It knows that "C:\foo\one.txt" and "c:/foo\two.txt" are two files in the | 81 * It knows that "C:\foo\one.txt" and "c:/foo\two.txt" are two files in the |
| 403 same directory. | 82 same directory. |
| 404 | 83 |
| 405 If you find a problem, surprise or something that's unclear, please don't | 84 ### What is a "path" in the browser? |
| 406 hesitate to [file a bug](http://dartbug.com/new) and let us know. | 85 |
| 86 If you use this package in a browser, then it considers the "platform" to be |
| 87 the browser itself and uses URL strings to represent "browser paths". |
| OLD | NEW |