All files / path / _common / format.ts

100.00% Branches 12/12
100.00% Lines 17/17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 
 
 
 
 
x47
x47
x47
 
x118
x118
x118
x118
x118
x118
x142
x118
 
x47
x110
x113
x113
 
x113
x110























// Copyright 2018-2025 the Deno authors. MIT license.
// This module is browser compatible.

import type { ParsedPath } from "../types.ts";

export function _format(
  sep: string,
  pathObject: Partial<ParsedPath>,
): string {
  const dir: string | undefined = pathObject.dir || pathObject.root;
  const base: string = pathObject.base ||
    (pathObject.name ?? "") + (pathObject.ext ?? "");
  if (!dir) return base;
  if (base === sep) return dir;
  if (dir === pathObject.root) return dir + base;
  return dir + sep + base;
}

export function assertArg(pathObject: Partial<ParsedPath>) {
  if (pathObject === null || typeof pathObject !== "object") {
    throw new TypeError(
      `The "pathObject" argument must be of type Object, received type "${typeof pathObject}"`,
    );
  }
}