type-info
v1.0.3
Published
the run-time type infomation of object
Downloads
17
Maintainers
Readme
type-info
This run-time type information can be streamable via using toObject() method to generate the parametric type object, or using JSON.stringify directly. It can also be used to validate the value of the type.
Creating the new type is very simple and easy through this framework. Just we need to understand the basic concepts of the following.
Concepts
- Type Factory: collects all type info classes and objects.
- Primitive Types
- All registered types are primitive types.
- Virtual Types
- It's a object of a primitive type.
- It can not be registered.
- It could be unlimited number of virtual types.
- Type Attributes: first determine(define) these attributes of the type, before creating a new type.
It's used to constrain the Type.
All types have the
name
andrequired
attributes.name
(string): the type name.- required = true: it must be required.
- enumerable = false: it can not be enumerable.
required
(boolean): the attribute whether is required(must be exists, not optional).
- Value: the value with corresponding to the type information.
Quick starts
npm install type-info
var TypeFactory = require('type-info')
get the type object
var TNumber = TypeFactory('Number')
create the virtual type
var TPositiveNumber = TypeFactory('Number', {min:0, cached: 'PositiveNumber'})
validate a value
assert.notOk(TPositiveNumber.isValid(-1)) assert.ok(TPositiveNumber.isValid(1))
create the value
var n = TPositiveNumber.create(123) assert.ok(n.isValid()) assert.equal(Number(n) + 3, 126) var bool = TypeFactory('Boolean').create(true) assert.equal(Number(bool), 1)
Known Types:
- String Type
min
: the minimum string lengthmax
: the maximum string length
- Number Type
min
: the minimum numbermax
: the maximum number- Integer Type
- Float Type
- Date Type
min
: the minimum date to limitmax
: the maximum date to limit
- Boolean Type
- It is a special number type. you can cast to number.
- 0 means false, 1 means true.
boolNames
: the boolean value string names.- defaults to {true:['true', 'yes'], false: ['false,'no']}
- enumerable: false
- Function Type
globalScope
: the set of variables this function can access to.global
: the set of local functions this function can access to. It can not be exported.
- [Array Type][array-type]
min
: the minimum array length to limitmax
: the maximum array length to limitof
: the each elment's type of the array to limit.
- Object Type
attributes
: the object attribute list.- It's used to constrain the value of the object.
- Class Type: a special object type. the value of class is the constructor of this class.
- TODO: not fined.
Cache the Virtual Types
Install cache-factory first.
npm install cache-factory
TypeFactory = require 'type-info'
cacheable = require 'cache-factory'
# apply the cache-able ability to TypeFactory
cacheable TypeFactory
# now cache the virtual types with name.
passwordType = TypeFactory 'String', min:6, cached: {name: 'Password'}
# or no named it:
passwordType1 = TypeFactory 'String', min:6, cached: true
p2 = TypeFactory 'String', min:6, cached: true
p3 = TypeFactory 'String', min:6, cached: {name: 'Password'}
assert p2 is passwordType1
assert p3 is passwordType
assert passwordType isnt passwordType1
more detail see cache-factory
Changes
v1.0.0
- use the abstract-type package.
- The pacakge just collects types only.
v0.8.0
- [Type] $attributes to hold the type's meta attributes.
- the type's meta attributes definition in the src/attributes directory.
- [ObjectType] add AttributeType to defineAttribute
- [ObjectType] use the primitive type of attribute if possible.
- [Type] isSame method to compare parametric object
- remove the parent attribute
- remove the encoding from type-info
- remove
Type::encode
method - remove
Type::decode
method
- remove
- [Type]
Type(aTypeName, aOptions)
:- get the global instance if no aOptions or aOptions is same as the original default value of attributes.
- create a new type object instance else
v0.7.0
- add JSON.stringify(aTypeObject) supports
- add Type.createFrom(string, encoding) static(class) method
- remove Type::
_encode
method - remove Type::
_decode
method - remove Type::
_isEncoded
method
- add Value::
_encode
, Value::_decode
optional methods- make sure the value can be converted to json correctly.
- add Value::fromJson(string)
- add Value::createFromJson(string)
- add Value::toString(aOptions)
- add Value.tryGetTypeName(aValue)
- Type::mergeOptions(options, exclude, serialized) distinguish serialized and non-serialized parameters
TODO
Usage
See abstract-type.
TypeFactory = require 'type-info'
Value = TypeFactory.Value
# get number type info object
# you can treat it as a global temporary type object.
num = TypeFactory 'Number'
assert.equal num, TypeFactory('Number')
# get a new number type info object(Virtual Type):
# create a virtual type object always if the options exists:
number = TypeFactory 'Number', min:1, max:6
assert.notEqual number, num
# get Number Type Class(Primitive Type):
NumberType = TypeFactory.registeredClass 'Number'
# create a number value:
n = Value(2) # try to guess the value type.
# n = Value(2, number)
# n = number.create(2)
# n = number.createValue(2)
assert.ok n.isValid()
assert.equal n+2, 4
assert.throw number.validate.bind(number, 13)
n.assign 5 # n = 5
assert.equal n+2, 7
# assign a new type options to the number type:
number.assign min:3, max:10
# or number.min = 3, number.max = 10
API
See abstract-type.
License
MIT