typeomatica 0.3.39 → 0.3.57

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/src/index.ts DELETED
@@ -1,301 +0,0 @@
1
- 'use strict';
2
-
3
- import { ErrorsNames } from './errors';
4
-
5
- import {
6
- functions,
7
- nullish,
8
- objects,
9
- primitives,
10
- special,
11
- isPrimitive
12
- } from './types';
13
-
14
- import { FieldConstructor } from './fields';
15
-
16
- const resolver = Object.entries({
17
- primitives,
18
- special,
19
- nullish,
20
- objects,
21
- functions
22
- }).reduce((obj: object, [key, _handler]) => {
23
- // @ts-ignore
24
- obj[key] = function (initialValue: object, receiver: object) {
25
- const handler = _handler(initialValue);
26
- return {
27
- get() {
28
- const invocationThis = this;
29
- if (invocationThis !== receiver) {
30
- throw new ReferenceError(ErrorsNames.ACCESS_DENIED);
31
- }
32
- const result = handler.get();
33
- return result;
34
- },
35
- set(replacementValue: unknown) {
36
- const invocationThis = this;
37
- if (invocationThis !== receiver) {
38
- throw new ReferenceError(ErrorsNames.ACCESS_DENIED);
39
- }
40
- const result = handler.set(replacementValue);
41
- return result;
42
- }
43
- };
44
- };
45
-
46
- return obj;
47
- }, {});
48
-
49
- const createProperty = (propName: string, initialValue: unknown, receiver: object) => {
50
-
51
- const value = initialValue;
52
- const valueIsPrimitive = isPrimitive(initialValue);
53
- const isObject = typeof initialValue === 'object';
54
- const isFunction = initialValue instanceof Function;
55
- const isNull = initialValue === null;
56
-
57
- /**
58
- * special: undefined or BigInt or Symbol
59
- * or other non constructible type
60
- */
61
-
62
- const type = valueIsPrimitive ? 'primitives' : (
63
- isObject ? (
64
- isNull ? 'nullish' : 'objects'
65
- ) : (
66
- isFunction ? 'functions' : 'special'
67
- )
68
- );
69
-
70
- const descriptor = (isObject && (value instanceof FieldConstructor)) ?
71
- value
72
- :
73
- {
74
- enumerable: true,
75
- // @ts-ignore
76
- ...resolver[type](value, receiver),
77
- };
78
-
79
- // if (value instanceof FieldConstructor) {
80
- // descriptor;
81
- // debugger;
82
- // }
83
-
84
- const result = Reflect.defineProperty(receiver, propName, descriptor);
85
- return result;
86
-
87
- };
88
-
89
- // line below 'href' is for util.inspect works, useful for v24
90
- const props2skip = new Set([Symbol.toStringTag, Symbol.iterator, 'href']);
91
- // const props2skip = new Set([Symbol.toStringTag, Symbol.iterator]);
92
- const util = require('util');
93
- const hasNodeInspect = (util && util.inspect && util.inspect.custom);
94
- hasNodeInspect && (props2skip.add(util.inspect.custom));
95
-
96
- const handlers = {
97
- get(target: object, prop: string | symbol, receiver: object) {
98
- const result = Reflect.get(target, prop, receiver);
99
- if (result !== undefined) {
100
- return result;
101
- }
102
- if (prop === 'toJSON') {
103
- return function (this: typeof target) {
104
- const entries = Object.entries(this);
105
- return JSON.stringify(entries.reduce((obj, [key, value]) => {
106
- // @ts-ignore
107
- obj[key] = value.valueOf();
108
- return obj;
109
- }, {}));
110
- };
111
- }
112
- if (props2skip.has(prop)) {
113
- return undefined;
114
- }
115
- throw new Error(`${ErrorsNames.MISSING_PROP}: [ ${String(prop).valueOf()} ] of ${receiver.constructor.name}`);
116
- },
117
- set(_: object, prop: string, value: unknown, receiver: object) {
118
- const result = createProperty(prop, value, receiver);
119
- return result;
120
- },
121
- // defineProperty(target: object, key: string, descriptor: object) {
122
- // debugger;
123
- // Reflect.defineProperty(target, key, descriptor);
124
- // return true;
125
- // }
126
- };
127
-
128
- // user have to precisely define all props
129
- const BaseTarget = Object.create(null);
130
-
131
- type Proto<P, T> = Pick<P, Exclude<keyof P, keyof T>> & T;
132
-
133
-
134
- export const BaseConstructorPrototype = function <
135
- P extends object,
136
- S extends Proto<T, P>,
137
- T extends {
138
- (): P
139
- new(): {
140
- [key in keyof S]: S[key]
141
- }
142
- },
143
- >(
144
- this: T,
145
- InstanceTarget: P = BaseTarget
146
- ): T {
147
- if (!new.target) {
148
-
149
- const self: {
150
- prototype: {
151
- constructor: typeof BaseConstructorPrototype
152
- }
153
- //@ts-ignore
154
- } = BaseConstructorPrototype.bind(this, InstanceTarget);
155
-
156
- self.prototype = {
157
- constructor: BaseConstructorPrototype
158
- };
159
-
160
- return self as T;
161
-
162
- }
163
-
164
- const InstancePrototype = new Proxy(InstanceTarget, handlers);
165
-
166
- let protoPointer = this as object;
167
- let protoConstrcutor;
168
- do {
169
- protoPointer = Reflect.getPrototypeOf(protoPointer) as object;
170
- protoConstrcutor = Reflect.getOwnPropertyDescriptor(protoPointer, 'constructor')!.value;
171
- } while (protoConstrcutor !== BaseConstructorPrototype);
172
-
173
- Reflect.setPrototypeOf(protoPointer, InstancePrototype);
174
- return this;
175
-
176
- } as {
177
- new(): unknown
178
- (): void
179
- };
180
-
181
- // const BaseConstructorProtoProxy = new Proxy(BaseConstructorPrototype, {
182
- // construct (target: Function, argumentsList: unknown[], newTarget: Function) {
183
- // debugger;
184
- // console.log('.construct invocation');
185
- // const result = Reflect.construct(target, argumentsList, newTarget);
186
- // debugger;
187
- // return result;
188
- // },
189
- // get (target: object, prop: string | symbol, receiver: object) {
190
- // debugger;
191
- // const result = Reflect.get(target, prop, receiver);
192
- // debugger;
193
- // return result;
194
- // },
195
- // set(_: object, prop: string, value: unknown, receiver: object) {
196
- // debugger;
197
- // const result = createProperty(prop, value, receiver);
198
- // return result;
199
- // },
200
- // defineProperty(target: object, key: string, descriptor: object) {
201
- // debugger;
202
- // Reflect.defineProperty(target, key, descriptor);
203
- // return true;
204
- // },
205
- // getPrototypeOf(target: object) {
206
- // debugger;
207
- // const result = Reflect.getPrototypeOf(target);
208
- // return result;
209
- // },
210
- // setPrototypeOf(target, prototype) {
211
- // debugger;
212
- // Reflect.setPrototypeOf(target, prototype);
213
- // return true;
214
- // },
215
- // });
216
-
217
- // as ObjectConstructor & {
218
- // (): void
219
- // new<T>(param?: T extends object ? T : {}): {
220
- // [key in keyof T]: T[key]
221
- // }
222
- // };
223
-
224
- Object.defineProperty(module, 'exports', {
225
- get() {
226
- // return BaseConstructorProtoProxy;
227
- return BaseConstructorPrototype;
228
- },
229
- enumerable: true
230
- });
231
-
232
-
233
- // export class BaseClass extends BaseConstructorProtoProxy { }
234
- // @ts-ignore
235
- export class BaseClass extends BaseConstructorPrototype { }
236
-
237
- export { FieldConstructor } from './fields';
238
- export const { SymbolInitialValue } = FieldConstructor;
239
-
240
- type StrictRuntime = {
241
- <T extends object>(...args: unknown[]): T
242
- }
243
-
244
- // export const { StrictPrototype, Strict } = {
245
- export const { Strict } = {
246
- // Strict: BaseConstructorProtoProxy,
247
- Strict: BaseConstructorPrototype,
248
- } as {
249
- // StrictPrototype: StrictRuntime
250
- Strict: StrictRuntime
251
- };
252
-
253
- Object.defineProperty(module.exports, 'BaseClass', {
254
- get() {
255
- return BaseClass;
256
- },
257
- enumerable: true
258
- });
259
- Object.defineProperty(module.exports, 'FieldConstructor', {
260
- get() {
261
- return FieldConstructor;
262
- },
263
- enumerable: true
264
- });
265
- Object.defineProperty(module.exports, 'SymbolInitialValue', {
266
- get() {
267
- return SymbolInitialValue;
268
- },
269
- enumerable: true
270
- });
271
- Object.defineProperty(module.exports, 'Strict', {
272
- get() {
273
- return function (prototypeTarget: object) {
274
- const decorator = function (
275
- this: object,
276
- Target: {
277
- new(): unknown
278
- (): void
279
- },
280
- ) {
281
- //@ts-ignore
282
- const Targeted = BaseConstructorPrototype.call(Target, prototypeTarget);
283
- //@ts-ignore
284
- const MyProxyClass = new Proxy(Targeted, {
285
- construct(target, argumentsList, newTarget) {
286
- //@ts-ignore
287
- const result = Reflect.construct(target, argumentsList, newTarget);
288
- return result;
289
- },
290
- });
291
- return MyProxyClass;
292
- };
293
-
294
- return decorator;
295
-
296
- };
297
- // return BaseConstructorProtoProxy;
298
- // return BaseConstructorPrototype;
299
- },
300
- enumerable: true
301
- });
@@ -1,7 +0,0 @@
1
- 'use strict';
2
-
3
- import { ErrorsNames } from '../errors';
4
-
5
- export const functions = () => {
6
- throw new TypeError(ErrorsNames.RIP_FUNCTIONS);
7
- };
@@ -1,17 +0,0 @@
1
- 'use strict';
2
-
3
- export { functions } from './functions';
4
- export { nullish } from './nullish';
5
- export { objects } from './objects';
6
- export { primitives } from './primitives';
7
- export { special } from './special';
8
-
9
- const PRIMITIVE_TYPES = [
10
- 'string',
11
- 'number',
12
- 'boolean',
13
- ];
14
-
15
- export const isPrimitive = (value: unknown) => {
16
- return PRIMITIVE_TYPES.includes(typeof value);
17
- };
@@ -1,15 +0,0 @@
1
- 'use strict';
2
-
3
- import { ErrorsNames } from '../errors';
4
-
5
- export const nullish = (value: object) => {
6
- return {
7
- get() {
8
- return value;
9
- },
10
- set() {
11
- const error = new TypeError(ErrorsNames.TYPE_MISMATCH);
12
- throw error;
13
- }
14
- };
15
- };
@@ -1,19 +0,0 @@
1
- 'use strict';
2
-
3
- import { ErrorsNames } from '../errors';
4
-
5
- export const objects = (value: object) => {
6
- return {
7
- get() {
8
- return value;
9
- },
10
- set(replacementValue: unknown) {
11
- if (replacementValue instanceof Object && replacementValue.constructor === value.constructor) {
12
- value = replacementValue;
13
- return value;
14
- }
15
- const error = new TypeError(ErrorsNames.TYPE_MISMATCH);
16
- throw error;
17
- }
18
- };
19
- };
@@ -1,69 +0,0 @@
1
- 'use strict';
2
-
3
- import { ErrorsNames } from '../errors';
4
-
5
- export const primitives = (initialValue: object) => {
6
- let value = Object(initialValue);
7
- const initialType = typeof initialValue;
8
-
9
- return {
10
- get() {
11
- const proxyAsValue = new Proxy(value, {
12
- // get(target, prop, receiver) {
13
- get(_, prop) {
14
- if (prop === Symbol.toPrimitive) {
15
- return function (hint: string) {
16
- if (hint !== initialType) {
17
- throw new ReferenceError(ErrorsNames.ACCESS_DENIED);
18
- }
19
- return value.valueOf();
20
- };
21
- }
22
-
23
- if (prop === 'valueOf') {
24
- return function () {
25
- return value.valueOf();
26
- };
27
- }
28
-
29
- // @ts-ignore
30
- if (value[prop] instanceof Function) {
31
- return value[prop].bind(value);
32
- }
33
-
34
- const answer = value[prop];
35
- return answer;
36
- }
37
- });
38
- return proxyAsValue;
39
- },
40
- // get() {
41
- // const preparedValue = {
42
- // [Symbol.toPrimitive]() {
43
- // return function () {
44
- // throw new ReferenceError(ErrorsNames.ACCESS_DENIED);
45
- // };
46
- // }
47
- // };
48
- // Reflect.setPrototypeOf(preparedValue, value);
49
- // debugger;
50
- // return preparedValue;
51
- // },
52
- set(replacementValue: unknown) {
53
- if (replacementValue instanceof value.constructor) {
54
- value = replacementValue;
55
- return value;
56
- }
57
-
58
- const prevalue = Object(replacementValue);
59
-
60
- if (prevalue instanceof value.constructor) {
61
- value = prevalue;
62
- return value;
63
- }
64
-
65
- const error = new TypeError(ErrorsNames.TYPE_MISMATCH);
66
- throw error;
67
- }
68
- };
69
- };
@@ -1,19 +0,0 @@
1
- 'use strict';
2
-
3
- import { ErrorsNames } from '../errors';
4
-
5
- export const special = (value: object) => {
6
- return {
7
- get() {
8
- return value;
9
- },
10
- set(replacementValue: object) {
11
- if (typeof replacementValue === typeof value) {
12
- value = replacementValue;
13
- return value;
14
- }
15
- const error = new TypeError(ErrorsNames.TYPE_MISMATCH);
16
- throw error;
17
- }
18
- };
19
- };
@@ -1,3 +0,0 @@
1
- // Jest Snapshot v1, https://goo.gl/fbAQLP
2
-
3
- exports[`props tests correct JSON.stringify 1`] = `""{\\"numberValue\\":123,\\"stringValue\\":\\"123\\",\\"booleanValue\\":false,\\"objectValue\\":{}}""`;