@zerospacegg/iolin
Version:
Pure TypeScript implementation of ZeroSpace game data processing (PKL-free)
66 lines • 2.27 kB
JavaScript
/**
* Core types and utilities for the PKL-free engine
* Ported from types.pkl
*/
/**
* Convert a pretty name to a slug
*/
export function slugify(name) {
return name
.toLowerCase()
.replace(/[^a-z0-9.]+/g, "-")
.replace(/^-+|-+$/g, "");
}
/**
* Convert a slug back to a pretty name (capitalize words)
*/
export function prettifySlug(slug) {
return slug
.split("-")
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(" ");
}
/**
* Convert a slug to camelCase
* Usage: slugToCamelCase("symbiotic-surge") => "symbioticSurge"
*/
export function slugToCamelCase(slug) {
return slug
.split("-")
.map((word, index) => index === 0 ? word.toLowerCase() : word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
.join("");
}
/**
* Create recursive slug (from PKL makeRecursiveSlug function)
*/
export function makeRecursiveSlug(name, ofKey, typeKey) {
const s = slugify(name);
const z = s === typeKey ? "" : `-${s}`;
return `${ofKey}-${typeKey}${z}`;
}
/**
* Parse filename to extract path-based ID and slug
* Extracts everything after "zerospace/" and removes file extensions
* Converts underscores to hyphens for slugs
*
* Examples:
* - "/path/to/zerospace/faction/grell/unit/seedling.ts" → { id: "faction/grell/unit/seedling", slug: "seedling" }
* - "zerospace/faction/grell/unit/man_eater.ts" → { id: "faction/grell/unit/man-eater", slug: "man-eater" }
*/
export function parseFilename(filename) {
// Remove file extension (.js, .ts, .mjs, .cjs, etc)
let cleanPath = filename.replace(/\.[^.]+$/, "");
// Extract everything after "zerospace/"
const zeroSpaceIndex = cleanPath.indexOf("zerospace/");
if (zeroSpaceIndex === -1) {
throw new Error(`Filename must contain "zerospace/" path: ${filename}`);
}
// Get the path after "zerospace/"
const pathAfterZerospace = cleanPath.substring(zeroSpaceIndex + "zerospace/".length);
// Convert underscores to hyphens for slug compatibility
const id = pathAfterZerospace.replace(/_/g, "-");
// Extract just the filename part for the slug
const slug = id.split("/").pop() || "";
return { id, slug };
}
//# sourceMappingURL=core.js.map