path.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. Copyright 2012-2015, Yahoo Inc.
  3. Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
  4. */
  5. 'use strict';
  6. const path = require('path');
  7. let parsePath = path.parse;
  8. let SEP = path.sep || /* istanbul ignore next */ '/';
  9. const origParser = parsePath;
  10. const origSep = SEP;
  11. function makeRelativeNormalizedPath(str, sep) {
  12. const parsed = parsePath(str);
  13. let root = parsed.root;
  14. let dir;
  15. let file = parsed.base;
  16. let quoted;
  17. let pos;
  18. // handle a weird windows case separately
  19. if (sep === '\\') {
  20. pos = root.indexOf(':\\');
  21. if (pos >= 0) {
  22. root = root.substring(0, pos + 2);
  23. }
  24. }
  25. dir = parsed.dir.substring(root.length);
  26. if (str === '') {
  27. return [];
  28. }
  29. if (sep !== '/') {
  30. quoted = new RegExp(sep.replace(/\W/g, '\\$&'), 'g');
  31. dir = dir.replace(quoted, '/');
  32. file = file.replace(quoted, '/'); // excessively paranoid?
  33. }
  34. if (dir !== '') {
  35. dir = dir + '/' + file;
  36. } else {
  37. dir = file;
  38. }
  39. if (dir.substring(0, 1) === '/') {
  40. dir = dir.substring(1);
  41. }
  42. dir = dir.split(/\/+/);
  43. return dir;
  44. }
  45. function Path(strOrArray) {
  46. if (Array.isArray(strOrArray)) {
  47. this.v = strOrArray;
  48. } else if (typeof strOrArray === 'string') {
  49. this.v = makeRelativeNormalizedPath(strOrArray, SEP);
  50. } else {
  51. throw new Error(
  52. 'Invalid Path argument must be string or array:' + strOrArray
  53. );
  54. }
  55. }
  56. Path.prototype.toString = function() {
  57. return this.v.join('/');
  58. };
  59. Path.prototype.hasParent = function() {
  60. return this.v.length > 0;
  61. };
  62. Path.prototype.parent = function() {
  63. if (!this.hasParent()) {
  64. throw new Error('Unable to get parent for 0 elem path');
  65. }
  66. const p = this.v.slice();
  67. p.pop();
  68. return new Path(p);
  69. };
  70. Path.prototype.elements = function() {
  71. return this.v.slice();
  72. };
  73. Path.prototype.contains = function(other) {
  74. let i;
  75. if (other.length > this.length) {
  76. return false;
  77. }
  78. for (i = 0; i < other.length; i += 1) {
  79. if (this.v[i] !== other.v[i]) {
  80. return false;
  81. }
  82. }
  83. return true;
  84. };
  85. Path.prototype.ancestorOf = function(other) {
  86. return other.contains(this) && other.length !== this.length;
  87. };
  88. Path.prototype.descendantOf = function(other) {
  89. return this.contains(other) && other.length !== this.length;
  90. };
  91. Path.prototype.commonPrefixPath = function(other) {
  92. const len = this.length > other.length ? other.length : this.length;
  93. let i;
  94. const ret = [];
  95. for (i = 0; i < len; i += 1) {
  96. if (this.v[i] === other.v[i]) {
  97. ret.push(this.v[i]);
  98. } else {
  99. break;
  100. }
  101. }
  102. return new Path(ret);
  103. };
  104. ['push', 'pop', 'shift', 'unshift', 'splice'].forEach(f => {
  105. Path.prototype[f] = function(...args) {
  106. const v = this.v;
  107. return v[f](...args);
  108. };
  109. });
  110. Path.compare = function(a, b) {
  111. const al = a.length;
  112. const bl = b.length;
  113. if (al < bl) {
  114. return -1;
  115. }
  116. if (al > bl) {
  117. return 1;
  118. }
  119. const astr = a.toString();
  120. const bstr = b.toString();
  121. return astr < bstr ? -1 : astr > bstr ? 1 : 0;
  122. };
  123. Object.defineProperty(Path.prototype, 'length', {
  124. enumerable: true,
  125. get() {
  126. return this.v.length;
  127. }
  128. });
  129. module.exports = Path;
  130. Path.tester = {
  131. setParserAndSep(p, sep) {
  132. parsePath = p;
  133. SEP = sep;
  134. },
  135. reset() {
  136. parsePath = origParser;
  137. SEP = origSep;
  138. }
  139. };