Quantizer
Installation
npm install quantizer --save
Basic Usage
Quantizer is library wich helps you store and validate your immutable data structures, Quantizer represents data as simple unordered tree.
Map:
; const user = State;
List:
; const users = State;
API
Node:
Under the hood core element of Quantizer graph is Node, and as agreement other elements wich build blocks of the data structure is also Node instances.
Mehods:
set(value)
- set value of nodeget()
- return value of node
; const node = State; node;node; 'foo'
Map:
Key-value storage. { id: 1, name: 'John' }
Mehods:
setAttribute(key, value)
- set value of the attribute.getAttribute(key)
- return value of attribute.set(value)
- clear structure and then save the serialized data.get(...keys)
- returns deserialized value of the node, there is few avaliable ways to get value, first way is callget()
wich return all the node attributes, other way is to dirrectly enumerate wich attributes will be deserializedget('id', 'name')
.merge(value)
- merge value to the node, like Object.assign for plain objects.find(key)
- returns serialized attribute orundefined
if value is not founnd.map(handler)
- iterator across the node, handler takes two arguments wichattribute
,keyName
, returns a list of values returned from handler.clear()
- clear current node.clone()
- clone current node.toJSON()
- convert node to JSON string.fromJSON(str)
- convert JSON string to node.
List:
Index-value storage. [1, 2, 3]
Mehods:
set(value)
- clear structure and then save the serialized data.get(args)
- returns deserialized data,args
will be passed toget
method of the child nodes.push(value)
- push value to current node.concat(list)
- concat list to current node.remove(node)
- remode child node.at(index)
- returns a child element by index, also avaliableat('first')
andat('last')
, returns first and last child from the list respectively.where(query)
- returns child nodes that have a matches with the query, there is a few ways using it method,where('id', 1)
returns node wich haveid === 1
, other way is tu pass couple params to find matcheswhere({ gender: 'male', age: 10 })
map(handler)
- iterator across the node, handler takes two arguments wichattribute
,index
, returns a list of values returned from handler.filter(handler)
- iterator across the node with calling filter function and return list of filtering results, if filter function returnstrue
element will be passed to result list otherwise it will be skipped.sortBy(key)
- sorts list by element valur or value of his key specified in argument.
Validation
For validation Quantizer provide two instruments wich is Type and Schema, this instruments help to describe and validate data structures in a declarative way.
Type:
Is a core feature wich validate input data, Type should know about couple things:
name
- name of the type validator, will be used for formating errors.validator
- validator function takes a value to validate and return boolean verdict.instance
- instance of the Quantizer node wich will be used for serialization.nested
- nested type of the node, currently supported by List.required
- is value need to be required or no.
Type API is just two simple methods:
validate(value)
- returns errors report.parse(value)
- serialize input value to the instance.
Example of the type:
; const type = ; type; 'Number' 'String' type; 1
Build in types:
- Any
- Boolean
- String
- Number
- List
- Map
- UUID
- ObjectID
Schema:
Actually, Schema is more variation of Type, it helps to describe complex structures
Schema API is just two simple methods like Type does:
validate(value)
- returns errors report.parse(value)
- serialize input value to the instance.
; const user = 'User' id: TypeNumber name: TypeString; type; 'Map' 'List' type; name: "User" count: 2 map: id: 'Number' 'String' name: 'String' 'Number' type; id: 1 name: 'John'
A few variations of using Schema:
; const userSchema = 'User' id: TypeNumber name: TypeString; // just type validationconst listWithTypeValidation = 'UserList' messages: TypeNumber; // validate fields according Schemaconst listWithSchemaValidation = 'UserList' messages: userSchema; // list like instance factoryMap static schema = userSchema; { superdata UserModelschema } const listWithIstanceFactory = 'UserList' messages: UserModel; // list type like instance factoryList { supervalue UserModel; } const userListType = name: 'UsersList' instance: DocumentFieldList validate: islist; const listTypeWithIstanceFactory = 'UserList' messages: userListType;