UNPKG

@shockpkg/dir-projector

Version:

Package for creating Shockwave Director projectors

124 lines (117 loc) 3.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.align = align; exports.htmlEncode = htmlEncode; exports.launcher = launcher; exports.pathRelativeBase = pathRelativeBase; exports.pathRelativeBaseMatch = pathRelativeBaseMatch; exports.trimDotSlash = trimDotSlash; exports.trimExtension = trimExtension; var _nodeZlib = require("node:zlib"); var _launchers = require("./launchers.js"); /** * HTML encode. * * @param s Raw strings. * @param dq Double quotes. * @param sq Single quotes. * @returns Encoded strings. */ function htmlEncode(s, dq = false, sq = false) { s = s.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;'); if (dq) { s = s.replace(/"/g, '&quot;'); } if (sq) { s = s.replace(/'/g, '&#39;'); } return s; } /** * Trim dot flash from head of path. * * @param path Path string. * @returns Trimmed path. */ function trimDotSlash(path) { return path.replace(/^(\.\/)+/, ''); } /** * Find path relative from base, if base matches. * * @param path Path to match against. * @param start Search start. * @param nocase Match case-insensitive. * @returns Returns path, or null. */ function pathRelativeBase(path, start, nocase = false) { const p = trimDotSlash(nocase ? path.toLowerCase() : path); const s = trimDotSlash(nocase ? start.toLowerCase() : start); if (p === s) { return ''; } if (p.startsWith(`${s}/`)) { return path.slice(s.length + 1); } return null; } /** * Same as pathRelativeBase, but retuns true on a match, else false. * * @param path Path to match against. * @param start Search start. * @param nocase Match case-insensitive. * @returns Returns true on match, else false. */ function pathRelativeBaseMatch(path, start, nocase = false) { return pathRelativeBase(path, start, nocase) !== null; } /** * Trim a file extenion. * * @param path File path. * @param ext File extension. * @param nocase Match case-insensitive. * @returns Path without file extension. */ function trimExtension(path, ext, nocase = false) { const p = nocase ? path.toLowerCase() : path; const e = nocase ? ext.toLowerCase() : ext; return p.endsWith(e) ? path.slice(0, p.length - e.length) : path; } /** * Align integer. * * @param i Integer value. * @param align Alignment amount. * @returns Aligned integer. */ function align(i, align) { const o = i % align; return o ? align - o + i : i; } /** * Get launcher data for an ID. * * @param id Laucher ID. * @returns Launcher data. */ async function launcher(id) { const b64 = _launchers.LAUNCHERS[id]; if (typeof b64 !== 'string') { // eslint-disable-next-line unicorn/prefer-type-error throw new Error(`Invalid launcher id: ${id}`); } return new Promise((resolve, reject) => { (0, _nodeZlib.inflateRaw)(Buffer.from(b64, 'base64'), (err, data) => { if (err) { reject(err); return; } resolve(new Uint8Array(data.buffer, data.byteOffset, data.byteLength)); }); }); } //# sourceMappingURL=util.js.map