somewhere
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
/// ///////////////////////////////////////////////////////////////////////////////////////// expresjs methods
|
||||
module.exports.res404 = (res, err) => {
|
||||
return res.status(404).json({ message: err })
|
||||
}
|
||||
|
||||
module.exports.res406 = (res, err) => {
|
||||
return res.status(406).json({ message: err })
|
||||
}
|
||||
|
||||
module.exports.res500 = (res, err) => {
|
||||
return res.status(500).json({ message: err })
|
||||
}
|
||||
|
||||
module.exports.checkValidations = validationResult => {
|
||||
return (req, res, next) => {
|
||||
const errors = validationResult(req)
|
||||
if (!errors.isEmpty()) return res.status(422).json({ validation: errors.mapped() })
|
||||
else next()
|
||||
}
|
||||
}
|
||||
|
||||
/// ///////////////////////////////////////////////////////////////////////////////////////// sting methods
|
||||
module.exports.removeWhiteSpaces = str => {
|
||||
return str.replace(/ /g, '')
|
||||
}
|
||||
|
||||
module.exports.isPersian = str => {
|
||||
return str.match(/^[\u0600-\u06FF\s]+$/)
|
||||
}
|
||||
|
||||
module.exports.isPersianWithDigits = str => {
|
||||
return str.match(/^[\u0600-\u06FF\s\d]+$/)
|
||||
}
|
||||
|
||||
module.exports.isLatin = str => {
|
||||
return str.match(/^[A-Za-z\s]+$/g)
|
||||
}
|
||||
|
||||
module.exports.isLatinWithDigits = str => {
|
||||
return str.match(/^[a-zA-Z\s0-9()*_\-!#$%^&*,."'\][]*$/g)
|
||||
}
|
||||
|
||||
module.exports.hasWhiteSpaces = str => {
|
||||
return str.includes(' ')
|
||||
}
|
||||
|
||||
/// ///////////////////////////////////////////////////////////////////////////////////////// number methods
|
||||
module.exports.generateRandomDigits = digitsLength => {
|
||||
function generateRandomDigits(DIGITS_LENGTH) {
|
||||
const add = 1
|
||||
let max = 12 - add // 12 is the min safe number Math.random() can generate without it starting to pad the end with zeros.
|
||||
if (DIGITS_LENGTH > max) {
|
||||
return generateRandomDigits(max) + generateRandomDigits(DIGITS_LENGTH - max)
|
||||
}
|
||||
max = Math.pow(10, DIGITS_LENGTH + add)
|
||||
const min = max / 10 // Math.pow(10, n) basically
|
||||
const number = Math.floor(Math.random() * (max - min + 1)) + min
|
||||
|
||||
return ('' + number).substring(add)
|
||||
}
|
||||
return generateRandomDigits(digitsLength)
|
||||
}
|
||||
|
||||
module.exports.generateRandomDigitsInRangeOf = max => {
|
||||
return Math.floor(Math.random() * max) + 1
|
||||
}
|
||||
|
||||
/// ///////////////////////////////////////////////////////////////////////////////////////// iran region methods
|
||||
module.exports.checkNationalCode = code => {
|
||||
/* eslint-disable eqeqeq */
|
||||
const L = code.length
|
||||
if (L < 8 || parseInt(code, 10) == 0) return false
|
||||
code = ('0000' + code).substr(L + 4 - 10)
|
||||
if (parseInt(code.substr(3, 6), 10) == 0) return false
|
||||
const c = parseInt(code.substr(9, 1), 10)
|
||||
let s = 0
|
||||
for (let i = 0; i < 9; i++) {
|
||||
s += parseInt(code.substr(i, 1), 10) * (10 - i)
|
||||
}
|
||||
s = s % 11
|
||||
return (s < 2 && c == s) || (s >= 2 && c == 11 - s)
|
||||
/* eslint-enable eqeqeq */
|
||||
}
|
||||
|
||||
module.exports.checkMobileNumber = number => {
|
||||
const regex = /^(\+98|0)?9\d{9}$/
|
||||
const result = regex.test(number)
|
||||
return result
|
||||
}
|
||||
|
||||
module.exports.normalizeMobileNumber = number => {
|
||||
return number.split('').reverse().join('').slice(0, 10).concat('0').split('').reverse().join('')
|
||||
}
|
||||
|
||||
module.exports.nameOptimizer = name => {
|
||||
function regex(str) {
|
||||
return new RegExp(str)
|
||||
}
|
||||
|
||||
return name
|
||||
.replace(regex('آقای'), '')
|
||||
.replace(regex('آقا'), '')
|
||||
.replace(regex('جنابه'), '')
|
||||
.replace(regex('جناب'), '')
|
||||
.replace(regex('سرکار خانوم'), '')
|
||||
.replace(regex('سرکار خانم'), '')
|
||||
.replace(regex('سرکارخانوم'), '')
|
||||
.replace(regex('سرکارخانومه'), '')
|
||||
.replace(regex('سرکارخانمه'), '')
|
||||
.replace(regex('سرکارخانم'), '')
|
||||
.replace(regex('سرکار'), '')
|
||||
.replace(regex('خانوم'), '')
|
||||
.replace(regex('خانم'), '')
|
||||
}
|
||||
Reference in New Issue
Block a user