diff --git a/src/internal/__tests__/is-sep.spec-d.ts b/src/internal/__tests__/is-sep.spec-d.ts new file mode 100644 index 00000000..a05490bb --- /dev/null +++ b/src/internal/__tests__/is-sep.spec-d.ts @@ -0,0 +1,13 @@ +/** + * @file Type Tests - isSep + * @module pathe/internal/tests/unit-d/isSep + */ + +import type { Sep } from '#src/types' +import testSubject from '../is-sep' + +describe('unit-d:internal/isSep', () => { + it('should guard Sep', () => { + expectTypeOf(testSubject).guards.toEqualTypeOf() + }) +}) diff --git a/src/internal/__tests__/is-sep.spec.ts b/src/internal/__tests__/is-sep.spec.ts new file mode 100644 index 00000000..1d03c863 --- /dev/null +++ b/src/internal/__tests__/is-sep.spec.ts @@ -0,0 +1,17 @@ +/** + * @file Unit Tests - isSep + * @module pathe/internal/tests/unit/isSep + */ + +import { posix, win32 } from 'node:path' +import testSubject from '../is-sep' + +describe('unit:internal/isSep', () => { + it('should return false if value is not path separator', () => { + expect(testSubject(win32.sep)).to.be.false + }) + + it('should return true if value is path separator', () => { + expect(testSubject(posix.sep)).to.be.true + }) +}) diff --git a/src/internal/is-sep.ts b/src/internal/is-sep.ts new file mode 100644 index 00000000..fd91977e --- /dev/null +++ b/src/internal/is-sep.ts @@ -0,0 +1,21 @@ +/** + * @file Internal - isSep + * @module pathe/internal/isSep + */ + +import sep from '#src/lib/sep' +import type { Sep } from '#src/types' + +/** + * Checks if `value` is a path separator. + * + * [1]: {@link ../lib/sep.ts} + * + * @see [`sep`][1] + * + * @param {unknown} value - Possible path separator + * @return {value is Sep} `true` if `str` is path separator + */ +const isSep = (value: unknown): value is Sep => value === sep + +export default isSep diff --git a/src/lib/basename.ts b/src/lib/basename.ts index a948840c..858d19ab 100644 --- a/src/lib/basename.ts +++ b/src/lib/basename.ts @@ -5,8 +5,8 @@ import ensurePosix from '#src/internal/ensure-posix' import isDrivePath from '#src/internal/is-drive-path' +import isSep from '#src/internal/is-sep' import validateString from '#src/internal/validate-string' -import sep from './sep' /** * Returns the last portion of a path, similar to the Unix `basename` command. @@ -55,7 +55,7 @@ const basename = (path: string, suffix?: string): string => { // get basename without attempting to remove suffix if (!suffix || suffix.length > path.length) { for (let i = path.length - 1; i >= start; --i) { - if (path.charAt(i) === sep) { + if (isSep(path.charAt(i))) { // if separator was reached, and is not a trailing separator, exit early if (!sep_match) { start = i + 1 @@ -94,7 +94,7 @@ const basename = (path: string, suffix?: string): string => { */ const char: string = path.charAt(i) - if (char === sep) { + if (isSep(char)) { // if separator was reached, and is not a trailing separator, exit early if (!sep_match) { start = i + 1