Giter Site home page Giter Site logo

cpf's Introduction

CPF

Build Status NPM package version License: MIT Minified size Minified+Gzip size

This package does some CPF magic. It allows you to create, validate and format CPF documents.

HINT: Check out the CNPJ counter part available at https://github.com/fnando/cnpj.

Installation

This lib is available as a NPM package. To install it, use the following command:

npm install @fnando/cpf --save

If you're using Yarn (and you should):

yarn add @fnando/cpf

Usage

// Node.js-specific
const cpf = require("@fnando/cpf/commonjs");

// @import
import * as cpf from "@fnando/cpf"; // import the whole library
import { isValid as isValidCpf } from "@fnando/cpf"; // import just one function

// import via <script>; the lib will available as window.CPF
// <script src="cpf.js"></script>

cpf.isValid("532.820.857-96");
//=> true

cpf.isValid("53282085796");
//=> true

cpf.strip("532.820.857-96");
//=> 53282085796

cpf.format("53282085796");
//=> 532.820.857-96

cpf.generate(true); // generate formatted number
//=> 838.684.734-40

cpf.generate(); // generate unformatted number
//=> 72777632898

On the web, without transformation, just use web/cpf.min.js.

Strict Validation

By default, validations will strip any characters you provide. This means that the following is valid, because only numbers will be considered:

cpf.isValid("101#688!!!!!!542......36");
//=> true

cpf.strip("101#688!!!!!!542......36");
//=> 10168854236

If you want to strict validate strings, use the following signature:

cpf.isValid(number, strict);

The same example would now return false:

cpf.isValid("101#688!!!!!!542......36", true);
//=> false

cpf's People

Contributors

dependabot[bot] avatar fnando avatar leonardomerlin avatar luancurti avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

cpf's Issues

Doesn't work in IE 11 and IE10

I have been using this repo into a vuejs application, in chrome, firefox and edge it has been working fine. But, in IE 11 and IE10 it hasn't been working.

Why is main file from src and not from dist?

This is a section of the package.json file in the module downloaded from npm.

"name": "@fnando/cpf",
  "version": "0.1.0",
  "description": "Validate, generate and format CPF numbers",
  "main": "src/cpf.js",

This causes an error when using node:

node_modules/@fnando/cpf/src/cpf.js:19
export function verifierDigit(numbers) {
^^^^^^

SyntaxError: Unexpected token export

Module doesn't build with react-app-rewired

I couldn't see what exactly was happening but I tried to use this module and it didn't build with the following error:

Failed to minify the code from this file: 
 	./node_modules/@fnando/cpf/src/cpf.js:59

Sorry, can't help on this one. I have to rush and use another module.

Uncaught TypeError: n.includes is not a function

Estou utilizando a lib em um projeto, ai recebi o relato de alguns clientes não conseguirem passar para frente.
testo no meu ambiente funciona perfeito, testei em iphone 7, android e foi lindo...

ai perguntei para um dos clientes qual modelo do aparelho, e debugando descobri que utiliza um motorola com chrome desatualizado, ai debugando no chrome inspect encontrei este erro, vou tentar outros testes. para ver se consigo contornar

function__webpack_modules__../node_modules/@fnando/cpf/commonjs/index.js.e.isValid @ index.js:formatted:1

(anonymous function) @ index.js:formatted:1

__webpack_modules__../node_modules/jquery/dist/jquery.js.jQuery.event.dispatch @ index.js:formatted:1

__webpack_modules__../node_modules/jquery/dist/jquery.js.jQuery.event.add.elemData.handle @ index.js:formatted:1

__trace__ @ index.js:formatted:1

Add type definitions for use with TS

I have used this a personal project that is using Typescript. So, I did the type definitions for it as follows:

declare module '@fnando/cpf' {
  const BLACKLIST: string[]

  const STRICT_STRIP_REGEX: RegExp
  const LOO_STRIP_REGEX: RegExp

  export function verifierDigit(numbers: string): number

  export function format(cpf: string): string

  export function strip(cpf: string, isStrict: boolean): string

  export function isValid(cpf: string, isStrict: boolean): boolean

  export function generate(useFormat: boolean): string
}

just create a types.d.ts and add a "types" tag to package.json with .d.ts address.

dist/cpf.js import not working

When i import from dist/cpf.js the functions are not implemented, but if i import from dist/node.js it works.
Maybe that's a problem of your webpack.config.js

Erro Ionic 3

Olá fnando!
Estou tentando usar sua biblioteca com o Ionic 3, porém recebo o seguinte erro quando tento compilar:

Runtime Error: webpackJsonP is not defined

Ao remover as referências à sua biblioteca tudo funciona normalmente, será que poderia me ajudar?
Grato!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.