Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added isTaxId #1336

Merged
merged 2 commits into from
Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ Validator | Description
**isSurrogatePair(str)** | check if the string contains any surrogate pairs chars.
**isUppercase(str)** | check if the string is uppercase.
**isSlug** | Check if the string is of type slug. `Options` allow a single hyphen between string. e.g. [`cn-cn`, `cn-c-c`]
**isTaxID(str, locale)** | Check if the given value is a valid Tax Identification Number. Default locale is `en-US`
**isURL(str [, options])** | check if the string is an URL.<br/><br/>`options` is an object which defaults to `{ protocols: ['http','https','ftp'], require_tld: true, require_protocol: false, require_host: true, require_valid_protocol: true, allow_underscores: false, host_whitelist: false, host_blacklist: false, allow_trailing_dot: false, allow_protocol_relative_urls: false, disallow_auth: false }`.<br/><br/>require_protocol - if set as true isURL will return false if protocol is not present in the URL.<br/>require_valid_protocol - isURL will check if the URL's protocol is present in the protocols option.<br/>protocols - valid protocols can be modified with this option.<br/>require_host - if set as false isURL will not check if host is present in the URL.<br/>allow_protocol_relative_urls - if set as true protocol relative URLs will be allowed.
**isUUID(str [, version])** | check if the string is a UUID (version 3, 4 or 5).
**isVariableWidth(str)** | check if the string contains a mixture of full and half-width chars.
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import isEAN from './lib/isEAN';
import isISIN from './lib/isISIN';
import isISBN from './lib/isISBN';
import isISSN from './lib/isISSN';
import isTaxID from './lib/isTaxID';

import isMobilePhone, { locales as isMobilePhoneLocales } from './lib/isMobilePhone';

Expand Down Expand Up @@ -207,6 +208,7 @@ const validator = {
normalizeEmail,
toString,
isSlug,
isTaxID,
isDate,
};

Expand Down
68 changes: 68 additions & 0 deletions src/lib/isTaxID.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import assertString from './util/assertString';

/**
* 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.
*/


/**
* Campus prefixes according to locales
*/

const campusPrefix = {
'en-US': {
andover: ['10', '12'],
atlanta: ['60', '67'],
austin: ['50', '53'],
brookhaven: ['01', '02', '03', '04', '05', '06', '11', '13', '14', '16', '21', '22', '23', '25', '34', '51', '52', '54', '55', '56', '57', '58', '59', '65'],
cincinnati: ['30', '32', '35', '36', '37', '38', '61'],
fresno: ['15', '24'],
internet: ['20', '26', '27', '45', '46', '47'],
kansas: ['40', '44'],
memphis: ['94', '95'],
ogden: ['80', '90'],
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'],
},
};


function getPrefixes(locale) {
const prefixes = [];

for (const location in campusPrefix[locale]) {
if (campusPrefix[locale].hasOwnProperty(location)) {
prefixes.push(...campusPrefix[locale][location]);
}
}

prefixes.sort();

return prefixes;
}

// tax id regex formats for various loacles

const taxIdFormat = {

'en-US': /^\d{2}[- ]{0,1}\d{7}$/,

};


export default function isTaxID(str, locale = 'en-US') {
assertString(str);
if (!taxIdFormat[locale].test(str)) {
return false;
}
return getPrefixes(locale).indexOf(str.substr(0, 2)) !== -1;
}

17 changes: 17 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -8357,6 +8357,23 @@ describe('Validators', () => {
});
});


it('should validate taxID', () => {
test({
validator: 'isTaxID',
valid: [
'01-1234567',
'01 1234567',
'011234567'],
invalid: [
'0-11234567',
'01#1234567',
'01 1234567',
'01 1234 567'],
});
});


it('should validate slug', () => {
test({
validator: 'isSlug',
Expand Down