New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

namespace

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

namespace

Library for defining namespaced properties.

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

namespace

Build Status

Library provides an API for creating sets of namespaced properties for any given objects (frozen or not). This effectively may be used for creating fields that are not part of object's public API, which is useful for hiding internal details or for adding fields to an existing objects (Built-ins or not) without mutating them and there for any risks of naming conflicts.

Usage

It is recommended to use this library with enabled WeakMaps. On node that simply means running it with additional flag: node --harmony_weakmaps. If weak maps are not available library will fallback to using imperfect weak map shim.

var ns = require('namespace/core').ns
var internals = ns()

internals(publicAPI).secret = secret

Namespace may be used with multiple objects:

var observable = ns()

function Observable() {
  observable(this).observers = []
}
Observable.prototype.observe = function(observer) {
  observable(this).observers.push(observer)
}

Also, multiple namespaces can be used with a same object without any conflicts.

var pending = ns()

function Eventual() {
  Observable.call(this)
  pending(this).realized = false
}
Eventual.prototype = Object.create(Observable.prototype)
Eventual.prototype.realize = function realize(value) {
  if (!pending(this).realized) {
    obesrvable(this).observers.splice.forEach(function(observer) {
      observer(value)
    })
  }
}

Access to the namespaced properties can be shared with other code by simple handing a namespace function. Although doing this across modules is not recommended, for example instead of sharing pending namespace one could share following function instead:

exports.isRealized = function isRealized(value) {
  return pending(value).realized
}

Namespaced objects create parallel inheritance chain, or more simply:

var foo = ns()
var ancestor = {}

foo(ancestor) === foo(Object.create(ancestor)) // => true

Namespaces are simply a sugar on top of ES.next WeakMaps allowing you to associate sets of namespaced properties to an object via weak references.

Install

npm install namespace

Keywords

namespace

FAQs

Package last updated on 17 May 2012

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts