Utils

Exports: Object

A collection of helper functions

6  

export the functions

8  
9  module.exports =
10  
11  

randomString

utils.randomString( string_length, speciallevel )

Generate a random string

Params
string_length Number string length to generate
speciallevel Number Level of complexity. 0 = only letters upper and lowercase, 52 possible chars; 1 = 0 + Numbers, 62 possible chars; 2 = 1 + "_-@:.", 67 possible chars; 3 = 2 + may speacial chars, 135 possible chars;
Returns
String The gerated string
25  
26    randomString: ( string_length = 5, specialLevel = 0 ) ->
27      chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
28      chars += "0123456789" if specialLevel >= 1
29      chars += "_-@:." if specialLevel >= 2
30      chars += "!\"§$%&/()=?*'_:;,.-#+¬”#£fi^\\˜·¯˙˚«∑€®†Ω¨⁄øπ•‘æœ@∆ºª©ƒ∂‚å–…∞µ~∫√ç≈¥" if specialLevel >= 3
31  
32      randomstring = ""
33      i = 0
34      
35      while i < string_length
36        rnum = Math.floor(Math.random() * chars.length)
37        randomstring += chars.substring(rnum, rnum + 1)
38        i++
39      randomstring
40  
41  
42  

randRange

utils.randRange( lowVal, highVal )

Create a random number bewtween two values

Params
lowVal Number Min number
highVal Number Max number
Returns
Number A random number
52  
53    randRange: ( lowVal, highVal )->
54      return Math.floor( Math.random()*(highVal-lowVal+1 ))+lowVal
55  
56  
57  

randRange

utils.randRange( lowVal, highVal )

Create a random number bewtween two values

Params
value Number String The value to pad
[padding=2] Number The padding size
[fill="0"] String The filler value.
Returns
String the padded value
68  
69    lpad: (value, padding = 2, fill = "0") ->
70      fill += fill for i in [1..padding]
71      return (fill + value).slice(padding * -1)
72