Util functions library
# Usando yarn
yarn add klawtil
## OU
# Usando npm
npm install klawtil
// Modules
const { whiteList, isFalsy } = require("klawtil");
// ES6
import { whiteList, isFalsy } from "klawtil";
- empty
- isArray
- isObject
- isString
- isInteger
- isFloat
- isNumeric
- isEmail
- isDate
- isDateBR
- isTime
- isFalsy
- isTruthy
- isJSON
- checkTypes
- slug
- currencyBR
- upperFirst
- removeAccent
- clearNumber
- insertAtPosition
- removeFromPosition
- applyMask
- applyVars
- quantity
- filesize
empty(""); // -> true
empty(null); // -> true
empty(false); // -> false
empty(undefined); // -> true
isArray([]); // -> true
isArray({}); // -> false
isArray(0); // -> false
isArray("claudio"); // -> false
isObject({}); // ->true
isObject([]); // ->true
isObject("claudio"); // ->false
isObject(1); // ->false
isObject(new Date()); // ->true
isString("claudio"); // -> true
isString("12"); // -> true
isString(12); // -> false
isString([12]); // -> false
isString({}); // -> false
isInteger("1"); // -> true
isInteger("1", true); // -> false
isInteger(1); // -> true
isInteger(1.2); // -> false
isInteger(1, true); // -> true
isInteger("1", true); // -> false
isInteger("claudio"); // -> false
isFloat("1"); // -> true
isFloat(1, true); // -> true
isFloat("1", true); // -> false
isFloat(1); // -> true
isFloat(1.2); // -> true
isFloat("1.2"); // -> true
isFloat("1.2", true); // -> false
isFloat("claudio"); // -> false
isFloat(false); // -> false
isNumeric("1"); // -> true
isNumeric(1, true); // -> true
isNumeric("1", true); // -> false
isNumeric(1); // -> true
isNumeric(1.2); // -> true
isNumeric("1.2"); // -> true
isNumeric("1.2", true); // -> false
isNumeric("claudio"); // -> false
isNumeric(false); // -> false
isEmail("claudio"); // -> false
isEmail("claudio@claudio.com"); // -> true
isEmail(""); // -> false
isEmail(null); // -> false
isEmail(undefined); // -> false
isEmail([]); // -> false
isEmail({}); // -> false
isEmail(123); // -> false
isDate("12/12/29"); // -> true
isDate("31/12/29"); // -> false
isDate("12/31/29"); // -> true
isDate("12/12/2029"); // -> true
isDate("2029-12-12"); // -> true
isDateBR("31/07/2020"); // true
isDateBR("32/07/2020"); // false
isDateBR("7/7/2020"); // false
isTime("12:00"); // -> true
isTime("23:00"); // -> true
isTime("23:59"); // -> true
isTime("24:00"); // -> false
isTime("00:00"); // -> true
isTime("-12:00"); // -> false
isTime("aa:pp"); // -> false
isTime("23:60"); // -> false
isTime("00:00:00", true); // -> true
isTime("23:59:59", true); // -> true
isTime("24:59:59", true); // -> false
isTime("23:60:59", true); // -> false
isTime("23:59:65", true); // -> false
isTime("aa:bb:dd", true); // -> false
isFalsy(0); // -> true
isFalsy(""); // -> true
isFalsy("0"); // -> true
isFalsy(NaN); // -> true
isFalsy(null); // -> true
isFalsy("NaN"); // -> true
isFalsy(false); // -> true
isFalsy("null"); // -> true
isFalsy("false"); // -> true
isFalsy(undefined); // -> true
isFalsy("undefined"); // -> true
isTruthy(0); // -> false
isTruthy(""); // -> false
isTruthy("0"); // -> false
isTruthy(NaN); // -> false
isTruthy(null); // -> false
isTruthy("NaN"); // -> false
isTruthy(false); // -> false
isTruthy("null"); // -> false
isTruthy("false"); // -> false
isTruthy(undefined); // -> false
isTruthy("undefined"); // -> false
isJSON(0); // -> false
isJSON(12); // -> false
isJSON(""); // -> false
isJSON(NaN); // -> false
isJSON(null); // -> false
isJSON(true); // -> false
isJSON(false); // -> false
isJSON("banna"); // -> false
isJSON(undefined); // -> false
isJSON(new Date()); // -> false
isJSON([1, 2, 3, 4]); // -> false
isJSON("{ age: 21 }"); // -> false
isJSON({ age: 21 }); // -> true
checkTypes(1, String); // -> false
checkTypes("1", String); // -> true
checkTypes(1, Number); // -> true
checkTypes("1", Number); // -> false
checkTypes(1, [String, Number]); // -> true
checkTypes("a", [String, Number]); // -> true
checkTypes({}, [String, Number]); // -> false
checkTypes([], [String, Number]); // -> false
checkTypes([], [String, Number, Array]); // -> true
checkTypes({}, [String, Number, Array]); // -> false
checkTypes({}, [String, Number, Array, Object]); // -> true
intersect([1, 2, 3], [3, 4, 5]); // -> [ 3 ]
let obj = { a: "1", b: { c: 10, d: 2, e: { f: "4", g: "5", h: { i: "6" } } } };
objectPath(obj, "b.e.h.i"); // -> '6'
const list = [
{"id":1,"name":"claudio","age":37,"city":"fortaleza"},
{"id":2,"name":"isa","age":9,"city":"natal"},
{"id":3,"name":"jose","age":37,"city":"fortaleza"},
{"id":4,"name":"marta","age":42,"city":"afonso bezerra"},
{"id":5,"name":"joelma","age":42,"city":"afonso bezerra"},
{"id":6,"name":"jose","age":24,"city":"assu"}
]
groupBy( list, 'name' )
{
"claudio":[
{"id":1,"name":"claudio","age":37,"city":"fortaleza"}
],
"isa":[
{"id":2,"name":"isa","age":9,"city":"natal"}
],
"jose":[
{"id":3,"name":"jose","age":37,"city":"fortaleza"},
{"id":6,"name":"jose","age":24,"city":"assu"}
],
"marta":[
{"id":4,"name":"marta","age":42,"city":"afonso bezerra"}
],
"joelma":[
{"id":5,"name":"joelma","age":42,"city":"afonso bezerra"}
]
}
const original = {
name: "ze",
age: 23,
};
prefixObjectKeys(original, "people.*.");
// -> { 'people.*.name': 'ze', 'people.*.age': 23}
// Dados de exemplo
const obj = {
name: 'claudio'
address: { street: 'Monkey St.' }
}
objectFlat( obj )
// -> { name: 'claudio', 'address.street': 'Monkey St.' }
objectFlat( obj, '_' )
// -> { name: 'claudio', address_street: 'Monkey St.' }
objectFlat( { name: 'ze', info: { age: 36 } }, '_' )
// -> { name: 'ze', info_age: 36 }
objectFlat( { name:'ze', info: { age: null } }, '_' )
// -> { name: 'ze', info_age: null }
objectFlat( { name:'ze', info: { birth: new Date('1982-07-31') } }, '_' )
// -> { name: 'ze', info_birth: 1982-07-31T00:00:00.000Z }
const address = {
id: 1,
description: "decrição",
city_id: 123,
city: { id: 123, name: "açu" },
};
whiteList(address, ["id", "description", "city_id"]);
// -> { id: 1, description: 'decrição', city_id: 123 }
whiteList([address, address], ["id", "city_id"]);
// -> [ { id: 1, city_id: '123' }, { id: 1, city_id: '123' } ]
clearFalsy({ id: 1, age: "0", confirmed: "false", birth: "" }); // -> { id: 1 }
clearFalsy({ id: 1, age: "0", idade: "NaN", birth: "" }); // -> { id: 1 }
sortByKey([{ name: "marta" }, { name: "claudio" }, { name: "isa" }], "name");
// -> [ {name: 'claudio',}, {name: 'isa',}, {name: 'marta',} ]
toBoolean("a"); // -> true
toBoolean(1); // -> true
toBoolean("true"); // -> true
toBoolean("0"); // -> false
toBoolean(0); // -> false
toBoolean("false"); // -> false
toBoolean(false); // -> false
toBoolean(""); // -> false
toBoolean("undefined"); // -> false
toBoolean(undefined); // -> false
toBoolean("NaN"); // -> false
toBoolean(NaN); // -> false
toBoolean("null"); // -> false
toBoolean(null); // -> false
slug("José Cláudio + "); // -> 'jose-claudio
slug("José -- /|<>Cláu=dio "); // -> 'jose-claudio
currencyBR(12.34); // -> 'R$ 12,34'
currencyBR("12.34"); // -> 'R$ 12,34'
currencyBR("12,34"); // -> null
upperFirst("jose claudio medeiros de lima"); // -> Jose Claudio Medeiros de Lima
upperFirst("JOSE CLAUDIO MEDEIROS DE LIMA"); // -> Jose Claudio Medeiros de Lima
upperFirst("JoSe cLaUdIo MeDeIrOs De LiMa"); // -> Jose Claudio Medeiros de Lima
removeAccent("Açu"); // -> Acu
removeAccent("José Cláudio"); // -> Jose Claudio
clearNumber(12345 - 6, 6); // -> 123456
clearNumber(12345678, 3); // -> 123
clearNumber(12345, 10); // -> 0000001234
insertAtPosition("AAABBB", "-", 3); // -> AAA-BBB
insertAtPosition("000011122223445555", "->", 7); // -> 0000111->22223445555
removeFromPosition("00001119922223445555", 7, v9); // -> 000011122223445555
removeFromPosition("AAACBBB", 3, 4); // -> AAABBB
applyMask("59650000", "00.000-000"); // -> 59.650-000
applyMask("99877665544", "(00) 0 0000-0000"); // -> (99) 8 7766-5544
// Object with some random vars
const vars = {
id: 1,
name: "claudio",
age: 39,
email: "email@mail.com",
address: {
street: "Monkey St.",
number: "599",
city: "Halalala",
zipcode: "9876543",
},
};
applyVars("My name is :name and my email is :email.", vars);
// -> 'My name is claudio and my email is email@mail.com.'
applyVars("My name is {name} and my email is {email}.", vars, {
start: "{",
end: "}",
});
// -> 'My name is claudio and my email is email@mail.com.'
applyVars("My name is {{name}} and my email is {{email}}.", vars, {
start: "{{",
end: "}}",
});
// -> 'My name is claudio and my email is email@mail.com.'
applyVars(
"My name is :name and my address is :address.street, :address.zipcode",
vars
)
// -> "My name is claudio and my address is Monkey St., 9876543");
applyVars(
"My name is {name} and my address is {address.street}, {address.zipcode}",
vars,
{ start: "{", end: "}" }
// -> "My name is claudio and my address is Monkey St., 9876543");
quantity(0, "mensagens", "mensagem");
// -> 0 mensagem
quantity(0, "mensagens", "mensagem", "nenhuma mensagem");
// -> nenhuma mensagem
quantity(1, "mensagens", "mensagem", "nenhuma mensagem");
// -> 1 mensagem
quantity(2, "mensagens", "mensagem", "nenhuma mensagem");
// -> 2 mensagens
filesize(null); // -> 0 Kb
filesize(""); // -> 0 Kb
filesize(12354353); // -> 11 mb
keyGenerator(5); // -> 11S9P
keyGenerator(5, false); // -> HrmTF
keyGenerator(5, false, false); // -> RHCWJ
keyGenerator(5, false, true, false); // -> vzuyn
randomNumber(8, true); // -> 00083159
randomNumber(4); // -> 831
randomLetter(); // -> A
randomLetter(); // -> S
Gera um uuid v4 baseado no tempo atual
timeUUID(); // -> 1268a7f9-f641-44b6-079d-1286a5000169
timeUUID(); // -> 1268a7f9-f641-44d7-56fe-cb23e7400188
100% tested and 100% tests coverage
Tested on node versions 18, 20 and 22