Skip to content

Commit

Permalink
feat(internal): isDrivePath
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed Dec 7, 2022
1 parent 12e2a26 commit 1e6f18d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/internal/__tests__/is-drive-path.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @file Unit Tests - isDrivePath
* @module pathe/internal/tests/unit/isDrivePath
*/

import testSubject from '../is-drive-path'

describe('unit:internal/isDrivePath', () => {
it('should return false if path does not start with disk designator', () => {
expect(testSubject('/tmp/myfile.html')).to.be.false
})

it('should return true if path starts with disk designator', () => {
expect(testSubject('C:\\temp\\myfile.html')).to.be.true
})
})
19 changes: 19 additions & 0 deletions src/internal/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @file Internal - Constants
* @module pathe/internal/constants
*/

/**
* Drive path regex.
*
* Determines if a path starts with a [drive letter][1].
*
* [1]: https://computerhope.com/jargon/d/drivelet.htm
*
* @see https://regex101.com/r/FsoDwJ
*
* @const {RegExp} DRIVE_PATH_REGEX
*/
const DRIVE_PATH_REGEX: RegExp = /^(?<drive>(?<letter>[a-z]):)/i

export { DRIVE_PATH_REGEX }
30 changes: 30 additions & 0 deletions src/internal/is-drive-path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @file Internal - isDrivePath
* @module pathe/internal/isDrivePath
*/

import { DRIVE_PATH_REGEX } from './constants'
import validateString from './validate-string'

/**
* Determines if a path starts with a disk designator.
*
* [1]: https://computerhope.com/jargon/d/drivelet.htm
*
* @see https://docs.microsoft.com/windows/win32/fileio/naming-a-file
*
* @example
* isDrivePath('C:\\temp\\myfile.html') // true
* @example
* isDrivePath('/tmp/myfile.html') // false
* @example
* isDrivePath('myfile.html') // false
*
* @param {string} path - Path to evaluate
* @return {boolean} `true` if path starts with [drive letter][1]
*/
const isDrivePath = (path: string): boolean => {
return validateString(path, 'path') && DRIVE_PATH_REGEX.test(path)
}

export default isDrivePath

0 comments on commit 1e6f18d

Please sign in to comment.