Created
October 16, 2021 17:06
-
-
Save edgvi10/70810a64b67444e5174283f49b9b58a9 to your computer and use it in GitHub Desktop.
javascript utils
This file contains hidden or 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 isJson = (string) => { | |
try { | |
JSON.parse(string); | |
return true; | |
} catch (err) { | |
return false; | |
} | |
} | |
export const extractInt = (string) => { | |
return string.toString().replace(/[^0-9]+/g, ''); | |
} | |
export const mask = function (m, str, limit_maxlenth) { | |
var m, | |
l = (m = m.split("")).length, | |
s = str.split(""), | |
j = 0, | |
h = ""; | |
for (var i = -1; ++i < l;) | |
if (m[i] != "#") { | |
if (m[i] == "\\" && (h += m[++i])) continue; | |
h += m[i]; | |
i + 1 == l && (s[j - 1] += h, h = ""); | |
} | |
else { | |
if (!s[j] && !(h = "")) break; | |
(s[j] = h + s[j++]) && (h = ""); | |
} | |
var masked = s.join("") + h; | |
if (limit_maxlenth) masked = masked.substr(0, m.length); | |
return masked; | |
}; | |
export const phoneMask = (phone, full) => { | |
phone = extractInt(phone).toString(); | |
var pattern = "(##) ####-####"; | |
if (phone.length >= 11) pattern = "(##) #####-####"; | |
return mask(pattern, phone, true); | |
} | |
export const documentMask = (document, full) => { | |
document = extractInt(document).toString(); | |
var pattern = "###.###.###-##"; | |
if (document.length > 11) pattern = "##.###.###/####-##"; | |
return mask(pattern, document, true); | |
} | |
export const nameCapitalize = (string) => { | |
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))) | |
formated_name = name_parts.join(" "); | |
return formated_name; | |
} | |
export const nameFormat = (string, joint) => { | |
if (!joint) 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])) name_parts.push(name[1]); | |
name_parts.push(name[0]); | |
} | |
formated_name = name_parts.join(" "); | |
return formated_name; | |
} | |
export const normalize_string = (string) => string.toString().replace(/[^a-zA-Z ]+/g, '').replace(/\s+/g, ' '); | |
export const slugify = (string, separator = "-") => string.toString().normalize('NFD').replace(/[\u0300-\u036f]/g, '').toLowerCase().trim().replace(/[^a-z0-9 ]/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 search = (list, keyword, options) => { | |
const results = []; | |
if (options === undefined) options = {}; | |
if (typeof list === "object") { | |
Object.keys(list).map(item_index => { | |
const item = list[item_index]; | |
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) === 0) found = true; | |
} else { | |
if (field.toLowerCase().search(keyword.toString().toLowerCase()) === 0) found = true; | |
} | |
} | |
}); | |
if (found) results.push(item); | |
} else { | |
if (item.toString().search(keyword) !== -1) results.push(item); | |
} | |
}); | |
} else { | |
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; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment