選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

index.js 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. // .dirname, .basename, and .extname methods are extracted from Node.js v8.11.1,
  2. // backported and transplited with Babel, with backwards-compat fixes
  3. // Copyright Joyent, Inc. and other Node contributors.
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a
  6. // copy of this software and associated documentation files (the
  7. // "Software"), to deal in the Software without restriction, including
  8. // without limitation the rights to use, copy, modify, merge, publish,
  9. // distribute, sublicense, and/or sell copies of the Software, and to permit
  10. // persons to whom the Software is furnished to do so, subject to the
  11. // following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included
  14. // in all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  19. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  20. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  21. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  22. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. // resolves . and .. elements in a path array with directory names there
  24. // must be no slashes, empty elements, or device names (c:\) in the array
  25. // (so also no leading and trailing slashes - it does not distinguish
  26. // relative and absolute paths)
  27. function normalizeArray(parts, allowAboveRoot) {
  28. // if the path tries to go above the root, `up` ends up > 0
  29. var up = 0;
  30. for (var i = parts.length - 1; i >= 0; i--) {
  31. var last = parts[i];
  32. if (last === '.') {
  33. parts.splice(i, 1);
  34. } else if (last === '..') {
  35. parts.splice(i, 1);
  36. up++;
  37. } else if (up) {
  38. parts.splice(i, 1);
  39. up--;
  40. }
  41. }
  42. // if the path is allowed to go above the root, restore leading ..s
  43. if (allowAboveRoot) {
  44. for (; up--; up) {
  45. parts.unshift('..');
  46. }
  47. }
  48. return parts;
  49. }
  50. // path.resolve([from ...], to)
  51. // posix version
  52. exports.resolve = function() {
  53. var resolvedPath = '',
  54. resolvedAbsolute = false;
  55. for (var i = arguments.length - 1; i >= -1 && !resolvedAbsolute; i--) {
  56. var path = (i >= 0) ? arguments[i] : process.cwd();
  57. // Skip empty and invalid entries
  58. if (typeof path !== 'string') {
  59. throw new TypeError('Arguments to path.resolve must be strings');
  60. } else if (!path) {
  61. continue;
  62. }
  63. resolvedPath = path + '/' + resolvedPath;
  64. resolvedAbsolute = path.charAt(0) === '/';
  65. }
  66. // At this point the path should be resolved to a full absolute path, but
  67. // handle relative paths to be safe (might happen when process.cwd() fails)
  68. // Normalize the path
  69. resolvedPath = normalizeArray(filter(resolvedPath.split('/'), function(p) {
  70. return !!p;
  71. }), !resolvedAbsolute).join('/');
  72. return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.';
  73. };
  74. // path.normalize(path)
  75. // posix version
  76. exports.normalize = function(path) {
  77. var isAbsolute = exports.isAbsolute(path),
  78. trailingSlash = substr(path, -1) === '/';
  79. // Normalize the path
  80. path = normalizeArray(filter(path.split('/'), function(p) {
  81. return !!p;
  82. }), !isAbsolute).join('/');
  83. if (!path && !isAbsolute) {
  84. path = '.';
  85. }
  86. if (path && trailingSlash) {
  87. path += '/';
  88. }
  89. return (isAbsolute ? '/' : '') + path;
  90. };
  91. // posix version
  92. exports.isAbsolute = function(path) {
  93. return path.charAt(0) === '/';
  94. };
  95. // posix version
  96. exports.join = function() {
  97. var paths = Array.prototype.slice.call(arguments, 0);
  98. return exports.normalize(filter(paths, function(p, index) {
  99. if (typeof p !== 'string') {
  100. throw new TypeError('Arguments to path.join must be strings');
  101. }
  102. return p;
  103. }).join('/'));
  104. };
  105. // path.relative(from, to)
  106. // posix version
  107. exports.relative = function(from, to) {
  108. from = exports.resolve(from).substr(1);
  109. to = exports.resolve(to).substr(1);
  110. function trim(arr) {
  111. var start = 0;
  112. for (; start < arr.length; start++) {
  113. if (arr[start] !== '') break;
  114. }
  115. var end = arr.length - 1;
  116. for (; end >= 0; end--) {
  117. if (arr[end] !== '') break;
  118. }
  119. if (start > end) return [];
  120. return arr.slice(start, end - start + 1);
  121. }
  122. var fromParts = trim(from.split('/'));
  123. var toParts = trim(to.split('/'));
  124. var length = Math.min(fromParts.length, toParts.length);
  125. var samePartsLength = length;
  126. for (var i = 0; i < length; i++) {
  127. if (fromParts[i] !== toParts[i]) {
  128. samePartsLength = i;
  129. break;
  130. }
  131. }
  132. var outputParts = [];
  133. for (var i = samePartsLength; i < fromParts.length; i++) {
  134. outputParts.push('..');
  135. }
  136. outputParts = outputParts.concat(toParts.slice(samePartsLength));
  137. return outputParts.join('/');
  138. };
  139. exports.sep = '/';
  140. exports.delimiter = ':';
  141. exports.dirname = function (path) {
  142. if (typeof path !== 'string') path = path + '';
  143. if (path.length === 0) return '.';
  144. var code = path.charCodeAt(0);
  145. var hasRoot = code === 47 /*/*/;
  146. var end = -1;
  147. var matchedSlash = true;
  148. for (var i = path.length - 1; i >= 1; --i) {
  149. code = path.charCodeAt(i);
  150. if (code === 47 /*/*/) {
  151. if (!matchedSlash) {
  152. end = i;
  153. break;
  154. }
  155. } else {
  156. // We saw the first non-path separator
  157. matchedSlash = false;
  158. }
  159. }
  160. if (end === -1) return hasRoot ? '/' : '.';
  161. if (hasRoot && end === 1) {
  162. // return '//';
  163. // Backwards-compat fix:
  164. return '/';
  165. }
  166. return path.slice(0, end);
  167. };
  168. function basename(path) {
  169. if (typeof path !== 'string') path = path + '';
  170. var start = 0;
  171. var end = -1;
  172. var matchedSlash = true;
  173. var i;
  174. for (i = path.length - 1; i >= 0; --i) {
  175. if (path.charCodeAt(i) === 47 /*/*/) {
  176. // If we reached a path separator that was not part of a set of path
  177. // separators at the end of the string, stop now
  178. if (!matchedSlash) {
  179. start = i + 1;
  180. break;
  181. }
  182. } else if (end === -1) {
  183. // We saw the first non-path separator, mark this as the end of our
  184. // path component
  185. matchedSlash = false;
  186. end = i + 1;
  187. }
  188. }
  189. if (end === -1) return '';
  190. return path.slice(start, end);
  191. }
  192. // Uses a mixed approach for backwards-compatibility, as ext behavior changed
  193. // in new Node.js versions, so only basename() above is backported here
  194. exports.basename = function (path, ext) {
  195. var f = basename(path);
  196. if (ext && f.substr(-1 * ext.length) === ext) {
  197. f = f.substr(0, f.length - ext.length);
  198. }
  199. return f;
  200. };
  201. exports.extname = function (path) {
  202. if (typeof path !== 'string') path = path + '';
  203. var startDot = -1;
  204. var startPart = 0;
  205. var end = -1;
  206. var matchedSlash = true;
  207. // Track the state of characters (if any) we see before our first dot and
  208. // after any path separator we find
  209. var preDotState = 0;
  210. for (var i = path.length - 1; i >= 0; --i) {
  211. var code = path.charCodeAt(i);
  212. if (code === 47 /*/*/) {
  213. // If we reached a path separator that was not part of a set of path
  214. // separators at the end of the string, stop now
  215. if (!matchedSlash) {
  216. startPart = i + 1;
  217. break;
  218. }
  219. continue;
  220. }
  221. if (end === -1) {
  222. // We saw the first non-path separator, mark this as the end of our
  223. // extension
  224. matchedSlash = false;
  225. end = i + 1;
  226. }
  227. if (code === 46 /*.*/) {
  228. // If this is our first dot, mark it as the start of our extension
  229. if (startDot === -1)
  230. startDot = i;
  231. else if (preDotState !== 1)
  232. preDotState = 1;
  233. } else if (startDot !== -1) {
  234. // We saw a non-dot and non-path separator before our dot, so we should
  235. // have a good chance at having a non-empty extension
  236. preDotState = -1;
  237. }
  238. }
  239. if (startDot === -1 || end === -1 ||
  240. // We saw a non-dot character immediately before the dot
  241. preDotState === 0 ||
  242. // The (right-most) trimmed path component is exactly '..'
  243. preDotState === 1 && startDot === end - 1 && startDot === startPart + 1) {
  244. return '';
  245. }
  246. return path.slice(startDot, end);
  247. };
  248. function filter (xs, f) {
  249. if (xs.filter) return xs.filter(f);
  250. var res = [];
  251. for (var i = 0; i < xs.length; i++) {
  252. if (f(xs[i], i, xs)) res.push(xs[i]);
  253. }
  254. return res;
  255. }
  256. // String.prototype.substr - negative index don't work in IE8
  257. var substr = 'ab'.substr(-1) === 'b'
  258. ? function (str, start, len) { return str.substr(start, len) }
  259. : function (str, start, len) {
  260. if (start < 0) start = str.length + start;
  261. return str.substr(start, len);
  262. }
  263. ;