1.2.0 • Published 7 months ago

js-essential-kit v1.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

Utility JavaScript Functions Library 📚

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.

Table of Contents

Getting Started ✈️

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

Dates

Calculate Age

  • Calculates the age based on the given birth date.
import { calculateAge } from 'js-essential-kit'

calculateAge('2000-01-01') // Ex: 23 anos

Convert Date Format

  • 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'

Formats

Format Real

  • 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'

Format Round

  • Rounds a number to the nearest integer.
import { formatRound } from 'js-essential-kit'

formatRound(4.567) // Ex: 5

Format Decimal

  • Formats a number to two decimal places.
import { formatDecimal } from 'js-essential-kit'

formatDecimal(1234.56) // Ex: '1234,56'

Masks

CPF or CNPJ Mask

  • 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'

Brazilian Zipcode Mask

  • Applies Brazilian zipcode mask to a string.
import { brazilianZipcodeMask } from 'js-essential-kit'

brazilianZipcodeMask('12345678') // Ex: '12345-678'

Brazilian Telephone Mask

  • Applies Brazilian telephone mask to a string.
import { brazilianTelephoneMask } from 'js-essential-kit'

brazilianTelephoneMask('21987654321') // Ex: '(21) 98765-4321'

Global Cellphone Mask

  • Applies a global cellphone mask based on country.
import { globalCellphoneMask } from 'js-essential-kit'

globalCellphoneMask('US', '1234567890') // Ex: '+1 (123) 456-7890'

Clear Mask

  • Clears any mask from a string.
import { clearMask } from 'js-essential-kit'

clearMask('123.456.789-09')) // Ex: '12345678909'

Normalize

Normalize Name

  • Normalizes a name string.
import { normalizeName } from 'js-essential-kit'

normalizeName(' João da Silva ') // Ex: 'João Da Silva'

Array to String with Quotes

  • 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"'

Utils

isEmpty

  • Checks if array is empty.
import { isEmpty } from 'js-essential-kit'

isEmpty([]) // Ex: true

isNotEmpty

  • Checks if array is not empty.
import { isNotEmpty } from 'js-essential-kit'

isNotEmpty(['apple', 'banana', 'cherry']) // Ex: true

Others

Base64 Encoding

  • Encodes a string in base64.
import { base64Encoding } from 'js-essential-kit'

base64Encoding('Hello, World!') // Ex: 'SGVsbG8sIFdvcmxkIQ=='

Base64 Deconding

  • Decodes a base64 string.
import { base64Decoding } from 'js-essential-kit'

base64Decoding('SGVsbG8sIFdvcmxkIQ==') // Ex: 'Hello, World!'

Generate Random Number

  • Generates a random number between min and max.
import { generateRandomNumber } from 'js-essential-kit'

generateRandomNumber(1, 10) // Ex: 7

Generate Random String

  • Generates a random string of specified length.
import { generateRandomString } from 'js-essential-kit'

generateRandomString(5, 10) // Ex: 'aBcDeF'

Generate Range

  • Generates an array of numbers from 0 to quantity-1.
import { generateRange } from 'js-essential-kit'

generateRange(5) // Ex: [1, 2, 3, 4, 5]

Create Slug

  • Creates a URL-friendly slug from a string.
import { createSlug } from 'js-essential-kit'

createSlug('Olá Mundo!') // 'ola-mundo'

Limit String

  • 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...'

Find LowestValue

  • 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' }

Generate Time Slots

  • 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 }
] */

Create First And Lastname

  • Splits a full name into first and last name.
import { createFirstAndLastName } from 'js-essential-kit'

createFirstAndLastName('John Michael Doe') // Ex: 'John Michael'

Calculate Distance In KM

  • Calculates the distance in kilometers.
import { calculateDistanceInKm } from 'js-essential-kit'

calculateDistanceInKm(1500) // Ex: 1.5

Is Empty Object

  • Checks if an object is empty.
import { isEmptyObject } from 'js-essential-kit'

isEmptyObject({}) // Ex: true

Round To Two

  • Rounds a number to two decimal places.
import { roundToTwo } from 'js-essential-kit'

roundToTwo(123.456) // Ex: 123.46

Find Max

  • Finds the maximum value in an array.
import { findMax } from 'js-essential-kit'

findMax([1, 2, 3, 4, 5]) // Ex: 5

Find Min

  • Finds the minimum value in an array.
import { findMin } from 'js-essential-kit'

findMin([1, 2, 3, 4, 5]) // Ex: 1

Remove Duplicates

  • 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]

Capitalize Words

  • Capitalizes the first letter of each word in a string.
import { capitalizeWords } from 'js-essential-kit'

capitalizeWords('hello world') // Ex: 'Hello World'

Validators

BrazilianCpfValidator

  • Validates a Brazilian CPF.
import { brazilianCpfValidator } from 'js-essential-kit'

brazilianCpfValidator('123.456.789-09') // Ex: true ou false

Brazilian CNPJ Validator

  • Validates a Brazilian CNPJ.
import { brazilianCnpjValidator } from 'js-essential-kit'

brazilianCnpjValidator('12.345.678/0001-95') // Ex: true ou false

Email Is Valid

  • Validates an email address.
import { emailIsValid } from 'js-essential-kit'

emailIsValid('example@example.com') // Ex: true ou false

Name Is Valid

  • Validates a name string.
import { nameIsValid } from 'js-essential-kit'

nameIsValid('John Doe') // Ex: true ou false

Fullname Is Valid

  • 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' }

Valid Name And Lastname

  • Validates a name with last name.
import { validNameAndLastName } from 'js-essential-kit'

validNameAndLastName('John Doe') // Ex: true ou false

Brazilian TelephoneValidator

  • Validates a Brazilian telephone number.
import { brazilianTelephoneValidator } from 'js-essential-kit'

brazilianTelephoneValidator('(21) 98765-4321') // Ex: true ou false

Birthdate Is 18 Plus

  • 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

Password Strong Validator

  • 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'] }

Contribution

Contributions are what make the open source community an incredible place to learn, inspire and create. Any contribution you make will be much appreciated.

  1. Fork the project
  2. Create a Branch for your Feature (git checkout -b feature/newFeature)
  3. Add your changes (git add .)
  4. Commit your changes (git commit -m 'Add new feature!)
  5. Push the Branch (git push origin feature/newFeature)
  6. Open a Pull Request

Feel free to contribute to this project or suggest new features. Happy coding! 😊


Contributors

Julio Sousa

Licence

The Apache 2.0 (APACHE 2.0)