cfprotected 0.1.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/{index.js → index.mjs} +9 -5
- package/package.json +18 -3
- package/test/{test.js → test.mjs} +2 -2
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@ to work around some the issues inherent in using private fields.
|
|
|
8
8
|
Let's just start with a simple example:
|
|
9
9
|
|
|
10
10
|
```js
|
|
11
|
-
|
|
11
|
+
import { share, accessor } from "cfprotected";
|
|
12
12
|
|
|
13
13
|
class Example {
|
|
14
14
|
//shared static private fields
|
package/{index.js → index.mjs}
RENAMED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
"use strict"
|
|
2
|
+
|
|
1
3
|
const memos = new WeakMap();
|
|
2
4
|
const ACCESSOR = Symbol();
|
|
3
5
|
|
|
@@ -14,7 +16,7 @@ function getAllOwnKeys(o) {
|
|
|
14
16
|
* @param {Object} members The object containing the properties being shared.
|
|
15
17
|
* @returns {Object} The fully constructed inheritance object.
|
|
16
18
|
*/
|
|
17
|
-
|
|
19
|
+
function share(inst, klass, members) {
|
|
18
20
|
let retval = {};
|
|
19
21
|
|
|
20
22
|
if ((typeof(inst) == "function")
|
|
@@ -124,7 +126,7 @@ module.exports.share = function share(inst, klass, members) {
|
|
|
124
126
|
* @param {Object} self The current class instance as seen from the constructor.
|
|
125
127
|
* @param {String} name The name of the field on which to bind the instance.
|
|
126
128
|
*/
|
|
127
|
-
|
|
129
|
+
function saveSelf(self, name) {
|
|
128
130
|
Object.defineProperty(self, name, {value: self});
|
|
129
131
|
if (typeof(self) == "function") {
|
|
130
132
|
Object.defineProperty(self.prototype, "cla$$", {value: self});
|
|
@@ -139,7 +141,7 @@ module.exports.share = function share(inst, klass, members) {
|
|
|
139
141
|
* @returns {Object} A tagged object that will be used to create the access
|
|
140
142
|
* bindings for the property.
|
|
141
143
|
*/
|
|
142
|
-
|
|
144
|
+
function accessor(desc) {
|
|
143
145
|
if ((typeof(desc) == "object") &&
|
|
144
146
|
(("get" in desc) || ("set" in desc))) {
|
|
145
147
|
return {
|
|
@@ -155,7 +157,7 @@ module.exports.accessor = function(desc) {
|
|
|
155
157
|
* instantiated is not a descendant of the current class.
|
|
156
158
|
* @param {Function} klass The constructor of the current class.
|
|
157
159
|
*/
|
|
158
|
-
|
|
160
|
+
function abstract(klass) {
|
|
159
161
|
return new Proxy(klass, {
|
|
160
162
|
construct(target, args, newTarget) {
|
|
161
163
|
if (newTarget.prototype === klass.prototype)
|
|
@@ -171,7 +173,7 @@ module.exports.abstract = function(klass) {
|
|
|
171
173
|
* constructed is a descendant of the current class.
|
|
172
174
|
* @param {Function} klass The constructor of the current class.
|
|
173
175
|
*/
|
|
174
|
-
|
|
176
|
+
function final(klass) {
|
|
175
177
|
return new Proxy(klass, {
|
|
176
178
|
construct(target, args, newTarget) {
|
|
177
179
|
if (newTarget.prototype !== klass.prototype)
|
|
@@ -181,3 +183,5 @@ module.exports.final = function(klass) {
|
|
|
181
183
|
}
|
|
182
184
|
});
|
|
183
185
|
};
|
|
186
|
+
|
|
187
|
+
export { share, saveSelf, accessor, abstract, final };
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cfprotected",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "An implementation of protected fields on top of class fields.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "jest"
|
|
7
|
+
"test": "NODE_OPTIONS=--experimental-vm-modules npx jest"
|
|
8
8
|
},
|
|
9
9
|
"keywords": [
|
|
10
10
|
"class",
|
|
@@ -19,6 +19,21 @@
|
|
|
19
19
|
"url": "https://github.com/rdking/CFProtected.git"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
|
-
"jest": "^27.4.
|
|
22
|
+
"jest": "^27.4.7",
|
|
23
|
+
"jest-esm-transformer": "^1.0.0"
|
|
24
|
+
},
|
|
25
|
+
"jest": {
|
|
26
|
+
"verbose": true,
|
|
27
|
+
"moduleFileExtensions": [
|
|
28
|
+
"js",
|
|
29
|
+
"mjs"
|
|
30
|
+
],
|
|
31
|
+
"transform": {
|
|
32
|
+
"\\.m?[j|t]sx?$": "babel-jest"
|
|
33
|
+
},
|
|
34
|
+
"testMatch": [
|
|
35
|
+
"**/__tests__/**/*.?(m)[jt]s?(x)",
|
|
36
|
+
"**/?(*.)+(spec|test).?(m)[tj]s?(x)"
|
|
37
|
+
]
|
|
23
38
|
}
|
|
24
39
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { TestWatcher } from "@jest/core";
|
|
2
|
+
import { share, saveSelf, accessor } from "../index"; //require("cfprotected");
|
|
3
3
|
|
|
4
4
|
class Base {
|
|
5
5
|
#prot = share(this, Base, {
|