This pack contains various tools to work with JavaScript objects. It is created to help you avoid monotonous work such as extract data fields from JS object, transform values(trim, substring, replace, and etc..) and validate object schema fields. The idea to create these tools came after long work with MeteorJS & MongoDB.
How to install it?
npm install isschematools
This pack of the three following modules:
- Core Module
- Methods:
- Transform Module
- Methods:
- transform
- register
- build
- Properties:
- transformers
- trim / trimRight / trimLeft
- substring / replace
- toUpper / toLower
- nullIfEmpty / default
- toType
- transformers
- Methods:
- Validation Module
- Methods:
- validate
- register
- messages
- Existing validators
- required
- type
- url
- digits
- maxLength
- minLength
- eqlLength
- range
- equalTo
- belongsTo
- regex
- Methods:
1. Core Module
matchTraverse
&traverse
functions are based on some variation of BFS algorithm, and they do not use recursion
This module has the following methods:
Traverse Function
- var t = ; t;
The first param should be an object. The second param should be a callback function which will be invoked on each node of object
Example:
/* * var t = ISSchemaTools; //in browser */ var t = ; var someObj = prop1: 'Some data' prop2: 'Some other data' prop3: prop1: insideProp1Prop3: 'Data' ; t; // RESULT: // 'Some data' // 'Some other data' // {prop1: ...} // [{insideProp1Prop3: ...}] // {insideProp1Prop3: ...} // 'Data'
Where:
- value - value of processing node
- key - property name of processing node
- type - type of processing node
- 'node' - type of
Object
orArray
- 'leaf' - any type except
Object
orArray
- 'node' - type of
- parentNode - reference on parent node (Object type). Has following props:
- key - node key (property name)
- value - node value (property value)
- level - level of object (root level = 1)
- path - array which consists of property keys from root object to current node
//for objectvar object =node1 :node2: 'value';//path for value in node2 will be ['node1', 'node2'];- parent - reference on parent node
- isCircular - shows if value of this node is circular reference
MatchTraverse Function
- /* * var t = ISSchemaTools; //in browser */ var t = ; t;
The first param is an object for
traverse
. The second param should be a pattern that describes expected structure of an object.matchTraverse
allows to traverse an object using special pattern.
Pattern should describe desired structure of object. Each end node of pattern should be a Vertex or Rule. Rule can contain additional info about end node.
/* * var t = ISSchemaTools; //in browser */ var t = ; var pattern = name: t surname: t nested: nested: value: t ;
Example:
/* * var t = ISSchemaTools; //in browser */ var t = ; var model = name: 'Sam' surname: 'Wartington' contact: isPrimary: true city: null phones: '+000 11 000 22 22' address: street: 'Calouss' building: 9 room: 10 ; var pattern = name: t surname: t contact: city: t phones: t address: street: t building: t room: t ; //returns array of nodes which are defined by pattern var nodeList = t; var cleanNodeList = nodeList; var result = cleanNodeList; /* result -> ['Sam', 'Wartington', '+000 11 000 22 22', 9, 10, 'Calouss'] */ /* node has following structure: * value: value of processed node, * key: property name of processed node, * level - level of object (root level = 1), * path - array which consists of property keys from root object to current node, * pattern - pattern describes current node (Vertex means empty pattern type) */ result = t; /* result -> { name: 'Sam', surname: 'Wartington', contact: { phones: ['+000 11 000 22 22'], address: [{ street: 'Calouss', building: 9, room: 10 }] } } */
Rule Function
-Pattern for the
matchTraverse
function can be defined by using rules. Rule is some kind of metadata which is describe a field, and can be used for your processing functions or other needs. You can declare rule by usingt.rule
function. You should pass int.rule
expected type of data, which is contained in node. Type of expected data is only one required param fort.rule
function.
t; //type must be a Ctor function t; //Short declaration
Example:
/* * var t = ISSchemaTools; //in browser */ var t = ; var model = name: 'John' surname: 'Doe' contact: city: 'Minsk' ; var pattern = name: t surname: t contact: city: t ; var nodes = t; nodes = nodes; var result = t; /* result => { contact: { city: 'Minsk' } } */
Besides
type
you can use following options(#reserved) inside at.rule
:
- omit (Boolean | Function:Boolean)
- name (String)
- label (String)
- type (Function)
- transform (Array)
- validation (Object)
- allowNull (Boolean)
var t = ISSchemaTools || ; var tr = ttransformtransformers; t
Build Function
-
t.build
function provides ability to gather result ofmatchTraveres
into new object. Lets try to find out how should we work with it:
var t = ; var listOfNodes = t; var result = t;
Where:
- listOfNodes - result of
t.matchTraverse
function - options
- clean - can be
true
orfalse
, if value istrue
then nodes which obey following rules will be excluded:- value is (NaN, Infinite, undefined, "" or null*)
- type of value is incorrect
- omit value for node is equal
true
, or omit function givestrue
- allowNull - global identifier which is used to save values with
null
in result object
- clean - can be
var t = ; //var t = ISSchemaTools; var listOfNodes = t; t; // => {name: 'slim', surname: 'shady', city: undefined, country: null} t; // => {name: 'slim', surname: 'shady'} t; // => {name: 'slim', surname: 'shady', country: null} //***********// listOfNodes = t; t; //=> {surname: null, password: '1234567'} listOfNodes = t; t; // => {surname: 'Dodood'}
Chain Function
-Creates a Chain
object that wraps value with explicit method chaining enabled.
Returns Chain wrapper object
.
t; // => `Chain object`
Example:
var model = filter: name: 'Jo%%%%' surname: '%%urndina%%' age: $lt: 18 order: name: 1 ; var pattern = filter: name: t surname: t age: t ; t; /* { filter: { name: 'Jo%%%%', surname: '%%urndina%%', age: {$lt: 18} } } */
DefineExtensison Function
- t;
Function defines module extension. Accepted params: 'module name', init function that returns (module) with methods.
Example:
var t = ; t; var model = name: 'SomeName' surname: 'SomeSurname' ; var pattern = name: t surname: t ; var result = t; /* result => { name: '@!!!@-SomeName-@!!!@', surname: 'SomeSurname' } */