lakutata
Version:
An IoC-based universal application framework.
112 lines (92 loc) • 3.37 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, {
value: "Module"
});
const a = require("path");
const e = require("../../exceptions/alias/InvalidAliasNameException.cjs");
const s = require("../../exceptions/alias/AliasExistsException.cjs");
const t = require("../../exceptions/alias/AliasNotFoundException.cjs");
require("../base/abstracts/Exception.cjs");
require("../base/internal/BasicInfo.cjs");
require("../helpers/As.cjs");
require("../../../vendor/Package.6.cjs");
require("../../../vendor/Package.5.cjs");
require("../../../vendor/Package.7.cjs");
require("../base/internal/ThrowWarning.cjs");
require("../helpers/Templating.cjs");
require("../base/internal/CamelCase.cjs");
require("../helpers/NoCase.cjs");
const i = a => a && a.__esModule ? a : {
default: a
};
const l = i(a);
class Alias {
constructor() {
this.aliasNameRegExp = /^@[a-zA-Z0-9]+$/;
this.aliasMap = new Map;
const a = l.default.resolve;
l.default.resolve = (...e) => {
const s = e.map((a => this.resolve(a)));
return a(...s);
};
}
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, t, i = false) {
if (!this.aliasNameRegExp.test(a)) throw new e.InvalidAliasNameException("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 s.AliasExistsException("The alias '{aliasName}' already exists.", {
aliasName: a
});
this.aliasMap.set(a, t);
}
get(a) {
if (!this.aliasNameRegExp.test(a)) throw new e.InvalidAliasNameException("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 t.AliasNotFoundException("The alias '{aliasName}' not found.", {
aliasName: a
});
const s = this.aliasMap.get(a);
return this.resolve(s);
}
has(a) {
if (!this.aliasNameRegExp.test(a)) throw new e.InvalidAliasNameException("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(a) {
let e = true;
const s = [];
this.aliasMap.forEach(((a, e) => s.push({
name: e,
path: a
})));
s.sort(((a, e) => a.name.length < e.name.length ? 1 : -1));
while (e) {
let t = false;
s.forEach((({name: e, path: s}) => {
if (a.includes(e)) {
t = true;
a = a.replace(e, s);
}
}));
e = t;
}
return l.default.normalize(a);
}
list() {
const a = {};
this.aliasMap.forEach(((e, s) => Reflect.set(a, s, e)));
return a;
}
}
exports.Alias = Alias;