generated from jankapunkt/npm-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
194 lines (146 loc) · 6.59 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Home</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Home</h1>
<h3> </h3>
<section>
<article><h1>:lock: Javascript Class-Privacy</h1>
<p><a href="https://travis-ci.org/jankapunkt/js-class-privacy"><img src="https://travis-ci.org/jankapunkt/npm-package-template.svg?branch=master" alt="Build Status"></a>
<a href="https://standardjs.com"><img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="JavaScript Style Guide"></a>
<a href="https://www.repostatus.org/#active"><img src="https://www.repostatus.org/badges/latest/active.svg" alt="Project Status: Active – The project has reached a stable, usable state and is being actively developed."></a>
<img src="https://img.shields.io/bundlephobia/min/class-privacy" alt="npm bundle size"></p>
<p>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.</p>
<h2>Installation and basic usage</h2>
<p>Install this package via NPM like</p>
<pre class="prettyprint source lang-bash"><code>$ npm install class-privacy
</code></pre>
<p>The packages exports only one function, that acts similar to an abstract factory.
You can pass in a <code>decide</code> 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.</p>
<pre class="prettyprint source lang-javascript"><code>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.`
</code></pre>
<h3>Options</h3>
<p>As shown in the example above, the factory can be created with certain
configurations, defined as <code>options</code>:</p>
<h4><code>decide</code></h4>
<p>A function that is invoked on every access request (proxy <code>get</code> trap)
and receives <code>key</code>, <code>type</code> and <code>ClassDefinition</code> to decide, whether
this member should be allowed to be public or kept being private.</p>
<p>Signature:</p>
<pre class="prettyprint source lang-javascript"><code>decide: (key, type, ClassDefinition) => Boolean
</code></pre>
<p>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.</p>
<h4><code>revealIsProxy</code></h4>
<p>If this option is set to <code>true</code> the <code>isProxy</code> property will be added to the
proxy in order to allow a classification of the Object as proxy.</p>
<pre class="prettyprint source lang-javascript"><code>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
</code></pre>
<h4><code>referenceClass</code></h4>
<p>If this option is set to <code>true</code> the <code>class</code> property will be added to the
proxy in order to allow a classification of the instance as proxy to the given
class definition.</p>
<pre class="prettyprint source lang-javascript"><code>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></pre>
<h2>Code Quality</h2>
<p>We use <code>standard</code> as opinionated but zero-config linter.
You can run lint in two modes:</p>
<h5>lint</h5>
<pre class="prettyprint source lang-bash"><code>$ npm run lint
</code></pre>
<h5>lint with auto fixing</h5>
<pre class="prettyprint source lang-bash"><code>$ npm run lint:fix
</code></pre>
<h2>Run the tests</h2>
<p>We use mocha and chai with a mostly (but not strict) behavioural style for testing.
You can run the tests in three different contexts:</p>
<h5>Single run</h5>
<pre class="prettyprint source lang-bash"><code>$ npm run test
</code></pre>
<h5>Watch mode</h5>
<pre class="prettyprint source lang-bash"><code>$ npm run test:watch
</code></pre>
<h5>Coverage</h5>
<pre class="prettyprint source lang-bash"><code>$ npm run test:coverage
</code></pre>
<h2>Documentation</h2>
<p>Documentation is using jsDoc and is available as <a href="docs/index.html">html</a> or <a href="api.md">markdown</a> version.</p>
<p>To build the documentation in development, you need to run</p>
<pre class="prettyprint source lang-bash"><code>$ npm run docs
</code></pre>
<h2>Build the package</h2>
<p>The package can be build into the <code>dist</code> folder using <code>babel</code> and the respective script:</p>
<pre class="prettyprint source lang-bash"><code>$ npm run build
</code></pre>
<h2>License</h2>
<p>MIT, see <a href="LICENSE">license file</a></p></article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.html#createFactory">createFactory</a></li><li><a href="global.html#defaultOptions">defaultOptions</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.5</a> on Tue Jul 28 2020 09:34:29 GMT+0200 (Central European Summer Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>