swint-helper
Helper functions and classes for Swint
Installation
$ npm install --save swint-helper
API
.defaultize(defVal, tgtVal)
Filling default value to an JavaScript object.
If tgtVal
doesn't have the key -> It is filled from defVal
, recursively.
If tgtVal
doesn't have enough values at the Array -> It is filed from defVal
, recursively.
Examples
var def tgt; def = a: 1 b: 'aaa' c: 1 2 3; tgt = b: 'bbb' c: 4; ;// tgt ==> { a: 1, b: 'bbb', c: [4, 2, 3] }
.validate(ruleVal, tgtVal)
Recursively looks through an object to see if its values match the rules
Rules
undefined
: Can be any type0
: Has to be a Number''
: Has to be a String'aaa\bbbb\bccc'
: Has to be enum('aaa', 'bbb', 'ccc')new Date(0)
: Has to be a Date{}
: Has to be an object[0]
: Has to be an Array of Numbers['']
: Has to be an Array of Strings[new Date(0)]
: Has to be an Array of Dates[[0]]
: Has to be an Array of Arrays of Numbers{ a: 0 }
: Has to be an Object with key of a: Number
Examples
var r = a: 0 b: '' c: d: 0 e: undefined t = a: 100 b: 'Monday' c: d: 123 e: 'Friday' ; var o = ;// o ==> [ true, [] ]
If the target didn't match the target, it will return false and a path to the failing entry:
var r = a: 0 b: '' c: d: 0 e: undefined t = a: 100 b: 'Monday' c: d: 'a' 'b' 'c' e: 'Friday' ; var o = ;// o ==> [ false, [ 'c', 'd', 0 ] ]
.print([level], msg1, [msg2, ...])
Prints messages and associated levels
Levels
- 0(
print.RAW
) : RAW - 1(
print.DEBUG
) : DEBUG(default) - 2(
print.INFO
) : INFO - 3(
print.WARN
) : WARNING - 4(
print.ERROR
) : ERROR
Examples
;;// ==> Raw message;;// ==> DEBUG/2015-05-07T13:55:35.612Z)// Debug message;;// ==> INFO /2015-05-07T13:55:35.612Z)// Info message;;// ==> WARN /2015-05-07T13:55:35.612Z)// Warning message;;// ==> ERROR/2015-05-07T13:55:35.612Z)// Error message // Setting the printLevel sets a minimum requirement for the levelsglobalswintVarprintLevel = 3; ; // Would not print; // Would not print; // Would not print; // Would print; // Would print
.walk(options)
Walks directory and returns the list of file. The dir should be a full path.
Options
The whitelist precedes blacklist.
dir
:String
, default:path.dirname(require.main.filename)
ext
:String
, default:'*'
whitelist
:function(fullPath)
, default: passblacklist
:function(fullPath)
, default: extension, starting with_
baseOrderRule
:Boolean
, default:true
, whether using baseOrderRuleorderRule
:function(a, b)
, default: ...head
:String
, default:'Intro'
tail
:String
, default:'Outro'
Example
var list = ;
.concat(fileList)
Concatenating file contents on the fileList and returns string
Example
var str = ;
.createHash(options)
Creates key-secret SHA-256 hash pair based on the salt. This pair can be verified the check-hash
middleware from swint-middleware
.
Options
key
:Number
, default: 15secret
:Number
, default: 25salt
:String
, default:SwintIsForTwins
Example
var hashPair = ; // hashPair.key, hashPair.secret is generated
.traverseWithQuery(struct, query)
Traverses through a structure by following a list of keys/indexs.
Example
var s = a: 1 2 b: 'ccc' s2 = a: b: 'ccc2' ; var o = o2 = ; // o ==> 'ccc' // o2 ==> 'ccc2'