Skip to content

Commit

Permalink
feat(recognize): gets recognized types and instances of given value
Browse files Browse the repository at this point in the history
… by using `instanceof`, `typeof` operator, and `typeOf()`, `isClass()`, `isFunction()` functions.
  • Loading branch information
sciborrudnicki committed Aug 16, 2021
1 parent a62bd0e commit d2f756e
Show file tree
Hide file tree
Showing 4 changed files with 408 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/recognize/src/recognize-instances.const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* An `Array` of default objects to check by using the `instanceof` operator.
*/
export const RECOGNIZE_INSTANCES = [
Array,
ArrayBuffer,
Boolean,
DataView,
Date,
Error,
EvalError,
Float32Array,
Float64Array,
Function,
Int16Array,
Int32Array,
Int8Array,
Map,
Number,
Object,
Promise,
RangeError,
ReferenceError,
RegExp,
Set,
Storage,
String,
SyntaxError,
TypeError,
URIError,
Uint16Array,
Uint32Array,
Uint8Array,
Uint8ClampedArray,
WeakMap,
WeakSet
];
44 changes: 44 additions & 0 deletions src/recognize/src/recognize-value.func.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Object.
import { is } from '../../is/lib/is.object';
// Function.
import { typeOf } from '../../lib/type-of.func';
// Type.
import { OfRecognized } from '../type/of-recognized.type';
// Const.
import { RECOGNIZE_INSTANCES } from './recognize-instances.const';
/**
* Gets recognized types and instances of given `value` by using `instanceof`, `typeof` operator, and `typeOf()`, `isClass()`,
* `isFunction()` functions.
* @param value The value of any type to recognize.
* @param onlyTrue Determines whether or not show only recognized as `true`.
* @param instances An optional array of objects to check by using `instanceof` operator.
* @returns The return value is an object of types and instances recognized as `true` or all even those recognized as `false`.
*/
export const recognizeValue = (
value: any,
onlyTrue: boolean = true,
instances: any[] = []
): OfRecognized => {
// Recognize types.
const ofRecognized: OfRecognized = {
class: is.class(value),
function: is.function(value),
typeOf: typeOf(value),
typeof: typeof value,
};

// Recognize instances.
RECOGNIZE_INSTANCES.concat(instances as any).forEach((instance) => (
Object.assign(ofRecognized, { [instance.name]: value instanceof instance })
));

// If only true remove false.
if (is.true(onlyTrue)) {
Object.keys(ofRecognized).filter((type: string) =>
is.false(ofRecognized[type as keyof OfRecognized])
? delete ofRecognized[type as keyof OfRecognized]
: true
);
}
return ofRecognized;
};
284 changes: 284 additions & 0 deletions src/recognize/test/recognize-value.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,284 @@
import { Testing, TestingToBeMatchers } from '@angular-package/testing';
// Function.
import { recognizeValue } from '../src/recognize-value.func';

/*
Initialize testing.
*/
const testing = new Testing(true, true);
const toBe = new TestingToBeMatchers();

class CustomClass {}
const customClass = new CustomClass();

const firstName = 'Artemis';

/*
Tests.
*/
testing.describe(recognizeValue.name, () =>
testing
.toEqual(`string`, recognizeValue(firstName), {
typeOf: 'string',
typeof: 'string',
})

.toEqual(`new String(firstName)`, recognizeValue(new String(firstName)), {
typeOf: 'string',
typeof: 'object',
Object: true,
String: true,
})

.toEqual(`CustomClass`, recognizeValue(customClass, true, [CustomClass]), {
typeOf: 'object',
typeof: 'object',
Object: true,
CustomClass: true,
})

.toEqual(`CustomClass`, recognizeValue(CustomClass, true, [CustomClass]), {
class: true,
typeOf: 'function',
typeof: 'function',
Function: true,
Object: true,
})

.toEqual(`new Array()`, recognizeValue(new Array()), {
Array: true,
Object: true,
typeOf: 'array',
typeof: 'object',
})

.toEqual(`Array`, recognizeValue(Array), {
Function: true,
Object: true,
function: true,
typeOf: 'function',
typeof: 'function',
})

.toEqual(`new ArrayBuffer(50)`, recognizeValue(new ArrayBuffer(50)), {
ArrayBuffer: true,
Object: true,
typeOf: 'arraybuffer',
typeof: 'object',
})

.toEqual(`new Boolean()`, recognizeValue(new Boolean()), {
Boolean: true,
Object: true,
typeOf: 'boolean',
typeof: 'object',
})

.toEqual(
`new DataView(new ArrayBuffer(16))`,
recognizeValue(new DataView(new ArrayBuffer(16))),
{ DataView: true, Object: true, typeOf: 'dataview', typeof: 'object' }
)

.toEqual(`new Date()`, recognizeValue(new Date()), {
Date: true,
Object: true,
typeOf: 'date',
typeof: 'object',
})

.toEqual(`new Error()`, recognizeValue(new Error()), {
Error: true,
Object: true,
typeOf: 'error',
typeof: 'object',
})

.toEqual(`new EvalError()`, recognizeValue(new EvalError()), {
Error: true,
EvalError: true,
Object: true,
typeOf: 'error',
typeof: 'object',
})

.toEqual(`new Int16Array()`, recognizeValue(new Int16Array()), {
Int16Array: true,
Object: true,
typeOf: 'int16array',
typeof: 'object',
})

.toEqual(`new Int32Array()`, recognizeValue(new Int32Array()), {
Int32Array: true,
Object: true,
typeOf: 'int32array',
typeof: 'object',
})

.toEqual(`new Int8Array()`, recognizeValue(new Int8Array()), {
Int8Array: true,
Object: true,
typeOf: 'int8array',
typeof: 'object',
})

.toEqual(`new Map()`, recognizeValue(new Map()), {
Map: true,
Object: true,
typeOf: 'map',
typeof: 'object',
})

// .toEqual(`new Promise((resolve, reject) => {
// setTimeout(() => {
// resolve('foo');
// }, 300);
// })`, recognizeValue(new Promise((resolve, reject) => {
// setTimeout(() => {
// resolve('foo');
// }, 300);
// })), {Promise: true, Object: true, typeOf: 'promise', typeof: 'object'})

.toEqual(`new RangeError()`, recognizeValue(new RangeError()), {
Error: true,
RangeError: true,
Object: true,
typeOf: 'error',
typeof: 'object',
})

.toEqual(`new ReferenceError()`, recognizeValue(new ReferenceError()), {
Error: true,
ReferenceError: true,
Object: true,
typeOf: 'error',
typeof: 'object',
})

.toEqual(`new RegExp(/[^]/g)`, recognizeValue(new RegExp(/[^]/g)), {
typeOf: 'regexp',
typeof: 'object',
Object: true,
RegExp: true,
})

.toEqual(`new Set()`, recognizeValue(new Set()), {
Set: true,
Object: true,
typeOf: 'set',
typeof: 'object',
})

.toEqual(`new SyntaxError()`, recognizeValue(new SyntaxError()), {
Error: true,
SyntaxError: true,
Object: true,
typeOf: 'error',
typeof: 'object',
})

.toEqual(`new Float32Array()`, recognizeValue(new Float32Array()), {
Float32Array: true,
Object: true,
typeOf: 'float32array',
typeof: 'object',
})

.toEqual(`new Float64Array()`, recognizeValue(new Float64Array()), {
Float64Array: true,
Object: true,
typeOf: 'float64array',
typeof: 'object',
})

.toEqual(`new Function()`, recognizeValue(new Function()), {
Function: true,
Object: true,
function: true,
typeOf: 'function',
typeof: 'function',
})

.toEqual(`new Number(5)`, recognizeValue(new Number(5)), {
Number: true,
Object: true,
typeOf: 'number',
typeof: 'object',
})

.toEqual(`new Object()`, recognizeValue(new Object()), {
Object: true,
typeOf: 'object',
typeof: 'object',
})

.toEqual(`new String()`, recognizeValue(new String()), {
Object: true,
String: true,
typeOf: 'string',
typeof: 'object',
})

.toEqual(`new TypeError()`, recognizeValue(new TypeError()), {
Error: true,
Object: true,
TypeError: true,
typeOf: 'error',
typeof: 'object',
})

.toEqual(`new Uint16Array()`, recognizeValue(new Uint16Array()), {
Object: true,
Uint16Array: true,
typeOf: 'uint16array',
typeof: 'object',
})

.toEqual(`new Uint32Array()`, recognizeValue(new Uint32Array()), {
Object: true,
Uint32Array: true,
typeOf: 'uint32array',
typeof: 'object',
})

.toEqual(`new Uint8Array()`, recognizeValue(new Uint8Array()), {
Object: true,
Uint8Array: true,
typeOf: 'uint8array',
typeof: 'object',
})

.toEqual(
`new Uint8ClampedArray()`,
recognizeValue(new Uint8ClampedArray()),
{
Object: true,
Uint8ClampedArray: true,
typeOf: 'uint8clampedarray',
typeof: 'object',
}
)

.toEqual(`new URIError()`, recognizeValue(new URIError()), {
Error: true,
Object: true,
URIError: true,
typeOf: 'error',
typeof: 'object',
})

.toEqual(`new WeakMap()`, recognizeValue(new WeakMap()), {
Object: true,
WeakMap: true,
typeOf: 'weakmap',
typeof: 'object',
})

.toEqual(`new WeakSet()`, recognizeValue(new WeakSet()), {
Object: true,
WeakSet: true,
typeOf: 'weakset',
typeof: 'object',
})
);
Loading

0 comments on commit d2f756e

Please sign in to comment.