All files / istanbul-lib-report/src path.ts

100% Statements 71/71
100% Branches 38/38
100% Functions 17/17
100% Lines 71/71

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179                    12x 12x     188x 188x   188x         188x 8x 8x 5x     188x   188x 1x     187x 8x 8x 8x     187x 144x   43x   187x 2x   187x 187x                           779x 590x 189x 188x   1x         932x       307x       276x 1x   275x 275x 275x       92x       5x         127x 1x   126x 49x 1x     125x       121x       2x       143x   143x   143x 264x 251x   13x     143x       5x 5x   5x 1x     4x 1x     3x 3x 3x     12x   23x 23x     23x 23x         12x 60x   182x         12x     1429x          
/*
 Copyright 2012-2015, Yahoo Inc.
 Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
 */
import path from "node:path";
 
type PathParser = (path: string) => path.ParsedPath;
 
let parsePath: PathParser = path.parse;
let SEP: string = path.sep;
const origParser = parsePath;
const origSep = SEP;
 
function makeRelativeNormalizedPath(str: string, sep: string): string[] {
  const parsed = parsePath(str);
  let root = parsed.root;
  let dir: string | string[];
  let file = parsed.base;
  let quoted: RegExp;
  let pos: number;
 
  // handle a weird windows case separately
  if (sep === "\\") {
    pos = root.indexOf(":\\");
    if (pos >= 0) {
      root = root.substring(0, pos + 2);
    }
  }
  dir = parsed.dir.substring(root.length);
 
  if (str === "") {
    return [];
  }
 
  if (sep !== "/") {
    quoted = new RegExp(sep.replace(/\W/g, "\\$&"), "g");
    dir = dir.replace(quoted, "/");
    file = file.replace(quoted, "/"); // excessively paranoid?
  }
 
  if (dir !== "") {
    dir = `${dir}/${file}`;
  } else {
    dir = file;
  }
  if (dir.substring(0, 1) === "/") {
    dir = dir.substring(1);
  }
  dir = dir.split(/\/+/);
  return dir;
}
 
class Path {
  declare v: string[];
 
  declare readonly length: number;
  declare push: (...items: string[]) => number;
  declare pop: () => string | undefined;
  declare shift: () => string | undefined;
  declare unshift: (...items: string[]) => number;
  declare splice: (start: number, deleteCount?: number, ...items: string[]) => string[];
 
  constructor(strOrArray: string | string[]) {
    if (Array.isArray(strOrArray)) {
      this.v = strOrArray;
    } else if (typeof strOrArray === "string") {
      this.v = makeRelativeNormalizedPath(strOrArray, SEP);
    } else {
      throw new Error(`Invalid Path argument must be string or array:${strOrArray}`);
    }
  }
 
  toString(): string {
    return this.v.join("/");
  }
 
  hasParent(): boolean {
    return this.v.length > 0;
  }
 
  parent(): Path {
    if (!this.hasParent()) {
      throw new Error("Unable to get parent for 0 elem path");
    }
    const p = this.v.slice();
    p.pop();
    return new Path(p);
  }
 
  elements(): string[] {
    return this.v.slice();
  }
 
  name(): string {
    return this.v.slice(-1)[0];
  }
 
  contains(other: Path): boolean {
    let i;
    if (other.length > this.length) {
      return false;
    }
    for (i = 0; i < other.length; i += 1) {
      if (this.v[i] !== other.v[i]) {
        return false;
      }
    }
    return true;
  }
 
  ancestorOf(other: Path): boolean {
    return other.contains(this) && other.length !== this.length;
  }
 
  descendantOf(other: Path): boolean {
    return this.contains(other) && other.length !== this.length;
  }
 
  commonPrefixPath(other: Path): Path {
    const len = this.length > other.length ? other.length : this.length;
    let i;
    const ret = [];
 
    for (i = 0; i < len; i += 1) {
      if (this.v[i] === other.v[i]) {
        ret.push(this.v[i]);
      } else {
        break;
      }
    }
    return new Path(ret);
  }
 
  static compare(a: Path, b: Path): number {
    const al = a.length;
    const bl = b.length;
 
    if (al < bl) {
      return -1;
    }
 
    if (al > bl) {
      return 1;
    }
 
    const astr = a.toString();
    const bstr = b.toString();
    return astr < bstr ? -1 : astr > bstr ? 1 : 0;
  }
 
  static tester = {
    setParserAndSep(p: PathParser, sep: string) {
      parsePath = p;
      SEP = sep;
    },
    reset() {
      parsePath = origParser;
      SEP = origSep;
    },
  };
}
 
(["push", "pop", "shift", "unshift", "splice"] as const).forEach((fn) => {
  Object.defineProperty(Path.prototype, fn, {
    value(this: Path, ...args: unknown[]) {
      return (this.v[fn] as (...args: unknown[]) => unknown).apply(this.v, args);
    },
  });
});
 
Object.defineProperty(Path.prototype, "length", {
  enumerable: true,
  get(this: Path) {
    return this.v.length;
  },
});
 
export default Path;