Skip to content

Commit d2f756e

Browse files
feat(recognize): gets recognized types and instances of given value by using instanceof, typeof operator, and typeOf(), isClass(), isFunction() functions.
1 parent a62bd0e commit d2f756e

File tree

4 files changed

+408
-0
lines changed

4 files changed

+408
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* An `Array` of default objects to check by using the `instanceof` operator.
3+
*/
4+
export const RECOGNIZE_INSTANCES = [
5+
Array,
6+
ArrayBuffer,
7+
Boolean,
8+
DataView,
9+
Date,
10+
Error,
11+
EvalError,
12+
Float32Array,
13+
Float64Array,
14+
Function,
15+
Int16Array,
16+
Int32Array,
17+
Int8Array,
18+
Map,
19+
Number,
20+
Object,
21+
Promise,
22+
RangeError,
23+
ReferenceError,
24+
RegExp,
25+
Set,
26+
Storage,
27+
String,
28+
SyntaxError,
29+
TypeError,
30+
URIError,
31+
Uint16Array,
32+
Uint32Array,
33+
Uint8Array,
34+
Uint8ClampedArray,
35+
WeakMap,
36+
WeakSet
37+
];
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Object.
2+
import { is } from '../../is/lib/is.object';
3+
// Function.
4+
import { typeOf } from '../../lib/type-of.func';
5+
// Type.
6+
import { OfRecognized } from '../type/of-recognized.type';
7+
// Const.
8+
import { RECOGNIZE_INSTANCES } from './recognize-instances.const';
9+
/**
10+
* Gets recognized types and instances of given `value` by using `instanceof`, `typeof` operator, and `typeOf()`, `isClass()`,
11+
* `isFunction()` functions.
12+
* @param value The value of any type to recognize.
13+
* @param onlyTrue Determines whether or not show only recognized as `true`.
14+
* @param instances An optional array of objects to check by using `instanceof` operator.
15+
* @returns The return value is an object of types and instances recognized as `true` or all even those recognized as `false`.
16+
*/
17+
export const recognizeValue = (
18+
value: any,
19+
onlyTrue: boolean = true,
20+
instances: any[] = []
21+
): OfRecognized => {
22+
// Recognize types.
23+
const ofRecognized: OfRecognized = {
24+
class: is.class(value),
25+
function: is.function(value),
26+
typeOf: typeOf(value),
27+
typeof: typeof value,
28+
};
29+
30+
// Recognize instances.
31+
RECOGNIZE_INSTANCES.concat(instances as any).forEach((instance) => (
32+
Object.assign(ofRecognized, { [instance.name]: value instanceof instance })
33+
));
34+
35+
// If only true remove false.
36+
if (is.true(onlyTrue)) {
37+
Object.keys(ofRecognized).filter((type: string) =>
38+
is.false(ofRecognized[type as keyof OfRecognized])
39+
? delete ofRecognized[type as keyof OfRecognized]
40+
: true
41+
);
42+
}
43+
return ofRecognized;
44+
};
Lines changed: 284 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,284 @@
1+
import { Testing, TestingToBeMatchers } from '@angular-package/testing';
2+
// Function.
3+
import { recognizeValue } from '../src/recognize-value.func';
4+
5+
/*
6+
Initialize testing.
7+
*/
8+
const testing = new Testing(true, true);
9+
const toBe = new TestingToBeMatchers();
10+
11+
class CustomClass {}
12+
const customClass = new CustomClass();
13+
14+
const firstName = 'Artemis';
15+
16+
/*
17+
Tests.
18+
*/
19+
testing.describe(recognizeValue.name, () =>
20+
testing
21+
.toEqual(`string`, recognizeValue(firstName), {
22+
typeOf: 'string',
23+
typeof: 'string',
24+
})
25+
26+
.toEqual(`new String(firstName)`, recognizeValue(new String(firstName)), {
27+
typeOf: 'string',
28+
typeof: 'object',
29+
Object: true,
30+
String: true,
31+
})
32+
33+
.toEqual(`CustomClass`, recognizeValue(customClass, true, [CustomClass]), {
34+
typeOf: 'object',
35+
typeof: 'object',
36+
Object: true,
37+
CustomClass: true,
38+
})
39+
40+
.toEqual(`CustomClass`, recognizeValue(CustomClass, true, [CustomClass]), {
41+
class: true,
42+
typeOf: 'function',
43+
typeof: 'function',
44+
Function: true,
45+
Object: true,
46+
})
47+
48+
.toEqual(`new Array()`, recognizeValue(new Array()), {
49+
Array: true,
50+
Object: true,
51+
typeOf: 'array',
52+
typeof: 'object',
53+
})
54+
55+
.toEqual(`Array`, recognizeValue(Array), {
56+
Function: true,
57+
Object: true,
58+
function: true,
59+
typeOf: 'function',
60+
typeof: 'function',
61+
})
62+
63+
.toEqual(`new ArrayBuffer(50)`, recognizeValue(new ArrayBuffer(50)), {
64+
ArrayBuffer: true,
65+
Object: true,
66+
typeOf: 'arraybuffer',
67+
typeof: 'object',
68+
})
69+
70+
.toEqual(`new Boolean()`, recognizeValue(new Boolean()), {
71+
Boolean: true,
72+
Object: true,
73+
typeOf: 'boolean',
74+
typeof: 'object',
75+
})
76+
77+
.toEqual(
78+
`new DataView(new ArrayBuffer(16))`,
79+
recognizeValue(new DataView(new ArrayBuffer(16))),
80+
{ DataView: true, Object: true, typeOf: 'dataview', typeof: 'object' }
81+
)
82+
83+
.toEqual(`new Date()`, recognizeValue(new Date()), {
84+
Date: true,
85+
Object: true,
86+
typeOf: 'date',
87+
typeof: 'object',
88+
})
89+
90+
.toEqual(`new Error()`, recognizeValue(new Error()), {
91+
Error: true,
92+
Object: true,
93+
typeOf: 'error',
94+
typeof: 'object',
95+
})
96+
97+
.toEqual(`new EvalError()`, recognizeValue(new EvalError()), {
98+
Error: true,
99+
EvalError: true,
100+
Object: true,
101+
typeOf: 'error',
102+
typeof: 'object',
103+
})
104+
105+
.toEqual(`new Int16Array()`, recognizeValue(new Int16Array()), {
106+
Int16Array: true,
107+
Object: true,
108+
typeOf: 'int16array',
109+
typeof: 'object',
110+
})
111+
112+
.toEqual(`new Int32Array()`, recognizeValue(new Int32Array()), {
113+
Int32Array: true,
114+
Object: true,
115+
typeOf: 'int32array',
116+
typeof: 'object',
117+
})
118+
119+
.toEqual(`new Int8Array()`, recognizeValue(new Int8Array()), {
120+
Int8Array: true,
121+
Object: true,
122+
typeOf: 'int8array',
123+
typeof: 'object',
124+
})
125+
126+
.toEqual(`new Map()`, recognizeValue(new Map()), {
127+
Map: true,
128+
Object: true,
129+
typeOf: 'map',
130+
typeof: 'object',
131+
})
132+
133+
// .toEqual(`new Promise((resolve, reject) => {
134+
// setTimeout(() => {
135+
// resolve('foo');
136+
// }, 300);
137+
// })`, recognizeValue(new Promise((resolve, reject) => {
138+
// setTimeout(() => {
139+
// resolve('foo');
140+
// }, 300);
141+
// })), {Promise: true, Object: true, typeOf: 'promise', typeof: 'object'})
142+
143+
.toEqual(`new RangeError()`, recognizeValue(new RangeError()), {
144+
Error: true,
145+
RangeError: true,
146+
Object: true,
147+
typeOf: 'error',
148+
typeof: 'object',
149+
})
150+
151+
.toEqual(`new ReferenceError()`, recognizeValue(new ReferenceError()), {
152+
Error: true,
153+
ReferenceError: true,
154+
Object: true,
155+
typeOf: 'error',
156+
typeof: 'object',
157+
})
158+
159+
.toEqual(`new RegExp(/[^]/g)`, recognizeValue(new RegExp(/[^]/g)), {
160+
typeOf: 'regexp',
161+
typeof: 'object',
162+
Object: true,
163+
RegExp: true,
164+
})
165+
166+
.toEqual(`new Set()`, recognizeValue(new Set()), {
167+
Set: true,
168+
Object: true,
169+
typeOf: 'set',
170+
typeof: 'object',
171+
})
172+
173+
.toEqual(`new SyntaxError()`, recognizeValue(new SyntaxError()), {
174+
Error: true,
175+
SyntaxError: true,
176+
Object: true,
177+
typeOf: 'error',
178+
typeof: 'object',
179+
})
180+
181+
.toEqual(`new Float32Array()`, recognizeValue(new Float32Array()), {
182+
Float32Array: true,
183+
Object: true,
184+
typeOf: 'float32array',
185+
typeof: 'object',
186+
})
187+
188+
.toEqual(`new Float64Array()`, recognizeValue(new Float64Array()), {
189+
Float64Array: true,
190+
Object: true,
191+
typeOf: 'float64array',
192+
typeof: 'object',
193+
})
194+
195+
.toEqual(`new Function()`, recognizeValue(new Function()), {
196+
Function: true,
197+
Object: true,
198+
function: true,
199+
typeOf: 'function',
200+
typeof: 'function',
201+
})
202+
203+
.toEqual(`new Number(5)`, recognizeValue(new Number(5)), {
204+
Number: true,
205+
Object: true,
206+
typeOf: 'number',
207+
typeof: 'object',
208+
})
209+
210+
.toEqual(`new Object()`, recognizeValue(new Object()), {
211+
Object: true,
212+
typeOf: 'object',
213+
typeof: 'object',
214+
})
215+
216+
.toEqual(`new String()`, recognizeValue(new String()), {
217+
Object: true,
218+
String: true,
219+
typeOf: 'string',
220+
typeof: 'object',
221+
})
222+
223+
.toEqual(`new TypeError()`, recognizeValue(new TypeError()), {
224+
Error: true,
225+
Object: true,
226+
TypeError: true,
227+
typeOf: 'error',
228+
typeof: 'object',
229+
})
230+
231+
.toEqual(`new Uint16Array()`, recognizeValue(new Uint16Array()), {
232+
Object: true,
233+
Uint16Array: true,
234+
typeOf: 'uint16array',
235+
typeof: 'object',
236+
})
237+
238+
.toEqual(`new Uint32Array()`, recognizeValue(new Uint32Array()), {
239+
Object: true,
240+
Uint32Array: true,
241+
typeOf: 'uint32array',
242+
typeof: 'object',
243+
})
244+
245+
.toEqual(`new Uint8Array()`, recognizeValue(new Uint8Array()), {
246+
Object: true,
247+
Uint8Array: true,
248+
typeOf: 'uint8array',
249+
typeof: 'object',
250+
})
251+
252+
.toEqual(
253+
`new Uint8ClampedArray()`,
254+
recognizeValue(new Uint8ClampedArray()),
255+
{
256+
Object: true,
257+
Uint8ClampedArray: true,
258+
typeOf: 'uint8clampedarray',
259+
typeof: 'object',
260+
}
261+
)
262+
263+
.toEqual(`new URIError()`, recognizeValue(new URIError()), {
264+
Error: true,
265+
Object: true,
266+
URIError: true,
267+
typeOf: 'error',
268+
typeof: 'object',
269+
})
270+
271+
.toEqual(`new WeakMap()`, recognizeValue(new WeakMap()), {
272+
Object: true,
273+
WeakMap: true,
274+
typeOf: 'weakmap',
275+
typeof: 'object',
276+
})
277+
278+
.toEqual(`new WeakSet()`, recognizeValue(new WeakSet()), {
279+
Object: true,
280+
WeakSet: true,
281+
typeOf: 'weakset',
282+
typeof: 'object',
283+
})
284+
);

0 commit comments

Comments
 (0)