@util.js/privates 0.41.0 → 0.41.2
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/dist/Privates.js +19 -54
- package/index.js +1 -0
- package/lib/Privates.js +4 -5
- package/package.json +4 -4
- package/test/PrivatesTest.js +10 -3
package/dist/Privates.js
CHANGED
|
@@ -1,38 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
4
|
var _weakMap = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/weak-map"));
|
|
6
|
-
|
|
7
5
|
var _concat = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/concat"));
|
|
8
|
-
|
|
9
6
|
var _forEach = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/for-each"));
|
|
10
|
-
|
|
11
7
|
var _entries = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/entries"));
|
|
12
|
-
|
|
13
8
|
var _bind = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/bind"));
|
|
14
|
-
|
|
15
9
|
var _getPrototypeOf = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/get-prototype-of"));
|
|
16
|
-
|
|
17
10
|
var _filter = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/filter"));
|
|
18
|
-
|
|
19
11
|
var _sort = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/sort"));
|
|
20
|
-
|
|
21
12
|
var _getOwnPropertyNames = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/get-own-property-names"));
|
|
22
|
-
|
|
23
13
|
var _map = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/map"));
|
|
24
|
-
|
|
25
14
|
var _getOwnPropertySymbols = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols"));
|
|
26
|
-
|
|
27
15
|
var _indexOf = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/index-of"));
|
|
28
|
-
|
|
29
16
|
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/slicedToArray"));
|
|
30
|
-
|
|
31
17
|
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/classCallCheck"));
|
|
32
|
-
|
|
33
18
|
var _createClass2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/createClass"));
|
|
34
|
-
|
|
35
19
|
var RethrownError = require("@util.js/errors/RethrownError");
|
|
20
|
+
|
|
36
21
|
/**
|
|
37
22
|
* JavaScript utility class for private member variables.
|
|
38
23
|
*
|
|
@@ -56,21 +41,18 @@ var RethrownError = require("@util.js/errors/RethrownError");
|
|
|
56
41
|
* @public
|
|
57
42
|
* @class Privates
|
|
58
43
|
*/
|
|
59
|
-
|
|
60
|
-
|
|
61
44
|
module.exports = /*#__PURE__*/function () {
|
|
62
45
|
function Privates() {
|
|
63
46
|
(0, _classCallCheck2["default"])(this, Privates);
|
|
64
47
|
this.privates = new _weakMap["default"]();
|
|
65
48
|
}
|
|
49
|
+
|
|
66
50
|
/**
|
|
67
51
|
* Calls `this.get(thiz, property)()`.
|
|
68
52
|
*
|
|
69
53
|
* @public
|
|
70
54
|
* @function Privates#call
|
|
71
55
|
*/
|
|
72
|
-
|
|
73
|
-
|
|
74
56
|
(0, _createClass2["default"])(Privates, [{
|
|
75
57
|
key: "call",
|
|
76
58
|
value: function call(thiz, property) {
|
|
@@ -81,7 +63,6 @@ module.exports = /*#__PURE__*/function () {
|
|
|
81
63
|
return this.get(thiz, property)();
|
|
82
64
|
} catch (e) {
|
|
83
65
|
var _context, _context2;
|
|
84
|
-
|
|
85
66
|
var propertyStr = getPropertyErrorString(property);
|
|
86
67
|
throw new RethrownError(e, (0, _concat["default"])(_context = (0, _concat["default"])(_context2 = "privates.call(".concat(thiz, ", ")).call(_context2, propertyStr, ") failed. ")).call(_context, e.message));
|
|
87
68
|
}
|
|
@@ -93,7 +74,6 @@ module.exports = /*#__PURE__*/function () {
|
|
|
93
74
|
return this.getProps(thiz)[property];
|
|
94
75
|
} catch (e) {
|
|
95
76
|
var _context3, _context4;
|
|
96
|
-
|
|
97
77
|
var propertyStr = getPropertyErrorString(property);
|
|
98
78
|
throw new RethrownError(e, (0, _concat["default"])(_context3 = (0, _concat["default"])(_context4 = "privates.get(".concat(thiz, ", ")).call(_context4, propertyStr, ") failed. ")).call(_context3, e.message));
|
|
99
79
|
}
|
|
@@ -122,13 +102,11 @@ module.exports = /*#__PURE__*/function () {
|
|
|
122
102
|
value: function lambdaize(properties) {
|
|
123
103
|
try {
|
|
124
104
|
var _context5;
|
|
125
|
-
|
|
126
105
|
var lambdaProperties = {};
|
|
127
106
|
(0, _forEach["default"])(_context5 = (0, _entries["default"])(properties)).call(_context5, function (_ref) {
|
|
128
107
|
var _ref2 = (0, _slicedToArray2["default"])(_ref, 2),
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
108
|
+
property = _ref2[0],
|
|
109
|
+
value = _ref2[1];
|
|
132
110
|
return lambdaProperties[property] = function () {
|
|
133
111
|
return value;
|
|
134
112
|
};
|
|
@@ -149,7 +127,6 @@ module.exports = /*#__PURE__*/function () {
|
|
|
149
127
|
value = lazyLoadFunction();
|
|
150
128
|
loaded = true;
|
|
151
129
|
}
|
|
152
|
-
|
|
153
130
|
return value;
|
|
154
131
|
});
|
|
155
132
|
}
|
|
@@ -157,19 +134,15 @@ module.exports = /*#__PURE__*/function () {
|
|
|
157
134
|
key: "lazyLoadProps",
|
|
158
135
|
value: function lazyLoadProps(thiz) {
|
|
159
136
|
var t = this;
|
|
160
|
-
|
|
161
137
|
for (var _len = arguments.length, lazilyLoadedProperties = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
162
138
|
lazilyLoadedProperties[_key - 1] = arguments[_key];
|
|
163
139
|
}
|
|
164
|
-
|
|
165
140
|
(0, _forEach["default"])(lazilyLoadedProperties).call(lazilyLoadedProperties, function (currentProperties) {
|
|
166
141
|
var _context6;
|
|
167
|
-
|
|
168
142
|
(0, _forEach["default"])(_context6 = (0, _entries["default"])(currentProperties)).call(_context6, function (_ref3) {
|
|
169
143
|
var _ref4 = (0, _slicedToArray2["default"])(_ref3, 2),
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
144
|
+
property = _ref4[0],
|
|
145
|
+
lazyLoadFunction = _ref4[1];
|
|
173
146
|
return t.lazyLoad(thiz, property, lazyLoadFunction);
|
|
174
147
|
});
|
|
175
148
|
});
|
|
@@ -181,7 +154,6 @@ module.exports = /*#__PURE__*/function () {
|
|
|
181
154
|
this.getProps(thiz)[property] = newValue;
|
|
182
155
|
} catch (e) {
|
|
183
156
|
var _context7, _context8, _context9;
|
|
184
|
-
|
|
185
157
|
var propertyStr = getPropertyErrorString(property);
|
|
186
158
|
throw new RethrownError(e, (0, _concat["default"])(_context7 = (0, _concat["default"])(_context8 = (0, _concat["default"])(_context9 = "privates.set(".concat(thiz, ", ")).call(_context9, propertyStr, ", ")).call(_context8, newValue, ") failed. ")).call(_context7, e.message));
|
|
187
159
|
}
|
|
@@ -190,24 +162,19 @@ module.exports = /*#__PURE__*/function () {
|
|
|
190
162
|
key: "setProps",
|
|
191
163
|
value: function setProps(thiz) {
|
|
192
164
|
var t = this;
|
|
193
|
-
|
|
194
165
|
for (var _len2 = arguments.length, properties = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
|
|
195
166
|
properties[_key2 - 1] = arguments[_key2];
|
|
196
167
|
}
|
|
197
|
-
|
|
198
168
|
(0, _forEach["default"])(properties).call(properties, function (currentProperties) {
|
|
199
169
|
var _context10, _context11;
|
|
200
|
-
|
|
201
170
|
(0, _forEach["default"])(_context10 = (0, _entries["default"])(currentProperties)).call(_context10, function (_ref5) {
|
|
202
171
|
var _ref6 = (0, _slicedToArray2["default"])(_ref5, 2),
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
172
|
+
property = _ref6[0],
|
|
173
|
+
value = _ref6[1];
|
|
206
174
|
return t.set(thiz, property, value);
|
|
207
175
|
});
|
|
208
176
|
(0, _forEach["default"])(_context11 = getAllMethods(currentProperties)).call(_context11, function (method) {
|
|
209
177
|
var _context12;
|
|
210
|
-
|
|
211
178
|
return t.set(thiz, method, (0, _bind["default"])(_context12 = currentProperties[method]).call(_context12, currentProperties));
|
|
212
179
|
});
|
|
213
180
|
});
|
|
@@ -216,54 +183,52 @@ module.exports = /*#__PURE__*/function () {
|
|
|
216
183
|
key: "subset",
|
|
217
184
|
value: function subset(thiz) {
|
|
218
185
|
var object = {};
|
|
219
|
-
|
|
220
186
|
for (var _len3 = arguments.length, properties = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
|
|
221
187
|
properties[_key3 - 1] = arguments[_key3];
|
|
222
188
|
}
|
|
223
|
-
|
|
224
189
|
for (var i = 0; i < properties.length; i++) {
|
|
225
190
|
object[properties[i]] = this.get(thiz, properties[i]);
|
|
226
191
|
}
|
|
227
|
-
|
|
228
192
|
return object;
|
|
229
193
|
}
|
|
230
194
|
}]);
|
|
231
195
|
return Privates;
|
|
232
196
|
}();
|
|
233
|
-
|
|
234
197
|
function getPropertyErrorString(property) {
|
|
235
198
|
// https://stackoverflow.com/a/7772724
|
|
236
199
|
if (typeof property === "string" || property instanceof String) return "\"".concat(property, "\"");
|
|
237
200
|
return "".concat(property);
|
|
238
201
|
}
|
|
239
|
-
|
|
240
202
|
function callProp(obj, property) {
|
|
241
203
|
try {
|
|
242
204
|
return obj[property]();
|
|
243
205
|
} catch (e) {
|
|
244
206
|
throw new RethrownError(e, "Calling \"".concat(property, "\" as a function failed."));
|
|
245
207
|
}
|
|
246
|
-
}
|
|
247
|
-
|
|
208
|
+
}
|
|
248
209
|
|
|
210
|
+
// https://stackoverflow.com/questions/31054910/get-functions-methods-of-a-class
|
|
249
211
|
function getAllMethods(obj) {
|
|
250
212
|
if (!(0, _getPrototypeOf["default"])(obj)) return [];
|
|
251
213
|
var props = [];
|
|
252
|
-
|
|
253
214
|
do {
|
|
254
215
|
var _context13, _context14, _context15, _context16;
|
|
255
|
-
|
|
256
216
|
var l = (0, _filter["default"])(_context13 = (0, _sort["default"])(_context14 = (0, _concat["default"])(_context15 = (0, _getOwnPropertyNames["default"])(obj)).call(_context15, (0, _map["default"])(_context16 = (0, _getOwnPropertySymbols["default"])(obj)).call(_context16, function (s) {
|
|
257
217
|
return s.toString();
|
|
258
218
|
}))).call(_context14)).call(_context13, function (p, i, arr) {
|
|
259
|
-
return typeof obj[p] === "function" &&
|
|
260
|
-
|
|
261
|
-
|
|
219
|
+
return typeof obj[p] === "function" &&
|
|
220
|
+
// only the methods
|
|
221
|
+
p !== "constructor" && (
|
|
222
|
+
// not the constructor
|
|
223
|
+
i === 0 || p !== arr[i - 1]) &&
|
|
224
|
+
// not overriding in this prototype
|
|
262
225
|
(0, _indexOf["default"])(props).call(props, p) === -1;
|
|
263
226
|
} // not overridden in a child
|
|
264
227
|
);
|
|
228
|
+
|
|
265
229
|
props = (0, _concat["default"])(props).call(props, l);
|
|
266
|
-
} while ((obj = (0, _getPrototypeOf["default"])(obj)) &&
|
|
230
|
+
} while ((obj = (0, _getPrototypeOf["default"])(obj)) &&
|
|
231
|
+
// walk-up the prototype chain
|
|
267
232
|
(0, _getPrototypeOf["default"])(obj) // not the the Object prototype methods (hasOwnProperty, etc...)
|
|
268
233
|
);
|
|
269
234
|
|
package/index.js
CHANGED
package/lib/Privates.js
CHANGED
|
@@ -111,10 +111,9 @@ module.exports = class Privates {
|
|
|
111
111
|
lazyLoadProps(thiz, ...lazilyLoadedProperties) {
|
|
112
112
|
const t = this;
|
|
113
113
|
lazilyLoadedProperties.forEach((currentProperties) => {
|
|
114
|
-
Object.entries(
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
t.lazyLoad(thiz, property, lazyLoadFunction)
|
|
114
|
+
Object.entries(currentProperties).forEach(
|
|
115
|
+
([property, lazyLoadFunction]) =>
|
|
116
|
+
t.lazyLoad(thiz, property, lazyLoadFunction)
|
|
118
117
|
);
|
|
119
118
|
});
|
|
120
119
|
}
|
|
@@ -180,7 +179,7 @@ function getAllMethods(obj) {
|
|
|
180
179
|
(p, i, arr) =>
|
|
181
180
|
typeof obj[p] === "function" && // only the methods
|
|
182
181
|
p !== "constructor" && // not the constructor
|
|
183
|
-
(i
|
|
182
|
+
(i === 0 || p !== arr[i - 1]) && // not overriding in this prototype
|
|
184
183
|
props.indexOf(p) === -1 // not overridden in a child
|
|
185
184
|
);
|
|
186
185
|
props = props.concat(l);
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"author": "Chris Topher <c@creemama.com>",
|
|
3
3
|
"dependencies": {
|
|
4
|
-
"@babel/runtime-corejs3": "7.
|
|
5
|
-
"@util.js/errors": "0.41.
|
|
4
|
+
"@babel/runtime-corejs3": "7.20.1",
|
|
5
|
+
"@util.js/errors": "0.41.2"
|
|
6
6
|
},
|
|
7
7
|
"description": "JavaScript utility class for private member variables",
|
|
8
8
|
"keywords": [
|
|
@@ -29,6 +29,6 @@
|
|
|
29
29
|
"type": "git",
|
|
30
30
|
"url": "https://github.com/creemama/utiljs/tree/master/packages/utiljs-privates"
|
|
31
31
|
},
|
|
32
|
-
"version": "0.41.
|
|
33
|
-
"gitHead": "
|
|
32
|
+
"version": "0.41.2",
|
|
33
|
+
"gitHead": "174b81ec0ead0221b9bb6617bc254b025bd20a5a"
|
|
34
34
|
}
|
package/test/PrivatesTest.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
const { expect } = require("chai")
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
const { expect } = require("chai");
|
|
4
|
+
const Privates = require("..");
|
|
5
|
+
const { RethrownError } = require("@util.js/errors");
|
|
6
6
|
|
|
7
7
|
const ExampleUsingGet = (function () {
|
|
8
8
|
const privates = new Privates();
|
|
@@ -11,9 +11,11 @@ const ExampleUsingGet = (function () {
|
|
|
11
11
|
constructor(options) {
|
|
12
12
|
privates.setProps(this, options);
|
|
13
13
|
}
|
|
14
|
+
|
|
14
15
|
getProperty1() {
|
|
15
16
|
return property1(this);
|
|
16
17
|
}
|
|
18
|
+
|
|
17
19
|
getProperty2() {
|
|
18
20
|
return property2(this);
|
|
19
21
|
}
|
|
@@ -39,9 +41,11 @@ const ExampleUsingCall = (function () {
|
|
|
39
41
|
constructor(options) {
|
|
40
42
|
privates.setProps(this, options);
|
|
41
43
|
}
|
|
44
|
+
|
|
42
45
|
getProperty1() {
|
|
43
46
|
return property1(this);
|
|
44
47
|
}
|
|
48
|
+
|
|
45
49
|
getProperty2() {
|
|
46
50
|
return property2(this);
|
|
47
51
|
}
|
|
@@ -104,6 +108,7 @@ describe("Privates#call", () => {
|
|
|
104
108
|
delegate() {
|
|
105
109
|
return "classObj";
|
|
106
110
|
}
|
|
111
|
+
|
|
107
112
|
call() {
|
|
108
113
|
return this.delegate();
|
|
109
114
|
}
|
|
@@ -141,6 +146,7 @@ describe("Privates#call", () => {
|
|
|
141
146
|
const thiz = {};
|
|
142
147
|
|
|
143
148
|
expect(() => privates.call()).to.throw(RethrownError);
|
|
149
|
+
// eslint-disable-next-line no-useless-call
|
|
144
150
|
expect(() => privates.call(null, "nonexistent")).to.throw(RethrownError);
|
|
145
151
|
expect(() => privates.call(thiz)).to.throw(RethrownError);
|
|
146
152
|
expect(() => privates.call(thiz, "nonexistent")).to.throw(RethrownError);
|
|
@@ -150,6 +156,7 @@ describe("Privates#call", () => {
|
|
|
150
156
|
delegate() {
|
|
151
157
|
return "classObj";
|
|
152
158
|
}
|
|
159
|
+
|
|
153
160
|
call() {
|
|
154
161
|
return this.delegate();
|
|
155
162
|
}
|