Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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.

path.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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;
  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. class Path {
  46. constructor(strOrArray) {
  47. if (Array.isArray(strOrArray)) {
  48. this.v = strOrArray;
  49. } else if (typeof strOrArray === 'string') {
  50. this.v = makeRelativeNormalizedPath(strOrArray, SEP);
  51. } else {
  52. throw new Error(
  53. `Invalid Path argument must be string or array:${strOrArray}`
  54. );
  55. }
  56. }
  57. toString() {
  58. return this.v.join('/');
  59. }
  60. hasParent() {
  61. return this.v.length > 0;
  62. }
  63. parent() {
  64. if (!this.hasParent()) {
  65. throw new Error('Unable to get parent for 0 elem path');
  66. }
  67. const p = this.v.slice();
  68. p.pop();
  69. return new Path(p);
  70. }
  71. elements() {
  72. return this.v.slice();
  73. }
  74. name() {
  75. return this.v.slice(-1)[0];
  76. }
  77. contains(other) {
  78. let i;
  79. if (other.length > this.length) {
  80. return false;
  81. }
  82. for (i = 0; i < other.length; i += 1) {
  83. if (this.v[i] !== other.v[i]) {
  84. return false;
  85. }
  86. }
  87. return true;
  88. }
  89. ancestorOf(other) {
  90. return other.contains(this) && other.length !== this.length;
  91. }
  92. descendantOf(other) {
  93. return this.contains(other) && other.length !== this.length;
  94. }
  95. commonPrefixPath(other) {
  96. const len = this.length > other.length ? other.length : this.length;
  97. let i;
  98. const ret = [];
  99. for (i = 0; i < len; i += 1) {
  100. if (this.v[i] === other.v[i]) {
  101. ret.push(this.v[i]);
  102. } else {
  103. break;
  104. }
  105. }
  106. return new Path(ret);
  107. }
  108. static compare(a, b) {
  109. const al = a.length;
  110. const bl = b.length;
  111. if (al < bl) {
  112. return -1;
  113. }
  114. if (al > bl) {
  115. return 1;
  116. }
  117. const astr = a.toString();
  118. const bstr = b.toString();
  119. return astr < bstr ? -1 : astr > bstr ? 1 : 0;
  120. }
  121. }
  122. ['push', 'pop', 'shift', 'unshift', 'splice'].forEach(fn => {
  123. Object.defineProperty(Path.prototype, fn, {
  124. value(...args) {
  125. return this.v[fn](...args);
  126. }
  127. });
  128. });
  129. Object.defineProperty(Path.prototype, 'length', {
  130. enumerable: true,
  131. get() {
  132. return this.v.length;
  133. }
  134. });
  135. module.exports = Path;
  136. Path.tester = {
  137. setParserAndSep(p, sep) {
  138. parsePath = p;
  139. SEP = sep;
  140. },
  141. reset() {
  142. parsePath = origParser;
  143. SEP = origSep;
  144. }
  145. };