https://github.com/jankapunkt/js-class-privacy
Create a given ES6 class with private members using Proxy and closures. Keeps class code clean, encourages SRP and DRY.
https://github.com/jankapunkt/js-class-privacy
abstract-factory es6-class es6-classes es6-javascript es6-proxy factory-method-pattern javascript private-members security
Last synced: 3 months ago
JSON representation
Create a given ES6 class with private members using Proxy and closures. Keeps class code clean, encourages SRP and DRY.
- Host: GitHub
- URL: https://github.com/jankapunkt/js-class-privacy
- Owner: jankapunkt
- License: mit
- Created: 2020-03-02T07:43:03.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T02:36:42.000Z (over 2 years ago)
- Last Synced: 2025-02-13T01:30:29.257Z (5 months ago)
- Topics: abstract-factory, es6-class, es6-classes, es6-javascript, es6-proxy, factory-method-pattern, javascript, private-members, security
- Language: JavaScript
- Homepage:
- Size: 1.71 MB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# :lock: Javascript Class-Privacy
[](https://travis-ci.org/jankapunkt/js-class-privacy)
[](https://standardjs.com)
[](https://www.repostatus.org/#active)
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
```bash
$ 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.```javascript
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:
```javascript
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.```javascript
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.```javascript
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
```bash
$ npm run lint
```##### lint with auto fixing
```bash
$ 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
```bash
$ npm run test
```##### Watch mode
```bash
$ npm run test:watch
```##### Coverage
```bash
$ npm run test:coverage
```## Documentation
Documentation is using jsDoc and is available as [html](docs/index.html) or [markdown](api.md) version.
To build the documentation in development, you need to run
```bash
$ npm run docs
```## Build the package
The package can be build into the `dist` folder using `babel` and the respective script:
```bash
$ npm run build
```## License
MIT, see [license file](LICENSE)