blob: 5a172cdf26320c70e5713766bd8ede385ec9eaa4 [file] [log] [blame] [view]
John McCutchan9f605592015-09-17 14:09:47 -07001A comprehensive, cross-platform path manipulation library for Dart.
2
3The path package provides common operations for manipulating paths:
4joining, splitting, normalizing, etc.
5
6We've tried very hard to make this library do the "right" thing on whatever
7platform you run it on, including in the browser. When you use the top-level
8functions, it will assume the current platform's path style and work with
9that. If you want to explicitly work with paths of a specific style, you can
10construct a `path.Context` for that style.
11
12## Using
13
14The path library was designed to be imported with a prefix, though you don't
15have to if you don't want to:
16
17```dart
18import 'package:path/path.dart' as path;
19```
20
21The most common way to use the library is through the top-level functions.
22These manipulate path strings based on your current working directory and
23the path style (POSIX, Windows, or URLs) of the host platform. For example:
24
25```dart
26path.join("directory", "file.txt");
27```
28
29This calls the top-level [join] function to join "directory" and
30"file.txt" using the current platform's directory separator.
31
32If you want to work with paths for a specific platform regardless of the
33underlying platform that the program is running on, you can create a
34[Context] and give it an explicit [Style]:
35
36```dart
37var context = new path.Context(style: Style.windows);
38context.join("directory", "file.txt");
39```
40
41This will join "directory" and "file.txt" using the Windows path separator,
42even when the program is run on a POSIX machine.
43
44## FAQ
45
46### Where can I use this?
47
48Pathos runs on the Dart VM and in the browser under both dart2js and Dartium.
49Under dart2js, it currently returns "." as the current working directory, while
50under Dartium it returns the current URL.
51
52### Why doesn't this make paths first-class objects?
53
54When you have path *objects*, then every API that takes a path has to decide if
55it accepts strings, path objects, or both.
56
57 * Accepting strings is the most convenient, but then it seems weird to have
58 these path objects that aren't actually accepted by anything that needs a
59 path. Once you've created a path, you have to always call `.toString()` on
60 it before you can do anything useful with it.
61
62 * Requiring objects forces users to wrap path strings in these objects, which
63 is tedious. It also means coupling that API to whatever library defines this
64 path class. If there are multiple "path" libraries that each define their
65 own path types, then any library that works with paths has to pick which one
66 it uses.
67
68 * Taking both means you can't type your API. That defeats the purpose of
69 having a path type: why have a type if your APIs can't annotate that they
70 expect it?
71
72Given that, we've decided this library should simply treat paths as strings.
73
74### How cross-platform is this?
75
76We believe this library handles most of the corner cases of Windows paths
77(POSIX paths are generally pretty straightforward):
78
79 * It understands that *both* "/" and "\" are valid path separators, not just
80 "\".
81
82 * It can accurately tell if a path is absolute based on drive-letters or UNC
83 prefix.
84
85 * It understands that "/foo" is not an absolute path on Windows.
86
87 * It knows that "C:\foo\one.txt" and "c:/foo\two.txt" are two files in the
88 same directory.
89
90### What is a "path" in the browser?
91
92If you use this package in a browser, then it considers the "platform" to be
93the browser itself and uses URL strings to represent "browser paths".