Skip to content

Commit

Permalink
Update algorithm to accept formatted and non formatted numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
Francisco Cardoso authored and Rui Marinho committed Apr 13, 2016
1 parent 566768d commit e9ed0bd
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 130 deletions.
1 change: 0 additions & 1 deletion .eslintrc

This file was deleted.

1 change: 1 addition & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extends: seegno
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
coverage
node_modules
3 changes: 0 additions & 3 deletions .jscsrc
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
plugins:
- 'jscs-config-seegno'

preset: seegno
9 changes: 6 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
language: node_js
language: bash

node_js:
- 4
script:
- docker-compose run --rm sut

services:
- docker
8 changes: 4 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Changelog

## [0.0.2](https://github.com/seegno/is-valid-ein/tree/0.0.2) (2016-02-17)
[Full Changelog](https://github.com/seegno/is-valid-ein/compare/0.0.1...0.0.2)
## [0.0.2](https://github.com/seegno/ein-validator/tree/0.0.2) (2016-02-17)
[Full Changelog](https://github.com/seegno/ein-validator/compare/0.0.1...0.0.2)

**Merged pull requests:**

- Update `es2015-node4` to `es2015` [\#3](https://github.com/seegno/is-valid-ein/pull/3) ([ruipenso](https://github.com/ruipenso))
- Update `es2015-node4` to `es2015` [\#3](https://github.com/seegno/ein-validator/pull/3) ([ruipenso](https://github.com/ruipenso))

## [0.0.1](https://github.com/seegno/is-valid-ein/tree/0.0.1) (2016-02-05)
## [0.0.1](https://github.com/seegno/ein-validator/tree/0.0.1) (2016-02-05)
92 changes: 71 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,44 +1,94 @@
# Employer Identification Number (EIN)
# ein-validator
Validate and mask a U.S. Employer Identification Number (EIN).

[![npm version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]

This modules allows you to check if a number is a valid.
## Status
[![npm version][npm-image]][npm-url] [![build status][travis-image]][travis-url]

## Installation
Install the package via `npm`:

Choose your preferred method:

* npm: `npm install --save is-valid-ein`
* Download: [is-valid-ein](https://github.com/seegno/is-valid-ein)
```sh
npm install ein-validator --save
```

## Usage
### `isValid(value)`
This method validates if the given value is a valid `Employer Identification Number`.

#### Arguments
1. `value` _(*)_: The value to validate.

*NOTE:* The input number **must not** be formated to `xxx-xxxxxx`.
#### Returns
_(boolean)_: Returns whether the input value is a valid EIN or not.

> Check if number is valid.
#### Example

```js
import isValidEin from 'is-valid-ein';
isValid({});
// => false

isValid('0112345-67');
// => false

isValidEin('xxxxxxxxx');
isValid('01-1234567');
// => true

isValid('011234567');
// => true
```

> Mask the number.
--------------------------------------------------------------------------------

### `mask(value)`
This method will help you protect this sensitive piece of information by obfuscating some digits.

#### Arguments
1. `value` _(*)_: The value to mask.

#### Returns
_(string)_: Returns the masked value by replacing value certain digits by 'X'.

#### Example

```js
import { mask } from 'is-valid-ein';
mask({});
// Throws an Error.

mask('0112345-67');
// Throws an Error.

mask('xxxxxxxxx');
mask('01-1234567');
// => XX-XXX0000

mask('011234567');
// => XXXXX4567
```

## Running tests
--------------------------------------------------------------------------------

## Tests
To test using a local installation of `node.js`:

```sh
npm test
```

[npm-image]: https://img.shields.io/npm/v/is-valid-ein.svg?style=flat-square
[npm-url]: https://npmjs.org/package/is-valid-ein
[travis-image]: https://img.shields.io/travis/seegno/is-valid-ein.svg?style=flat-square
[travis-url]: https://travis-ci.org/seegno/is-valid-ein
To test using Docker exclusively:

```sh
docker-compose run --rm sut
```

## Release

```sh
npm version [<newversion> | major | minor | patch] -m "Release %s"
```

## License
MIT

[npm-image]: https://img.shields.io/npm/v/ein-validator.svg?style=flat-square
[npm-url]: https://npmjs.org/package/ein-validator
[travis-image]: https://img.shields.io/travis/seegno/ein-validator.svg?style=flat-square
[travis-url]: https://img.shields.io/travis/seegno/ein-validator.svg?style=flat-square
4 changes: 4 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sut:
image: seegno/node:4-test
volumes:
- .:/app
78 changes: 47 additions & 31 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,43 +1,59 @@
{
"name": "is-valid-ein",
"name": "ein-validator",
"version": "0.0.2",
"description": "Validate Employer Identification Number",
"main": "dist/index.js",
"homepage": "https://github.com/seegno/is-valid-ein",
"bugs": "https://github.com/seegno/is-valid-ein/issues",
"repository": "seegno/is-valid-ein",
"author": "Seegno",
"license": "MIT",
"description": "Employer Identification Number validator and masker",
"keywords": [
"EIN",
"IRS",
"TIN"
"TIN",
"US",
"USA",
"identification",
"taxpayer",
"validator"
],
"options": {
"mocha": "--compilers js:babel-register --recursive --require should"
"homepage": "https://github.com/seegno/ein-validator",
"bugs": "https://github.com/seegno/ein-validator/issues",
"license": "MIT",
"author": "Seegno",
"main": "dist/src",
"repository": {
"type": "git",
"url": "https://github.com/seegno/ein-validator.git"
},
"scripts": {
"build": "rm -rf dist/* && babel --copy-files src/ --out-dir dist/",
"changelog": "github_changelog_generator --no-issues",
"changelog": "github_changelog_generator --no-issues --header-label='# Changelog' --future-release=v$npm_config_future_release && sed -i '' -e :a -e '$d;N;2,4ba' -e 'P;D' CHANGELOG.md",
"coverage": "babel-node node_modules/.bin/isparta cover --report html _mocha -- $npm_package_options_mocha",
"lint": "eslint src test && jscs src test",
"prepublish": "npm run lint && npm test && npm run build",
"test": "NODE_ENV=test mocha $npm_package_options_mocha"
},
"dependencies": {
"lodash": "3.10.1"
"prepublish": "npm run transpile",
"test": "mocha $npm_package_options_mocha",
"testdocker": "docker-compose run --rm sut",
"transpile": "rm -rf dist/* && babel src --out-dir dist/src",
"version": "npm run changelog --future-release=$npm_package_version && npm run transpile && git add -A CHANGELOG.md dist"
},
"devDependencies": {
"babel-cli": "^6.4.5",
"babel-eslint": "5.0.0-beta9",
"babel-preset-es2015": "6.5.0",
"babel-register": "6.4.3",
"coveralls": "2.11.6",
"eslint": "1.8.0",
"eslint-config-seegno": "2.0.0",
"eslint-plugin-babel": "3.1.0",
"jscs": "2.9.0",
"jscs-config-seegno": "1.1.0",
"mocha": "2.4.5",
"should": "8.2.1"
}
"babel-cli": "^6.4.0",
"babel-eslint": "^6.0.2",
"babel-preset-es2015": "^6.5.0",
"babel-register": "^6.3.13",
"eslint": "^2.7.0",
"eslint-config-seegno": "^4.0.0",
"eslint-plugin-babel": "^3.0.0",
"eslint-plugin-sort-class-members": "^1.0.1",
"isparta": "^4.0.0",
"jscs": "^2.11.0",
"jscs-config-seegno": "^2.0.0",
"mocha": "^2.3.4",
"pre-commit": "^1.1.2",
"should": "^8.0.0"
},
"engines": {
"node": ">=0.10"
},
"options": {
"mocha": "--compilers js:babel-register --recursive --require should"
},
"pre-commit": [
"lint"
]
}
53 changes: 28 additions & 25 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@

/**
* See `http://www.irs.gov/Businesses/Small-Businesses-&-Self-Employed/How-EINs-are-Assigned-and-Valid-EIN-Prefixes` for more information.
*
* An Employer Identification Number (EIN), also known as a Federal Tax Identification Number, is used to identify a business entity.
*
* NOTES:
*
* - Prefix 47 is being reserved for future use
* - Prefixes 26, 27, 45, 46 and 47 were previously assigned by the Philadelphia campus.
*
* See `http://www.irs.gov/Businesses/Small-Businesses-&-Self-Employed/How-EINs-are-Assigned-and-Valid-EIN-Prefixes` for more information.
*/

/**
* Module dependencies.
*/

import _ from 'lodash';

/**
* Campus prefixes.
*/
Expand All @@ -34,38 +27,48 @@ const campus = {
philadelphia: ['33', '39', '41', '42', '43', '46', '48', '62', '63', '64', '66', '68', '71', '72', '73', '74', '75', '76', '77', '81', '82', '83', '84', '85', '86', '87', '88', '91', '92', '93', '98', '99'],
sba: ['31']
};
const prefixes = _.flatten(_.valuesIn(campus));
const expression = /^(\d{9})$/;

/**
* Cache all available prefixes.
*/

const prefixes = [];

for (const location in campus) {
prefixes.push(...campus[location]);
}

prefixes.sort();

/**
* Expression.
*/

const expression = /^\d{2}[- ]{0,1}\d{7}$/;

/**
* Validate function.
*/

function isValid(ein) {
if (!expression.test(ein)) {
export function isValid(value) {
if (!expression.test(value)) {
return false;
}

return _.includes(prefixes, ein.substr(0, 2));
return prefixes.indexOf(value.substr(0, 2)) !== -1;
}

/**
* Masks the EIN with "X" placeholders to protect sensitive data,
* Mask the EIN with "X" placeholders to protect sensitive data,
* while keeping some of the original digits for contextual recognition.
*
* E.g. "123456789" -> "XXXXX6789"
* E.g. "123456789" -> "XXXXX6789", "12-3456789" -> "XX-XXX6789".
*/

export function mask(ein) {
if (!isValid(ein)) {
export function mask(value) {
if (!isValid(value)) {
throw new Error('Invalid Employer Identification Number');
}

return ein.substr(0, ein.length - 4).replace(/[\w]/g, 'X') + ein.substr(-4);
return `${value.substr(0, value.length - 4).replace(/[\w]/g, 'X')}${value.substr(-4)}`;
}

/**
* Export default.
*/

export { isValid as default };
Loading

0 comments on commit e9ed0bd

Please sign in to comment.