npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@lopatnov/namespace

v1.3.0

Published

Dynamic namespace creation

Downloads

6

Readme

@lopatnov/namespace Twitter

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 sobe.ru 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

namespace creation

const ns = new Namespace(); // <-- create namespace variable

ns.namespace("a.b.c.d"); // <-- create inner namespaces
ns.namespace("a.b.c.e"); // <-- Function namespace(path: NamespacePath): Namespace
ns.namespace("a.b.f.g"); // <--
ns.namespace("a.i.h.k"); // <--
ns.namespace("a.i.h.l"); // <--
ns.namespace("a.m.n.o"); // <--

// Access to inner namespaces
const anyNS: any = ns; // <-- to avoid type casting
console.log(anyNS.a.b.f.g instanceof Namespace); // true
console.log((ns as any).a.b.c.d instanceof Namespace); // true
console.log((ns as any).a.b.c.e instanceof Namespace); // true

attach namespace to a current object

let shapes = {
    color: "green",
    count: 3
};
let shapesSpace = Namespace.attach(shapes);

shapesSpace.namespace('Triangles');
shapesSpace.namespace('Circles');
shapesSpace.namespace('Rectangles.Squares');

console.log(shapesSpace.exists('Triangles')); // true
console.log(shapesSpace.exists('Circles')); // true
console.log(shapesSpace.exists('Rectangles.Squares')); // true
console.log(shapesSpace.color); // "green"
console.log(shapesSpace.count); // 3
console.log(shapes === shapesSpace); // true

checking that object inside namespace exists

// Function exists(path: NamespacePath): boolean
console.log(ns.exists('a.b.c.d')); // true
console.log(ns.exists('a.b.c.e')); // true
console.log(ns.exists('a.b.f.g')); // true
console.log(ns.exists('a.i.h.k')); // true
console.log(ns.exists('a.i.h.l')); // true
console.log(ns.exists('a.m.n.o')); // true

create namespace through direct call

var dynamicNamespace: any = Namespace; // <-- escape current TypeScript restrictions with any Type
var a = dynamicNamespace('Pacific.Ocean'); // <-- call as function
console.log(a.Pacific.Ocean instanceof Namespace); // true

create namespace through new operator

var n: any = new Namespace("Hello.World");
console.log(n.Hello.World instanceof Namespace); // true
var eeny: any = new Namespace('meeny.miny.moe[Catch][a][tiger][by][the][toe]');
console.log(eeny.meeny.miny.moe.Catch.a.tiger.by.the.toe instanceof Namespace);  // true

go to inner object

var kitchenRadar = new Namespace();
    kitchenRadar.namespace('big.fruits');
    kitchenRadar.namespace('big.eggs');
    kitchenRadar.namespace('small.dishes');
    kitchenRadar.namespace('small.cookies');

    var fruits = kitchenRadar.goto('big.fruits'); // <-- goto(path: NamespacePath): any
    var eggs = kitchenRadar.goto('big.eggs');
    var dishes = kitchenRadar.goto('small.dishes');
    var cookies = kitchenRadar.goto('small.cookies');

    console.log(fruits === (kitchenRadar as any).big.fruits); // true
    console.log(eggs === (kitchenRadar as any).big.eggs); // true
    console.log(dishes === (kitchenRadar as any).small.dishes); // true
    console.log(cookies === (kitchenRadar as any).small.cookies); // true

apply namespace as a property of an object

var persons: any = {};
var extension = new Namespace('with.small.kitty');

extension.applyTo(persons, 'Julia'); // Function applyTo(context: any, name: string): void
extension.applyTo(persons, 'Kathy');
extension.applyTo(persons, 'Liza');

console.log(persons.Julia.with.small.kitty instanceof Namespace); // true
console.log(persons.Kathy.with.small.kitty instanceof Namespace); // true
console.log(persons.Liza.with.small.kitty instanceof Namespace); // 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–2021 Oleksandr Lopatnov