mirror of
https://github.com/denoland/std.git
synced 2024-11-22 04:59:05 +00:00
388800f301
Co-authored-by: Nathan Whitaker <nathan@deno.com> Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
41 lines
1.1 KiB
TypeScript
41 lines
1.1 KiB
TypeScript
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
// This module is browser compatible.
|
|
|
|
import { CHAR_COLON } from "../_common/constants.ts";
|
|
import { assertPath } from "../_common/assert_path.ts";
|
|
import { isPathSeparator, isWindowsDeviceRoot } from "./_util.ts";
|
|
|
|
/**
|
|
* Verifies whether provided path is absolute.
|
|
*
|
|
* @example Usage
|
|
* ```ts
|
|
* import { isAbsolute } from "@std/path/windows/is-absolute";
|
|
* import { assert, assertFalse } from "@std/assert";
|
|
*
|
|
* assert(isAbsolute("C:\\foo\\bar"));
|
|
* assertFalse(isAbsolute("..\\baz"));
|
|
* ```
|
|
*
|
|
* @param path The path to verify.
|
|
* @returns `true` if the path is absolute, `false` otherwise.
|
|
*/
|
|
export function isAbsolute(path: string): boolean {
|
|
assertPath(path);
|
|
|
|
const len = path.length;
|
|
if (len === 0) return false;
|
|
|
|
const code = path.charCodeAt(0);
|
|
if (isPathSeparator(code)) {
|
|
return true;
|
|
} else if (isWindowsDeviceRoot(code)) {
|
|
// Possible device root
|
|
|
|
if (len > 2 && path.charCodeAt(1) === CHAR_COLON) {
|
|
if (isPathSeparator(path.charCodeAt(2))) return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|