typeomatica 0.3.3 → 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/lib/errors.js +1 -0
- package/lib/fields.js +1 -0
- package/lib/index.d.ts +14 -7
- package/lib/index.js +131 -16
- package/lib/types/functions.js +1 -0
- package/lib/types/index.js +1 -0
- package/lib/types/nullish.js +1 -0
- package/lib/types/objects.js +1 -0
- package/lib/types/primitives.js +1 -0
- package/lib/types/special.js +1 -0
- package/package.json +40 -19
- package/.editorconfig +0 -11
- package/.eslintignore +0 -0
- package/.eslintrc.js +0 -72
- package/.gitattributes +0 -22
- package/.github/workflows/node.js.yml +0 -62
- package/.husky/pre-commit +0 -4
- package/jest.config.js +0 -8
- package/src/errors.ts +0 -9
- package/src/fields.ts +0 -76
- package/src/index.ts +0 -208
- package/src/types/functions.ts +0 -7
- package/src/types/index.ts +0 -17
- package/src/types/nullish.ts +0 -15
- package/src/types/objects.ts +0 -19
- package/src/types/primitives.ts +0 -69
- package/src/types/special.ts +0 -19
- package/test/__snapshots__/index.ts.snap +0 -3
- package/test/addition.js +0 -44
- package/test/index.ts +0 -683
- package/tsconfig.jest.json +0 -38
- package/tsconfig.json +0 -34
package/src/index.ts
DELETED
|
@@ -1,208 +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
|
-
const props2skip = new Set([Symbol.toStringTag, Symbol.iterator]);
|
|
90
|
-
const util = require('util');
|
|
91
|
-
const hasNodeInspect = (util && util.inspect && util.inspect.custom);
|
|
92
|
-
hasNodeInspect && (props2skip.add(util.inspect.custom));
|
|
93
|
-
|
|
94
|
-
const handlers = {
|
|
95
|
-
get(target: object, prop: string | symbol, receiver: object) {
|
|
96
|
-
const result = Reflect.get(target, prop, receiver);
|
|
97
|
-
if (result !== undefined) {
|
|
98
|
-
return result;
|
|
99
|
-
}
|
|
100
|
-
if (prop === 'toJSON') {
|
|
101
|
-
// eslint-disable-next-line no-unused-vars
|
|
102
|
-
return function (this: typeof target) {
|
|
103
|
-
const entries = Object.entries(this);
|
|
104
|
-
return JSON.stringify(entries.reduce((obj, [key, value]) => {
|
|
105
|
-
// @ts-ignore
|
|
106
|
-
obj[key] = value.valueOf();
|
|
107
|
-
return obj;
|
|
108
|
-
}, {}));
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
// @ts-ignore
|
|
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
|
-
// Reflect.defineProperty(target, key, descriptor);
|
|
123
|
-
// return true;
|
|
124
|
-
// }
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
// user have to precisely define all props
|
|
128
|
-
const BaseTarget = Object.create(null);
|
|
129
|
-
|
|
130
|
-
type Proto<P, T> = Pick<P, Exclude<keyof P, keyof T>> & T;
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
export const BaseConstructorPrototype = function <
|
|
134
|
-
P extends object,
|
|
135
|
-
S extends Proto<T, P>,
|
|
136
|
-
T extends {
|
|
137
|
-
(): P
|
|
138
|
-
new (): {
|
|
139
|
-
[key in keyof S]: S[key]
|
|
140
|
-
}
|
|
141
|
-
},
|
|
142
|
-
>(
|
|
143
|
-
this: T,
|
|
144
|
-
InstanceTarget: P = BaseTarget
|
|
145
|
-
): T {
|
|
146
|
-
if (!new.target) {
|
|
147
|
-
|
|
148
|
-
const self: {
|
|
149
|
-
prototype: {
|
|
150
|
-
constructor: typeof BaseConstructorPrototype
|
|
151
|
-
}
|
|
152
|
-
} = BaseConstructorPrototype.bind(this, InstanceTarget);
|
|
153
|
-
|
|
154
|
-
self.prototype = {
|
|
155
|
-
constructor: BaseConstructorPrototype
|
|
156
|
-
};
|
|
157
|
-
|
|
158
|
-
return self as T;
|
|
159
|
-
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
const InstancePrototype = new Proxy(InstanceTarget, handlers);
|
|
163
|
-
|
|
164
|
-
let protoPointer = this as object;
|
|
165
|
-
let protoConstrcutor;
|
|
166
|
-
do {
|
|
167
|
-
protoPointer = Reflect.getPrototypeOf(protoPointer) as object;
|
|
168
|
-
protoConstrcutor = Reflect.getOwnPropertyDescriptor(protoPointer, 'constructor')!.value;
|
|
169
|
-
} while (protoConstrcutor !== BaseConstructorPrototype);
|
|
170
|
-
|
|
171
|
-
Reflect.setPrototypeOf(protoPointer, InstancePrototype);
|
|
172
|
-
return this;
|
|
173
|
-
|
|
174
|
-
};
|
|
175
|
-
|
|
176
|
-
// as ObjectConstructor & {
|
|
177
|
-
// (): void
|
|
178
|
-
// // eslint-disable-next-line no-unused-vars
|
|
179
|
-
// new<T>(param?: T extends object ? T : {}): {
|
|
180
|
-
// [key in keyof T]: T[key]
|
|
181
|
-
// }
|
|
182
|
-
// };
|
|
183
|
-
|
|
184
|
-
Object.defineProperty(module, 'exports', {
|
|
185
|
-
get() {
|
|
186
|
-
return BaseConstructorPrototype;
|
|
187
|
-
},
|
|
188
|
-
enumerable: true
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
// eslint-disable-next-line new-cap
|
|
193
|
-
// @ts-ignore
|
|
194
|
-
export class BaseClass extends BaseConstructorPrototype { }
|
|
195
|
-
export { FieldConstructor } from './fields';
|
|
196
|
-
|
|
197
|
-
Object.defineProperty(module.exports, 'BaseClass', {
|
|
198
|
-
get() {
|
|
199
|
-
return BaseClass;
|
|
200
|
-
},
|
|
201
|
-
enumerable: true
|
|
202
|
-
});
|
|
203
|
-
Object.defineProperty(module.exports, 'FieldConstructor', {
|
|
204
|
-
get() {
|
|
205
|
-
return FieldConstructor;
|
|
206
|
-
},
|
|
207
|
-
enumerable: true
|
|
208
|
-
});
|
package/src/types/functions.ts
DELETED
package/src/types/index.ts
DELETED
|
@@ -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
|
-
};
|
package/src/types/nullish.ts
DELETED
package/src/types/objects.ts
DELETED
|
@@ -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
|
-
};
|
package/src/types/primitives.ts
DELETED
|
@@ -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
|
-
};
|
package/src/types/special.ts
DELETED
|
@@ -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
|
-
};
|
package/test/addition.js
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const BasePrototype = require('..');
|
|
4
|
-
|
|
5
|
-
// eslint-disable-next-line new-cap
|
|
6
|
-
class Base extends BasePrototype({
|
|
7
|
-
additionalProp: 321,
|
|
8
|
-
}) {
|
|
9
|
-
numberValue = 123;
|
|
10
|
-
constructor() {
|
|
11
|
-
super();
|
|
12
|
-
this.stringValue = '123';
|
|
13
|
-
this.booleanValue = true;
|
|
14
|
-
this.objectValue = {};
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const baseInstance = new Base;
|
|
19
|
-
|
|
20
|
-
describe('props tests', () => {
|
|
21
|
-
|
|
22
|
-
test('base instance has props', () => {
|
|
23
|
-
expect(Object.keys(baseInstance)).toEqual(['numberValue', 'stringValue', 'booleanValue', 'objectValue']);
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
test('JavaScript class fields allow re-definition', () => {
|
|
27
|
-
baseInstance.numberValue = '123';
|
|
28
|
-
expect(baseInstance.numberValue).toEqual('123');
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
test('everything the rest is the same', () => {
|
|
32
|
-
expect(baseInstance.additionalProp).toEqual(321);
|
|
33
|
-
expect(() => {
|
|
34
|
-
baseInstance.stringValue = 123;
|
|
35
|
-
}).toThrow(new TypeError('Type Mismatch'));
|
|
36
|
-
expect(() => {
|
|
37
|
-
baseInstance.booleanValue = 123;
|
|
38
|
-
}).toThrow(new TypeError('Type Mismatch'));
|
|
39
|
-
expect(() => {
|
|
40
|
-
baseInstance.objectValue = null;
|
|
41
|
-
}).toThrow(new TypeError('Type Mismatch'));
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
});
|