Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deprecation(path): split off all constants into their own files and deprecate old names #4153

Merged
merged 10 commits into from
Jan 18, 2024
4 changes: 2 additions & 2 deletions fs/_is_subdir.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// Copyright the Browserify authors. MIT License.

import { SEP } from "../path/separator.ts";
import { SEPARATOR } from "../path/constants.ts";
import { toPathString } from "./_to_path_string.ts";

/**
Expand All @@ -13,7 +13,7 @@ import { toPathString } from "./_to_path_string.ts";
export function isSubdir(
src: string | URL,
dest: string | URL,
sep: string = SEP,
sep = SEPARATOR,
): boolean {
if (src === dest) {
return false;
Expand Down
12 changes: 4 additions & 8 deletions fs/_is_subdir_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Copyright the Browserify authors. MIT License.

import { assertEquals } from "../assert/mod.ts";
import { SEP as SEP_POSIX } from "../path/posix/separator.ts";
import { SEP as SEP_WIN32 } from "../path/windows/separator.ts";
import { SEPARATOR as SEP_POSIX } from "../path/posix/constants.ts";
import { SEPARATOR as SEP_WIN32 } from "../path/windows/constants.ts";
import { isSubdir } from "./_is_subdir.ts";

Deno.test("isSubdir() returns a boolean indicating if dir is a subdir", function () {
Expand All @@ -16,13 +16,9 @@ Deno.test("isSubdir() returns a boolean indicating if dir is a subdir", function
["../first", "../first/second", true, SEP_POSIX],
["c:\\first", "c:\\first", false, SEP_WIN32],
["c:\\first", "c:\\first\\second", true, SEP_WIN32],
];
] as const;

pairs.forEach(function (p) {
const src = p[0] as string;
const dest = p[1] as string;
const expected = p[2] as boolean;
const sep = p[3] as string;
pairs.forEach(function ([src, dest, expected, sep]) {
assertEquals(
isSubdir(src, dest, sep),
expected,
Expand Down
6 changes: 3 additions & 3 deletions fs/expand_glob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { joinGlobs } from "../path/join_globs.ts";
import { isGlob } from "../path/is_glob.ts";
import { isAbsolute } from "../path/is_absolute.ts";
import { resolve } from "../path/resolve.ts";
import { SEP_PATTERN } from "../path/separator.ts";
import { SEPARATOR_PATTERN } from "../path/constants.ts";
import { walk, walkSync } from "./walk.ts";
import { assert } from "../assert/assert.ts";
import { toPathString } from "./_to_path_string.ts";
Expand Down Expand Up @@ -55,10 +55,10 @@ interface SplitPath {
}

function split(path: string): SplitPath {
const s = SEP_PATTERN.source;
const s = SEPARATOR_PATTERN.source;
const segments = path
.replace(new RegExp(`^${s}|${s}$`, "g"), "")
.split(SEP_PATTERN);
.split(SEPARATOR_PATTERN);
const isAbsolute_ = isAbsolute(path);
return {
segments,
Expand Down
4 changes: 2 additions & 2 deletions http/file_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import { extname } from "../path/extname.ts";
import { join } from "../path/join.ts";
import { relative } from "../path/relative.ts";
import { resolve } from "../path/resolve.ts";
import { SEP_PATTERN } from "../path/separator.ts";
import { SEPARATOR_PATTERN } from "../path/constants.ts";
import { contentType } from "../media_types/content_type.ts";
import { calculate, ifNoneMatch } from "./etag.ts";
import {
Expand Down Expand Up @@ -309,7 +309,7 @@ async function serveDirIndex(
const urlRoot = options.urlRoot ? "/" + options.urlRoot : "";
const dirUrl = `/${
relative(options.target, dirPath).replaceAll(
new RegExp(SEP_PATTERN, "g"),
new RegExp(SEPARATOR_PATTERN, "g"),
"/",
)
}`;
Expand Down
7 changes: 5 additions & 2 deletions path/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// This module is browser compatible.

import { _common } from "./_common/common.ts";
import { SEP } from "./separator.ts";
import { SEPARATOR } from "./constants.ts";

/** Determines the common path from a set of paths, using an optional separator,
* which defaults to the OS default separator.
Expand All @@ -16,6 +16,9 @@ import { SEP } from "./separator.ts";
* console.log(p); // "./deno/std/"
* ```
*/
export function common(paths: string[], sep: typeof SEP = SEP): string {
export function common(
paths: string[],
sep: string = SEPARATOR,
Copy link
Contributor

@iuioiua iuioiua Jan 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sidenote: I see no use case for this sep argument. Ditto for the Windows version. Perhaps, it should be deprecated in another separate PR.

Copy link
Contributor Author

@lino-levan lino-levan Jan 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably work for another PR, but this makes sense 👍

): string {
return _common(paths, sep);
}
7 changes: 7 additions & 0 deletions path/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
import { isWindows } from "./_os.ts";

export const DELIMITER = isWindows ? ";" as const : ":" as const;
export const SEPARATOR = isWindows ? "\\" as const : "/" as const;
export const SEPARATOR_PATTERN = isWindows ? /[\\/]+/ : /\/+/;
4 changes: 2 additions & 2 deletions path/join_globs_test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "../assert/mod.ts";
import { joinGlobs } from "./join_globs.ts";
import { SEP } from "./mod.ts";
import { SEPARATOR } from "./mod.ts";

Deno.test("joinGlobs() checks options.globstar", function () {
assertEquals(joinGlobs(["**", ".."], { globstar: true }), `**${SEP}..`);
assertEquals(joinGlobs(["**", ".."], { globstar: true }), `**${SEPARATOR}..`);
});
16 changes: 11 additions & 5 deletions path/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,28 @@
* @module
*/

import { isWindows } from "./_os.ts";
import * as _windows from "./windows/mod.ts";
import * as _posix from "./posix/mod.ts";
import { DELIMITER, SEPARATOR } from "./constants.ts";

/** @deprecated (will be removed after 1.0.0) Import from {@link https://deno.land/std/path/windows/mod.ts} instead. */
export const win32: typeof _windows = _windows;

/** @deprecated (will be removed after 1.0.0) Import from {@link https://deno.land/std/posix/mod.ts} instead. */
export const posix: typeof _posix = _posix;

export const sep: "/" | "\\" = isWindows ? _windows.sep : _posix.sep;
export const delimiter: ":" | ";" = isWindows
? _windows.delimiter
: _posix.delimiter;
/**
* @deprecated (will be removed in 0.215.0) Use {@linkcode SEPARATOR} instead.
*/
export const sep = SEPARATOR;

/**
* @deprecated (will be removed in 0.215.0) Use {@linkcode DELIMITER} instead.
*/
export const delimiter = DELIMITER;

export * from "./basename.ts";
export * from "./constants.ts";
export * from "./dirname.ts";
export * from "./extname.ts";
export * from "./format.ts";
Expand Down
7 changes: 5 additions & 2 deletions path/normalize_glob_test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "../assert/mod.ts";
import { normalizeGlob } from "./normalize_glob.ts";
import { SEP } from "./mod.ts";
import { SEPARATOR } from "./constants.ts";

Deno.test("normalizeGlob() checks options.globstar", function () {
assertEquals(normalizeGlob(`**${SEP}..`, { globstar: true }), `**${SEP}..`);
assertEquals(
normalizeGlob(`**${SEPARATOR}..`, { globstar: true }),
`**${SEPARATOR}..`,
);
});
7 changes: 5 additions & 2 deletions path/posix/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// This module is browser compatible.

import { _common } from "../_common/common.ts";
import { SEP } from "./separator.ts";
import { SEPARATOR } from "./constants.ts";

/** Determines the common path from a set of paths, using an optional separator,
* which defaults to the OS default separator.
Expand All @@ -16,6 +16,9 @@
* console.log(p); // "./deno/std/"
* ```
*/
export function common(paths: string[], sep: typeof SEP = SEP): string {
export function common(
paths: string[],
sep: string = SEPARATOR,

Check warning on line 21 in path/posix/common.ts

View check run for this annotation

Codecov / codecov/patch

path/posix/common.ts#L19-L21

Added lines #L19 - L21 were not covered by tests
): string {
return _common(paths, sep);
}
6 changes: 6 additions & 0 deletions path/posix/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

export const DELIMITER = ":" as const;
export const SEPARATOR = "/" as const;
export const SEPARATOR_PATTERN = /\/+/;
4 changes: 2 additions & 2 deletions path/posix/join_globs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { GlobOptions } from "../_common/glob_to_reg_exp.ts";
import { join } from "./join.ts";
import { SEP } from "./separator.ts";
import { SEPARATOR } from "./constants.ts";
import { normalizeGlob } from "./normalize_glob.ts";

/** Like join(), but doesn't collapse "**\/.." when `globstar` is true. */
Expand All @@ -20,7 +20,7 @@ export function joinGlobs(
const path = glob;
if (path.length > 0) {
if (!joined) joined = path;
else joined += `${SEP}${path}`;
else joined += `${SEPARATOR}${path}`;
}
}
if (!joined) return ".";
Expand Down
10 changes: 9 additions & 1 deletion path/posix/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,18 @@
* @module
*/

/**
* @deprecated (will be removed in 0.215.0) Use {@linkcode SEPARATOR} from {@link https://deno.land/std/path/posix/constants.ts} instead.
*/
export const sep = "/";
export const delimiter = ":";

/**
* @deprecated (will be removed in 0.215.0) Use {@linkcode DELIMITER} from {@link https://deno.land/std/path/posix/constants.ts} instead.
*/
export const delimiter = "/";

export * from "./basename.ts";
export * from "./constants.ts";
export * from "./dirname.ts";
export * from "./extname.ts";
export * from "./format.ts";
Expand Down
4 changes: 2 additions & 2 deletions path/posix/normalize_glob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { GlobOptions } from "../_common/glob_to_reg_exp.ts";
import { normalize } from "./normalize.ts";
import { SEP_PATTERN } from "./separator.ts";
import { SEPARATOR_PATTERN } from "./constants.ts";

/** Like normalize(), but doesn't collapse "**\/.." when `globstar` is true. */
export function normalizeGlob(
Expand All @@ -16,7 +16,7 @@ export function normalizeGlob(
if (!globstar) {
return normalize(glob);
}
const s = SEP_PATTERN.source;
const s = SEPARATOR_PATTERN.source;
const badParentPattern = new RegExp(
`(?<=(${s}|^)\\*\\*${s})\\.\\.(?=${s}|$)`,
"g",
Expand Down
6 changes: 6 additions & 0 deletions path/posix/separator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

/**
* @deprecated (will be removed in 0.215.0) Use {@linkcode SEPARATOR} from {@link https://deno.land/std/path/posix/constants.ts} instead.
*/
export const SEP = "/";
/**
* @deprecated (will be removed in 0.215.0) Use {@linkcode SEPARATOR_PATTERN} from {@link https://deno.land/std/path/posix/constants.ts} instead.
*/
export const SEP_PATTERN = /\/+/;
6 changes: 6 additions & 0 deletions path/separator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,11 @@

import { isWindows } from "./_os.ts";

/**
* @deprecated (will be removed in 0.215.0) Use {@linkcode SEPARATOR} from {@link https://deno.land/std/path/constants.ts} instead.
*/

Check warning on line 8 in path/separator.ts

View check run for this annotation

Codecov / codecov/patch

path/separator.ts#L8

Added line #L8 was not covered by tests
export const SEP: "/" | "\\" = isWindows ? "\\" : "/";
/**
* @deprecated (will be removed in 0.215.0) Use {@linkcode SEPARATOR_PATTERN} from {@link https://deno.land/std/path/constants.ts} instead.
*/

Check warning on line 12 in path/separator.ts

View check run for this annotation

Codecov / codecov/patch

path/separator.ts#L12

Added line #L12 was not covered by tests
export const SEP_PATTERN: RegExp = isWindows ? /[\\/]+/ : /\/+/;
7 changes: 5 additions & 2 deletions path/windows/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// This module is browser compatible.

import { _common } from "../_common/common.ts";
import { SEP } from "./separator.ts";
import { SEPARATOR } from "./constants.ts";

/** Determines the common path from a set of paths, using an optional separator,
* which defaults to the OS default separator.
Expand All @@ -16,6 +16,9 @@
* console.log(p); // "./deno/std/"
* ```
*/
export function common(paths: string[], sep: typeof SEP = SEP): string {
export function common(
paths: string[],
sep: string = SEPARATOR,

Check warning on line 21 in path/windows/common.ts

View check run for this annotation

Codecov / codecov/patch

path/windows/common.ts#L19-L21

Added lines #L19 - L21 were not covered by tests
): string {
return _common(paths, sep);
}
6 changes: 6 additions & 0 deletions path/windows/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

export const DELIMITER = ";" as const;
export const SEPARATOR = "\\" as const;
export const SEPARATOR_PATTERN = /[\\/]+/;
4 changes: 2 additions & 2 deletions path/windows/join_globs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { GlobOptions } from "../_common/glob_to_reg_exp.ts";
import { join } from "./join.ts";
import { SEP } from "./separator.ts";
import { SEPARATOR } from "./constants.ts";
import { normalizeGlob } from "./normalize_glob.ts";

/** Like join(), but doesn't collapse "**\/.." when `globstar` is true. */
Expand All @@ -20,7 +20,7 @@ export function joinGlobs(
const path = glob;
if (path.length > 0) {
if (!joined) joined = path;
else joined += `${SEP}${path}`;
else joined += `${SEPARATOR}${path}`;
}
}
if (!joined) return ".";
Expand Down
8 changes: 8 additions & 0 deletions path/windows/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,18 @@
* @module
*/

/**
* @deprecated (will be removed in 0.215.0) Use {@linkcode SEPARATOR} from {@link https://deno.land/std/path/windows/constants.ts} instead.
*/
export const sep = "\\";

/**
* @deprecated (will be removed in 0.215.0) Use {@linkcode DELIMITER} from {@link https://deno.land/std/path/windows/constants.ts} instead.
*/
export const delimiter = ";";

export * from "./basename.ts";
export * from "./constants.ts";
export * from "./dirname.ts";
export * from "./extname.ts";
export * from "./format.ts";
Expand Down
4 changes: 2 additions & 2 deletions path/windows/normalize_glob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { globToRegExp as _globToRegExp } from "./glob_to_regexp.ts";
import { GlobOptions } from "../_common/glob_to_reg_exp.ts";
import { normalize } from "./normalize.ts";
import { SEP_PATTERN } from "./separator.ts";
import { SEPARATOR_PATTERN } from "./constants.ts";

/** Like normalize(), but doesn't collapse "**\/.." when `globstar` is true. */
export function normalizeGlob(
Expand All @@ -17,7 +17,7 @@ export function normalizeGlob(
if (!globstar) {
return normalize(glob);
}
const s = SEP_PATTERN.source;
const s = SEPARATOR_PATTERN.source;
const badParentPattern = new RegExp(
`(?<=(${s}|^)\\*\\*${s})\\.\\.(?=${s}|$)`,
"g",
Expand Down
6 changes: 6 additions & 0 deletions path/windows/separator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

/**
* @deprecated (will be removed in 0.216.0) Use {@linkcode SEPARATOR} from {@link https://deno.land/std/path/windows/constants.ts} instead.
*/
export const SEP = "\\";
/**
* @deprecated (will be removed in 0.216.0) Use {@linkcode SEPARATOR_PATTERN} from {@link https://deno.land/std/path/windows/constants.ts} instead.
*/
export const SEP_PATTERN = /[\\/]+/;