UNPKG

lakutata

Version:

An IoC-based universal application framework.

100 lines (84 loc) 3.1 kB
import a from "path"; import { InvalidAliasNameException as s } from "../../exceptions/alias/InvalidAliasNameException.mjs"; import { AliasExistsException as t } from "../../exceptions/alias/AliasExistsException.mjs"; import { AliasNotFoundException as e } from "../../exceptions/alias/AliasNotFoundException.mjs"; import "../base/abstracts/Exception.mjs"; import "../base/internal/BasicInfo.mjs"; import "../helpers/As.mjs"; import "../../../vendor/Package.6.mjs"; import "../../../vendor/Package.5.mjs"; import "../../../vendor/Package.7.mjs"; import "../base/internal/ThrowWarning.mjs"; import "../helpers/Templating.mjs"; import "../base/internal/CamelCase.mjs"; import "../helpers/NoCase.mjs"; class Alias { constructor() { this.aliasNameRegExp = /^@[a-zA-Z0-9]+$/; this.aliasMap = new Map; const s = a.resolve; a.resolve = (...a) => { const t = a.map((a => this.resolve(a))); return s(...t); }; } static init() { const a = new this; Reflect.defineMetadata(this, a, this); } static getAliasInstance() { if (!Reflect.hasOwnMetadata(this, this)) this.init(); return Reflect.getMetadata(this, this); } set(a, e, i = false) { if (!this.aliasNameRegExp.test(a)) throw new s("The alias should start with '@' and only contain characters a-z|A-Z|0-9. The current alias '{aliasName}' does not comply with this rule.", { aliasName: a }); if (this.aliasMap.has(a) && !i) throw new t("The alias '{aliasName}' already exists.", { aliasName: a }); this.aliasMap.set(a, e); } get(a) { if (!this.aliasNameRegExp.test(a)) throw new s("The alias should start with '@' and only contain characters a-z|A-Z|0-9. The current alias '{aliasName}' does not comply with this rule.", { aliasName: a }); if (!this.aliasMap.has(a)) throw new e("The alias '{aliasName}' not found.", { aliasName: a }); const t = this.aliasMap.get(a); return this.resolve(t); } has(a) { if (!this.aliasNameRegExp.test(a)) throw new s("The alias should start with '@' and only contain characters a-z|A-Z|0-9. The current alias '{aliasName}' does not comply with this rule.", { aliasName: a }); return this.aliasMap.has(a); } resolve(s) { let t = true; const e = []; this.aliasMap.forEach(((a, s) => e.push({ name: s, path: a }))); e.sort(((a, s) => a.name.length < s.name.length ? 1 : -1)); while (t) { let a = false; e.forEach((({name: t, path: e}) => { if (s.includes(t)) { a = true; s = s.replace(t, e); } })); t = a; } return a.normalize(s); } list() { const a = {}; this.aliasMap.forEach(((s, t) => Reflect.set(a, t, s))); return a; } } export { Alias };