Comparing version 1.0.0 to 1.1.0
@@ -11,6 +11,3 @@ { | ||
"node": true | ||
}, | ||
"globals": { | ||
"define": false | ||
} | ||
} |
@@ -6,3 +6,2 @@ const babel = require('gulp-babel'); | ||
const gulp = require('gulp'); | ||
const hash = require('gulp-hash-filename'); | ||
const plumber = require('gulp-plumber'); | ||
@@ -14,3 +13,7 @@ const PrettyError = require('pretty-error'); | ||
const uglify = require('gulp-uglify'); | ||
const umd = require('gulp-umd'); | ||
const replace = require('gulp-replace'); | ||
// //////////////////////////////////////// | ||
const babelOptions = JSON.parse(fs.readFileSync('./.babelrc', 'utf8')); | ||
@@ -20,4 +23,7 @@ const env = process.env.NODE_ENV; | ||
const hashOptions = { | ||
format: '{name}.{hash:8}{ext}' | ||
const umdOptions = { | ||
dependencies: () => [], | ||
exports: () => 'CPF', | ||
namespace: () => 'CPF', | ||
templateName: 'amdNodeWeb', | ||
}; | ||
@@ -35,13 +41,15 @@ | ||
// //////////////////////////////////////// | ||
function production(done) { | ||
gulp.src('src/index.js') | ||
.pipe(plumber(plumberOptions)) | ||
.pipe(sourcemaps.init()) | ||
.pipe(replace(/export\s(default CPF;)?/g, '')) | ||
.pipe(babel(babelOptions)) | ||
.pipe(umd(umdOptions)) | ||
.pipe(rename('cpf.js')) | ||
.pipe(hash(hashOptions)) | ||
.pipe(gulp.dest('./dist')) | ||
.pipe(uglify()) | ||
.pipe(hash(hashOptions)) | ||
.pipe(rename({ suffix: '.min' })) | ||
@@ -58,3 +66,5 @@ .pipe(sourcemaps.write('.')) | ||
.pipe(sourcemaps.init()) | ||
.pipe(replace(/export\s(default CPF;)?/g, '')) | ||
.pipe(babel(babelOptions)) | ||
.pipe(umd(umdOptions)) | ||
.pipe(sourcemaps.write('.')) | ||
@@ -99,2 +109,4 @@ .pipe(browserSync.stream()) | ||
// //////////////////////////////////////// | ||
gulp.task('watch', watch); | ||
@@ -101,0 +113,0 @@ gulp.task('development', development); |
{ | ||
"name": "cpf-check", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Validador de CPF Moderno, Isomórfico e Super Leve.", | ||
@@ -9,5 +9,8 @@ "main": "dist/index.js", | ||
"start": "npm run clean && cross-env NODE_ENV=development gulp", | ||
"build": "npm run clean && cross-env NODE_ENV=production gulp", | ||
"cover": "npm test && nyc report -x ./src/index.spec.js --reporter=text-lcov | coveralls", | ||
"build": "npm run lint && npm test && npm run clean && cross-env NODE_ENV=production gulp", | ||
"lint": "eslint gulpfile.js && eslint src/index.js", | ||
"clean": "rimraf dist" | ||
"clean": "rimraf dist", | ||
"flow": "flow", | ||
"prepublish": "npm run build" | ||
}, | ||
@@ -34,3 +37,2 @@ "repository": { | ||
"babel-plugin-transform-flow-strip-types": "^6.22.0", | ||
"babel-preset-env": "^1.6.0", | ||
"babel-preset-es2015": "^6.24.1", | ||
@@ -40,2 +42,3 @@ "babel-preset-flow": "^6.23.0", | ||
"chai": "^4.1.0", | ||
"coveralls": "^2.13.1", | ||
"cross-env": "^5.0.2", | ||
@@ -46,2 +49,4 @@ "eslint": "^3.19.0", | ||
"eslint-plugin-import": "^2.7.0", | ||
"flow-bin": "^0.52.0", | ||
"flow-remove-types": "^1.2.1", | ||
"gulp": "^3.9.1", | ||
@@ -53,12 +58,18 @@ "gulp-babel": "^6.1.2", | ||
"gulp-rename": "^1.2.2", | ||
"gulp-replace": "^0.6.1", | ||
"gulp-sourcemaps": "^2.6.0", | ||
"gulp-uglify": "^3.0.0", | ||
"gulp-umd": "^0.2.1", | ||
"mocha": "^3.5.0", | ||
"nyc": "^11.1.0", | ||
"pretty-error": "^2.1.1", | ||
"rewire": "^2.5.2", | ||
"rimraf": "^2.6.1", | ||
"run-sequence": "^2.1.0" | ||
}, | ||
"dependencies": { | ||
"pretty-error": "^2.1.1" | ||
"nyc": { | ||
"include": [ | ||
"src/index.js" | ||
] | ||
} | ||
} |
@@ -1,1 +0,68 @@ | ||
# cpf-check | ||
# cpf-check | ||
CPF Validator, Generator e Formatter, runs both on browser and server-side. Incredibly Small 2.2KB (890bytes gziped!!). | ||
[](https://travis-ci.org/flasd/cpf-check) [](https://coveralls.io/github/flasd/cpf-check?branch=master) [](https://www.npmjs.com/package/cpf-check) | ||
### Instalation | ||
Add the latest version of cpf-check to your `package.json`. | ||
``` | ||
npm install cpf-check --save | ||
``` | ||
Then you can use it in your index.html | ||
```html | ||
<script type="text/javascript" src="./node_modules/cpf-check/dist/cpf.min.js"></script> | ||
``` | ||
Or, you can import it in your module bundler of choice: | ||
```javascript | ||
const CPF = require('cpf-check'); | ||
// or | ||
import CPF from 'cpf-check'; | ||
``` | ||
## Usage & API | ||
### CPF(String): Boolean | ||
The main export is a function that validates CPF's. | ||
```javascript | ||
const CPF = require('cpf-check') | ||
const myCpf = '676.754.677-10'; | ||
CPF(myCpf); | ||
// true | ||
CPF.validate(myCpf); | ||
// true | ||
``` | ||
The `CPF.validate()` is an alias for the `CPF()` method. | ||
### CPF.generate(): String | ||
```javascript | ||
CPF.generate(); | ||
// '676.754.677-10' | ||
``` | ||
### CPF.parse(String): String | ||
```javascript | ||
const myText = 'Hey, my CPF is 676.754.677-10'; | ||
CPF.parse(myText); | ||
// '676.754.677-10' | ||
``` | ||
### CPF.format(String): String | ||
```javascript | ||
const myCpf = '67675467710'; | ||
CPF.format(myCpf); | ||
// '676.754.677-10' | ||
``` | ||
### CPF.strip(String): String | ||
```javascript | ||
const myCpf = '676.754.677-10'; | ||
CPF.strip(myCpf); | ||
// '67675467710' | ||
``` | ||
### Copyright and License | ||
Copyright (c) 2017 [Marcel Coelho](https://github.com/flasd) under the [MIT license](https://github.com/flasd/cpf-check/blob/master/LICENSE.md) |
326
src/index.js
// @flow | ||
(() => { | ||
/** | ||
* Determines if given input is a string. | ||
* | ||
* @param {Any} stg The input. | ||
* @return {boolean} True if string, False otherwise. | ||
*/ | ||
function isString(stg: any): Boolean { | ||
// failproffish check | ||
return stg.constructor.toString().indexOf('String') !== -1; | ||
} | ||
/** | ||
* Determines if given input is a string. | ||
* | ||
* @param {Any} stg The input. | ||
* @return {boolean} True if string, False otherwise. | ||
*/ | ||
export function isString(stg: any): Boolean { | ||
// failproffish check | ||
return stg.constructor.toString().indexOf('String') !== -1; | ||
} | ||
/** | ||
* Returns a random number inside the min-max window. | ||
* | ||
* @param {number} max The maximum number. | ||
* @param {number} min The minimum number. | ||
* @return {number} the random number. | ||
*/ | ||
function random(max: Number, min: Number): Number { | ||
// Round given range numbers | ||
const rMin = Math.ceil(min); | ||
const rMax = Math.floor(max); | ||
/** | ||
* Returns a random number inside the min-max window. | ||
* | ||
* @param {number} max The maximum number. | ||
* @param {number} min The minimum number. | ||
* @return {number} the random number. | ||
*/ | ||
export function random(max: Number, min: Number): Number { | ||
// Round given range numbers | ||
const rMin = Math.ceil(min); | ||
const rMax = Math.floor(max); | ||
// Generates a pseudo-random number with the given range | ||
return Math.floor(Math.random() * (rMax - (rMin + 1))) + rMin; | ||
// Generates a pseudo-random number with the given range | ||
return Math.floor(Math.random() * (rMax - (rMin + 1))) + rMin; | ||
} | ||
/** | ||
* Parse a CPF from any text. For example 'this is my cpf 000.000.000-00' | ||
* will return '000.000.000-00'. | ||
* | ||
* @param {string} raw The raw text string. | ||
* @return {string} the found CPF or an empty string. | ||
*/ | ||
export function parse(raw: String): String { | ||
if (!isString(raw)) { | ||
throw new Error(`CPF.parse Error\nExpected String but instead got ${typeof raw}`); | ||
} | ||
// Extracts all cpf matches from an text string | ||
const matches = raw.match(/\d{3}(.|-)?\d{3}(.|-)?\d{3}(.|-)?\d{2}/); | ||
/** | ||
* Parse a CPF from any text. For example 'this is my cpf 000.000.000-00' | ||
* will return '000.000.000-00'. | ||
* | ||
* @param {string} raw The raw text string. | ||
* @return {string} the found CPF or an empty string. | ||
*/ | ||
function parse(raw: String): String { | ||
// Extracts all cpf matches from an text string | ||
const matches = raw.match(/\d{3}(.|-)?\d{3}(.|-)?\d{3}(.|-)?\d{2}/); | ||
// If no matches | ||
if (matches === null) { | ||
return ''; | ||
} | ||
// If no matches | ||
if (matches === null) { | ||
return ''; | ||
} | ||
// Return the first match | ||
return matches[0]; | ||
} | ||
// Return the first match | ||
return matches[0]; | ||
/** | ||
* Strip '-', '.', and anything that is not a digit from the provided input. | ||
* | ||
* @param {string} raw The text input. | ||
* @return {string} Striped down input. | ||
*/ | ||
export function strip(raw: String): String { | ||
if (!isString(raw)) { | ||
throw new Error(`CPF.strip Error\nExpected String but instead got ${typeof raw}`); | ||
} | ||
/** | ||
* Strip '-', '.', and anything that is not a digit from the provided input. | ||
* | ||
* @param {string} raw The text input. | ||
* @return {string} Striped down input. | ||
*/ | ||
function strip(raw: String): String { | ||
return raw.replace(/[^\d]/g, '').trim(); | ||
return raw.replace(/[^\d]/g, '').trim(); | ||
} | ||
/** | ||
* Format a given cpf. For example '00000000000' will result in | ||
* '000.000.000-00'. | ||
* | ||
* @param {string} raw The unformated CPF. | ||
* @return {string} The formated CPF. | ||
*/ | ||
export function format(raw: String): String { | ||
if (!isString(raw)) { | ||
throw new Error(`CPF.format Error\nExpected String but instead got ${typeof raw}`); | ||
} | ||
/** | ||
* Format a given cpf. For example '00000000000' will result in | ||
* '000.000.000-00'. | ||
* | ||
* @param {string} raw The unformated CPF. | ||
* @return {string} The formated CPF. | ||
*/ | ||
function format(raw: String): String { | ||
const regex = /^(\d{3})(\d{3})(\d{3})(\d{2})$/; | ||
return strip(parse(raw)).replace(regex, '$1.$2.$3-$4'); | ||
const regex = /^(\d{3})(\d{3})(\d{3})(\d{2})$/; | ||
return strip(parse(raw)).replace(regex, '$1.$2.$3-$4'); | ||
} | ||
/** | ||
* Transforms a given CPF string into an Array of the cpfs numbers. | ||
* | ||
* @param {string} raw The raw cpf string. | ||
* @return {Array} CPF parsed numbers in an array. | ||
*/ | ||
export function transform(raw: String): Array { | ||
if (!isString(raw)) { | ||
throw new Error(`CPF.format Error\nExpected String but instead got ${typeof raw}`); | ||
} | ||
/** | ||
* Transforms a given CPF string into an Array of the cpfs numbers. | ||
* | ||
* @param {string} raw The raw cpf string. | ||
* @return {Array} CPF parsed numbers in an array. | ||
*/ | ||
function transform(raw: String): Array { | ||
// Transform input into array and parse the numbers | ||
const digits = raw.split('').map(digit => parseInt(digit, 10)); | ||
// Transform input into array and parse the numbers | ||
const digits = raw.split('').map(digit => parseInt(digit, 10)); | ||
// if the input did't contain a CPF, parseInt will return NaN, so | ||
// we check for this. | ||
for (let i = digits.length - 1; i >= 0; i -= 1) { | ||
if (Number.isNaN(digits[i])) { | ||
return []; | ||
} | ||
// if the input did't contain a CPF, parseInt will return NaN, so | ||
// we check for this. | ||
for (let i = digits.length - 1; i >= 0; i -= 1) { | ||
if (Number.isNaN(digits[i])) { | ||
throw new Error(`CPF.transform Error\nExpected digits only string but instead got ${raw}`); | ||
} | ||
return digits; | ||
} | ||
/** | ||
* Returns the verifier digit for a given input. | ||
* | ||
* @param {Array} digits The CPF digits. | ||
* @return {Number} The verifier digit. | ||
*/ | ||
function checkSum(digits: Array): Array { | ||
const size = digits.length + 1; | ||
return digits; | ||
} | ||
// Do some magic. JK. forEach number, we multiply it by the array size | ||
// plus one (10 or 11 if cpf is valid). Than we sum all the indexes | ||
const sum = digits | ||
.map((number, index) => number * (size - index)) | ||
.reduce((total, number) => total + number); | ||
// We then multiply by 10 and get the remainder of dividing by 11. | ||
const remainder = (sum * 10) % 11; | ||
// If the remainder is 10 or 11, return 0, else return the remainder. | ||
return remainder > 9 ? 0 : remainder; | ||
/** | ||
* Returns the verifier digit for a given input. | ||
* | ||
* @param {Array} digits The CPF digits. | ||
* @return {Number} The verifier digit. | ||
*/ | ||
export function checkSum(digits: Array): Number { | ||
if (!Array.isArray(digits)) { | ||
throw new Error(`CPF.checkSum Error\nExpected digits to be an array but instead got ${typeof digits}`); | ||
} | ||
/** | ||
* Validates a given CPF. | ||
* | ||
* @param {string} raw The raw cpf string, it can be dirty, like | ||
* 'my cpf is 000.000.000-00'. | ||
* @return {boolean} True if valid, False otherwise. | ||
*/ | ||
function validate(raw: String): Boolean { | ||
if (!isString(raw)) { | ||
throw new Error(`Expected CPF to be a string, instead got ${typeof raw}`); | ||
} | ||
const size = digits.length + 1; | ||
// Get the Array<Number> for the CPF's digits | ||
const digits = transform(strip(parse(raw))); | ||
// Do some magic. JK. forEach number, we multiply it by the array size | ||
// plus one (10 or 11 if cpf is valid). Than we sum all the indexes | ||
const sum = digits | ||
.map((number, index) => number * (size - index)) | ||
.reduce((total, number) => total + number); | ||
// If length is not 11, CPF is not valid! | ||
if (digits.length !== 11) { | ||
return false; | ||
} | ||
// We then multiply by 10 and get the remainder of dividing by 11. | ||
const remainder = (sum * 10) % 11; | ||
// We extract the verifier digits from the CPF digits | ||
const verifiers = digits.slice(9, 11); | ||
// If the remainder is 10 or 11, return 0, else return the remainder. | ||
return remainder > 9 ? 0 : remainder; | ||
} | ||
// We compute the correct verifiers based on the 9 first digits | ||
const first = checkSum(digits.slice(0, 9)); | ||
const second = checkSum(digits.slice(0, 9).concat([first])); | ||
/** | ||
* Validates a given CPF. | ||
* | ||
* @param {string} raw The raw cpf string, it can be dirty, like | ||
* 'my cpf is 000.000.000-00'. | ||
* @return {boolean} True if valid, False otherwise. | ||
*/ | ||
export function validate(raw: String): Boolean { | ||
if (!isString(raw)) { | ||
throw new Error(`CPF.validate Error\nExpected CPF to be a string, instead got ${typeof raw}`); | ||
} | ||
// We check if the provided verifiers match the computed ones | ||
if (verifiers[0] === first && verifiers[1] === second) { | ||
return true; | ||
} | ||
// Get the Array<Number> for the CPF's digits | ||
const digits = transform(strip(parse(raw))); | ||
// If length is not 11, CPF is not valid! | ||
if (digits.length !== 11) { | ||
return false; | ||
} | ||
/** | ||
* Generates a given CPF | ||
* | ||
* @return {String} The generated CPF | ||
*/ | ||
function generate(): String { | ||
let randomNum = ''; | ||
// We extract the verifier digits from the CPF digits | ||
const verifiers = digits.slice(9, 11); | ||
// We generate the first nine digits randomly | ||
for (let i = 0; i < 9; i += 1) { | ||
randomNum = randomNum.concat(random(9, 1).toString(10)); | ||
} | ||
// We compute the correct verifiers based on the 9 first digits | ||
const first = checkSum(digits.slice(0, 9)); | ||
const second = checkSum(digits.slice(0, 9).concat([first])); | ||
// We transform the random digits into an Array<Number> of the digits. | ||
const digits = transform(randomNum); | ||
// We check if the provided verifiers match the computed ones | ||
if (verifiers[0] === first && verifiers[1] === second) { | ||
return true; | ||
} | ||
// Generate the verifiers based on the random digits | ||
const first = checkSum(digits.slice(0, 9)); | ||
const second = checkSum(digits.slice(0, 9).concat([first])); | ||
return false; | ||
} | ||
// Return a formated version | ||
return format(`${digits.join('')}${first}${second}`); | ||
/** | ||
* Generates a given CPF | ||
* | ||
* @return {String} The generated CPF | ||
*/ | ||
export function generate(): String { | ||
let randomNum = ''; | ||
// We generate the first nine digits randomly | ||
for (let i = 0; i < 9; i += 1) { | ||
randomNum = randomNum.concat(random(9, 1).toString(10)); | ||
} | ||
const CPF = validate; | ||
// We transform the random digits into an Array<Number> of the digits. | ||
const digits = transform(randomNum); | ||
// Export the API | ||
CPF.parse = parse; | ||
CPF.strip = strip; | ||
CPF.format = format; | ||
CPF.checkSum = checkSum; | ||
CPF.transform = transform; | ||
CPF.validate = validate; | ||
CPF.generate = generate; | ||
// Generate the verifiers based on the random digits | ||
const first = checkSum(digits.slice(0, 9)); | ||
const second = checkSum(digits.slice(0, 9).concat([first])); | ||
// This supports AMD modules | ||
if (typeof define === 'function' && define.amd) { | ||
define(() => CPF); | ||
} else if (typeof exports !== 'undefined') { | ||
// This supports CommonJs and Note modules | ||
if (typeof module !== 'undefined' && module.exports) { | ||
exports = CPF; | ||
module.exports = CPF; | ||
} | ||
// But always support CommonJS module 1.1.1 spec (`exports` cannot be a function) | ||
exports.CPF = CPF; | ||
} else if (typeof global !== 'undefined') { | ||
// eslint-disable-next-line no-param-reassign | ||
global.CPF = CPF; | ||
} | ||
})(); | ||
// Return a formated version | ||
return format(`${digits.join('')}${first}${second}`); | ||
} | ||
// We define the exported API | ||
const CPF = validate; | ||
CPF.parse = parse; | ||
CPF.strip = strip; | ||
CPF.format = format; | ||
CPF.generate = generate; | ||
export default CPF; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
37877
0
15
599
68
32
1
- Removedpretty-error@^2.1.1
- Removedansi-regex@2.1.1(transitive)
- Removedboolbase@1.0.0(transitive)
- Removedcss-select@4.3.0(transitive)
- Removedcss-what@6.1.0(transitive)
- Removeddom-converter@0.2.0(transitive)
- Removeddom-serializer@1.4.1(transitive)
- Removeddomelementtype@2.3.0(transitive)
- Removeddomhandler@4.3.1(transitive)
- Removeddomutils@2.8.0(transitive)
- Removedentities@2.2.0(transitive)
- Removedhtmlparser2@6.1.0(transitive)
- Removedlodash@4.17.21(transitive)
- Removednth-check@2.1.1(transitive)
- Removedpretty-error@2.1.2(transitive)
- Removedrenderkid@2.0.7(transitive)
- Removedstrip-ansi@3.0.1(transitive)
- Removedutila@0.4.0(transitive)