Files
ostandari/server/plugins/controllersHelperFunctions.js
T
2024-10-21 10:22:26 +03:30

80 lines
2.3 KiB
JavaScript

module.exports.res404 = (res, err) => {
return res.status(404).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()
}
}
module.exports.removeWhiteSpaces = str => {
return str.replace(/ /g, '')
}
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('خانم'), '')
}
module.exports.checkNationalCode = code => {
var 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
var c = parseInt(code.substr(9, 1), 10)
var s = 0
for (var 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))
}
module.exports.isLatinCharactersWithoutSymbol = str => {
return str.match(/^[A-Za-z]+$/g)
}
module.exports.isLatinCharactersWithSymbol = str => {
return str.match(/^[a-zA-Z0-9()*_\-!#$%^&*,."\'\][]*$/g)
}
module.exports.hasWhiteSpaces = str => {
return str.includes(' ')
}
module.exports.generateRandomDigits = digitsLength => {
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 (digitsLength > max) {
return generateRandomDigits(max) + generateRandomDigits(digitsLength - max)
}
max = Math.pow(10, digitsLength + add)
var min = max / 10 // Math.pow(10, n) basically
var number = Math.floor(Math.random() * (max - min + 1)) + min
return ('' + number).substring(add)
}