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.

upath.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * upath http://github.com/anodynos/upath/
  3. *
  4. * A proxy to `path`, replacing `\` with `/` for all results & new methods to normalize & join keeping leading `./` and add, change, default, trim file extensions.
  5. * Version 1.2.0 - Compiled on 2019-09-02 23:33:57
  6. * Repository git://github.com/anodynos/upath
  7. * Copyright(c) 2019 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 = '1.2.0'; // 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 val instanceof 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. var double;
  30. p = p.replace(/\\/g, "/");
  31. double = /\/\//;
  32. while (p.match(double)) {
  33. p = p.replace(double, "/");
  34. }
  35. return p;
  36. };
  37. for (propName in path) {
  38. propValue = path[propName];
  39. if (isFunction(propValue)) {
  40. upath[propName] = function (propName) {
  41. return function () {
  42. var args, result;
  43. args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
  44. args = args.map(function (p) {
  45. if (isString(p)) {
  46. return toUnix(p);
  47. } else {
  48. return p;
  49. }
  50. });
  51. result = path[propName].apply(path, args);
  52. if (isString(result)) {
  53. return toUnix(result);
  54. } else {
  55. return result;
  56. }
  57. };
  58. }(propName);
  59. } else {
  60. upath[propName] = propValue;
  61. }
  62. }
  63. upath.sep = "/";
  64. extraFunctions = {
  65. toUnix: toUnix,
  66. normalizeSafe: function (p) {
  67. p = toUnix(p);
  68. if (p.startsWith("./")) {
  69. if (p.startsWith("./..") || p === "./") {
  70. return upath.normalize(p);
  71. } else {
  72. return "./" + upath.normalize(p);
  73. }
  74. } else {
  75. return upath.normalize(p);
  76. }
  77. },
  78. normalizeTrim: function (p) {
  79. p = upath.normalizeSafe(p);
  80. if (p.endsWith("/")) {
  81. return p.slice(0, +(p.length - 2) + 1 || 9000000000);
  82. } else {
  83. return p;
  84. }
  85. },
  86. joinSafe: function () {
  87. var p, result;
  88. p = 1 <= arguments.length ? slice.call(arguments, 0) : [];
  89. result = upath.join.apply(null, p);
  90. if (p[0].startsWith("./") && !result.startsWith("./")) {
  91. result = "./" + result;
  92. }
  93. return result;
  94. },
  95. addExt: function (file, ext) {
  96. if (!ext) {
  97. return file;
  98. } else {
  99. if (ext[0] !== ".") {
  100. ext = "." + ext;
  101. }
  102. return file + (file.endsWith(ext) ? "" : ext);
  103. }
  104. },
  105. trimExt: function (filename, ignoreExts, maxSize) {
  106. var oldExt;
  107. if (maxSize == null) {
  108. maxSize = 7;
  109. }
  110. oldExt = upath.extname(filename);
  111. if (isValidExt(oldExt, ignoreExts, maxSize)) {
  112. return filename.slice(0, +(filename.length - oldExt.length - 1) + 1 || 9000000000);
  113. } else {
  114. return filename;
  115. }
  116. },
  117. removeExt: function (filename, ext) {
  118. if (!ext) {
  119. return filename;
  120. } else {
  121. ext = ext[0] === "." ? ext : "." + ext;
  122. if (upath.extname(filename) === ext) {
  123. return upath.trimExt(filename);
  124. } else {
  125. return filename;
  126. }
  127. }
  128. },
  129. changeExt: function (filename, ext, ignoreExts, maxSize) {
  130. if (maxSize == null) {
  131. maxSize = 7;
  132. }
  133. return upath.trimExt(filename, ignoreExts, maxSize) + (!ext ? "" : ext[0] === "." ? ext : "." + ext);
  134. },
  135. defaultExt: function (filename, ext, ignoreExts, maxSize) {
  136. var oldExt;
  137. if (maxSize == null) {
  138. maxSize = 7;
  139. }
  140. oldExt = upath.extname(filename);
  141. if (isValidExt(oldExt, ignoreExts, maxSize)) {
  142. return filename;
  143. } else {
  144. return upath.addExt(filename, ext);
  145. }
  146. }
  147. };
  148. isValidExt = function (ext, ignoreExts, maxSize) {
  149. if (ignoreExts == null) {
  150. ignoreExts = [];
  151. }
  152. return ext && ext.length <= maxSize && indexOf.call(ignoreExts.map(function (e) {
  153. return (e && e[0] !== "." ? "." : "") + e;
  154. }), ext) < 0;
  155. };
  156. for (name in extraFunctions) {
  157. if (!hasProp.call(extraFunctions, name))
  158. continue;
  159. extraFn = extraFunctions[name];
  160. if (upath[name] !== void 0) {
  161. throw new Error("path." + name + " already exists.");
  162. } else {
  163. upath[name] = extraFn;
  164. }
  165. }
  166. ;