@@ -182,37 +182,32 @@ added: v0.11.15
182
182
The ` path.format() ` method returns a path string from an object. This is the
183
183
opposite of [ ` path.parse() ` ] [ ] .
184
184
185
- The following process is used when constructing the path string:
186
-
187
- * ` output ` is set to an empty string.
188
- * If ` pathObject.dir ` is specified, ` pathObject.dir ` is appended to ` output `
189
- followed by the value of ` path.sep ` ;
190
- * Otherwise, if ` pathObject.root ` is specified, ` pathObject.root ` is appended
191
- to ` output ` .
192
- * If ` pathObject.base ` is specified, ` pathObject.base ` is appended to ` output ` ;
193
- * Otherwise:
194
- * If ` pathObject.name ` is specified, ` pathObject.name ` is appended to ` output `
195
- * If ` pathObject.ext ` is specified, ` pathObject.ext ` is appended to ` output ` .
196
- * Return ` output `
185
+ When providing properties to the ` pathObject ` remember that there are
186
+ combinations where one property has priority over another:
187
+
188
+ * ` pathObject.root ` is ignored if ` pathObject.dir ` is provided
189
+ * ` pathObject.ext ` and ` pathObject.name ` are ignored if ` pathObject.base ` exists
197
190
198
191
For example, on POSIX:
199
192
200
193
``` js
201
- // If `dir` and `base` are provided,
194
+ // If `dir`, `root` and `base` are provided,
202
195
// `${dir}${path.sep}${base}`
203
- // will be returned.
196
+ // will be returned. `root` is ignored.
204
197
path .format ({
198
+ root: ' /ignored' ,
205
199
dir: ' /home/user/dir' ,
206
200
base: ' file.txt'
207
201
});
208
202
// Returns: '/home/user/dir/file.txt'
209
203
210
204
// `root` will be used if `dir` is not specified.
211
205
// If only `root` is provided or `dir` is equal to `root` then the
212
- // platform separator will not be included.
206
+ // platform separator will not be included. `ext` will be ignored.
213
207
path .format ({
214
208
root: ' /' ,
215
- base: ' file.txt'
209
+ base: ' file.txt' ,
210
+ ext: ' ignored'
216
211
});
217
212
// Returns: '/file.txt'
218
213
@@ -223,23 +218,14 @@ path.format({
223
218
ext: ' .txt'
224
219
});
225
220
// Returns: '/file.txt'
226
-
227
- // `base` will be returned if `dir` or `root` are not provided.
228
- path .format ({
229
- base: ' file.txt'
230
- });
231
- // Returns: 'file.txt'
232
221
```
233
222
234
223
On Windows:
235
224
236
225
``` js
237
226
path .format ({
238
- root : " C:\\ " ,
239
- dir : " C:\\ path\\ dir" ,
240
- base : " file.txt" ,
241
- ext : " .txt" ,
242
- name : " file"
227
+ dir : " C:\\ path\\ dir" ,
228
+ base : " file.txt"
243
229
});
244
230
// Returns: 'C:\\path\\dir\\file.txt'
245
231
```
0 commit comments