path.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. var path = require('path'),
  7. parsePath = require('path-parse'),
  8. SEP = path.sep || /* istanbul ignore next */ '/',
  9. origParser = parsePath,
  10. origSep = SEP;
  11. function makeRelativeNormalizedPath(str, sep) {
  12. var parsed = parsePath(str),
  13. root = parsed.root,
  14. dir,
  15. file = parsed.base,
  16. quoted,
  17. 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('Invalid Path argument must be string or array:' + strOrArray);
  52. }
  53. }
  54. Path.prototype.toString = function () {
  55. return this.v.join('/');
  56. };
  57. Path.prototype.hasParent = function () {
  58. return this.v.length > 0;
  59. };
  60. Path.prototype.parent = function () {
  61. if (!this.hasParent()) {
  62. throw new Error('Unable to get parent for 0 elem path');
  63. }
  64. var p = this.v.slice();
  65. p.pop();
  66. return new Path(p);
  67. };
  68. Path.prototype.elements = function () {
  69. return this.v.slice();
  70. };
  71. Path.prototype.contains = function (other) {
  72. var i;
  73. if (other.length > this.length) {
  74. return false;
  75. }
  76. for (i = 0; i < other.length; i += 1) {
  77. if (this.v[i] !== other.v[i]) {
  78. return false;
  79. }
  80. }
  81. return true;
  82. };
  83. Path.prototype.ancestorOf = function (other) {
  84. return other.contains(this) && other.length !== this.length;
  85. };
  86. Path.prototype.descendantOf = function (other) {
  87. return this.contains(other) && other.length !== this.length;
  88. };
  89. Path.prototype.commonPrefixPath = function (other) {
  90. var len = this.length > other.length ? other.length : this.length,
  91. i,
  92. ret = [];
  93. for (i = 0; i < len; i +=1 ) {
  94. if (this.v[i] === other.v[i]) {
  95. ret.push(this.v[i]);
  96. } else {
  97. break;
  98. }
  99. }
  100. return new Path(ret);
  101. };
  102. ['push', 'pop', 'shift', 'unshift', 'splice'].forEach(function (f) {
  103. Path.prototype[f] = function () {
  104. var args = Array.prototype.slice.call(arguments),
  105. v = this.v;
  106. return v[f].apply(v, args);
  107. };
  108. });
  109. Path.compare = function (a, b) {
  110. var al = a.length,
  111. bl = b.length,
  112. astr,
  113. bstr;
  114. if (al < bl) {
  115. return -1;
  116. }
  117. if (al > bl) {
  118. return 1;
  119. }
  120. astr = a.toString();
  121. bstr = b.toString();
  122. return astr < bstr ? -1 : astr > bstr ? 1 : 0;
  123. };
  124. Object.defineProperty(Path.prototype, 'length', {
  125. enumerable: true,
  126. get: function () {
  127. return this.v.length;
  128. }
  129. });
  130. module.exports = Path;
  131. Path.tester = {
  132. setParserAndSep: function (p, sep) {
  133. parsePath = p;
  134. SEP = sep;
  135. },
  136. reset: function () {
  137. parsePath = origParser;
  138. SEP = origSep;
  139. }
  140. };