namespace

Dynamic namespace creation

APACHE-2.0 License

Downloads
12
Stars
0
Committers
1
namespace - @lopatnov/namespace Latest Release

Published by lopatnov about 4 years ago

@lopatnov/namespace

npm
NPM version
License
GitHub issues
GitHub forks
GitHub stars
GitHub top language

build-and-test-package
publish-npm-package
Libraries.io dependency status for latest release

Patreon
Twitter
LinkedIn

Dynamic namespace creation

Install

https://nodei.co/npm/@lopatnov/namespace.png?downloads=true&downloadRank=true&stars=true

npm install @lopatnov/namespace

Browser

<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); // 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);

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

namespace - @lopatnov/namespace

Published by lopatnov about 4 years ago

@lopatnov/namespace

npm
NPM version
License
GitHub issues
GitHub forks
GitHub stars
GitHub top language

build-and-test-package
publish-npm-package
Libraries.io dependency status for latest release

Patreon
Twitter
LinkedIn

Dynamic namespace creation

Install

https://nodei.co/npm/@lopatnov/namespace.png?downloads=true&downloadRank=true&stars=true

npm install @lopatnov/namespace

Browser

<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");

How to use

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

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