upath.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * upath http://github.com/anodynos/upath/
  3. *
  4. * A proxy to `path`, replacing `\` with `/` for all results (supports UNC paths) & new methods to normalize & join keeping leading `./` and add, change, default, trim file extensions.
  5. * Version 2.0.1 - Compiled on 2020-11-07 16:59:47
  6. * Repository git://github.com/anodynos/upath
  7. * Copyright(c) 2020 Angelos Pikoulas <agelos.pikoulas@gmail.com>
  8. * License MIT
  9. */
  10. // Generated by uRequire v0.7.0-beta.33 target: 'lib' template: 'nodejs'
  11. var VERSION = '2.0.1'; // injected by urequire-rc-inject-version
  12. var extraFn, extraFunctions, isFunction, isString, isValidExt, name, path, propName, propValue, toUnix, upath, slice = [].slice, indexOf = [].indexOf || function (item) {
  13. for (var i = 0, l = this.length; i < l; i++) {
  14. if (i in this && this[i] === item)
  15. return i;
  16. }
  17. return -1;
  18. }, hasProp = {}.hasOwnProperty;
  19. path = require("path");
  20. isFunction = function (val) {
  21. return typeof val === "function";
  22. };
  23. isString = function (val) {
  24. return typeof val === "string" || !!val && typeof val === "object" && Object.prototype.toString.call(val) === "[object String]";
  25. };
  26. upath = exports;
  27. upath.VERSION = typeof VERSION !== "undefined" && VERSION !== null ? VERSION : "NO-VERSION";
  28. toUnix = function (p) {
  29. p = p.replace(/\\/g, "/");
  30. p = p.replace(/(?<!^)\/+/g, "/");
  31. return p;
  32. };
  33. for (propName in path) {
  34. propValue = path[propName];
  35. if (isFunction(propValue)) {
  36. upath[propName] = function (propName) {
  37. return function () {
  38. var args, result;
  39. args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
  40. args = args.map(function (p) {
  41. if (isString(p)) {
  42. return toUnix(p);
  43. } else {
  44. return p;
  45. }
  46. });
  47. result = path[propName].apply(path, args);
  48. if (isString(result)) {
  49. return toUnix(result);
  50. } else {
  51. return result;
  52. }
  53. };
  54. }(propName);
  55. } else {
  56. upath[propName] = propValue;
  57. }
  58. }
  59. upath.sep = "/";
  60. extraFunctions = {
  61. toUnix: toUnix,
  62. normalizeSafe: function (p) {
  63. var result;
  64. p = toUnix(p);
  65. result = upath.normalize(p);
  66. if (p.startsWith("./") && !result.startsWith("./") && !result.startsWith("..")) {
  67. result = "./" + result;
  68. } else if (p.startsWith("//") && !result.startsWith("//")) {
  69. if (p.startsWith("//./")) {
  70. result = "//." + result;
  71. } else {
  72. result = "/" + result;
  73. }
  74. }
  75. return result;
  76. },
  77. normalizeTrim: function (p) {
  78. p = upath.normalizeSafe(p);
  79. if (p.endsWith("/")) {
  80. return p.slice(0, +(p.length - 2) + 1 || 9000000000);
  81. } else {
  82. return p;
  83. }
  84. },
  85. joinSafe: function () {
  86. var p, p0, result;
  87. p = 1 <= arguments.length ? slice.call(arguments, 0) : [];
  88. result = upath.join.apply(null, p);
  89. if (p.length > 0) {
  90. p0 = toUnix(p[0]);
  91. if (p0.startsWith("./") && !result.startsWith("./") && !result.startsWith("..")) {
  92. result = "./" + result;
  93. } else if (p0.startsWith("//") && !result.startsWith("//")) {
  94. if (p0.startsWith("//./")) {
  95. result = "//." + result;
  96. } else {
  97. result = "/" + result;
  98. }
  99. }
  100. }
  101. return result;
  102. },
  103. addExt: function (file, ext) {
  104. if (!ext) {
  105. return file;
  106. } else {
  107. if (ext[0] !== ".") {
  108. ext = "." + ext;
  109. }
  110. return file + (file.endsWith(ext) ? "" : ext);
  111. }
  112. },
  113. trimExt: function (filename, ignoreExts, maxSize) {
  114. var oldExt;
  115. if (maxSize == null) {
  116. maxSize = 7;
  117. }
  118. oldExt = upath.extname(filename);
  119. if (isValidExt(oldExt, ignoreExts, maxSize)) {
  120. return filename.slice(0, +(filename.length - oldExt.length - 1) + 1 || 9000000000);
  121. } else {
  122. return filename;
  123. }
  124. },
  125. removeExt: function (filename, ext) {
  126. if (!ext) {
  127. return filename;
  128. } else {
  129. ext = ext[0] === "." ? ext : "." + ext;
  130. if (upath.extname(filename) === ext) {
  131. return upath.trimExt(filename, [], ext.length);
  132. } else {
  133. return filename;
  134. }
  135. }
  136. },
  137. changeExt: function (filename, ext, ignoreExts, maxSize) {
  138. if (maxSize == null) {
  139. maxSize = 7;
  140. }
  141. return upath.trimExt(filename, ignoreExts, maxSize) + (!ext ? "" : ext[0] === "." ? ext : "." + ext);
  142. },
  143. defaultExt: function (filename, ext, ignoreExts, maxSize) {
  144. var oldExt;
  145. if (maxSize == null) {
  146. maxSize = 7;
  147. }
  148. oldExt = upath.extname(filename);
  149. if (isValidExt(oldExt, ignoreExts, maxSize)) {
  150. return filename;
  151. } else {
  152. return upath.addExt(filename, ext);
  153. }
  154. }
  155. };
  156. isValidExt = function (ext, ignoreExts, maxSize) {
  157. if (ignoreExts == null) {
  158. ignoreExts = [];
  159. }
  160. return ext && ext.length <= maxSize && indexOf.call(ignoreExts.map(function (e) {
  161. return (e && e[0] !== "." ? "." : "") + e;
  162. }), ext) < 0;
  163. };
  164. for (name in extraFunctions) {
  165. if (!hasProp.call(extraFunctions, name))
  166. continue;
  167. extraFn = extraFunctions[name];
  168. if (upath[name] !== void 0) {
  169. throw new Error("path." + name + " already exists.");
  170. } else {
  171. upath[name] = extraFn;
  172. }
  173. }
  174. ;