From 59b9235b5857d2bf03446690602dea7526c1109a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ro=C5=BCek?= Date: Fri, 11 Oct 2019 16:56:28 +0200 Subject: [PATCH] feat: implement stripRoot function * fix(join): handle absolute segments * feat: stripRoot * revert: "fix(join): handle absolute segments" This reverts commit 85a378475cf2580f4498b2aede6cee6413b343a3. * chore: re-export --- src/__tests__/stripRoot.spec.ts | 18 ++++++++++++++++++ src/index.ts | 3 ++- src/stripRoot.ts | 7 +++++++ 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 src/__tests__/stripRoot.spec.ts create mode 100644 src/stripRoot.ts diff --git a/src/__tests__/stripRoot.spec.ts b/src/__tests__/stripRoot.spec.ts new file mode 100644 index 0000000..f78181f --- /dev/null +++ b/src/__tests__/stripRoot.spec.ts @@ -0,0 +1,18 @@ +import { stripRoot } from '../stripRoot'; + +describe('stripRoot', () => { + it.each(['foo', 'test/a', 'a/b'])('does not alter "%s" path', (path) => { + expect(stripRoot(path)).toEqual(path); + }); + + it.each` + actual | expected + ${'/foo'} | ${'foo'} + ${'c:\\foo'} | ${'foo'} + ${'\\foo'} | ${'foo'} + ${'////a'} | ${'a'} + ${'/\\///a'} | ${'a'} + `('strip root from "$actual" path', ({ actual, expected }) => { + expect(stripRoot(actual)).toEqual(expected); + }); +}); diff --git a/src/index.ts b/src/index.ts index 630fe8d..dbf4e0f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,6 +11,7 @@ export * from './relative'; export * from './resolve'; export * from './sep'; export * from './startsWithWindowsDrive'; +export * from './stripRoot'; export * from './toFSPath'; export * from './types'; -export * from './srn'; \ No newline at end of file +export * from './srn'; diff --git a/src/stripRoot.ts b/src/stripRoot.ts new file mode 100644 index 0000000..9ff7d08 --- /dev/null +++ b/src/stripRoot.ts @@ -0,0 +1,7 @@ +import { parse } from './parse'; +import { sep } from './sep'; + +export const stripRoot = (path: string) => + parse(path) + .path.filter(Boolean) + .join(sep);