mirror of
https://github.com/smogon/sprites.git
synced 2026-12-04 22:18:12 -06:00
Annotation colons are tight (ternaries keep their spaces), plain double-quoted strings become single-quoted, and .editorconfig comes over verbatim. Rule cmd values are untouched; the sheet rules reran once because their tool sources are declared deps, byte-identically. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
47 lines
1.2 KiB
TypeScript
47 lines
1.2 KiB
TypeScript
|
|
import pathlib from 'path';
|
|
|
|
// Slight variation of pathlib parse, less fields, different ext handling
|
|
export type Path = {dir: string, name: string, ext: string | null};
|
|
|
|
export function parse(s: string): Path {
|
|
let {dir, name, ext: dotext} = pathlib.parse(s);
|
|
let ext;
|
|
if (dotext === '') {
|
|
ext = null;
|
|
} else {
|
|
ext = dotext.slice(1);
|
|
}
|
|
return {dir, name, ext};
|
|
}
|
|
|
|
export function format({dir, name, ext}: Path) {
|
|
const dotext = ext === null ? '' : `.${ext}`;
|
|
return pathlib.format({dir, name, ext: dotext});
|
|
}
|
|
|
|
export function join(s: string, {dir, name, ext}: Path): Path {
|
|
return {dir: pathlib.join(s, dir), name, ext};
|
|
}
|
|
|
|
export type Delta = Partial<Path>;
|
|
|
|
export function update({dir, name, ext}: Path, delta: Delta): Path {
|
|
return {
|
|
dir: delta.dir ?? dir,
|
|
name: delta.name ?? name,
|
|
ext: delta.ext === undefined ? ext : delta.ext
|
|
};
|
|
}
|
|
|
|
// Convenience function
|
|
|
|
export type PathLike = Path | string;
|
|
|
|
export function path(p: PathLike, delta?: Delta): Path {
|
|
let parsed = typeof p === 'string' ? parse(p) : p;
|
|
if (delta !== undefined)
|
|
parsed = update(parsed, delta);
|
|
return parsed;
|
|
}
|