@util.js/privates 0.41.5 → 0.41.6

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.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/dist/Privates.js +0 -231
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "author": "Chris Topher <c@creemama.com>",
3
3
  "dependencies": {
4
4
  "@babel/runtime-corejs3": "7.29.7",
5
- "@util.js/errors": "0.41.5"
5
+ "@util.js/errors": "0.41.6"
6
6
  },
7
7
  "description": "JavaScript utility class for private member variables",
8
8
  "keywords": [
@@ -29,5 +29,5 @@
29
29
  "type": "git",
30
30
  "url": "https://github.com/creemama/utiljs/tree/master/packages/utiljs-privates"
31
31
  },
32
- "version": "0.41.5"
32
+ "version": "0.41.6"
33
33
  }
package/dist/Privates.js DELETED
@@ -1,231 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
4
- var _weakMap = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/weak-map"));
5
- var _concat = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/concat"));
6
- var _forEach = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/for-each"));
7
- var _entries = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/entries"));
8
- var _bind = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/bind"));
9
- var _getPrototypeOf = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/get-prototype-of"));
10
- var _filter = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/filter"));
11
- var _sort = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/sort"));
12
- var _getOwnPropertyNames = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/get-own-property-names"));
13
- var _map = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/map"));
14
- var _getOwnPropertySymbols = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols"));
15
- var _indexOf = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/index-of"));
16
- var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/slicedToArray"));
17
- var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/classCallCheck"));
18
- var _createClass2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/createClass"));
19
- var RethrownError = require("@util.js/errors/RethrownError");
20
-
21
- /**
22
- * JavaScript utility class for private member variables.
23
- *
24
- * This class decreases some of the boilerplate of the
25
- * [WeakMap]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap}
26
- * pattern as described by
27
- * ["Private Variables in JavaScript with ES6 WeakMaps"]{@link https://modernweb.com/private-variables-in-javascript-with-es6-weakmaps/}
28
- * and
29
- * ["Privates In ES2015 Javascript Classes"]{@link https://ilikekillnerds.com/2015/09/privates-in-es2015-javascript-classes/}.
30
- * This utility handles shallow copying at construction.
31
- *
32
- * Private fields may become a part of the ECMA standard. The following is a
33
- * StackOverflow answer to the question
34
- * ["Private properties in JavaScript ES6 classes"]{@link https://stackoverflow.com/a/52237988}:
35
- * "Private fields are being implemented in the ECMA standard. You can start
36
- * using them today with babel 7 and stage 3 preset." Wouldn't private fields
37
- * make this class obsolete? Not necessarily. If each of your privates is a
38
- * function (e.g., you want to lazy load all member variables), this class has
39
- * convenience methods for that.
40
- *
41
- * @public
42
- * @class Privates
43
- */
44
- module.exports = /*#__PURE__*/function () {
45
- function Privates() {
46
- (0, _classCallCheck2["default"])(this, Privates);
47
- this.privates = new _weakMap["default"]();
48
- }
49
-
50
- /**
51
- * Calls `this.get(thiz, property)()`.
52
- *
53
- * @public
54
- * @function Privates#call
55
- */
56
- return (0, _createClass2["default"])(Privates, [{
57
- key: "call",
58
- value: function call(thiz, property) {
59
- try {
60
- // this.get(thiz, property)() does not work all the time unless we
61
- // call bind, which we do in the set method. See
62
- // https://javascript.info/object-methods.
63
- return this.get(thiz, property)();
64
- } catch (e) {
65
- var _context, _context2;
66
- var propertyStr = getPropertyErrorString(property);
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));
68
- }
69
- }
70
- }, {
71
- key: "get",
72
- value: function get(thiz, property) {
73
- try {
74
- return this.getProps(thiz)[property];
75
- } catch (e) {
76
- var _context3, _context4;
77
- var propertyStr = getPropertyErrorString(property);
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));
79
- }
80
- }
81
- }, {
82
- key: "getCallProxy",
83
- value: function getCallProxy(thiz) {
84
- return new Proxy(this.getProps(thiz), {
85
- get: callProp
86
- });
87
- }
88
- }, {
89
- key: "getProps",
90
- value: function getProps(thiz) {
91
- try {
92
- var properties = this.privates.get(thiz);
93
- if (!properties) this.privates.set(thiz, properties = {});
94
- return properties;
95
- } catch (e) {
96
- if (thiz == null) throw new RethrownError(e, "thiz cannot be null or undefined.");
97
- throw e;
98
- }
99
- }
100
- }, {
101
- key: "lambdaize",
102
- value: function lambdaize(properties) {
103
- try {
104
- var _context5;
105
- var lambdaProperties = {};
106
- (0, _forEach["default"])(_context5 = (0, _entries["default"])(properties)).call(_context5, function (_ref) {
107
- var _ref2 = (0, _slicedToArray2["default"])(_ref, 2),
108
- property = _ref2[0],
109
- value = _ref2[1];
110
- return lambdaProperties[property] = function () {
111
- return value;
112
- };
113
- });
114
- return lambdaProperties;
115
- } catch (e) {
116
- if (properties == null) throw new RethrownError(e, "properties cannot be null or undefined.");
117
- }
118
- }
119
- }, {
120
- key: "lazyLoad",
121
- value: function lazyLoad(thiz, property, lazyLoadFunction) {
122
- if (typeof lazyLoadFunction !== "function") throw new TypeError("lazyLoadFunction (".concat(lazyLoadFunction, ") must be a function."));
123
- var value;
124
- var loaded = false;
125
- this.set(thiz, property, function () {
126
- if (!loaded) {
127
- value = lazyLoadFunction();
128
- loaded = true;
129
- }
130
- return value;
131
- });
132
- }
133
- }, {
134
- key: "lazyLoadProps",
135
- value: function lazyLoadProps(thiz) {
136
- var t = this;
137
- for (var _len = arguments.length, lazilyLoadedProperties = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
138
- lazilyLoadedProperties[_key - 1] = arguments[_key];
139
- }
140
- (0, _forEach["default"])(lazilyLoadedProperties).call(lazilyLoadedProperties, function (currentProperties) {
141
- var _context6;
142
- (0, _forEach["default"])(_context6 = (0, _entries["default"])(currentProperties)).call(_context6, function (_ref3) {
143
- var _ref4 = (0, _slicedToArray2["default"])(_ref3, 2),
144
- property = _ref4[0],
145
- lazyLoadFunction = _ref4[1];
146
- return t.lazyLoad(thiz, property, lazyLoadFunction);
147
- });
148
- });
149
- }
150
- }, {
151
- key: "set",
152
- value: function set(thiz, property, newValue) {
153
- try {
154
- this.getProps(thiz)[property] = newValue;
155
- } catch (e) {
156
- var _context7, _context8, _context9;
157
- var propertyStr = getPropertyErrorString(property);
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));
159
- }
160
- }
161
- }, {
162
- key: "setProps",
163
- value: function setProps(thiz) {
164
- var t = this;
165
- for (var _len2 = arguments.length, properties = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
166
- properties[_key2 - 1] = arguments[_key2];
167
- }
168
- (0, _forEach["default"])(properties).call(properties, function (currentProperties) {
169
- var _context0, _context1;
170
- (0, _forEach["default"])(_context0 = (0, _entries["default"])(currentProperties)).call(_context0, function (_ref5) {
171
- var _ref6 = (0, _slicedToArray2["default"])(_ref5, 2),
172
- property = _ref6[0],
173
- value = _ref6[1];
174
- return t.set(thiz, property, value);
175
- });
176
- (0, _forEach["default"])(_context1 = getAllMethods(currentProperties)).call(_context1, function (method) {
177
- var _context10;
178
- return t.set(thiz, method, (0, _bind["default"])(_context10 = currentProperties[method]).call(_context10, currentProperties));
179
- });
180
- });
181
- }
182
- }, {
183
- key: "subset",
184
- value: function subset(thiz) {
185
- var object = {};
186
- for (var _len3 = arguments.length, properties = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
187
- properties[_key3 - 1] = arguments[_key3];
188
- }
189
- for (var i = 0; i < properties.length; i++) object[properties[i]] = this.get(thiz, properties[i]);
190
- return object;
191
- }
192
- }]);
193
- }();
194
- function getPropertyErrorString(property) {
195
- // https://stackoverflow.com/a/7772724
196
- if (typeof property === "string" || property instanceof String) return "\"".concat(property, "\"");
197
- return "".concat(property);
198
- }
199
- function callProp(obj, property) {
200
- try {
201
- return obj[property]();
202
- } catch (e) {
203
- throw new RethrownError(e, "Calling \"".concat(property, "\" as a function failed."));
204
- }
205
- }
206
-
207
- // https://stackoverflow.com/questions/31054910/get-functions-methods-of-a-class
208
- function getAllMethods(obj) {
209
- if (!(0, _getPrototypeOf["default"])(obj)) return [];
210
- var props = [];
211
- do {
212
- var _context11, _context12, _context13, _context14;
213
- var l = (0, _filter["default"])(_context11 = (0, _sort["default"])(_context12 = (0, _concat["default"])(_context13 = (0, _getOwnPropertyNames["default"])(obj)).call(_context13, (0, _map["default"])(_context14 = (0, _getOwnPropertySymbols["default"])(obj)).call(_context14, function (s) {
214
- return s.toString();
215
- }))).call(_context12)).call(_context11, function (p, i, arr) {
216
- return typeof obj[p] === "function" &&
217
- // only the methods
218
- p !== "constructor" && (
219
- // not the constructor
220
- i === 0 || p !== arr[i - 1]) &&
221
- // not overriding in this prototype
222
- (0, _indexOf["default"])(props).call(props, p) === -1;
223
- } // not overridden in a child
224
- );
225
- props = (0, _concat["default"])(props).call(props, l);
226
- } while ((obj = (0, _getPrototypeOf["default"])(obj)) &&
227
- // walk-up the prototype chain
228
- (0, _getPrototypeOf["default"])(obj) // not the the Object prototype methods (hasOwnProperty, etc...)
229
- );
230
- return props;
231
- }