All files / src/utils Toolbox.js

100% Statements 9/9
100% Branches 0/0
100% Functions 4/4
100% Lines 9/9
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    1x                 64x                     1180x                     128x 128x 1180x 1180x   128x       1x  
'use strict';
 
const url = require('url');
 
class Toolbox {
  /**
   * Remove trailing slash to the parametered string if presents
   * @param string the string to clean
   * @returns string the cleaned string
   */
  static removeTrailingSlash(str) {
    return str.replace(/\/$/, '');
  }
 
  /**
   * Resolve shortest path from a path
   * With '/root/mocks/../data' => '/root/data'
   * @param string root the first part of the path
   * @param string path the last part of the path
   * @returns string the resolved path
   */
  static resolveAbsolutePath(root, path) {
    return url.resolve(root+'/', path).replace(/(\/)\.\*$/, '.*');
  }
 
  /**
   * Prepend the basePath parameter for each path contained in the paths parameter.
   * Then convert these relative paths to the absolute paths
   * @param string basePath a path to prepend to the paths
   * @param paths array the paths to convert
   * @returns array the absolute converted paths
   */
  static buildAbsolutePaths(basePath, paths) {
    let absolutePaths = [];
    paths.forEach((element) => {
      let path = basePath + element;
      absolutePaths.push(Toolbox.resolveAbsolutePath(__dirname, path));
    });
    return absolutePaths;
  }
}
 
module.exports = Toolbox;