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

oktopost-namespace

v2.0.2

Published

Namespace library for js

Downloads

38

Readme

oktopost-namespace

npm version Build Status

The oktopost-namespace library aims to implement the usage of Namespaces inside JavaScript projects.

Table Of Contents

Installation

npm install oktopost-namespace --save

Basic Example

The following example will assume the next directory structure:

src
  Example
    Subdir
      sub.js
      sum.js
    calc.js
namespace.json
index.js

./src/Example/Subdir/sub.js

Define a new function named sub inside the namespace Example.Subdir

namespace('Example.Subdir', function () 
{
	this.sub = function sub(a, b)
	{
		return a - b;
	}
});

./src/Example/Subdir/sum.js

Define a new function named sum inside the namespace Example.Subdir

namespace('Example.Subdir', function () 
{
	this.sum = function sum(a, b)
	{
		return a + b;
	}
});

./src/Example/calc.js

Define a new function named calc inside the namespace Example

namespace('Example', function () 
{
	this.sum = function sum(a, b)
	{
		return a + b;
	}
});

./namespace.json

namespace.json is the configuration file for the Namespace library.

{
	"map":
	{
		"dir":
		{
			"Example": "./src/"
		}
	}
}

./index.js

Load and setup the Namespace library. After calling the method virtual, the function namespace is registered into the global scope and can be called using global.namesapce(...) or just namespace(...).

var root = require('oktopost-namespace').virtual(__dirname);
module.exports = root.Example;

Most initialization methods, including virtual, will return the root object in which all the namespaces are stored.

Few notes:

  • Directory names should match the namespace path.
  • In the current version, this should not introduce more then one new definition per file into the namespaces scope. Or in other words, don't use this.something = something; more then once in the same JavaScript file.

Building With Gulp Example

Inside your gulp.js file, you can use the following snippet:

let result = require('oktopost-namespace').getDependencies(
	__dirname, 
	() => {}, 
	(root) =>
	{
		const calc = root.Example.calc;
	});

The result variable will be equal to an array of file names ordered by thier dependency priority. Starting from the files that have no depends at all, and all the way to the enrty-point file of the project - that depends on all other library files.

In this case result it will be equal to:

[
	'src/Example/Subdir/sub.js',
	'src/Example/Subdir/sum.js',
	'src/Example/calc.js'
]

If your project depends on any other files from different libraries, they will also be included inside this array. For example:

[
	'node_modules/my_lib/src/other_file.js',
	'src/Example/calc.js'
]

The getDependencies(path, setupCallback, initCallback) method:

  • path must be the full directory path to index.js file.
  • setupCallback this function is called before resolving dependencies. You can leave it empty for most cases.
  • initCallback is a function that is used to load all dependencies. In most cases this can be done by loading the entry-point object of your library. In this case it's the calc function.

More To Read

For more generic example, see the content of docs/example_01

git clone [email protected]:Oktopost/namespace.git
cd namespace/docs/example_01
npm install
node run_me.js
node run_me_gulp.js