node-js-utilities
v2.0.0
Published
Node.js module exposing some of the basic utils that helps in basic development
Downloads
3
Readme
node-utils
Node.js module exposing some of the basic utils that helps in basic development which I came across during development
Author: Gaurav Joshi
Start
const Utils = require('node-utils');
- node-utils
- Methods
beautifyDate(date, [format, seperator])
beautifyMoney(amount, [currency, raw])
beautifyNumber(number, [decPlaces])
containsWord(string, word)
getDaysDiff(date)
getPathFromUrl(url)
getQueryParamByName(param, url)
getQueryString(url)
getRandomNumber(min, max)
getRandomString(len, charStr)
isUrlValid(url)
startsWith(string, searchString, [position])
stripHtml(html, [all])
- Methods
Methods
beautifyDate(date, [format, seperator])
Generates a decorated date object where:
date
- date.format
- optional format. Available formats are yyyy, mm, yyyy-mm-dd, yyyy-mm-dd h:i, mm-dd-yyyy hh:ss .seperator
- optional Date sepeartor default "-".
Utils.beautifyDate('01/16/2016', 'YYYY');
Returns following result:
2017
beautifyNumber(number, [decPlaces])
Generates a decorated Number where:
number
- number.decPlaces
- optional format. Decimal place to round of number .
Utils.beautifyNumber(1050000, 2);
Returns following result:
1.05M
beautifyMoney(amount, [currency, raw])
Generates a decorated amount where:
amount
- amount.currency
- optional currency. Available currency format USD,EUR,CRC,GBP,ILS,INR,JPY,KRW,NGN,PHP,PLN,PYG,THB,UAH,VND. Default USDraw
- optional raw. {boolean}
Utils.beautifyMoney(1545, 'GBP')
Returns following result:
£1.55K
getDaysDiff(date)
Returns days diff from current date
date
- date.
Utils.getDaysDiff('01/01/2017')
Returns following result:
16
stripHtml(html, [all])
Strips HTML content from HTML string. (Returns a promise)
html
- HTML string.all
- Boolean If true strips all HTML content otherwise strips only script tag and its content
Utils.stripHtml('<script>alert(1);</script><p>HI GAURAV JOSHI</p><script>alert(1);</script>', true)
.then( str => {
console.log(str);
});
Returns following result:
HI GAURAV JOSHI
containsWord(string, word)
Checks word exists in the string and return true or false accordingly. (*NOTE: check existence of complete word not sub string)
string
- string - string in which to checkword
- word - word to check
Utils.containsWord('Hi I am a MEAN stack developer', 'MEAN');
AND
Utils.containsWord('Hi I am a MEAN stack developer', 'velop');
Returns following result:
true
AND
false
getRandomNumber(min, max)
Returns random number between passed range
min
- number - min range default 0max
- number - max range default 1
Utils.getRandomNumber(1, 5);
Returns number
isUrlValid(url)
Returns random number between passed range
url
- string - URL string
Utils.isUrlValid('https://☺.damowmow.com/');
Returns boolean
true
getPathFromUrl(url)
Returns path from url skipping query parameters
url
- string - URL string
Utils.getPathFromUrl('https://☺.damowmow.com?a=3');
Returns string
https://☺.damowmow.com
getQueryString(url)
Returns query string parameters as an javascript oblect
url
- string - URL string
Utils.getQueryString('https://☺.damowmow.com?a=3&b=4');
Returns object
{"a": 3, "b": 4}
getQueryParamByName(param, url)
Returns query string values
param
- string - Param nameurl
- string - URL string
Utils.getQueryParamByName('a', 'https://☺.damowmow.com?a=3&b=4');
Returns value or null
3
startsWith(string, searchString, [position])
Determines whether the beginning of searchString instance matches the specified string when compared using the position.
string
- string - The string from compare.searchString
- string - The string to compare.position
- number - position
Utils.startsWith('Hello MEAN developers', 'EAN', 7);
Returns boolean
true
getRandomString(len, charStr)
Returns random string of passed length
len
- number - length of random string default 4charStr
- string - charcters list to create random string default aplha numeric
Utils.getRandomString(4);
Returns string