Last active
December 2, 2022 15:01
-
-
Save edgvi10/eca5c05bea251a6945211f5a0d8cd92e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const isSet = (value) => (value !== undefined && value !== null); | |
export const isEmpty = (value) => ( | |
(Array.isArray(value) && value.length === 0) | |
|| (typeof value === "object" && Object.keys(value).length === 0) | |
|| (typeof value === "string" && value.trim().length === 0) | |
|| (typeof value === "number" && isNaN(value)) | |
|| value === undefined | |
|| value === null | |
); | |
export const floatMask = (value, decimals = 2) => { | |
value = value.replace(/[^0-9]/g, ""); | |
value = value.padStart(decimals + 1, "0"); | |
value = value.split("").reverse().join(""); | |
let floated = value.substring(0, decimals); | |
let integers = value.substring(decimals); | |
value = `${floated}.${integers}`.split("").reverse().join(""); | |
value = parseFloat(value).toFixed(decimals); | |
return value; | |
} | |
export const nameCapitalize = (string) => { | |
if (!string) return null; | |
const joint = ["de", "da", "do", "das", "dos",]; | |
var formated_name = ""; | |
const name = string.split(" "); | |
const name_parts = []; | |
for (let part of name) name_parts.push((joint.includes(part)) ? part : (part.charAt(0).toUpperCase() + part.slice(1).toLowerCase())) | |
formated_name = name_parts.join(" "); | |
return formated_name; | |
} | |
export const nameFormat = (string, joint) => { | |
if (!string) return null; | |
joint = ["de", "da", "do", "das", "dos"]; | |
var formated_name = ""; | |
const name = string.split(" "); | |
const name_parts = []; | |
name_parts.push(name[0]); | |
if (name.length > 1) { | |
name.reverse(); | |
if (joint.includes(name[1].toLowerCase())) name_parts.push(name[1]); | |
name_parts.push(name[0]); | |
} | |
if (name_parts.length > 1) if (name_parts[0] == name_parts[name_parts.length - 1]) name_parts.pop(); | |
formated_name = name_parts.join(" "); | |
return formated_name.trim(); | |
} | |
export const normalizeString = (string) => string.toString().normalize('NFD').replace(/[^a-zA-Z0-9 ]+/g, '').replace(/\s+/g, ' '); | |
export const slugify = (string, separator = "-") => string.toString().normalize('NFD').replace(/[\u0300-\u036f]/g, '').toLowerCase().replace(/[^a-z0-9 ]separator/g, '').replace(/\s+/g, separator); | |
export const base64Encode = (string) => new Buffer.from(string).toString("base64"); | |
export const base64Decode = (string) => new Buffer.from(string, "base64").toString("ascii"); | |
export const randomHex = (size) => [...Array(size)].map(() => Math.floor(Math.random() * 16).toString(16)).join(''); | |
export const extractInt = (string) => { | |
if (!string) return ""; | |
return (string ?? "").toString().replace(/[^0-9]+/g, ''); | |
} | |
export const mask = function (pattern, str, limit_maxlenth) { | |
if (!str) return null; | |
var pattern, length = (pattern = pattern.split("")).length, string = str.split(""), j = 0, h = ""; | |
for (var i = -1; ++i < length;) { | |
if (pattern[i] != "#") { | |
if (pattern[i] == "\\" && (h += pattern[++i])) continue; | |
h += pattern[i]; | |
i + 1 == length && (string[j - 1] += h, h = ""); | |
} else { | |
if (!string[j] && !(h = "")) break; | |
(string[j] = h + string[j++]) && (h = ""); | |
} | |
} | |
var masked = string.join("") + h; | |
if (limit_maxlenth) masked = masked.substring(0, pattern.length); | |
return masked; | |
} | |
export const search = (list, keyword, options) => { | |
const results = []; | |
if (options === undefined) options = {}; | |
if (typeof list === "object") list = Object.values(list); | |
list.map(item => { | |
if (typeof item === "object") { | |
var keys = Object.keys(item); | |
if ("fields" in options) | |
keys = options["fields"]; | |
let found = false; | |
keys.map(key => { | |
if (item[key] !== null) { | |
var field = item[key].toString(); | |
if ("ignoreCase" in options) { | |
if (field.search(keyword) !== -1) found = true; | |
} else { | |
if (field.toLowerCase().search(keyword.toString().toLowerCase()) !== -1) found = true; | |
} | |
} | |
}); | |
if (found) results.push(item); | |
} else { | |
if (item.toString().search(keyword) !== -1) results.push(item); | |
} | |
}); | |
return results; | |
}; | |
export const validateCnpj = (value) => { | |
if (!value) return false | |
const isString = typeof value === 'string' | |
const validTypes = isString || Number.isInteger(value) || Array.isArray(value) | |
if (!validTypes) return false | |
if (isString) { | |
if (value.length > 18) return false | |
const digitsOnly = /^\d{14}$/.test(value) | |
const validFormat = /^\d{2}.\d{3}.\d{3}\/\d{4}-\d{2}$/.test(value) | |
if (digitsOnly || validFormat) true | |
else return false | |
} | |
const match = value.toString().match(/\d/g) | |
const numbers = Array.isArray(match) ? match.map(Number) : [] | |
if (numbers.length !== 14) return false | |
const items = [...new Set(numbers)] | |
if (items.length === 1) return false | |
const calc = (x) => { | |
const slice = numbers.slice(0, x) | |
let factor = x - 7 | |
let sum = 0 | |
for (let i = x; i >= 1; i--) { | |
const n = slice[x - i] | |
sum += n * factor-- | |
if (factor < 2) factor = 9 | |
} | |
const result = 11 - (sum % 11) | |
return result > 9 ? 0 : result | |
} | |
const digits = numbers.slice(12) | |
const digit0 = calc(12) | |
if (digit0 !== digits[0]) return false | |
const digit1 = calc(13) | |
return digit1 === digits[1] | |
} | |
export const validateCpf = (strCPF) => { | |
if (new Set(strCPF).size == 1) return false | |
let cpf = Array.from(strCPF).map(Number); | |
return [9, 10].every(pos => { | |
let multiplier = pos + 1 | |
let vd = cpf.slice(0, pos).reduce((total, amount, index) => total + amount * (multiplier - index), 0); | |
vd = (vd * 10) % 11; | |
vd = (vd > 9) ? 0 : vd | |
if (vd == cpf[pos]) return true | |
}) | |
} | |
export const validateDocument = (document) => { | |
document = extractInt(document); | |
if (!document) return false; | |
if (document.toString().length == 11) { | |
return validateCpf(document); | |
} else if (document.toString().length == 14) { | |
return validateCnpj(document); | |
} else { | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment