Package Exports
- @lopatnov/namespace
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@lopatnov/namespace) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@lopatnov/namespace
Dynamic namespace creation
Install
npm install @lopatnov/namespace<script src="//lopatnov.github.io/namespace/dist/namespace.min.js"></script>Import package to the project
TypeScript
import Namespace from "@lopatnov/namespace";JavaScript
var Namespace = require("@lopatnov/namespace");TypeScript and JavaScript samples
new operator
var n = new Namespace("Hello.World");
console.log((n as any).Hello.World instanceof Namespace);window.globalSpace = new Namespace(
"Eeny.meeny.miny.moe[Catch][a][tiger][by][the][toe]"
);
console.log(Eeny.meeny.miny.moe.Catch.a.tiger.by.the.toe);direct call
var n = Namespace("A.Pacific.Ocean"); // <-- without new
console.log(n.A.Pacific.Ocean instanceof Namespace);apply namespace object to another
var x = new Namespace("Games.World");
var y = {};
var z = x.goto("Games.World");
x.applyTo(y, "Hello");
console.log(z === y.Hello.Games.World); // truevar space: any = {};
var n = new Namespace("cruising.airliner");
n.applyTo(space, "A"); // <-- applyTo(context: any, name: string): void
console.log(space.A.cruising.airliner instanceof Namespace);get inner object
var n: any = new Namespace("Yellow.Submarine");
var y = n.goto("Yellow.Submarine"); // <-- goto(path: NamespacePath): any
console.log(y === n.Yellow.Submarine);make inner namespace
const glob: Namespace & any = new Namespace();
function namespace(path: string) {
return function (
target: any,
propertyKey?: string,
descriptor?: PropertyDescriptor
) {
const ns = glob.namespace(path); // <-- make inner namespace
const name = propertyKey || target.prototype.constructor.name;
ns[name] = propertyKey ? target[propertyKey] : target;
};
}
@namespace("white.animals")
class Actions {
@namespace("white.animals")
makeAlbino(animal: string) {
return `${animal} is white now`;
}
}
console.log(glob.white.animals.makeAlbino("unicorn")); // 'unicorn is white now'
console.log(new glob.white.animals.Actions().makeAlbino("rose panther")); // 'rose panther is white now'check if path exists
const ns = new Namespace();
ns.namespace("a.b.c.d");
ns.namespace("a.b.c.e");
ns.namespace("a.b.f.g");
ns.namespace("a.i.h.k");
ns.namespace("a.i.h.l");
ns.namespace("a.m.n.o");
console.log(ns.exists('a.b.c.d'));
console.log(ns.exists('a.b.c.e'));
console.log(ns.exists('a.b.f.g'));
console.log(ns.exists('a.i.h.k'));
console.log(ns.exists('a.i.h.l'));
console.log(ns.exists('a.m.n.o'));Demo
See, how it's working: https://runkit.com/lopatnov/namespace
Test it with a runkit: https://npm.runkit.com/@lopatnov/namespace
Rights and Agreements
License Apache-2.0
Copyright 2020 Oleksandr Lopatnov
