mirror of
https://github.com/smogon/sprites.git
synced 2026-04-22 17:57:25 -05:00
34 lines
959 B
TypeScript
34 lines
959 B
TypeScript
|
|
import pathlib from 'path';
|
|
|
|
type PathAttrs = {dir : string, name : string, ext : string | null};
|
|
|
|
export class Path {
|
|
public readonly dir : string;
|
|
public readonly name : string;
|
|
public readonly ext : string | null;
|
|
|
|
constructor({dir, name, ext} : PathAttrs) {
|
|
this.dir = dir;
|
|
this.name = name;
|
|
this.ext = ext;
|
|
}
|
|
|
|
static parse(x : string) {
|
|
const {dir, name, ext} = pathlib.parse(x);
|
|
return new Path({dir, name, ext: ext === "" ? null : ext.slice(1)});
|
|
}
|
|
|
|
update({dir, name, ext} : Partial<PathAttrs>) {
|
|
return new Path({dir: dir ?? this.dir,
|
|
name : name ?? this.name,
|
|
ext : ext === undefined ? this.ext : ext});
|
|
}
|
|
|
|
format() {
|
|
return pathlib.format({dir : this.dir,
|
|
name : this.name,
|
|
ext : this.ext === null ? "" : `.${this.ext}`});
|
|
}
|
|
}
|