recognizeValue()
recognizeValue()
recognizeValue()Gets recognized types and instances of given value.
const recognizeValue = (
value: any,
onlyTrue: boolean = true,
instances: any[] = []
): OfRecognized => {
// Recognize types.
const ofRecognized: OfRecognized = {
'Array.isArray': Array.isArray(value),
isClass: is.class(value),
isFunction: is.function(value),
'Number.isInteger': Number.isInteger(value),
'Number.isFinite': Number.isFinite(value),
'Number.isNaN': Number.isNaN(value),
'Number.isSafeInteger': Number.isSafeInteger(value),
typeOf: typeOf(value),
typeof: typeof value,
};
try { Object.assign(ofRecognized, { isFinite: isFinite(value) }); } catch (e) {}
try { Object.assign(ofRecognized, { isNaN: isNaN(value) }); } catch (e) {}
// 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;
};Parameters
value: any
value: anyThe value of any type to recognize.
onlyTrue: boolean
onlyTrue: booleanDetermines whether to show only recognized as true.
instances: any[]
instances: any[]An optional array of objects to check by using instanceof operator.
Return type
OfRecognized
OfRecognizedThe return type is an interface of the operators and functions used to check.
Returns
The return value is an object of types and instances recognized as true or all even those recognized as false.
Example usage
// Example usage.
import { recognizeValue } from '@angular-package/type';
// Class.
class CustomClass {}
const customClass = new CustomClass();
// String.
const firstName = 'Artemis';
/*
Returns {
"typeOf": "object",
"typeof": "object",
"isNaN": true,
"Object": true,
"CustomClass": true
}
*/
recognizeValue(customClass, true, [CustomClass]);
/*
Returns {
"isClass": true,
"typeOf": "function",
"typeof": "function",
"isNaN": true,
"Function": true,
"Object": true
}
*/
recognizeValue(CustomClass);
/*
Returns {
"typeOf": "string",
"typeof": "string",
"isNaN": true
}
*/
recognizeValue(firstName);
/*
Returns {
"typeOf": "string",
"typeof": "object",
"isNaN": true,
"Object": true,
"String": true
}
*/
recognizeValue(new String(firstName));Last updated
Was this helpful?