UNPKG

@tasolutions/express-core

Version:
145 lines (119 loc) 4.88 kB
const md5 = require('md5'); const moment = require('moment'); const config = require('../config'); const statusEnum = require('./enum.util').statusEnum; const limitExpiredDocs = require('./enum.util').limitExpiredDocs; module.exports = { isEmail: function (email) { const emailRegex = /^[a-zA-Z0-9._-]+@([A-Za-z0-9_\-.])+\.([A-Za-z]{2,4})*$/; return emailRegex.test(email); }, generateOtpToken: function () { return md5(Math.random().toString(36).substring(2, 30)); }, generateOtp: function (n) { var add = 1, max = 12 - add; // 12 is the min safe number Math.random() can generate without it starting to pad the end with zeros. if (n > max) { return generate(max) + generate(n - max); } max = Math.pow(10, n + add); var min = max / 10; // Math.pow(10, n) basically var number = Math.floor(Math.random() * (max - min + 1)) + min; return ("" + number).substring(add); }, generateOtpCode: function () { return Math.floor(1000 + (9999 - 1000) * Math.random()); // '1234';// Math.random().toString(36).substring(2, 30); }, encodeBase64: function (string) { return Buffer.from(string).toString('base64'); }, decodeBase64: function (string) { return Buffer.from(string, 'base64').toString('ascii'); }, getNumberInString: function (string) { const numberRegex = /[+-]?\d+(\.\d+)?/g; return string.match(numberRegex); }, randomImageName: function () { return Math.random().toString(36).substring(2, 30) + Math.floor(1000 + (9999 - 1000) * Math.random()); }, isSpaceInString: function (s) { return /\s/g.test(s); }, passwordRandom: function () { return Math.random().toString(36).slice(2); }, getTimeBetween2Days: function (date1, date2, unit = 'h') { if (unit === 's') return Math.abs(date1 - date2) / 1000; return Math.abs(date1 - date2) / 36e5; }, getFirstPart: function (str) { return str.split('_')[0]; }, getDateFormat: function () { const date = new Date(); return date.getFullYear() + '/' + date.getMonth() + '/' + date.getDate() + '_' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds(); }, getAge: function (d) { const diff = new Date().getTime() - d.getTime(); return Math.floor(diff / (1000 * 60 * 60 * 24 * 365.25)); }, isValidDate: function (value) { const date = new Date(value); return !isNaN(date.getDate()) && value !== null && moment(date, 'YYYY-MM-DD', true).isValid(); }, getDiffDays: function (d1, d2) { const start = moment(d1); const end = moment(d2); return end.diff(start, 'days'); }, isSpecialCharacter: function (string) { const format = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/; return !!format.test(string); }, generateQRCode: function (name, uuid) { return `https://${config.env}.${config.domain}/redirect?param=SITESMART_${name.toUpperCase()}_${uuid}`; }, getStatusByExpiryDate: function (expiry_date, limit_expiring_soon = null) { const limit = limit_expiring_soon || limitExpiredDocs.ByDate; const date = moment(expiry_date).diff(moment(), 'days'); return date < 0 ? statusEnum.EXPIRED : date <= limit ? statusEnum.EXPIRING_SOON : statusEnum.CURRENT; }, hexadecimalToDecimal: function (hexStr, start = null, end = null) { hexStr = hexStr.toString(); if (start && end) hexStr = hexStr.slice(start, end); return parseInt(hexStr.toString(), 16); }, hexadecimalToLatLng: function (hexStr, start = null, end = null) { let dec = hexadecimalToDecimal(hexStr, start, end); return (dec < parseInt('7FFFFFFF', 16)) ? dec * 0.0000001 : 0 - ((parseInt('FFFFFFFF', 16) - dec) * 0.0000001); }, formatPhoneNumber: (phone) => { if (phone.startsWith('840') && phone.length == 12) { phone = phone.replace(phone.substr(0, 2), ''); return phone; } if (phone.startsWith('84') && phone.length == 11) { phone = phone.replace(phone.substr(0, 2), ''); return '0' + phone; } else if (phone.startsWith('+840') && phone.length == 13) { phone = phone.replace(phone.substr(0, 3), ''); return phone; } else if (phone.startsWith('+84') && phone.length == 12) { phone = phone.replace(phone.substr(0, 3), ''); return '0' + phone; } else if (!phone.startsWith('0') && !phone.startsWith('+') && phone.length == 9) { return '0' + phone; } return phone; } }