Would you like to clone this notebook?

When you clone a notebook you are able to make changes without affecting the original notebook.

Cancel

@lopatnov/namespace

node v10.24.1
version: 1.1.0
endpointsharetweet
const Namespace = require("@lopatnov/namespace")
var x = new Namespace("Eeny.meeny.miny.moe[Catch][a][tiger][by][the][toe]"); // <-- new Namespace console.log(x.Eeny.meeny.miny.moe.Catch.a.tiger.by.the.toe); console.log(x)
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);
var space = {}; 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 = new Namespace("Yellow.Submarine"); var y = n.goto("Yellow.Submarine"); // <-- goto(path: NamespacePath): any console.log(y === n.Yellow.Submarine);
var glob = new Namespace(); function namespace(path) { return function (target, propertyKey, descriptor) { const ns = glob.namespace(path); // <-- make inner namespace const name = propertyKey || target.prototype.constructor.name; ns[name] = propertyKey ? target[propertyKey] : target; }; } // decorators are not supported with runkit, but we can use it like a function namespace("white.animals") ( class Actions { makeAlbino(animal) { return `${animal} is white now`; } } ) console.log(new glob.white.animals.Actions().makeAlbino("rose panther"));
var 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'));
var store = Namespace('system.sample'); store.system.sample.Car = function Car( options ) { // some defaults this.doors = options.doors || 4; this.state = options.state || "brand new"; this.color = options.color || "silver"; } store.system.sample.Truck = function Truck( options){ this.state = options.state || "used"; this.wheelSize = options.wheelSize || "large"; this.color = options.color || "blue"; } // Define a skeleton vehicle factory store.system.sample.VehicleFactory = function VehicleFactory() {} // Define the prototypes and utilities for this factory // Our default vehicleClass is Car store.system.sample.VehicleFactory.prototype.vehicleClass = store.system.sample.Car; // Our Factory method for creating new Vehicle instances store.system.sample.VehicleFactory.prototype.createVehicle = function ( options ) { switch(options.vehicleType){ case "car": this.vehicleClass = store.system.sample.Car; break; case "truck": this.vehicleClass = store.system.sample.Truck; break; //defaults to VehicleFactory.prototype.vehicleClass (Car) } return new this.vehicleClass( options ); }; // Create an instance of our factory that makes cars const VehicleFactory = store.system.sample.VehicleFactory; var carFactory = new VehicleFactory(); var car = carFactory.createVehicle( { vehicleType: "car", color: "yellow", doors: 6 } ); // Test to confirm our car was created using the vehicleClass/prototype Car // Outputs: true console.log( car instanceof store.system.sample.Car ); // Outputs: Car object of color "yellow", doors: 6 in a "brand new" state console.log( car );
Loading…

no comments

    sign in to comment