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

namespace-proxy

v0.1.1

Published

A simple ES2015 Proxy-based namespace factory

Downloads

3

Readme

namespace-proxy

Build Status npm License

The namespace-proxy is a utility for creating namespaces in objects (similar to those provided by Mozilla's SDK).

A namespace can be used to prevent symbol collisions in object's properties, or to create private APIs and properties.

The main difference between the namespace-proxy and Mozilla's namespace is the use of ES2015 Proxy which allows the namespace-proxy to store the namespaced properties directly on the target object, instead of having a separate storage of these properties.

Furthermore, the namespace-proxy stores all properties via ES2015 symbols, which are unique to each created namespace and thus prevent name collisions.

Setup

You can install the namespace-proxy utility using npm:

npm install --save namespace-proxy

Usage example

The createNamespaceProxy function creates a function that accepts an object and returns a proxy object which is used to access the namespace within the passed-in object.

The created function also is a Proxy, which provides automatically generated symbols by accessing them, and can be used for static properties.

import createNamespaceProxy from "namespace-proxy"

const PRIVATE = createNamespaceProxy()
const PROTECTED = createNamespaceProxy(true) // properties are enumerable

export default class Point {
  constructor(x, y) {
    PROTECTED(this).x = x // stores x in this instance using a symbol named "x"
    PROTECTED(this).y = y
  }

  static get PROTECTED() {
    return PROTECTED // allow other code (subclases) to access this namespace
  }

  get x() {
    return PROTECTED(this).x
  }

  get y() {
    return PROTECTED(this).x
  }

  get distanceFromOrigin() {
    let d1 = PRIVATE.getDistance(this.x, 0)
    let d2 = PRIVATE.getDistance(this.y, 0)
    return Math.sqrt(Math.pow(d1, 2) + Math.pow(d2, 2))
  }

  // method named using a symbol named "getDistance"
  [PRIVATE.getDistance](d1, d2) {
    return Math.abs(d2 - d1)
  }

  // static method named using a symbol named "create"
  static [PROTECTED.create](x, y) {
    return new Point(x, y)
  }
}

Browser and Node.js support

Since the namespace-proxy requires the Proxy API which cannot be polyfilled, only Node.js 6+ and modern browsers are supported. You can check the current list of supported browsers at caniuse.com or the MDN.

At the moment of writing this, the following browsers were supported:

  • Chrome/Chromium 49+
  • Firefox 18+
  • Edge 12+
  • Opera & Opera Mobile 36+
  • Chrome & Firefox for Android
  • Android System WebView / Android Browser 49+

It appears the Safari and iOS browsers will be supported from the version 10.

The current state of this project

There are no current plans for additional features (unless a good case for adding them is made), but the project accepts bug fixes if new bugs are discovered.

Contributing

Time is precious, so, if you would like to contribute (bug fixing, test writing or feature addition), follow these steps:

  • fork
  • implement (follow the code style)
  • pull request
  • wait for approval/rejection of the pull request

Filing a bug issue might get me to fix the bug, but filing a feature request will not as I don't have the time to work on this project full-time. You may still propose new features through pull requests. Thank you for understanding.