4.1.1 • Published 1 year ago

runtime-type v4.1.1

Weekly downloads
104
License
MIT
Repository
github
Last release
1 year ago

runtime-type

Module for casting to types at runtime.

Installation

npm install --save runtime-type

Usage

import Type from 'runtime-type';

Type.integer.cast('5'); // returns 5
Type.integer.cast('test'); // throws an Error

Type.integer.canCast('5'); // returns true
Type.integer.isValidType('5'); // returns false
Type.integer.isValid('5'); // returns false

Functions

Every type has several methods.

cast(value)

Casts the value to the type. It returns value in the desired type or throws an Error.

saveCast(value, defaultValue)

Tries to cast the value. If the cast fails the defaultValue is returned.

canCast(value)

Checks if the value is valid. It calls the cast method and if the cast is successful true is returned. If the cast throw an error false is returned.

isValidType(value)

Chekcs the value using typeof defined in the inherited type.

isValid(value)

Checks if the value is strictly valid. Calls canCast(value) and isValidType(value).

Types

TypeDescriptiontypeofJS cast
anyIt's just a placeholder. The value is returned in cast method and canCast is always true.
arrayOf(type: Type)The values of array in the cast method are casted by the type.object
booleanThe value is validated with the base javascript Boolean function in cast method. If the value is 'false' or '0' it's casted as false.booleanBoolean(value)
dateThe value is used as parameter in the Date constructor in cast method.objectnew Date(value)
enum(defaultValue: string, ...values: string[])The cast method checks if the value is in the values array.string
floatThe value is parsed by parseFloat function in cast method.numberparseFloat(value)
instanceOf(cls: Class)Equivivalent of the instanceof.value instanceOf Class
integerThe value is parsed by parseInt function in cast method. isValidType method checks if the value is not decimal.numberparseInt(value)
objectThe value is validated with typeof value === 'object'.objecttypeof value === 'object'
shapeThe value's keys are validated with the specified types.object
string(shape: Object.<string, Type>)The value is converted to string with toString method.stringvalue.toString()

enum_

The enum_ function is alias to enum because of typescript.

Migrating to v3

The isValid method now validates the value strictly as mentioned above. The easiest way to migrate is just to rename all references to canCast. Everything else remains the same.

// v2
Type.integer.isValid('5'); // returns true
// v3
Type.integer.isValid('5'); // returns false
Type.integer.canCast('5'); // returns true
4.1.0

1 year ago

4.1.1

1 year ago

4.0.4

2 years ago

4.0.3

3 years ago

4.0.2

3 years ago

4.0.1

3 years ago

4.0.0

3 years ago

3.8.3

3 years ago

3.8.2

4 years ago

3.8.1

4 years ago

3.8.0

4 years ago

3.7.3

4 years ago

3.7.2

4 years ago

3.7.1

4 years ago

3.7.0

4 years ago

3.6.1

4 years ago

3.6.0

4 years ago

3.5.4

4 years ago

3.5.3

4 years ago

3.5.2

4 years ago

3.5.1

4 years ago

3.5.0

4 years ago

3.4.0

4 years ago

3.3.0

4 years ago

3.2.0

4 years ago

3.1.0

5 years ago

3.0.0

5 years ago

2.0.0

5 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago