Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including @lacussoft/cpf-fmt with all npm packages installed. Try it out:

var cpfFmt = require("@lacussoft/cpf-fmt")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

@lacussoft/cpf-fmt v1.0.2

Basic function to validate CPF (Brazilian ID document).

LacusSoft :: cpf-fmt

npm version install size npm downloads build status

Basic function to generate valid CPF (Brazilian ID document).

Installation:

$ npm install @lacussoft/cpf-fmt

Import:

// ES6+ notation
import cpfFmt from '@lacussoft/cpf-fmt'

// Common JS
const cpfFmt = require('@lacussoft/cpf-fmt')

or import it through your HTML file, using CDN:

<script src="https://cdn.jsdelivr.net/npm/@lacussoft/cpf-fmt@latest/dist/cpf-fmt.min.js"></script>

Usage:

const cpf = '47844241055'
cpfFmt(cpf)       // returns '478.442.410-55'

cpfFmt(cpf, {     // returns '478.***.***-**'
  hidden: true
})

cpfFmt(cpf, {     // returns '478442410_55'
  delimiters: {
    dot: '',
    dash: '_'
  }
})

Formating options:

cpfFmt(cpf, {
  delimiters: {
    dot: '.',       // string to replace the dot characters
    dash: '-',      // string to replace the dash character
  },
  escape: false,    // boolean to define if the result should be HTML escaped
  hidden: false,    // boolean to define if digits should be hidden
  hiddenKey: '*',   // string to replace hidden digits
  hiddenRange: {
    start: 3,       // starting index of the numeric sequence to be hidden (min 0)
    end: 10,        // ending index of the numeric sequence to be hidden (max 10)
  },
  onFail(value) {   // fallback function to be invoked in case a non-11-digits is passed
    returnthrow new EvalError()
  }
})
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free