# oktopost-namespace

> Namespace library for js

Latest version **2.1.2** (published 2025-08-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install oktopost-namespace
pnpm add oktopost-namespace
yarn add oktopost-namespace
bun add oktopost-namespace
```

## Health

**Score 30/100 (F)** — status: maintenance-mode.

Positive: no vulnerabilities; has provenance.

Warnings: low downloads; no types; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.2 |
| Published | 2025-08-21 |
| First published | 2017-04-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 83.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 0 |
| Maintainers | hillel-oktopost, oktopost-bot, roman_oktopost, orlov_s_s, alexey.pkv, grohman, ivan-demchenkov |

## Links

- npm: https://www.npmjs.com/package/oktopost-namespace
- Repository: https://github.com/Oktopost/namespace
- Homepage: https://github.com/Oktopost/namespace#readme
- Issues: https://github.com/Oktopost/namespace/issues
- npm.io page: https://npm.io/package/oktopost-namespace

## Recent versions

- 2.1.2 (latest) — 2025-08-21
- 2.1.0 — 2025-08-21
- 2.0.2 — 2017-07-16
- 2.0.1 — 2017-07-14
- 2.0.0 — 2017-06-15
- 1.2.0 — 2017-04-10
- 1.1.0 — 2017-04-09
- 1.0.3 — 2017-04-09
- 1.0.2 — 2017-04-03
- 1.0.1 — 2017-04-03
- 1.0.0 — 2017-04-02

## README

# oktopost-namespace 

[![npm version](https://img.shields.io/npm/v/oktopost-namespace.svg)](https://www.npmjs.com/package/oktopost-namespace)
[![Build Status](https://travis-ci.org/Oktopost/namespace.svg?branch=master)](https://travis-ci.org/Oktopost/namespace)

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


## Table Of Contents

  * [Installation](#installation)
  * [Basic Example](#basic-example)
  * [Building With Gulp Example](#building-with-gulp-example)
  * [More To Read](#more-to-read)


## Installation

```bash
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`
 
```js
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`

```js
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`

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

**./namespace.json**

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

```json
{
	"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(...)`.

```js
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:

```js
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:
```js
[
	'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: 

```js
[
	'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](docs/example_01)**

```ssh
git clone git@github.com:Oktopost/namespace.git
cd namespace/docs/example_01
npm install
node run_me.js
node run_me_gulp.js
```

---
_Source: https://npm.io/package/oktopost-namespace · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
