Dynamic namespace creation
npm install @lopatnov/namespace
<script src="//lopatnov.github.io/namespace/dist/namespace.min.js"></script>
import Namespace from "@lopatnov/namespace";
var Namespace = require("@lopatnov/namespace");
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);
var n = Namespace("A.Pacific.Ocean"); // <-- without new
console.log(n.A.Pacific.Ocean instanceof Namespace);
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); // true
var 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);
var n: any = new Namespace("Yellow.Submarine");
var y = n.goto("Yellow.Submarine"); // <-- goto(path: NamespacePath): any
console.log(y === n.Yellow.Submarine);
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'
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'));
See, how it's working: https://runkit.com/lopatnov/namespace
Test it with a runkit: https://npm.runkit.com/@lopatnov/namespace
License Apache-2.0
Copyright 2020 Oleksandr Lopatnov
Published by lopatnov about 4 years ago
Dynamic namespace creation
npm install @lopatnov/namespace
<script src="//lopatnov.github.io/namespace/dist/namespace.min.js"></script>
import Namespace from "@lopatnov/namespace";
var Namespace = require("@lopatnov/namespace");
window.globalSpace = new Namespace('Eeny.meeny.miny.moe[Catch][a][tiger][by][the][toe]');
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); // true
See, how it's working: https://runkit.com/lopatnov/namespace
Test it with a runkit: https://npm.runkit.com/@lopatnov/namespace
License Apache-2.0
Copyright 2020 Oleksandr Lopatnov