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

es6-privatize

v1.2.0

Published

Privatizes es6 and es7 classes with minimal overhead

Downloads

3

Readme

es6-privatize

Privatizes es6 and es7 classes with minimal overhead

Options

The options object has three properties:

  • warn - boolean value which indicates if the Privatize function warns about possible data leakage. Default: true
  • privatePrefix - string value which precedes private members. Default: __
  • errorOnPrivate - boolean value which indicates if attempts to access private members will throw an exception. If false, they return undefined. Default: false
  • apply - boolean value which indicates if the Privatize function can implement the ProxyHandler.apply function. Default: true
  • construct - boolean value which indicates if the Privatize function can implement the ProxyHandler.construct function. Default: true
  • defineProperty - boolean value which indicates if the Privatize function can implement the ProxyHandler.defineProperty function. Default: false
  • deleteProperty - boolean value which indicates if the Privatize function can implement the ProxyHandler.deleteProperty function. Default: false
  • getOwnPropertyDescriptor - boolean value which indicates if the Privatize function can implement the ProxyHandler.getOwnPropertyDescriptor function. Default: true
  • getPrototypeOf - boolean value which indicates if the Privatize function can implement the ProxyHandler.getPrototypeOf function. Default: true
  • has - boolean value which indicates if the Privatize function can implement the ProxyHandler.has function. Default: true
  • isExtensible - boolean value which indicates if the Privatize function can implement the ProxyHandler.isExtensible function. Default: false
  • ownKeys - boolean value which indicates if the Privatize function can implement the ProxyHandler.ownKeys function. Default: true
  • preventExtensions - boolean value which indicates if the Privatize function can implement the ProxyHandler.preventExtensions function. Default: true
  • setPrototypeOf - boolean value which indicates if the Privatize function can implement the ProxyHandler.setPrototypeOf function. Default: false

Usage

var Privatize = require('es6-privatize');
var util = require('util');

class Person {
    constructor(name, age) {
        this.__name = name;
        this.__age = age;

        return Privatize(this, { 
            warn: true, 
            privatePrefix: '__',
            errorOnPrivate: false
        });
    }

    /* Private variables and methods start with '__' (defined by options.privatePrefix) */

    __birthday() {
        this.__age ++;
    }

    /* These methods are needed to intercept reflection attempts */

    /**
     * Return a map of public property names to private data names. This
     * allows Privatize to intercept Object.keys, Object.getOwnPropertyDescriptor,
     * Object.getOwnPropertyDescriptors, and Object.getOwnPropertyNames
     * 
     * @returns 
     * @memberof Person
     */
    __privatize__keyMap() {
        return {
            'Age': '__age',
            'Name': '__name'
        };
    }

    /**
     * Used by Privatize to intercept JSON.stringify and return JSON
     * data to represent the publically available data.
     * 
     * @returns {any} 
     * @memberof Person
     */
    toJSON() {
        return {
            Name: this.Name,
            Age: this.Age
        };
    }

    /** Public Methods */

    get Name() {
        return this.__name;
    }

    set Name(value) {
        if (typeof value !== 'string') {
            throw new TypeError();
        }
        this.__name = value;
    }

    get Age() {
        return this.__age;
    }

    set Age(value) {
        if (typeof value !== 'number') {
            throw new TypeError();
        }
        this.__age = value;
    }
}

var michael = new Person('Michael Anderson', 42);

console.log(michael.__age);        // undefined
console.log('__age' in michael);   // false
console.log(Object.keys(michael)); // [ Age, Name ]

michael.__age = '45';              // fails silently (same as undefined = '45')
michael.Age = '45';                // throws TypeError("Invalid Type")
michael.Age = 45;                  // success
michael.__birthday();              // throws TypeError("michael.__birthday is not a function")

Notes

privatePrefix can contain reserved characters if you are willing to use brackets to access the private members from within your class. For easier implementation, stick with unreserved characters.