lual-utils
v0.0.5
Published
This repository aims to guide a tutorial on how to create an npm module, and mainly to add useful functions that I use in my daily life in various projects.
Downloads
25
Maintainers
Readme
Introduction 📖
lual-utils
is a library that was built for creating an article about developing a JavaScript package for publication on NPM. The article is in Portuguese and can be accessed at the following link:
Artigo: Como criar um pacote JavaScript para o NPM
In this article, I cover step by step the process of building this package, from project creation to publishing on NPM. However, to make it more than just a theoretical article, I created this package with some utility functions that I use frequently in my daily life and that might be useful for you too.
Features 🚀
- Validate CPF ✅
- Validate CNPJ ✅
- Generate Random Code ✅
- Phone Number Masking for Major Countries ✅
- Validate Email ✅
- Validate URL ✅
- New features coming soon ⏱️
Contributing 🤝
If you want to contribute to this project, feel free to fork this repository and submit a pull request. I will be happy to review and approve it.
Installation 📦
To install lual-utils, run the following command:
npm install lual-utils
Validate CPF and CNPJ
Validation of CPF and CNPJ using regular expressions and validation algorithms.
// Import the lual-utils module
import { isCpfOrCnpjValid } from 'lual-utils'
const cpf = '12345678901'
const cnpj = '12345678901234'
console.log(isCpfOrCnpjValid(cpf)) // true or false
console.log(isCpfOrCnpjValid(cnpj)) // true or false
Validate Email
Email validation using regular expressions.
// Import the lual-utils module
import { isEmailValid } from 'lual-utils'
const emailExample = '[email protected]'
console.log(isEmailValid(emailExample)) // true or false
Generate Random Code
Generate a random code to be used as a token, password, etc. This generation uses the Math.random()
algorithm.
// Import the lual-utils module
import { genRandomStrCode } from 'lual-utils'
const password = genRandomStrCode(8)
console.log(password) // 7sa2d3f4
Phone Number Masking
Phone number masking for major countries, using regular expressions based on the country codes of the 25+ most used countries.
// Import the lual-utils module
import { setPhoneMask } from 'lual-utils'
const phone = '5511999999999'
const phoneMask = setPhoneMask(phone) // +55 (11) 99999-9999
Validate URL
URL validation using regular expressions.
// Import the lual-utils module
import { isUrlValid } from 'lual-utils'
// Validate only HTTPS URLs
validateUrl('https://example.com', { protocols: ['https'] }) // true
validateUrl('http://example.com', { protocols: ['https'] }) // false
// Validate URLs HTTP and HTTPS without IP
validateUrl('http://example.com', { allowIp: false }) // true
validateUrl('http://192.168.0.1', { allowIp: false }) // false
// Validate URLs HTTPS and HTTPS with IP
validateUrl('https://192.168.0.1', { protocols: ['https'], allowIp: true }) // true
validateUrl('http://192.168.0.1', { protocols: ['https'], allowIp: true }) // false