All files / src/asset AssetFileEntity.ts

100% Statements 10/10
100% Branches 2/2
100% Functions 4/4
100% Lines 10/10

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 371x           1x                 1x 1x             3x 1x   2x           1x 1x   1x  
import * as path from 'path';
 
/**
 * Elements of AssetFileMap.
 * It contains file path and some modification methods.
 */
export default class AssetFileEntity {
  /**
   * Absolute file path
   */
  public readonly filePath!: string;
 
  /**
   * Returns file extension including period (.).
   */
  get extension(): string {
    return path.extname(this.filePath);
  }
 
  /**
   * Constructor throws exception when given path is invalid as absolute path.
   */
  constructor(absolutePath: string) {
    if (!path.isAbsolute(absolutePath)) {
      throw new Error('AssetFileEntity only accepts absolute path as constructor argument');
    }
    this.filePath = absolutePath;
  }
 
  /**
   * Change absolute path to relative path based on given path.
   */
  public relativeLocalPath(basePath: string): string {
    return this.filePath.replace(basePath, '').replace(/^\//, '');
  }
}