Skip to content

Instantly share code, notes, and snippets.

@puncoz
Last active April 5, 2019 08:26
Show Gist options
  • Save puncoz/3c41be0c5e21215ae319acbdedf00842 to your computer and use it in GitHub Desktop.
Save puncoz/3c41be0c5e21215ae319acbdedf00842 to your computer and use it in GitHub Desktop.
Some useful javascript helper functions.
export const kebabToPascalCase = str.replace(/(?:^\w|[A-Z]|\b\w)/g, (letter, index) => letter.toUpperCase()).replace(/[^\w]+/g, "");
// eg: some-string => SomeString
export const toNepaliNumber = n => (n + '').replace(/\d/g, i => '०१२३४५६७८९'[i])
export const numberToWord = amount => {
const a = ["", "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine ", "Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen"];
const b = ["", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"];
amount = (amount.toFixed(2)).toString().split(".");
let number = amount[0].split(",").join("");
let precision = amount.length > 1 ? amount[1].split(",").join("") : "00";
if ((number = number.toString()).length > 9) return "overflow";
let n = ("000000000" + number).substr(-9).match(/^(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})$/);
let p = ("00" + precision).substr(-2).match(/^(\d{2})$/);
if (!n) {
return;
}
let word = "";
word += (Number(n[1]) !== 0) ? (a[Number(n[1])] || b[n[1][0]] + " " + a[n[1][1]]) + "Crore " : "";
word += (Number(n[2]) !== 0) ? (a[Number(n[2])] || b[n[2][0]] + " " + a[n[2][1]]) + "Lakh " : "";
word += (Number(n[3]) !== 0) ? (a[Number(n[3])] || b[n[3][0]] + " " + a[n[3][1]]) + "Thousand " : "";
word += (Number(n[4]) !== 0) ? (a[Number(n[4])] || b[n[4][0]] + " " + a[n[4][1]]) + "Hundred " : "";
word += (Number(n[5]) !== 0) ? ((word !== "") ? "and " : "") + (a[Number(n[5])] || b[n[5][0]] + " " + a[n[5][1]]) + "Ruppees, " : "";
word += (Number(p[1]) !== 0) ? (a[Number(p[1])] || b[p[1][0]] + " " + a[p[1][1]]) + "Paisa only " : "";
return word;
};
export const Cookies = function () {
return {
set: (key, value, exp) => {
const date = new Date();
date.setTime(date.getTime() + (exp * 24 * 60 * 60 * 1000));
const expires = "expires=" + date.toUTCString();
document.cookie = key + "=" + value + ";" + expires + ";path=/";
},
get: (key) => {
const name = key + "=";
const decodedCookie = decodeURIComponent(document.cookie);
const ca = decodedCookie.split(';');
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1);
}
if (c.indexOf(name) === 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
}
}();
const getViewPort = () => {
let e = window,
a = 'inner';
if (!('innerWidth' in window)) {
a = 'client';
e = document.documentElement || document.body;
}
return {
width: e[a + 'Width'],
height: e[a + 'Height']
};
}
export const encode = str => encodeURIComponent(btoa(str))
export const decode = str => atob(decodeURIComponent(str))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment