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

refactor: split path into per-os modules #3649

Merged
merged 6 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions http/file_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
* @module
*/

import { posixJoin } from "../path/_join.ts";
import { posixNormalize } from "../path/_normalize.ts";
import { join as posixJoin } from "../path/posix/join.ts";
import { normalize as posixNormalize } from "../path/posix/normalize.ts";
import { extname } from "../path/extname.ts";
import { join } from "../path/join.ts";
import { relative } from "../path/relative.ts";
Expand Down
104 changes: 0 additions & 104 deletions path/_basename.ts

This file was deleted.

10 changes: 10 additions & 0 deletions path/_common/assert_path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
// Copyright the Browserify authors. MIT License.

export function assertPath(path: string) {
if (typeof path !== "string") {
throw new TypeError(
`Path must be a string. Received ${JSON.stringify(path)}`,
);
}
}
53 changes: 53 additions & 0 deletions path/_common/basename.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

import { assertPath } from "./assert_path.ts";

export function stripSuffix(name: string, suffix: string): string {
if (suffix.length >= name.length) {
return name;
}

const lenDiff = name.length - suffix.length;

for (let i = suffix.length - 1; i >= 0; --i) {
if (name.charCodeAt(lenDiff + i) !== suffix.charCodeAt(i)) {
return name;
}
}

return name.slice(0, -suffix.length);
}

export function lastPathSegment(
path: string,
isSep: (char: number) => boolean,
start = 0,
): string {
let matchedNonSeparator = false;
let end = path.length;

for (let i = path.length - 1; i >= start; --i) {
if (isSep(path.charCodeAt(i))) {
if (matchedNonSeparator) {
start = i + 1;
break;
}
} else if (!matchedNonSeparator) {
matchedNonSeparator = true;
end = i + 1;
}
}

return path.slice(start, end);
}

export function assertArgs(path: string, suffix: string) {
assertPath(path);
if (path.length === 0) return path;
if (typeof suffix !== "string") {
throw new TypeError(
`Suffix must be a string. Received ${JSON.stringify(suffix)}`,
);
}
}
26 changes: 26 additions & 0 deletions path/_common/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

export function _common(paths: string[], sep: string): string {
const [first = "", ...remaining] = paths;
if (first === "" || remaining.length === 0) {
return first.substring(0, first.lastIndexOf(sep) + 1);
}
const parts = first.split(sep);

let endOfPrefix = parts.length;
for (const path of remaining) {
const compare = path.split(sep);
for (let i = 0; i < endOfPrefix; i++) {
if (compare[i] !== parts[i]) {
endOfPrefix = i;
}
}

if (endOfPrefix === 0) {
return "";
}
}
const prefix = parts.slice(0, endOfPrefix).join(sep);
return prefix.endsWith(sep) ? prefix : `${prefix}${sep}`;
}
File renamed without changes.
9 changes: 9 additions & 0 deletions path/_common/dirname.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

import { assertPath } from "./assert_path.ts";

export function assertArg(path: string) {
assertPath(path);
if (path.length === 0) return ".";
}
24 changes: 3 additions & 21 deletions path/_format.ts → path/_common/format.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

import type { FormatInputPathObject } from "./_interface.ts";
import type { FormatInputPathObject } from "../_interface.ts";

function _format(
export function _format(
sep: string,
pathObject: FormatInputPathObject,
): string {
Expand All @@ -16,28 +16,10 @@ function _format(
return dir + sep + base;
}

function assertArg(pathObject: FormatInputPathObject) {
export function assertArg(pathObject: FormatInputPathObject) {
if (pathObject === null || typeof pathObject !== "object") {
throw new TypeError(
`The "pathObject" argument must be of type Object. Received type ${typeof pathObject}`,
);
}
}

/**
* Generate a path from `FormatInputPathObject` object.
* @param pathObject with path
*/
export function posixFormat(pathObject: FormatInputPathObject): string {
assertArg(pathObject);
return _format("/", pathObject);
}

/**
* Generate a path from `FormatInputPathObject` object.
* @param pathObject with path
*/
export function windowsFormat(pathObject: FormatInputPathObject): string {
assertArg(pathObject);
return _format("\\", pathObject);
}
10 changes: 10 additions & 0 deletions path/_common/from_file_url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

export function assertArg(url: URL | string) {
url = url instanceof URL ? url : new URL(url);
if (url.protocol !== "file:") {
throw new TypeError("Must be a file URL.");
}
return url;
}
Loading