wise-helper
v2.4.3
Published
usefull simple helper functions: print, random, randomInt
Downloads
10
Maintainers
Readme
Wise-helper
Usefull functions like getting a random number, printing, and more using simple syntax
- Math.floor(Math.random())-> random(MAX)
- console.log("string") -> print("string")
- random(255) -> Random number between 0-255
- randString(6) -> Random String w/ length 6
<doc version 1.2 last updated 2017-11-22>
install
npm install --save wise-helper
at top of your project file
require('wise-helper');
//ready to go!
Useage
inside of your 'app.js'
require('wise-helper');
var a,b,c,d,e,f,g,h;
a = random( 255 ); // int 0-255
b = random( 10 ); // int 0-10
c = randInt( 0, 100 ); // int 0-100
d = randInt( -100, 100 ); // int -100 - 100
//or print like this
print( random(255) );
print( random(10) );
//prints random characters
//randChar() : ascii 32("space") though 127 ("")
//randChar(Max) : ascii char 0 ("NULL") though Max
e = randChar(); // char a-z A-Z 0-9 ~!#$%^&**()_+_+,./;'[]\<>?:"{}|
f = randChar(99); // char 0-99 ascii characters
h = randChar(64,90); // char A-Z
console.log("a: ", a, ", b: ", b, ", c: ", c, ", d: ", d, ", e: ", e, ", f: ", f, ", g: ", g,", h: ", h);
print = console.log.bind( console );
random
Name : random()
--------------------------------------------
Desc : genrates radnom number
--------------------------------------------
Param : random( intager)
Return: random value between 0 and max
--------------------------------------------
EX :
var number = random(255);
print(number); // 0 - 255
default no params
var number = random();
print(number); // 0 - 100
--------------------------------------------
--------------------------------------------
function random( max ){
if( max ){
return randInt(0, max);
}else{
max = 100;
return randInt(0, 100);
}
}
randomInt
Name : randInt()
--------------------------------------------
Desc : generates random numbers
between to values min, max
--------------------------------------------
Param :
Return: randInt(integer,integer)
randInt(Min,Max)
--------------------------------------------
Long :returns a sumirandom number
between max and min
--------------------------------------------
--------------------------------------------
function randomInt( min, max ) {
return Math.floor( Math.random() * ( max - min + 1 ) ) + min;
}
randChar
Name : randChar()
--------------------------------------------
Param : randChar(Integer, Integer)
Return: Char random between min, max ascii
--------------------------------------------
--------------------------------------------
function randChar( min, max ) {
if( max && max >=0 ){
return String.fromCharCode( randInt( min, max ) );
}else if( min && min >=0 ){
return String.fromCharCode( randInt( 0, min ) );
}else{
return String.fromCharCode( randInt( 32, 127 ) );
}
}
randString
Name : randString()
--------------------------------------------
Desc : genartes random string of n length
--------------------------------------------
Param : randString(intager)
Return: string ( 0-n length) Chars 'A' to '-'
--------------------------------------------
Long : returns string made up of ascii values
32 - 127 (A to '-') respectivily
exampe output
93nx-./1a
--------------------------------------------
--------------------------------------------
function randString( n ) {
var string = '';
for( var i = 0; i < n; i++ ){
string += randChar();
}
return string;
}
padding
Name : padding()
--------------------------------------------
Desc : returns correct padding for printing
--------------------------------------------
Param : padding(String, Integer)
Return: String
--------------------------------------------
Long : returns correct padding when
printing out multi line outputs
--------------------------------------------
EX :
Pad strings START and END.
var start = "START";
var end = "END";
var out = start + padding( start, 13 ) + ": " + end;
print(out);
--------------------------------------------
--------------------------------------------
function padding(str, padding) {
if(str) {
var inputString = str.toString();
var spacer = '';
var len = padding - inputString.length;
if (len < 0) {
len = 0;
}
for (var i = 0; i < len; i++) {
spacer += ' ';
}
return spacer;
}else{
console.error("Error : padding()'s first argument is null or undefined.");
console.error("FIX : correct uses padding(String, Number).");
}
}
arrToString
Name : arrToString()
--------------------------------------------
Desc : concatnate array to string
--------------------------------------------
Param : arrToString(Array)
Return: String of A[i] + A[i+1]....A[i+n]
--------------------------------------------
Long :
EX :
arrToString(['Alis','#*%','KYLE','1'])
//Alis#*%KYLE1"
arrToString([4,6888,-1,101,2,0])
//'"46888-110120"'
arrToString(['Alis','#*%','KYLE','1'], ' ');
//"Alis #*% KYLE 1"
arrToString([4,6888,-1,101,2,0], ' ')
//"4 6888 -1 101 2 0"
arrToString([4,6888,-1,101,2,0], ', ')
//"4, 6888, -1, 101, 2, 0"
--------------------------------------------
--------------------------------------------
function arrToString( arr, paddingChar){
if(! paddingChar ) paddingChar = '';
var joined = '';
for( var i in arr ){
if( parseInt( i ) === arr.length - 1 ) {
joined += arr[ i ];
}else{
joined += arr[ i ] + paddingChar;
}
}
return joined;
}