deploy: spritedata take 2 (spritename)

This commit is contained in:
Christopher Monsanto 2020-07-27 22:03:52 -04:00
parent 1d8a4d2379
commit e7e3d9bc3c
3 changed files with 56 additions and 93 deletions

View File

@ -1,31 +1,10 @@
import pathlib from 'path';
const toPathStringSym = Symbol('toPathStringSym');
// Slight variation of pathlib parse, less fields, different ext handling
export type Path = {dir : string, name : string, ext : string | null};
export interface IPath {
[toPathStringSym](): string;
}
export type PathLike = IPath | string;
class PathClass {
constructor(public readonly dir : string,
public readonly name : string,
public readonly ext : string | null) {}
[toPathStringSym]() {
return pathlib.format({dir : this.dir,
name : this.name,
ext : this.ext === null ? "" : `.${this.ext}`});
}
}
export type Path = PathClass;
export type PathDelta = {dir? : string, name? : string, ext? : string | null};
export function path(p : PathLike, delta : PathDelta = {}) : Path {
const s = typeof p === 'string' ? p : p[toPathStringSym]();
export function parse(s : string) : Path {
let {dir, name, ext: dotext} = pathlib.parse(s);
let ext;
if (dotext === "") {
@ -33,8 +12,31 @@ export function path(p : PathLike, delta : PathDelta = {}) : Path {
} else {
ext = dotext.slice(1);
}
dir = delta.dir ?? dir;
name = delta.name ?? name;
ext = delta.ext === undefined ? ext : delta.ext;
return new PathClass(dir, name, ext);
return {dir, name, ext};
}
export function format({dir, name, ext} : Path) {
const dotext = ext === null ? "" : `.${ext}`;
return pathlib.format({dir, name, ext : dotext});
}
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;
}

View File

@ -1,65 +0,0 @@
type SpriteDataAttrs = {id : number | string,
extra : [string, string][]};
export class SpriteData {
public readonly id : number | string;
private extra : Map<string, string>;
constructor({id, extra}: SpriteDataAttrs) {
this.id = id;
this.extra = new Map;
for (const [k,v] of extra) {
if (v === "") {
// Don't store empty entries. Can't just avoid set()-ing,
// because might set "" after another entry to blot it out
this.extra.delete(k);
} else {
this.extra.set(k, v);
}
}
}
get(x : string) : string {
return this.extra.get(x) ?? "";
}
[Symbol.iterator]() {
return this.extra[Symbol.iterator]();
}
static parse(s : string) {
const parts = s.split("-");
const id = parts[0].match(/^[0-9]+$/) ? parseInt(parts[0], 10) : parts[0];
const extra : [string, string][] = [];
for (const part of parts.slice(1)) {
if (part.length === 0)
throw new Error(`Can't parse ${s}`);
extra.push([part[0], part.slice(1)]);
}
return new SpriteData({id, extra});
}
update({id, extra} : Partial<SpriteDataAttrs>) {
return new SpriteData({id : id ?? this.id,
extra: [...this, ...(extra ?? [])]});
}
format() {
let s = this.id.toString();
const extra = Array.from(this);
extra.sort(([k1,], [k2,]) => {
if (k1 < k2) {
return -1;
} else if (k1 > k2) {
return 1;
} else {
return 0;
}
});
for (const [k, v] of extra) {
s += `-${k}${v};`
}
return s;
}
}

View File

@ -0,0 +1,26 @@
export type SpriteName = {id : number | string, extra : [string, string][]};
export function parse(s : string) : SpriteName {
const parts = s.split("-");
const id = parts[0].match(/^[0-9]+$/) ? parseInt(parts[0], 10) : parts[0];
const extra : [string, string][] = [];
for (const part of parts.slice(1)) {
if (part.length === 0)
throw new Error(`Can't parse ${s}`);
extra.push([part[0], part.slice(1)]);
}
return {id, extra};
}
export function format(si : SpriteName) {
let s = si.id.toString();
const extra = [];
for (const [k, v] of si.extra) {
extra.push(`-${k}${v}`);
}
extra.sort();
return s + extra.join('');
}
// TODO: update()?