README
abstract-type
The abstract-type library includes the abstract Type
class and Value
class for streamable type info and validating value.
Concepts
- Primitive Types
- All registered types are primitive types.
- It's the singleton type object on the global type factory.
- Virtual Types
- It's an object of a primitive type.
- It can not be registered to the global type factory.
- It could be unlimited number of virtual types.
- It could use the LRU-cache factory to cache these virtual types(see usage).
- Type Attributes: the attributes of the type. It's used to constrain the Type.
All types have the
name
andrequired
attributes.name
(string): the type name.- required = true: the type name must be required.
- enumerable = false: the type name can not be enumerable.
required
(boolean): the attribute whether is required(must be exists, not optional). default to false.
- Value: the value with corresponding to the type information.
Usage
Create the number type
The type has a name and can verify whether a value belongs to that type. We can draw the two concepts related to the type, from here:
Attributes: the attributes(meta data) of this type.
Value: the value of this type.
The Type Class
- Properties:
- $attributes (object): the attributes of this type.
- Methods(should be overridden):
_initialize(aOptions)
: initialize the type object._assign(options)
: assign an options of type to itself._validate(aValue, aOptions)
: validate a value whether is valid.valueToString(aValue)
: (optional) convert the value to string, it's used to convert to json.toValue(aString)
: (optional) convert the string to the value, it's used to convert from json and assign from value.ValueType
property: (optional) defaults toValue
Class. unless implement your own Value class.
- Properties:
The Value Class
- Properties:
value
: store the value here.$type
(Type): point to the type of this value.
- Static/Class Methods:
tryGetTypeName(Value)
: try to guess the type name of the value.constructor(value[, type[, options]])
: create a value instance.value
: the assigned value. it will guess the type of the value if no type provided.type
(Type): the type of the valueoptions
(object): the optional type of value options. it will create a new type if exists.
- Methods:
clone()
: clone this value object.assign(value, options)
: assign the value.aOptions
(object):checkValidity
(boolean): defaults to true.
fromJson(json)
: assign a value from json string.createFromJson(json)
: create a new value object from json string.isValid()
: whether the value is valid.toObject(aOptions)
: return a parametric object of the value. it wont include type info. unless set thewithType
is true.- aOptions (object):
withType
(boolean): whether includes the type info. default to false
- aOptions (object):
- These methods could be overridden:
_toObject(aOptions)
: return the parametric object of this value.valueOf()
: return the value._assign(value)
: assign the value to itself.
- Properties:
The Attributes class: describe the attributes of a type. an attribute could include these properties:
name
(string): the attribute name. you can specify a non-english name.- the english name(the attributes' key) is used in the internal of the type.
- the
name
only used on export(toObject
) or import(assign
).
type
(string): the attribute type.enumerable
(boolean): the attribute whether is a hidden attribute, defaults to true.- the hidden attribute can not export to the parametric object(serialized).
- note: It's a hidden attribute too if attribute name begins with '