Welcome to the Utility JavaScript Functions Library! This library provides a comprehensive set of utility functions for various common tasks, including date calculations, formatting, masking, normalizing data, and validation. Each function is designed to make your development process easier and more efficient.
if using npm:
$ npm install js-essential-kit --save
if using yarn:
$ yarn add js-essential-kit
if using pnpm:
$ pnpm install js-essential-kit
- Calculates the age based on the given birth date.
import { calculateAge } from 'js-essential-kit'
calculateAge('2000-01-01') // Ex: 23 anos- Converts a date string to a different format.
import { convertDateFormat } from 'js-essential-kit'
convertDateFormat('2024-06-26') // Ex: '26/06/2024'
convertDateFormat('26/06/2024') // Ex: '2024-06-26'-
formatReal(amount: number | string): string -
Formats a number or string as Brazilian Real currency.
import { formatReal } from 'js-essential-kit'
formatReal(1234.56) // Ex: 'R$ 1.234,56'- Rounds a number to the nearest integer.
import { formatRound } from 'js-essential-kit'
formatRound(4.567) // Ex: 5- Formats a number to two decimal places.
import { formatDecimal } from 'js-essential-kit'
formatDecimal(1234.56) // Ex: '1234,56'- Applies CPF or CNPJ mask to a string.
import { cpfOrCnpjMask } from 'js-essential-kit'
cpfOrCnpjMask('12345678909')) // Ex CPF: '123.456.789-09'
cpfOrCnpjMask('68451802000151')) // EX CNPJ: '68.451.802/0001-51'- Applies Brazilian zipcode mask to a string.
import { brazilianZipcodeMask } from 'js-essential-kit'
brazilianZipcodeMask('12345678') // Ex: '12345-678'- Applies Brazilian telephone mask to a string.
import { brazilianTelephoneMask } from 'js-essential-kit'
brazilianTelephoneMask('21987654321') // Ex: '(21) 98765-4321'- Applies a global cellphone mask based on country.
import { globalCellphoneMask } from 'js-essential-kit'
globalCellphoneMask('US', '1234567890') // Ex: '+1 (123) 456-7890'- Clears any mask from a string.
import { clearMask } from 'js-essential-kit'
clearMask('123.456.789-09')) // Ex: '12345678909'- Normalizes a name string.
import { normalizeName } from 'js-essential-kit'
normalizeName(' João da Silva ') // Ex: 'João Da Silva'- Converts an array of strings to a single string with each item in quotes.
import { arrayToStringWithQuotes } from 'js-essential-kit'
arrayToStringWithQuotes(['apple', 'banana', 'cherry']) // Ex: '"apple", "banana", "cherry"'- Checks if array is empty.
import { isEmpty } from 'js-essential-kit'
isEmpty([]) // Ex: true- Checks if array is not empty.
import { isNotEmpty } from 'js-essential-kit'
isNotEmpty(['apple', 'banana', 'cherry']) // Ex: true- Encodes a string in base64.
import { base64Encoding } from 'js-essential-kit'
base64Encoding('Hello, World!') // Ex: 'SGVsbG8sIFdvcmxkIQ=='- Decodes a base64 string.
import { base64Decoding } from 'js-essential-kit'
base64Decoding('SGVsbG8sIFdvcmxkIQ==') // Ex: 'Hello, World!'- Generates a random number between min and max.
import { generateRandomNumber } from 'js-essential-kit'
generateRandomNumber(1, 10) // Ex: 7- Generates a random string of specified length.
import { generateRandomString } from 'js-essential-kit'
generateRandomString(5, 10) // Ex: 'aBcDeF'- Generates an array of numbers from 0 to quantity-1.
import { generateRange } from 'js-essential-kit'
generateRange(5) // Ex: [1, 2, 3, 4, 5]- Creates a URL-friendly slug from a string.
import { createSlug } from 'js-essential-kit'
createSlug('Olá Mundo!') // 'ola-mundo'- Limits the length of a string, optionally adding ellipsis.
import { limitString } from 'js-essential-kit'
// Limits a string to the specified length, optionally adding an ellipsis.
limitString('Hello World', 10, true)) // Ex: 'Hello W...'- Finds the lowest value in an option group.
import { findLowestValue } from 'js-essential-kit'
const options = {
options: [{ value: '10' }, { value: '5' }, { value: '20' }],
}
findLowestValue(options) // Ex: { value: '5' }- Generates a set of time slots.
import { generateTimeSlots } from 'js-essential-kit'
generateTimeSlots()
/* [
{ index: 0, key: '00:00 - 02:00', value: 0 },
{ index: 1, key: '02:00 - 04:00', value: 0 },
...,
{ index: 11, key: '22:00 - 00:00', value: 0 }
] */- Splits a full name into first and last name.
import { createFirstAndLastName } from 'js-essential-kit'
createFirstAndLastName('John Michael Doe') // Ex: 'John Michael'- Calculates the distance in kilometers.
import { calculateDistanceInKm } from 'js-essential-kit'
calculateDistanceInKm(1500) // Ex: 1.5- Checks if an object is empty.
import { isEmptyObject } from 'js-essential-kit'
isEmptyObject({}) // Ex: true- Rounds a number to two decimal places.
import { roundToTwo } from 'js-essential-kit'
roundToTwo(123.456) // Ex: 123.46- Finds the maximum value in an array.
import { findMax } from 'js-essential-kit'
findMax([1, 2, 3, 4, 5]) // Ex: 5- Finds the minimum value in an array.
import { findMin } from 'js-essential-kit'
findMin([1, 2, 3, 4, 5]) // Ex: 1- Removes duplicate values from an array.
import { removeDuplicates } from 'js-essential-kit'
removeDuplicates([1, 2, 2, 3, 4, 4, 5]) // Ex: [1, 2, 3, 4, 5]- Capitalizes the first letter of each word in a string.
import { capitalizeWords } from 'js-essential-kit'
capitalizeWords('hello world') // Ex: 'Hello World'- Validates a Brazilian CPF.
import { brazilianCpfValidator } from 'js-essential-kit'
brazilianCpfValidator('123.456.789-09') // Ex: true ou false- Validates a Brazilian CNPJ.
import { brazilianCnpjValidator } from 'js-essential-kit'
brazilianCnpjValidator('12.345.678/0001-95') // Ex: true ou false- Validates an email address.
import { emailIsValid } from 'js-essential-kit'
emailIsValid('example@example.com') // Ex: true ou false- Validates a name string.
import { nameIsValid } from 'js-essential-kit'
nameIsValid('John Doe') // Ex: true ou false- Validates a full name string.
import { fullnameIsValid } from 'js-essential-kit'
fullnameIsValid('John Doe') // Ex: { valid: true, message: '' }
fullnameIsValid('John Doe') // Ex: { valid: false, message: 'No extra spaces allowed' }- Validates a name with last name.
import { validNameAndLastName } from 'js-essential-kit'
validNameAndLastName('John Doe') // Ex: true ou false- Validates a Brazilian telephone number.
import { brazilianTelephoneValidator } from 'js-essential-kit'
brazilianTelephoneValidator('(21) 98765-4321') // Ex: true ou false- Checks if the birthdate is 18 years or older, with an option to allow minors.
import { birthdateIs18Plus } from 'js-essential-kit'
birthdateIs18Plus('2000-01-01', false)) // Ex: true ou false- Validates the strength of a password.
import { passwordStrongValidator } from 'js-essential-kit'
passwordStrongValidator('StrongP@ssword1')
// Ex: { passwordIsValid: true }
passwordStrongValidator('weak')
// Ex: { passwordIsValid: false, errors: ['passwordLength', 'noNumber', 'noUpperCaseLetter', 'noLowerCaseLetter'] }Contributions are what make the open source community an incredible place to learn, inspire and create. Any contribution you make will be much appreciated.
- Fork the project
- Create a Branch for your Feature (
git checkout -b feature/newFeature) - Add your changes (
git add .) - Commit your changes (
git commit -m 'Add new feature!) - Push the Branch (
git push origin feature/newFeature) - Open a Pull Request
Feel free to contribute to this project or suggest new features. Happy coding! 😊
Julio Sousa |
|---|
The Apache 2.0 (APACHE 2.0)