klawtil
v0.16.0
Published
Biblioteca de funções úteis
Downloads
54
Maintainers
Readme
klawtil
Util functions library
Install
# Usando yarn
yarn add klawtil
## OU
# Usando npm
npm install klawtil
Import
// Modules
const { whiteList, isFalsy } = require("klawtil");
// ES6
import { whiteList, isFalsy } from "klawtil";
Content Table
- 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
Comparation
empty
empty(""); // -> true
empty(null); // -> true
empty(false); // -> false
empty(undefined); // -> true
isArray
isArray([]); // -> true
isArray({}); // -> false
isArray(0); // -> false
isArray("claudio"); // -> false
isObject
isObject({}); // ->true
isObject([]); // ->true
isObject("claudio"); // ->false
isObject(1); // ->false
isObject(new Date()); // ->true
isString
isString("claudio"); // -> true
isString("12"); // -> true
isString(12); // -> false
isString([12]); // -> false
isString({}); // -> false
isInteger
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
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
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
isEmail("claudio"); // -> false
isEmail("[email protected]"); // -> true
isEmail(""); // -> false
isEmail(null); // -> false
isEmail(undefined); // -> false
isEmail([]); // -> false
isEmail({}); // -> false
isEmail(123); // -> false
isDate
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
isDateBR("31/07/2020"); // true
isDateBR("32/07/2020"); // false
isDateBR("7/7/2020"); // false
isTime
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
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
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
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
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
Object
intersect
intersect([1, 2, 3], [3, 4, 5]); // -> [ 3 ]
objectPath
let obj = { a: "1", b: { c: 10, d: 2, e: { f: "4", g: "5", h: { i: "6" } } } };
objectPath(obj, "b.e.h.i"); // -> '6'
groupBy
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"}
]
}
prefixObjectKeys
const original = {
name: "ze",
age: 23,
};
prefixObjectKeys(original, "people.*.");
// -> { 'people.*.name': 'ze', 'people.*.age': 23}
objectFlat
// 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 }
whiteList
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
clearFalsy({ id: 1, age: "0", confirmed: "false", birth: "" }); // -> { id: 1 }
clearFalsy({ id: 1, age: "0", idade: "NaN", birth: "" }); // -> { id: 1 }
Array
sortByKey
sortByKey([{ name: "marta" }, { name: "claudio" }, { name: "isa" }], "name");
// -> [ {name: 'claudio',}, {name: 'isa',}, {name: 'marta',} ]
Boolean
toBoolean
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
String
slug
slug("José Cláudio + "); // -> 'jose-claudio
slug("José -- /|<>Cláu=dio "); // -> 'jose-claudio
currencyBR
currencyBR(12.34); // -> 'R$ 12,34'
currencyBR("12.34"); // -> 'R$ 12,34'
currencyBR("12,34"); // -> null
upperFirst
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
removeAccent("Açu"); // -> Acu
removeAccent("José Cláudio"); // -> Jose Claudio
clearNumber
clearNumber(12345 - 6, 6); // -> 123456
clearNumber(12345678, 3); // -> 123
clearNumber(12345, 10); // -> 0000001234
insertAtPosition
insertAtPosition("AAABBB", "-", 3); // -> AAA-BBB
insertAtPosition("000011122223445555", "->", 7); // -> 0000111->22223445555
removeFromPosition
removeFromPosition("00001119922223445555", 7, v9); // -> 000011122223445555
removeFromPosition("AAACBBB", 3, 4); // -> AAABBB
applyMask
applyMask("59650000", "00.000-000"); // -> 59.650-000
applyMask("99877665544", "(00) 0 0000-0000"); // -> (99) 8 7766-5544
applyVars
// Object with some random vars
const vars = {
id: 1,
name: "claudio",
age: 39,
email: "[email protected]",
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 protected].'
applyVars("My name is {name} and my email is {email}.", vars, {
start: "{",
end: "}",
});
// -> 'My name is claudio and my email is [email protected].'
applyVars("My name is {{name}} and my email is {{email}}.", vars, {
start: "{{",
end: "}}",
});
// -> 'My name is claudio and my email is [email protected].'
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
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
filesize(null); // -> 0 Kb
filesize(""); // -> 0 Kb
filesize(12354353); // -> 11 mb
Random
keyGenerator
keyGenerator(5); // -> 11S9P
keyGenerator(5, false); // -> HrmTF
keyGenerator(5, false, false); // -> RHCWJ
keyGenerator(5, false, true, false); // -> vzuyn
randomNumber
randomNumber(8, true); // -> 00083159
randomNumber(4); // -> 831
randomLetter
randomLetter(); // -> A
randomLetter(); // -> S
timeUUID
Gera um uuid v4 baseado no tempo atual
timeUUID(); // -> 1268a7f9-f641-44b6-079d-1286a5000169
timeUUID(); // -> 1268a7f9-f641-44d7-56fe-cb23e7400188
Tests
100% tested and 100% tests coverage
Build
Tested on node versions 18, 20 and 22