typing
npm install @orioro/typing
yarn add @orioro/typing
Simple type validation utilities. Meant for checking types of argument input in microlibraries. For more complex use cases, such as object property or array item validation, see either:
-
@orioro/expression
- https://github.com/orioro/node-expression -
@orioro/validate
- https://github.com/orioro/node-validate -
@orioro/schema
- https://github.com/orioro/schema
API Docs
_getType()
_isType()
anyType(metadata)
singleType(type, metadata)
oneOfTypes(types, metadata)
enumType(values, metadata)
indefiniteArrayOfType(itemType, metadata)
indefiniteObjectOfType(propertyType, metadata)
tupleType(items, metadata)
objectType(properties, metadata)
castTypeSpec(value, metadata)
CORE_TYPES
typing(types)
isType(expectedType, value)
validateType(expectedType, value)
getType(value)
_validateType()
anyType(metadata)
Constant to be used to express that any type is allowed:
-
isType
always returns true -
validateType
never throws -
metadata
{Object}
singleType(type, metadata)
-
type
{String} -
metadata
{Object} - Returns: {SingleTypeSpec}
oneOfTypes(types, metadata)
-
types
{TypeSpec[]} -
metadata
{Object} - Returns: {OneOfTypesSpec}
enumType(values, metadata)
-
values
{*[]} -
metadata
{Object} - Returns: {EnumTypeSpec}
indefiniteArrayOfType(itemType, metadata)
-
itemType
{TypeSpec} -
metadata
{Object} - Returns: {IndefiniteArrayOfTypeSpec}
indefiniteObjectOfType(propertyType, metadata)
-
propertyType
{TypeSpec} -
metadata
{Object} - Returns: {IndefiniteObjectOfTypeSpec}
tupleType(items, metadata)
-
items
{TypeSpec[]} -
metadata
{Object} - Returns: {TupleTypeSpec}
objectType(properties, metadata)
-
properties
{Object} -
metadata
{Object} - Returns: {ObjectTypeSpec}
castTypeSpec(value, metadata)
Attempts to cast a value into a TypeSpec object:
-
if an
ObjectTypeSpec
, simply return the value itself -
if a
string
, converts it into aSingleTypeSpec
-
if an
array
, converts it into anOneOfTypesSpec
-
if a
plain object
, converts it into anObjectTypeSpec
-
otherwise returns
null
upon casting failure -
value
{*} -
metadata
{Object} -
Returns: {TypeSpec | null}
CORE_TYPES
typing(types)
-
types
{TypeAlternatives | TypeMap} - Returns: {{ isType, validateType, getType }}
isType(expectedType, value)
-
expectedType
{TypeSpec} -
value
{*} - Returns: {boolean}
validateType(expectedType, value)
If typing is invalid, throws TypeError. Returns nothing (undefined) otherwise.
-
expectedType
{string[] | string} -
value
{*} - Returns: {undefined}
getType(value)
-
value
{*} - Returns:
type
{string} Possible values for default configuration:- string
- regexp
- number
- bigint
- nan
- null
- undefined
- boolean
- function
- object
- array
- date
- symbol
- map
- set
- weakmap
- weakset