/**
 * @license
 * Copyright (c) 2019 The Polymer Project Authors. All rights reserved.
 * This code may only be used under the BSD style license found at
 * http://polymer.github.io/LICENSE.txt
 * The complete set of authors may be found at
 * http://polymer.github.io/AUTHORS.txt
 * The complete set of contributors may be found at
 * http://polymer.github.io/CONTRIBUTORS.txt
 * Code distributed by Google as part of the polymer project is also
 * subject to an additional IP rights grant found at
 * http://polymer.github.io/PATENTS.txt
 */
import {relative} from 'path';

export const ensureLeadingDot = (path: string): string =>
    (path.startsWith('../') || path.startsWith('./')) ? path : './' + path;

export const forwardSlashesOnlyPlease = (path: string): string =>
    path.replace(/\\/g, '/');

export const getBasePath = (href: string): string =>
    href.replace(/[^\/]+$/, '');

export const noLeadingSlash = (href: string): string => href.replace(/^\//, '');

export const relativePath = (from: string, to: string): string => {
  from = forwardSlashesOnlyPlease(from);
  to = forwardSlashesOnlyPlease(to);
  if (!from.endsWith('/')) {
    from = from.replace(/[^/]*$/, '');
  }
  return ensureLeadingDot(relative(from, to));
};

/**
 * @license
 * Copyright (c) 2019 The Polymer Project Authors. All rights reserved.
 * This code may only be used under the BSD style license found at
 * http://polymer.github.io/LICENSE.txt
 * The complete set of authors may be found at
 * http://polymer.github.io/AUTHORS.txt
 * The complete set of contributors may be found at
 * http://polymer.github.io/CONTRIBUTORS.txt
 * Code distributed by Google as part of the polymer project is also
 * subject to an additional IP rights grant found at
 * http://polymer.github.io/PATENTS.txt
 */
import {relative} from 'path';

export const ensureLeadingDot = (path: string): string =>
    (path.startsWith('../') || path.startsWith('./')) ? path : './' + path;

export const forwardSlashesOnlyPlease = (path: string): string =>
    path.replace(/\\/g, '/');

export const getBasePath = (href: string): string =>
    href.replace(/[^\/]+$/, '');

export const noLeadingSlash = (href: string): string => href.replace(/^\//, '');

export const relativePath = (from: string, to: string): string => {
  from = getBasePath(forwardSlashesOnlyPlease(from));
  to = forwardSlashesOnlyPlease(to);
  return ensureLeadingDot(relative(from, to));
};

Use the getBasePath function instead of duplicate regexp.
Use the getBasePath function instead of duplicate regexp.
