# All files / src/file-readers github-path-adapter.ts

31.03% Statements 9/29
9.09% Branches 2/22
50% Functions 1/2
32.14% Lines 9/28

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
    541x

    5543x
    5543x
    5543x
    5543x
    5543x

    5543x

    46x

    46x

     export function githubPathAdapter(path: string) {
      // examples of supported cases
      // https://raw.githubusercontent.com/open-numbers/ddf--open_numbers--world_development_indicators/master
      // https://raw.githubusercontent.com/open-numbers/ddf--open_numbers--world_development_indicators/master/
      // https://raw.githubusercontent.com/open-numbers/ddf--open_numbers--world_development_indicators/master/datapackage.json

      // assumes master branch
      // https://github.com/open-numbers/ddf--open_numbers--world_development_indicators
      // https://github.com/open-numbers/ddf--open_numbers--world_development_indicators.git
      // https://github.com/open-numbers/ddf--open_numbers--world_development_indicators/blob/master/datapackage.json
      const githubusercontent = 'https://raw.githubusercontent.com/';
      const githubcom = 'https://github.com/';
      const datapackage = '/datapackage.json';
      const dotgit = '.git';
      const howManySlashes = (str: string) => (str.match(/\//g) || []).length;

      Iif (path.startsWith(githubusercontent)) {
        if (path.endsWith(datapackage)) {
          path = path.replace(datapackage, '');
        }
        if (path.endsWith('/')) {
          path = path.replace(/.$/, ''); // remove trailing slash
        }
        if (howManySlashes(path) === 4) {
          path = path + '/master';
        }
        return path;

      } else Iif (path.startsWith(githubcom)) {
        if (path.endsWith(dotgit)) {
          path = path.replace(dotgit, '');
        }
        if (path.endsWith(datapackage)) {
          path = path.replace(datapackage, '');
        }
        if (path.endsWith('/')) {
          path = path.replace(/.$/, ''); // remove trailing slash
        }

        path = path.replace(githubcom, githubusercontent);

        if (path.includes('/blob')) {
          path = path.replace('/blob', '');
        }
        if (howManySlashes(path) === 4) {
          path = path + '/master';
        }
        return path;

      } else {
        return path;
      }
    }