All files / src/filesystem path.js

90.91% Statements 20/22
83.33% Branches 10/12
100% Functions 6/6
100% Lines 20/20

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        530x 337x 193x 1x   192x         522x 26x 26x     496x   496x       1x 1x 1x       1x 1x 1x       138x 138x 138x       50x      
// helper class to handle paths
 
export default class Path {
  constructor (path) {
    if (typeof path === 'string') {
      this.path = path
    } else if (!(path instanceof Path)) {
      throw new Error('It\'s neither Path nor string')
    } else {
      this.path = path.path
    }
  }
 
  normalize () {
    if (this.path === '\\' || this.path === '/') {
      this.path = '/'
      return this
    }
 
    this.path = this.path.split(/[/\\]+/).join('/')
 
    return this
  }
 
  get basename () {
    const parts = this.path.match(/(\/)?(\w+\.(\S+))/)
    Iif (parts.size < 2) return undefined
    return parts[2]
  }
 
  get extension () {
    const parts = this.path.match(/(\/)?(\w+\.(\S+))/)
    Iif (parts.size < 3) return undefined
    return parts[3]
  }
 
  get parent () {
    const parts = this.path.split('/')
    parts.pop()
    return new Path(parts.join('/')).normalize()
  }
 
  get isRoot () {
    return this.path === ''
  }
}