All files / src/node Dirent.ts

83.33% Statements 20/24
100% Branches 0/0
60% Functions 6/10
83.33% Lines 20/24

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  65x 65x     65x         65x   511x 511x   511x 511x 511x 511x   511x     511x 511x 511x 511x     200x       148x       42x                       10x                       65x  
import { Link } from '../node';
import { constants } from '../constants';
import { TEncodingExtended, strToEncoding, TDataOut } from '../encoding';
import type { IDirent } from './types/misc';
 
const { S_IFMT, S_IFDIR, S_IFREG, S_IFBLK, S_IFCHR, S_IFLNK, S_IFIFO, S_IFSOCK } = constants;
 
/**
 * A directory entry, like `fs.Dirent`.
 */
export class Dirent implements IDirent {
  static build(link: Link, encoding: TEncodingExtended | undefined) {
    const dirent = new Dirent();
    const { mode } = link.getNode();
 
    dirent.name = strToEncoding(link.getName(), encoding);
    dirent.mode = mode;
    dirent.path = link.getParentPath();
    dirent.parentPath = dirent.path;
 
    return dirent;
  }
 
  name: TDataOut = '';
  path = '';
  parentPath = '';
  private mode: number = 0;
 
  private _checkModeProperty(property: number): boolean {
    return (this.mode & S_IFMT) === property;
  }
 
  isDirectory(): boolean {
    return this._checkModeProperty(S_IFDIR);
  }
 
  isFile(): boolean {
    return this._checkModeProperty(S_IFREG);
  }
 
  isBlockDevice(): boolean {
    return this._checkModeProperty(S_IFBLK);
  }
 
  isCharacterDevice(): boolean {
    return this._checkModeProperty(S_IFCHR);
  }
 
  isSymbolicLink(): boolean {
    return this._checkModeProperty(S_IFLNK);
  }
 
  isFIFO(): boolean {
    return this._checkModeProperty(S_IFIFO);
  }
 
  isSocket(): boolean {
    return this._checkModeProperty(S_IFSOCK);
  }
}
 
export default Dirent;