Skip to content

Commit

Permalink
feat(internal): isSep
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed Dec 8, 2022
1 parent 054b623 commit 3ece912
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/internal/__tests__/is-sep.spec-d.ts
Original file line number Diff line number Diff line change
@@ -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<Sep>()
})
})
17 changes: 17 additions & 0 deletions src/internal/__tests__/is-sep.spec.ts
Original file line number Diff line number Diff line change
@@ -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
})
})
21 changes: 21 additions & 0 deletions src/internal/is-sep.ts
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions src/lib/basename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3ece912

Please sign in to comment.