2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-08-07 07:45:33 +00:00
|
|
|
// This module is browser compatible.
|
|
|
|
|
2023-08-25 09:07:43 +00:00
|
|
|
import { isWindows } from "./_os.ts";
|
2023-09-28 10:54:53 +00:00
|
|
|
import { relative as posixRelative } from "./posix/relative.ts";
|
|
|
|
import { relative as windowsRelative } from "./windows/relative.ts";
|
2023-08-07 07:45:33 +00:00
|
|
|
|
|
|
|
/**
|
2024-06-02 02:46:36 +00:00
|
|
|
* Return the relative path from `from` to `to` based on current working
|
|
|
|
* directory.
|
2023-08-07 07:45:33 +00:00
|
|
|
*
|
2024-06-02 02:46:36 +00:00
|
|
|
* @example Usage
|
|
|
|
* ```ts
|
|
|
|
* import { relative } from "@std/path/relative";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2023-08-07 07:45:33 +00:00
|
|
|
*
|
2024-06-02 02:46:36 +00:00
|
|
|
* if (Deno.build.os === "windows") {
|
|
|
|
* const path = relative("C:\\foobar\\test\\aaa", "C:\\foobar\\impl\\bbb");
|
|
|
|
* assertEquals(path, "..\\..\\impl\\bbb");
|
|
|
|
* } else {
|
|
|
|
* const path = relative("/data/foobar/test/aaa", "/data/foobar/impl/bbb");
|
|
|
|
* assertEquals(path, "../../impl/bbb");
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param from Path in current working directory.
|
|
|
|
* @param to Path in current working directory.
|
|
|
|
* @returns The relative path from `from` to `to`.
|
2023-08-07 07:45:33 +00:00
|
|
|
*/
|
|
|
|
export function relative(from: string, to: string): string {
|
|
|
|
return isWindows ? windowsRelative(from, to) : posixRelative(from, to);
|
|
|
|
}
|