Skip to content

Commit

Permalink
feat: Add common to std/path (denoland#4527)
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsonk committed Mar 30, 2020
1 parent d795d34 commit 7670a13
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 23 deletions.
38 changes: 38 additions & 0 deletions std/path/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.

import { SEP } from "./constants.ts";

/** Determines the common path from a set of paths, using an optional separator,
* which defaults to the OS default separator.
*
* import { common } from "https://deno.land/std/path/mod.ts";
* const p = common([
* "./deno/std/path/mod.ts",
* "./deno/std/fs/mod.ts",
* ]);
* console.log(p); // "./deno/std/"
*
*/
export function common(paths: string[], sep = SEP): 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}`;
}
47 changes: 47 additions & 0 deletions std/path/common_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.

const { test } = Deno;
import { assertEquals } from "../testing/asserts.ts";

import { common } from "./mod.ts";

test({
name: "path - common - basic usage",
fn() {
const actual = common(
[
"file:https://deno/cli/js/deno.ts",
"file:https://deno/std/path/mod.ts",
"file:https://deno/cli/js/main.ts",
],
"/"
);
assertEquals(actual, "file:https://deno/");
},
});

test({
name: "path - common - no shared",
fn() {
const actual = common(
["file:https://deno/cli/js/deno.ts", "https://deno.land/std/path/mod.ts"],
"/"
);
assertEquals(actual, "");
},
});

test({
name: "path - common - windows sep",
fn() {
const actual = common(
[
"c:\\deno\\cli\\js\\deno.ts",
"c:\\deno\\std\\path\\mod.ts",
"c:\\deno\\cli\\js\\main.ts",
],
"\\"
);
assertEquals(actual, "c:\\deno\\");
},
});
31 changes: 17 additions & 14 deletions std/path/mod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright the Browserify authors. MIT License.
// Ported from https://github.com/browserify/path-browserify/
// Ported mostly from https://github.com/browserify/path-browserify/

import * as _win32 from "./win32.ts";
import * as _posix from "./posix.ts";
Expand All @@ -10,20 +10,23 @@ const path = isWindows ? _win32 : _posix;

export const win32 = _win32;
export const posix = _posix;
export const resolve = path.resolve;
export const normalize = path.normalize;
export const isAbsolute = path.isAbsolute;
export const join = path.join;
export const relative = path.relative;
export const toNamespacedPath = path.toNamespacedPath;
export const dirname = path.dirname;
export const basename = path.basename;
export const extname = path.extname;
export const format = path.format;
export const parse = path.parse;
export const sep = path.sep;
export const delimiter = path.delimiter;
export const {
resolve,
normalize,
isAbsolute,
join,
relative,
toNamespacedPath,
dirname,
basename,
extname,
format,
parse,
sep,
delimiter,
} = path;

export { common } from "./common.ts";
export { EOL, SEP, SEP_PATTERN, isWindows } from "./constants.ts";
export * from "./interface.ts";
export * from "./glob.ts";
Expand Down
9 changes: 0 additions & 9 deletions std/path/test.ts

This file was deleted.

0 comments on commit 7670a13

Please sign in to comment.