You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

342 lines
18 KiB

4 years ago
  1. # upath v1.2.0
  2. [![Build Status](https://travis-ci.org/anodynos/upath.svg?branch=master)](https://travis-ci.org/anodynos/upath)
  3. [![Up to date Status](https://david-dm.org/anodynos/upath.png)](https://david-dm.org/anodynos/upath)
  4. A drop-in replacement / proxy to nodejs's `path` that:
  5. * Replaces the windows `\` with the unix `/` in all string params & results. This has significant positives - see below.
  6. * Adds **filename extensions** functions `addExt`, `trimExt`, `removeExt`, `changeExt`, and `defaultExt`.
  7. * Add a `normalizeSafe` function to preserve any meaningful leading `./` & a `normalizeTrim` which additionally trims any useless ending `/`.
  8. * Plus a helper `toUnix` that simply converts `\` to `/` and consolidates duplicates.
  9. **Useful note: these docs are actually auto generated from [specs](https://github.com/anodynos/upath/blob/master/source/spec/upath-spec.coffee), running on Linux.**
  10. Notes:
  11. * `upath.sep` is set to `'/'` for seamless replacement (as of 1.0.3).
  12. * upath has no runtime dependencies, except built-in `path` (as of 1.0.4)
  13. * travis-ci tested in node versions 4 to 12
  14. ## Why ?
  15. Normal `path` doesn't convert paths to a unified format (ie `/`) before calculating paths (`normalize`, `join`), which can lead to numerous problems.
  16. Also path joining, normalization etc on the two formats is not consistent, depending on where it runs. Running `path` on Windows yields different results than when it runs on Linux / Mac.
  17. In general, if you code your paths logic while developing on Unix/Mac and it runs on Windows, you may run into problems when using `path`.
  18. Note that using **Unix `/` on Windows** works perfectly inside nodejs (and other languages), so there's no reason to stick to the Windows legacy at all.
  19. ##### Examples / specs
  20. Check out the different (improved) behavior to vanilla `path`:
  21. `upath.normalize(path)` --returns-->
  22. `'c:/windows/nodejs/path'` ---> `'c:/windows/nodejs/path'` // equal to `path.normalize()`
  23. `'c:/windows/../nodejs/path'` ---> `'c:/nodejs/path'` // equal to `path.normalize()`
  24. `'c:\\windows\\nodejs\\path'` ---> `'c:/windows/nodejs/path'` // `path.normalize()` gives `'c:\windows\nodejs\path'`
  25. `'c:\\windows\\..\\nodejs\\path'` ---> `'c:/nodejs/path'` // `path.normalize()` gives `'c:\windows\..\nodejs\path'`
  26. `'//windows\\unix/mixed'` ---> `'/windows/unix/mixed'` // `path.normalize()` gives `'/windows\unix/mixed'`
  27. `'\\windows//unix/mixed'` ---> `'/windows/unix/mixed'` // `path.normalize()` gives `'\windows/unix/mixed'`
  28. `'////\\windows\\..\\unix/mixed/'` ---> `'/unix/mixed/'` // `path.normalize()` gives `'/\windows\..\unix/mixed/'`
  29. Joining paths can also be a problem:
  30. `upath.join(paths...)` --returns-->
  31. `'some/nodejs/deep', '../path'` ---> `'some/nodejs/path'` // equal to `path.join()`
  32. `'some/nodejs\\windows', '../path'` ---> `'some/nodejs/path'` // `path.join()` gives `'some/path'`
  33. `'some\\windows\\only', '..\\path'` ---> `'some/windows/path'` // `path.join()` gives `'some\windows\only/..\path'`
  34. Parsing with `path.parse()` should also be consistent across OSes:
  35. `upath.parse(path)` --returns-->
  36. `'c:\Windows\Directory\somefile.ext'` ---> `{ root: '', dir: 'c:/Windows/Directory', base: 'somefile.ext', ext: '.ext', name: 'somefile' }`
  37. // `path.parse()` gives `'{ root: '', dir: '', base: 'c:\\Windows\\Directory\\somefile.ext', ext: '.ext', name: 'c:\\Windows\\Directory\\somefile' }'`
  38. `'/root/of/unix/somefile.ext'` ---> `{ root: '/', dir: '/root/of/unix', base: 'somefile.ext', ext: '.ext', name: 'somefile' }` // equal to `path.parse()`
  39. ## Added functions
  40. #### `upath.toUnix(path)`
  41. Just converts all `` to `/` and consolidates duplicates, without performing any normalization.
  42. ##### Examples / specs
  43. `upath.toUnix(path)` --returns-->
  44. `'.//windows\//unix//mixed////'` ---> `'./windows/unix/mixed/'`
  45. `'..///windows\..\\unix/mixed'` ---> `'../windows/../unix/mixed'`
  46. #### `upath.normalizeSafe(path)`
  47. Exactly like `path.normalize(path)`, but it keeps the first meaningful `./`.
  48. Note that the unix `/` is returned everywhere, so windows `\` is always converted to unix `/`.
  49. ##### Examples / specs & how it differs from vanilla `path`
  50. `upath.normalizeSafe(path)` --returns-->
  51. `''` ---> `'.'` // equal to `path.normalize()`
  52. `'.'` ---> `'.'` // equal to `path.normalize()`
  53. `'./'` ---> `'./'` // equal to `path.normalize()`
  54. `'.//'` ---> `'./'` // equal to `path.normalize()`
  55. `'.\\'` ---> `'./'` // `path.normalize()` gives `'.\'`
  56. `'.\\//'` ---> `'./'` // `path.normalize()` gives `'.\/'`
  57. `'./..'` ---> `'..'` // equal to `path.normalize()`
  58. `'.//..'` ---> `'..'` // equal to `path.normalize()`
  59. `'./../'` ---> `'../'` // equal to `path.normalize()`
  60. `'.\\..\\'` ---> `'../'` // `path.normalize()` gives `'.\..\'`
  61. `'./../dep'` ---> `'../dep'` // equal to `path.normalize()`
  62. `'../dep'` ---> `'../dep'` // equal to `path.normalize()`
  63. `'../path/dep'` ---> `'../path/dep'` // equal to `path.normalize()`
  64. `'../path/../dep'` ---> `'../dep'` // equal to `path.normalize()`
  65. `'dep'` ---> `'dep'` // equal to `path.normalize()`
  66. `'path//dep'` ---> `'path/dep'` // equal to `path.normalize()`
  67. `'./dep'` ---> `'./dep'` // `path.normalize()` gives `'dep'`
  68. `'./path/dep'` ---> `'./path/dep'` // `path.normalize()` gives `'path/dep'`
  69. `'./path/../dep'` ---> `'./dep'` // `path.normalize()` gives `'dep'`
  70. `'.//windows\\unix/mixed/'` ---> `'./windows/unix/mixed/'` // `path.normalize()` gives `'windows\unix/mixed/'`
  71. `'..//windows\\unix/mixed'` ---> `'../windows/unix/mixed'` // `path.normalize()` gives `'../windows\unix/mixed'`
  72. `'windows\\unix/mixed/'` ---> `'windows/unix/mixed/'` // `path.normalize()` gives `'windows\unix/mixed/'`
  73. `'..//windows\\..\\unix/mixed'` ---> `'../unix/mixed'` // `path.normalize()` gives `'../windows\..\unix/mixed'`
  74. #### `upath.normalizeTrim(path)`
  75. Exactly like `path.normalizeSafe(path)`, but it trims any useless ending `/`.
  76. ##### Examples / specs
  77. `upath.normalizeTrim(path)` --returns-->
  78. `'./'` ---> `'.'` // `upath.normalizeSafe()` gives `'./'`
  79. `'./../'` ---> `'..'` // `upath.normalizeSafe()` gives `'../'`
  80. `'./../dep/'` ---> `'../dep'` // `upath.normalizeSafe()` gives `'../dep/'`
  81. `'path//dep\\'` ---> `'path/dep'` // `upath.normalizeSafe()` gives `'path/dep/'`
  82. `'.//windows\\unix/mixed/'` ---> `'./windows/unix/mixed'` // `upath.normalizeSafe()` gives `'./windows/unix/mixed/'`
  83. #### `upath.joinSafe([path1][, path2][, ...])`
  84. Exactly like `path.join()`, but it keeps the first meaningful `./`.
  85. Note that the unix `/` is returned everywhere, so windows `\` is always converted to unix `/`.
  86. ##### Examples / specs & how it differs from vanilla `path`
  87. `upath.joinSafe(path)` --returns-->
  88. `'some/nodejs/deep', '../path'` ---> `'some/nodejs/path'` // equal to `path.join()`
  89. `'./some/local/unix/', '../path'` ---> `'./some/local/path'` // `path.join()` gives `'some/local/path'`
  90. `'./some\\current\\mixed', '..\\path'` ---> `'./some/current/path'` // `path.join()` gives `'some\current\mixed/..\path'`
  91. `'../some/relative/destination', '..\\path'` ---> `'../some/relative/path'` // `path.join()` gives `'../some/relative/destination/..\path'`
  92. ## Added functions for *filename extension* manipulation.
  93. **Happy notes:**
  94. In all functions you can:
  95. * use both `.ext` & `ext` - the dot `.` on the extension is always adjusted correctly.
  96. * omit the `ext` param (pass null/undefined/empty string) and the common sense thing will happen.
  97. * ignore specific extensions from being considered as valid ones (eg `.min`, `.dev` `.aLongExtIsNotAnExt` etc), hence no trimming or replacement takes place on them.
  98. #### `upath.addExt(filename, [ext])`
  99. Adds `.ext` to `filename`, but only if it doesn't already have the exact extension.
  100. ##### Examples / specs
  101. `upath.addExt(filename, 'js')` --returns-->
  102. `'myfile/addExt'` ---> `'myfile/addExt.js'`
  103. `'myfile/addExt.txt'` ---> `'myfile/addExt.txt.js'`
  104. `'myfile/addExt.js'` ---> `'myfile/addExt.js'`
  105. `'myfile/addExt.min.'` ---> `'myfile/addExt.min..js'`
  106. It adds nothing if no `ext` param is passed.
  107. `upath.addExt(filename)` --returns-->
  108. `'myfile/addExt'` ---> `'myfile/addExt'`
  109. `'myfile/addExt.txt'` ---> `'myfile/addExt.txt'`
  110. `'myfile/addExt.js'` ---> `'myfile/addExt.js'`
  111. `'myfile/addExt.min.'` ---> `'myfile/addExt.min.'`
  112. #### `upath.trimExt(filename, [ignoreExts], [maxSize=7])`
  113. Trims a filename's extension.
  114. * Extensions are considered to be up to `maxSize` chars long, counting the dot (defaults to 7).
  115. * An `Array` of `ignoreExts` (eg `['.min']`) prevents these from being considered as extension, thus are not trimmed.
  116. ##### Examples / specs
  117. `upath.trimExt(filename)` --returns-->
  118. `'my/trimedExt.txt'` ---> `'my/trimedExt'`
  119. `'my/trimedExt'` ---> `'my/trimedExt'`
  120. `'my/trimedExt.min'` ---> `'my/trimedExt'`
  121. `'my/trimedExt.min.js'` ---> `'my/trimedExt.min'`
  122. `'../my/trimedExt.longExt'` ---> `'../my/trimedExt.longExt'`
  123. It is ignoring `.min` & `.dev` as extensions, and considers exts with up to 8 chars.
  124. `upath.trimExt(filename, ['min', '.dev'], 8)` --returns-->
  125. `'my/trimedExt.txt'` ---> `'my/trimedExt'`
  126. `'my/trimedExt.min'` ---> `'my/trimedExt.min'`
  127. `'my/trimedExt.dev'` ---> `'my/trimedExt.dev'`
  128. `'../my/trimedExt.longExt'` ---> `'../my/trimedExt'`
  129. `'../my/trimedExt.longRExt'` ---> `'../my/trimedExt.longRExt'`
  130. #### `upath.removeExt(filename, ext)`
  131. Removes the specific `ext` extension from filename, if it has it. Otherwise it leaves it as is.
  132. As in all upath functions, it be `.ext` or `ext`.
  133. ##### Examples / specs
  134. `upath.removeExt(filename, '.js')` --returns-->
  135. `'removedExt.js'` ---> `'removedExt'`
  136. `'removedExt.txt.js'` ---> `'removedExt.txt'`
  137. `'notRemoved.txt'` ---> `'notRemoved.txt'`
  138. It does not care about the length of exts.
  139. `upath.removeExt(filename, '.longExt')` --returns-->
  140. `'removedExt.longExt'` ---> `'removedExt'`
  141. `'removedExt.txt.longExt'` ---> `'removedExt.txt'`
  142. `'notRemoved.txt'` ---> `'notRemoved.txt'`
  143. #### `upath.changeExt(filename, [ext], [ignoreExts], [maxSize=7])`
  144. Changes a filename's extension to `ext`. If it has no (valid) extension, it adds it.
  145. * Valid extensions are considered to be up to `maxSize` chars long, counting the dot (defaults to 7).
  146. * An `Array` of `ignoreExts` (eg `['.min']`) prevents these from being considered as extension, thus are not changed - the new extension is added instead.
  147. ##### Examples / specs
  148. `upath.changeExt(filename, '.js')` --returns-->
  149. `'my/module.min'` ---> `'my/module.js'`
  150. `'my/module.coffee'` ---> `'my/module.js'`
  151. `'my/module'` ---> `'my/module.js'`
  152. `'file/withDot.'` ---> `'file/withDot.js'`
  153. `'file/change.longExt'` ---> `'file/change.longExt.js'`
  154. If no `ext` param is given, it trims the current extension (if any).
  155. `upath.changeExt(filename)` --returns-->
  156. `'my/module.min'` ---> `'my/module'`
  157. `'my/module.coffee'` ---> `'my/module'`
  158. `'my/module'` ---> `'my/module'`
  159. `'file/withDot.'` ---> `'file/withDot'`
  160. `'file/change.longExt'` ---> `'file/change.longExt'`
  161. It is ignoring `.min` & `.dev` as extensions, and considers exts with up to 8 chars.
  162. `upath.changeExt(filename, 'js', ['min', '.dev'], 8)` --returns-->
  163. `'my/module.coffee'` ---> `'my/module.js'`
  164. `'file/notValidExt.min'` ---> `'file/notValidExt.min.js'`
  165. `'file/notValidExt.dev'` ---> `'file/notValidExt.dev.js'`
  166. `'file/change.longExt'` ---> `'file/change.js'`
  167. `'file/change.longRExt'` ---> `'file/change.longRExt.js'`
  168. #### `upath.defaultExt(filename, [ext], [ignoreExts], [maxSize=7])`
  169. Adds `.ext` to `filename`, only if it doesn't already have _any_ *old* extension.
  170. * (Old) extensions are considered to be up to `maxSize` chars long, counting the dot (defaults to 7).
  171. * An `Array` of `ignoreExts` (eg `['.min']`) will force adding default `.ext` even if one of these is present.
  172. ##### Examples / specs
  173. `upath.defaultExt(filename, 'js')` --returns-->
  174. `'fileWith/defaultExt'` ---> `'fileWith/defaultExt.js'`
  175. `'fileWith/defaultExt.js'` ---> `'fileWith/defaultExt.js'`
  176. `'fileWith/defaultExt.min'` ---> `'fileWith/defaultExt.min'`
  177. `'fileWith/defaultExt.longExt'` ---> `'fileWith/defaultExt.longExt.js'`
  178. If no `ext` param is passed, it leaves filename intact.
  179. `upath.defaultExt(filename)` --returns-->
  180. `'fileWith/defaultExt'` ---> `'fileWith/defaultExt'`
  181. `'fileWith/defaultExt.js'` ---> `'fileWith/defaultExt.js'`
  182. `'fileWith/defaultExt.min'` ---> `'fileWith/defaultExt.min'`
  183. `'fileWith/defaultExt.longExt'` ---> `'fileWith/defaultExt.longExt'`
  184. It is ignoring `.min` & `.dev` as extensions, and considers exts with up to 8 chars.
  185. `upath.defaultExt(filename, 'js', ['min', '.dev'], 8)` --returns-->
  186. `'fileWith/defaultExt'` ---> `'fileWith/defaultExt.js'`
  187. `'fileWith/defaultExt.min'` ---> `'fileWith/defaultExt.min.js'`
  188. `'fileWith/defaultExt.dev'` ---> `'fileWith/defaultExt.dev.js'`
  189. `'fileWith/defaultExt.longExt'` ---> `'fileWith/defaultExt.longExt'`
  190. `'fileWith/defaultExt.longRext'` ---> `'fileWith/defaultExt.longRext.js'`
  191. Copyright(c) 2014-2019 Angelos Pikoulas (agelos.pikoulas@gmail.com)
  192. Permission is hereby granted, free of charge, to any person
  193. obtaining a copy of this software and associated documentation
  194. files (the "Software"), to deal in the Software without
  195. restriction, including without limitation the rights to use,
  196. copy, modify, merge, publish, distribute, sublicense, and/or sell
  197. copies of the Software, and to permit persons to whom the
  198. Software is furnished to do so, subject to the following
  199. conditions:
  200. The above copyright notice and this permission notice shall be
  201. included in all copies or substantial portions of the Software.
  202. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  203. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  204. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  205. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  206. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  207. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  208. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  209. OTHER DEALINGS IN THE SOFTWARE.