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

class-privacy

v1.1.1

Published

Simple way to define private members on ES6 classes, keep their code clean.

Downloads

2

Readme

:lock: Javascript Class-Privacy

Build Status JavaScript Style Guide Project Status: Active – The project has reached a stable, usable state and is being actively developed. npm bundle size

Lean dry no-dep srp :cup: package to create instances from classes with defined private members. Keep your classes clean und use this instead to define private properties. Uses proxies to hide information.

Installation and basic usage

Install this package via NPM like

$ npm install class-privacy

The packages exports only one function, that acts similar to an abstract factory. You can pass in a decide function to define rules (e.g. whitelist) for members. The created factory can be used to create (proxies to) instances that contain only the public members.

import createFactory from 'class-privacy'

export class Person {
  constructor ({ name, age }) {
    this.name = name
    this.age = age
  }

  greet () {
    return `Hello, my name is "${this.name}". I am ${this.age} years old.`
  }
}

// make all functions public, all other members are private 
const decide = (key, type) => type === 'function'

// create the factory for private persons 
const createPrivatePerson = createFactory(Person, { decide })

const anon = createPrivatePerson({ name: 'John Doe', age: 42 })
anon.name // undefined
anon.age // undefined
anon.greet() // `Hello, my name is "John Doe". I am 42 years old.`

Options

As shown in the example above, the factory can be created with certain configurations, defined as options:

decide

A function that is invoked on every access request (proxy get trap) and receives key, type and ClassDefinition to decide, whether this member should be allowed to be public or kept being private.

Signature:

decide: (key, type, ClassDefinition) => Boolean

Non-boolean return values are evaluated as truthy/falsy. If not passed in options, all members are included by default to preserve the original state.

revealIsProxy

If this option is set to true the isProxy property will be added to the proxy in order to allow a classification of the Object as proxy.

import createFactory from 'class-privacy'

export class Person {
  constructor ({ name, age }) {
    this.name = name
    this.age = age
  }

  greet () {
    return `Hello, my name is "${this.name}". I am ${this.age} years old.`
  }
}

const createPrivatePerson = createFactory(Person, { revealIsProxy: true })
const anon = createPrivatePerson({ name: 'John Doe', age: 42 })
anon.isProxy // true

referenceClass

If this option is set to true the class property will be added to the proxy in order to allow a classification of the instance as proxy to the given class definition.

import createFactory from 'class-privacy'

export class Person {
  constructor ({ name, age }) {
    this.name = name
    this.age = age
  }

  greet () {
    return `Hello, my name is "${this.name}". I am ${this.age} years old.`
  }
}

const createPrivatePerson = createFactory(Person, { referenceClass: true })
const anon = createPrivatePerson({ name: 'John Doe', age: 42 })
anon.class === Person // true

Code Quality

We use standard as opinionated but zero-config linter. You can run lint in two modes:

lint
$ npm run lint
lint with auto fixing
$ npm run lint:fix

Run the tests

We use mocha and chai with a mostly (but not strict) behavioural style for testing. You can run the tests in three different contexts:

Single run
$ npm run test
Watch mode
$ npm run test:watch
Coverage
$ npm run test:coverage

Documentation

Documentation is using jsDoc and is available as html or markdown version.

To build the documentation in development, you need to run

$ npm run docs

Build the package

The package can be build into the dist folder using babel and the respective script:

$ npm run build

License

MIT, see license file