@cashfarm/lang
Version:
Extends TypeScript/Javascript with basic classes and functions
39 lines • 1.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const is_1 = require("./is");
const symbols_1 = require("./symbols");
function FQN(fqn) {
return (target) => {
target[symbols_1.Symbols.FQN] = fqn;
};
}
exports.FQN = FQN;
function parseFQN(fqn) {
const [pkg, property, ...rest] = fqn.split(':');
if (is_1.isEmpty(pkg) || is_1.isEmpty(property) || rest.length)
throw new Error(`Invalid FQN "${fqn}". FQN format should be "{PACKAGE}:[NS.]?{CLASS}"`);
const parts = property.split('.');
return {
package: pkg,
namespace: parts[0],
propertyPath: parts.join('.'),
className: parts.pop()
};
}
exports.parseFQN = parseFQN;
function requireByFQN(fqn) {
const info = parseFQN(fqn);
const pkg = require(info.package);
const props = info.propertyPath.split('.');
let currLocation = `package ${pkg}`;
return props.reduce((currNS, currProp, index) => {
if (typeof currNS[currProp] === undefined) {
throw new Error(`Requiring from FQN ${fqn} failed.` +
`Property ${currProp} does not exist in ${currLocation}`);
}
currLocation = props.slice(0, index).join('.');
return currNS[currProp];
}, pkg);
}
exports.requireByFQN = requireByFQN;
//# sourceMappingURL=fqn.js.map