function shortenSpecifier
shortenSpecifier(specifier: string): string

Renders the shortest form of a specifier that still resolves back to it.

The CLI echoes the resolved specifier once, as a header, and uses this short form everywhere else — in listings and in the paste-ready TRY lines — since the shorthand only helps if the tool teaches it. Each candidate is classified and expanded again before it is offered, so a shortening that would be read back as something else (a path, say) is never proposed.

Examples

The implied scheme and scope drop away

import { assertEquals } from "@std/assert";
import { shortenSpecifier } from "./specifier.ts";

assertEquals(shortenSpecifier("jsr:@binstruct/png"), "png");
assertEquals(shortenSpecifier("jsr:@hertzg/xhb"), "@hertzg/xhb");
assertEquals(shortenSpecifier("npm:foo"), "npm:foo");

Shortening never produces something that reads back as a path

import { assertEquals } from "@std/assert";
import { resolveSpecifier, shortenSpecifier } from "./specifier.ts";

const short = shortenSpecifier("jsr:@binstruct/png/sub");

assertEquals(short, "@binstruct/png/sub");
assertEquals(resolveSpecifier(short).specifier, "jsr:@binstruct/png/sub");
assertEquals(resolveSpecifier("png/sub").form, "path");

Parameters

specifier: string

A resolved module specifier.

Return Type

string

The shortest equivalent input, or the specifier itself when it cannot be shortened.