typeomatica 0.3.2 → 0.3.4

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/test/index.ts DELETED
@@ -1,622 +0,0 @@
1
- 'use strict';
2
-
3
- import { describe, expect, test } from '@jest/globals';
4
-
5
- // BasePrototype & BaseClass are the same function
6
- // go as you want for being meaningfull
7
- // or meaningless
8
-
9
- const BasePrototype = require('..');
10
- // @ts-ignore
11
- import { BaseClass, IDEF, FieldConstructor } from '..';
12
-
13
- const { SymbolInitialValue } = FieldConstructor;
14
-
15
- interface IBase {
16
- get getterField(): string
17
- set setterField(value: string)
18
- numberValue: number
19
- stringValue: string
20
- booleanValue: boolean
21
- objectValue: object
22
- }
23
-
24
- class Base extends BasePrototype({
25
- additionalProp: 321,
26
- someMethod() {
27
- return this.numberValue.valueOf();
28
- },
29
- }) implements IBase {
30
- numberValue = 123;
31
- stringValue: string
32
- booleanValue: boolean
33
- objectValue: object
34
-
35
- get getterField() {
36
- const answer = `${this.stringValue}`;
37
- return answer;
38
- }
39
-
40
- set setterField(value: string) {
41
- this.stringValue = value;
42
- }
43
-
44
- constructor() {
45
- super();
46
- this.stringValue = '123';
47
- this.booleanValue = true;
48
- this.objectValue = {};
49
- }
50
- }
51
- const baseInstance = new Base;
52
-
53
- const upperInstance = Object.create(baseInstance);
54
-
55
- class SimpleBase extends BaseClass {
56
- stringProp = '123';
57
- };
58
- const simpleInstance = new SimpleBase;
59
-
60
- type MyFunctionalConstructorInstance = {
61
- stringProp: string
62
- };
63
-
64
- const MyFunctionalConstructor = function () {
65
- // @ts-ignore
66
- this.stringProp = '123';
67
- } as IDEF<MyFunctionalConstructorInstance>;
68
-
69
- Reflect.setPrototypeOf(MyFunctionalConstructor.prototype, new BasePrototype);
70
-
71
- const myFunctionalInstance = new MyFunctionalConstructor();
72
-
73
- class SecondaryExtend extends Base { second = 123 };
74
- class TripleExtend extends SecondaryExtend { };
75
- const tiripleExtendInstance = new TripleExtend;
76
-
77
- class NetworkedExtention extends BasePrototype(tiripleExtendInstance) { };
78
-
79
- const networkedInstance = new NetworkedExtention;
80
-
81
- class ExtendedArray extends BasePrototype([1, 2, 3]) { };
82
- class ExtendedSet extends BasePrototype(new Set([1, 2, 3])) { };
83
-
84
- const extendedArrayInstance = new ExtendedArray;
85
- const extendedSetInstance = new ExtendedSet;
86
-
87
- const MUTATION_VALUE = -2;
88
-
89
-
90
- class MyFieldConstructorNoRe extends FieldConstructor {
91
- _value: string
92
- constructor(value: string) {
93
- super(value);
94
- Reflect.defineProperty(this, 'enumerable', {
95
- value: true
96
- });
97
- this._value = value;
98
- }
99
- }
100
- class MyFieldConstructorReGet extends MyFieldConstructorNoRe {
101
- constructor(value: string) {
102
- super(value);
103
- const self = this;
104
- Reflect.defineProperty(this, 'enumerable', {
105
- value: true
106
- });
107
- Reflect.defineProperty(this, 'get', {
108
- get() {
109
- return function () {
110
- return self._value;
111
- }
112
- },
113
- enumerable: true
114
- });
115
- }
116
- }
117
- class MyFieldConstructorReSet extends MyFieldConstructorNoRe {
118
- constructor(value: string) {
119
- super(value);
120
- const self = this;
121
- Reflect.defineProperty(this, 'enumerable', {
122
- value: true
123
- });
124
- Reflect.defineProperty(this, 'set', {
125
- get() {
126
- return function (value: string) {
127
- self._value = value;
128
- }
129
- },
130
- enumerable: true
131
- });
132
- }
133
- }
134
- class MyFieldConstructor extends MyFieldConstructorReGet {
135
- constructor(value: string) {
136
- super(value);
137
- const self = this;
138
- Reflect.defineProperty(this, 'enumerable', {
139
- value: true
140
- });
141
- Reflect.defineProperty(this, 'set', {
142
- get() {
143
- return function (value: string) {
144
- self._value = value;
145
- }
146
- },
147
- enumerable: true
148
- });
149
- }
150
- }
151
-
152
- const myField = new MyFieldConstructor('initial value');
153
- const myFieldReGet = new MyFieldConstructorReGet('initial value for get check');
154
- const myFieldReSet = new MyFieldConstructorReSet('initial value for set check');
155
-
156
- class MadeFieldClass extends BaseClass {
157
- myField = myField as unknown | string
158
- get [SymbolInitialValue] () {
159
- const self = this;
160
- return (fieldName: 'myField') => {
161
- if (fieldName !== 'myField') {
162
- return self[fieldName];
163
- }
164
- // @ts-ignore
165
- const answer = myField[SymbolInitialValue];
166
- return answer;
167
- };
168
- }
169
- };
170
- class SecondMadeFieldClass extends BaseClass { myField = myField as unknown | string };
171
- const madeFieldInstance = new MadeFieldClass;
172
- const secondMadeFieldInstance = new MadeFieldClass;
173
- const thirdMadeFieldInstance = new SecondMadeFieldClass;
174
-
175
- class MadeReGet extends BaseClass { myField = myFieldReGet as unknown | string }
176
- class MadeReSet extends BaseClass { myField = myFieldReSet as unknown | string }
177
- const madeReGet = new MadeReGet;
178
- const madeReSet = new MadeReSet;
179
-
180
- describe('props tests', () => {
181
-
182
- test('base instance has props', () => {
183
- var gf: string;
184
- var sv: string;
185
- expect(Object.keys(baseInstance)).toEqual(["numberValue", "stringValue", "booleanValue", "objectValue"]);
186
-
187
- gf = baseInstance.getterField;
188
- expect(gf).toEqual('123');
189
- sv = baseInstance.stringValue;
190
- expect(sv.valueOf()).toEqual('123');
191
-
192
-
193
- baseInstance.setterField = '12345';
194
-
195
- gf = baseInstance.getterField;
196
- expect(gf).toEqual('12345');
197
- sv = baseInstance.stringValue;
198
- expect(sv.valueOf()).toEqual('12345');
199
-
200
- baseInstance.setterField = '123';
201
-
202
- gf = baseInstance.getterField;
203
- expect(gf).toEqual('123');
204
- sv = baseInstance.stringValue;
205
- expect(`${sv}`).toEqual('123');
206
- });
207
-
208
- test('simple instance works & strings too', () => {
209
- expect(simpleInstance.stringProp.toString()).toBe('123');
210
- expect(simpleInstance.stringProp.length).toBe(3);
211
-
212
-
213
- expect(/String$/.test(simpleInstance.stringProp.constructor.name)).toBe(true);
214
- expect(() => {
215
-
216
- // @ts-ignore
217
- simpleInstance.stringProp = 123;
218
-
219
- }).toThrow(new TypeError('Type Mismatch'));
220
- });
221
-
222
- test('correct boolean comparison with type coercion', () => {
223
- expect(() => {
224
-
225
- const { booleanValue } = baseInstance;
226
- booleanValue != false;
227
-
228
- }).toThrow(new TypeError('Value Access Denied'));
229
- });
230
-
231
- test('fails boolean arithmetics', () => {
232
- expect(() => {
233
-
234
- // @ts-ignore
235
- baseInstance.booleanValue + 5;
236
-
237
- }).toThrow(new ReferenceError('Value Access Denied'));
238
- });
239
-
240
- test('correct boolean assignment', () => {
241
-
242
- let { booleanValue } = baseInstance;
243
- expect(booleanValue.valueOf()).toEqual(true);
244
-
245
- // warning!
246
- // booleanValue does not rely on baseInstance anymore!
247
- // @ts-ignore
248
- booleanValue = new Boolean(false);
249
-
250
- let value = baseInstance.booleanValue.valueOf();
251
- expect(value).toEqual(true);
252
-
253
- // @ts-ignore
254
- baseInstance.booleanValue = new Boolean(false);
255
- value = baseInstance.booleanValue.valueOf();
256
- expect(value).toEqual(false);
257
-
258
- });
259
-
260
- test('correct JSON.stringify', () => {
261
- const stringifyResult = JSON.stringify(baseInstance);
262
- expect(stringifyResult).toMatchSnapshot();
263
- });
264
-
265
- test('correct boolean constructor', () => {
266
- expect(baseInstance.booleanValue).toBeInstanceOf(Boolean);
267
- });
268
-
269
- test('correct object assignment', () => {
270
- baseInstance.objectValue = { a: 123 };
271
- // @ts-ignore
272
- expect(baseInstance.objectValue.a).toEqual(123);
273
- });
274
-
275
- test('correct custom field creation', () => {
276
- expect(madeFieldInstance.myField).toEqual('initial value');
277
- });
278
-
279
- test('correct custom field assignment', () => {
280
- madeFieldInstance.myField = 'replaced';
281
- // @ts-ignore
282
- const initialValue = madeFieldInstance[SymbolInitialValue]('myField');
283
- expect(initialValue).toEqual('initial value');
284
- expect(secondMadeFieldInstance.myField).toEqual('replaced');
285
- expect(thirdMadeFieldInstance.myField).toEqual('replaced');
286
-
287
- madeFieldInstance.myField = 'replaced secondary';
288
- expect(secondMadeFieldInstance.myField).toEqual('replaced secondary');
289
- expect(thirdMadeFieldInstance.myField).toEqual('replaced secondary');
290
-
291
- madeFieldInstance.myField = 'replaced';
292
- expect(secondMadeFieldInstance.myField).toEqual('replaced');
293
- expect(thirdMadeFieldInstance.myField).toEqual('replaced');
294
- });
295
-
296
- test('correct custom field no-re-assignment', () => {
297
- expect(madeReGet.myField).toEqual('initial value for get check');
298
- expect(() => {
299
-
300
- madeReGet.myField = 'replaced';
301
-
302
- }).toThrow(new TypeError('Re-Assirnment is Forbidden'));
303
- });
304
-
305
- test('correct custom field setter only', () => {
306
- madeReSet.myField = 'replaced';
307
- expect(madeReSet.myField).toEqual('initial value for set check');
308
- });
309
-
310
- test('takes error on wrong field definition', () => {
311
- expect(() => {
312
- class WrongFieldConstructor extends FieldConstructor {
313
- value: number
314
- constructor(value: number) {
315
- super(value)
316
- this.value = value;
317
- }
318
- }
319
- const wrongField = new WrongFieldConstructor(123);
320
- class WithWrongField extends BaseClass {
321
- erroredField = wrongField
322
- }
323
- new WithWrongField;
324
-
325
- }).toThrow();
326
- });
327
-
328
- test('correct custom missing prop search creation', () => {
329
- // @ts-ignore
330
- expect(madeFieldInstance[Symbol.toStringTag]).toEqual(undefined);
331
- // @ts-ignore
332
- expect(madeFieldInstance[Symbol.iterator]).toEqual(undefined);
333
- const util = require('util');
334
- // @ts-ignore
335
- expect(madeFieldInstance[util.inspect.custom]).toEqual(undefined);
336
- // @ts-ignore
337
- const inspected = util.inspect(madeFieldInstance);
338
- const expected = 'MadeFieldClass { myField: [Getter/Setter] }';
339
- expect(expected).toEqual(expected);
340
- });
341
-
342
- test('wrong assignment to objects', () => {
343
-
344
- expect(() => {
345
- // @ts-ignore
346
- baseInstance.objectValue = 123;
347
-
348
- }).toThrow(new TypeError('Type Mismatch'));
349
-
350
- expect(() => {
351
-
352
- baseInstance.objectValue = new Set();
353
-
354
- }).toThrow(new TypeError('Type Mismatch'));
355
-
356
- });
357
-
358
- test('fails number arithmetics', () => {
359
- expect(() => {
360
-
361
- baseInstance.numberValue + 5;
362
-
363
- }).toThrow();
364
- });
365
-
366
- test('correct number arithmetics using .valueOf()', () => {
367
-
368
- baseInstance.numberValue = MUTATION_VALUE;
369
-
370
- const result = baseInstance.numberValue.valueOf() + 5;
371
- expect(result).toStrictEqual(3);
372
-
373
- });
374
-
375
- test('correct number arithmetics using hinting for Symbol.toPrimitive (hint)', () => {
376
-
377
- const result = 3 + +baseInstance.numberValue;
378
- expect(result).toStrictEqual(1);
379
-
380
- });
381
-
382
- test('correct number value', () => {
383
- expect(baseInstance.numberValue.toString()).toStrictEqual(MUTATION_VALUE.toString());
384
- expect(/Number$/.test(baseInstance.numberValue.constructor.name)).toBe(true);
385
- });
386
-
387
-
388
- test('wrong assignment', () => {
389
- expect(() => {
390
- // @ts-ignore
391
- baseInstance.booleanValue = 123;
392
-
393
- }).toThrow(new TypeError('Type Mismatch'));
394
- });
395
-
396
- test('correct null value', () => {
397
- baseInstance.nullValue = null;
398
- expect(baseInstance.nullValue).toEqual(null);
399
- });
400
-
401
- test('any assignment to null value', () => {
402
- expect(() => {
403
-
404
- baseInstance.nullValue = 123;
405
-
406
- }).toThrow(new TypeError('Type Mismatch'));
407
- });
408
-
409
-
410
- test('correct undefined value', () => {
411
- baseInstance.undefinedValue = undefined;
412
- expect(baseInstance.undefinedValue).toEqual(undefined);
413
- });
414
-
415
- test('wrong assignment to undefined value', () => {
416
- expect(() => {
417
-
418
- baseInstance.undefinedValue = 123;
419
-
420
- }).toThrow(new TypeError('Type Mismatch'));
421
- });
422
-
423
- test('correct BigInt value', () => {
424
- baseInstance.BigIntValue = BigInt(1);
425
- expect(baseInstance.BigIntValue).toEqual(BigInt(1));
426
- });
427
-
428
- test('correct assignment to BigInt value', () => {
429
- baseInstance.BigIntValue = BigInt(2);
430
- expect(baseInstance.BigIntValue).toEqual(BigInt(2));
431
- });
432
-
433
- test('wrong assignment to BigInt value', () => {
434
- expect(() => {
435
-
436
- baseInstance.BigIntValue = 123;
437
-
438
- }).toThrow(new TypeError('Type Mismatch'));
439
- });
440
-
441
- test('correct Symbol value', () => {
442
- baseInstance.SymbolValue = Symbol('test');
443
- expect(typeof baseInstance.SymbolValue).toEqual('symbol');
444
- });
445
-
446
- test('wrong assignment to BigInt value', () => {
447
- expect(() => {
448
-
449
- baseInstance.SymbolValue = 123;
450
-
451
- }).toThrow(new TypeError('Type Mismatch'));
452
- });
453
-
454
- test('correct prototype correction', () => {
455
- expect(baseInstance.additionalProp).toEqual(321);
456
- expect(baseInstance.toString).toBeInstanceOf(Function);
457
- });
458
-
459
- test('missing value fails', () => {
460
- expect(() => {
461
-
462
- baseInstance.missingValue > 1;
463
-
464
- }).toThrow(new TypeError('Attempt to Access to Undefined Prop: [ missingValue ] of Base'));
465
- });
466
-
467
- });
468
-
469
- describe('prototype mutations tests', () => {
470
-
471
- test('incorrect prototype invocation number get', () => {
472
- expect(() => {
473
-
474
- upperInstance.numberValue > 1;
475
-
476
- }).toThrow(new ReferenceError('Value Access Denied'));
477
- });
478
-
479
- test('incorrect prototype invocation number set', () => {
480
- expect(() => {
481
-
482
- upperInstance.numberValue = new Number(1);
483
-
484
- }).toThrow(new ReferenceError('Value Access Denied'));
485
- });
486
-
487
-
488
- test('incorrect prototype invocation string get', () => {
489
- expect(() => {
490
-
491
- upperInstance.stringValue > '1';
492
-
493
- }).toThrow(new ReferenceError('Value Access Denied'));
494
- });
495
-
496
- test('incorrect prototype invocation string set', () => {
497
- expect(() => {
498
-
499
- upperInstance.stringValue = new String(1);
500
-
501
- }).toThrow(new ReferenceError('Value Access Denied'));
502
- });
503
-
504
- });
505
-
506
- describe('methods tests', () => {
507
-
508
- test('functions are restricted for data type', () => {
509
-
510
- expect(() => {
511
-
512
- baseInstance.someMethod = function () { };
513
-
514
- }).toThrow(new TypeError('Functions are Restricted'));
515
-
516
- });
517
-
518
- test('proxy proto methods are SOLID', () => {
519
-
520
- const result = baseInstance.someMethod();
521
- expect(result).toBe(MUTATION_VALUE);
522
-
523
- });
524
-
525
- });
526
-
527
- describe('property re-definition works', () => {
528
-
529
- test('exact prototype invocation for correct property extraction', () => {
530
-
531
- Object.defineProperty(upperInstance, 'stringProp', {
532
- get() {
533
- const target = Reflect.getPrototypeOf(upperInstance) as {
534
- stringValue: string
535
- };
536
- return target.stringValue;
537
- }
538
- });
539
-
540
- const value = upperInstance.stringProp;
541
- expect(`${value}`).toEqual('123');
542
-
543
- });
544
- });
545
-
546
-
547
- describe('functional constructors tests', () => {
548
- test('construction made properly', () => {
549
-
550
- const { stringProp,
551
- constructor: {
552
- name
553
- }
554
- } = myFunctionalInstance;
555
- expect(name).toEqual('MyFunctionalConstructor');
556
- expect(stringProp.valueOf()).toEqual('123');
557
-
558
- });
559
- });
560
-
561
- describe('instanceof works', () => {
562
- test('for class construction', () => {
563
-
564
- expect(baseInstance).toBeInstanceOf(Base);
565
-
566
- });
567
- test('for simple construction', () => {
568
-
569
- expect(simpleInstance).toBeInstanceOf(SimpleBase);
570
-
571
- });
572
- test('for functional construction', () => {
573
-
574
- expect(myFunctionalInstance).toBeInstanceOf(MyFunctionalConstructor);
575
-
576
- });
577
- });
578
-
579
- describe('deep extend works', () => {
580
- test('class extended three times construction', () => {
581
-
582
- expect(tiripleExtendInstance).toBeInstanceOf(Base);
583
- expect(tiripleExtendInstance).toBeInstanceOf(SecondaryExtend);
584
- expect(tiripleExtendInstance).toBeInstanceOf(TripleExtend);
585
- expect(`${tiripleExtendInstance.stringValue}`).toEqual('123');
586
- expect(tiripleExtendInstance.second).toBeInstanceOf(Number);
587
-
588
- });
589
-
590
- test('network extention works', () => {
591
- expect(networkedInstance).toBeInstanceOf(NetworkedExtention);
592
-
593
- expect(() => {
594
-
595
- `${networkedInstance.stringValue}`;
596
-
597
- }).toThrow(new ReferenceError('Value Access Denied'));
598
-
599
- });
600
-
601
- test('builtin types works', () => {
602
- expect(extendedArrayInstance).toBeInstanceOf(Array);
603
- expect(extendedArrayInstance).toBeInstanceOf(ExtendedArray);
604
- expect(extendedArrayInstance[0]).toBe(1);
605
- extendedArrayInstance.unshift(0);
606
- extendedArrayInstance.push(5);
607
- expect(+extendedArrayInstance.length).toBe(5);
608
- expect(+extendedArrayInstance[0]).toBe(0);
609
- expect(+extendedArrayInstance[4]).toBe(5);
610
-
611
- expect(extendedSetInstance).toBeInstanceOf(Set);
612
- expect(extendedSetInstance).toBeInstanceOf(ExtendedSet);
613
-
614
- // JS Object.create(new Set([1, 2, 3])) is doing the same error!
615
- expect(() => {
616
-
617
- extendedSetInstance.has(0);
618
-
619
- }).toThrow(new TypeError('Method Set.prototype.has called on incompatible receiver [object Object]'));
620
-
621
- });
622
- });
@@ -1,38 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "module": "commonjs",
4
- "target": "ES2015",
5
- "lib": [
6
- "ES6",
7
- ],
8
- "strict": true,
9
- "alwaysStrict": true,
10
- "strictFunctionTypes": true,
11
- "diagnostics": true,
12
- "noImplicitThis": true,
13
- "extendedDiagnostics": true,
14
- "noImplicitReturns": true,
15
- "noFallthroughCasesInSwitch": true,
16
- "noUnusedLocals": true,
17
- "noUnusedParameters": true,
18
- "declaration": true,
19
- "sourceMap": false,
20
- "outDir": "lib",
21
- "traceResolution": false,
22
- "removeComments": true,
23
- "experimentalDecorators": true,
24
- "forceConsistentCasingInFileNames": true,
25
- "types": [
26
- "jest",
27
- "node"
28
- ]
29
- },
30
- "include": [
31
- "./test/**/*.ts"
32
- ],
33
- "exclude": [
34
- "./lib/**/*",
35
- "./coverage/**/*",
36
- "./node_modules/**/*",
37
- ]
38
- }
package/tsconfig.json DELETED
@@ -1,34 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "module": "commonjs",
4
- "target": "ES2015",
5
- "lib": [
6
- "ES6",
7
- ],
8
- "strict": true,
9
- "alwaysStrict": true,
10
- "strictFunctionTypes": true,
11
- "diagnostics": true,
12
- "noImplicitThis": true,
13
- "extendedDiagnostics": true,
14
- "noImplicitReturns": true,
15
- "noFallthroughCasesInSwitch": true,
16
- "noUnusedLocals": true,
17
- "noUnusedParameters": true,
18
- "declaration": true,
19
- "sourceMap": false,
20
- "outDir": "lib",
21
- "traceResolution": false,
22
- "removeComments": true,
23
- "experimentalDecorators": true,
24
- "forceConsistentCasingInFileNames": true,
25
- "types": ["node"]
26
- },
27
- "include": [
28
- "./src/**/*.ts",
29
- ],
30
- "exclude": [
31
- "./lib/**/*",
32
- "./coverage/**/*",
33
- ]
34
- }