diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..47fe10e --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,7 @@ +# Change Log + +## v0.3.0 + +1. added Phone to verifications (IR only for now) +2. added UK to NationalID locales support +3. transformed json files info js variables (no more json-loader, yay!) \ No newline at end of file diff --git a/README.md b/README.md index 4c3e279..cdc15ba 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Verifications -A **TypeScript Ready** universal validation library for developers +A **TypeScript Ready** universal verification library (Server-Side and Client-Side) for developers. [![npm](https://img.shields.io/npm/v/verifications.svg)](https://www.npmjs.com/package/verifications) [![npm](https://img.shields.io/npm/dm/verifications.svg)](https://www.npmjs.com/package/verifications) @@ -9,85 +9,100 @@ A **TypeScript Ready** universal validation library for developers [TOC] -### Installation +## Installation `npm i -s verifications` -### Usage +## Usage -#### require / import +### require / import ```javascript -const Verifications = require('verifications').default; -// or +// Node.js : +const Verifications = require('verifications'); +// ES6 : import Verifications from 'verifications'; +// Typescript : +import * as Verifications from 'verifications'; ``` -#### Available Methods +### Available Methods -- NationalID - (static method) +- NationalID - verify(code: string, locale?: string): boolean -- CreditCard - (static method) +- CreditCard - verify(code: string): boolean - type(code: string): string | undefined - issuer(code: string): { name: string, alias: string, website: string } | undefined - identify(code: string): { type: Type, issuer: Issuer } | undefined +- Phone + - identify(number: string): identity: { [key: string]: any } | undefined + - country(number: string): { name: string, alias: string } | undefined + - fancy(number: string): string + - normalize(number: string): string -##### NationalID +#### NationalID -returns true if the code matches any supported format +##### Verify ```javascript -var isNationalIDValid = Verifications - .NationalID - .verify('xxx-xxxxxx-x'); +Verifications + .NationalID + .verify('xxx-xxxxxx-x'); +// returns true if the code matches any supported format ``` you can also enforce the locale ```javascript -isNationalIdValid = Verifications - .NationalID - .verify('xxx-xx-xxxx', 'US'); +Verifications + .NationalID + .verify('xxx-xx-xxxx', 'US'); ``` **Supported Locales:** - Iran (IR) - کد ملی -- United States (US) - Social Security Number +- United States (US) - Social Security Number (SSN) +- United Kingdom (UK) - National Insurance Number (NINO) -##### CreditCard +#### CreditCard -returns true if the code matches any supported format + +##### Verify ```javascript -var isCreditCardValid = Verifications - .CreditCard - .verify('xxxx-xxxx-xxxx-xxxx'); +Verifications + .CreditCard + .verify('xxxx-xxxx-xxxx-xxxx'); +// returns true if the code matches any supported format ``` -returns identity of the card/issuer +##### Identify ```javascript -var isCreditCardValid = Verifications - .CreditCard - .identify('xxxx-xxxx-xxxx-xxxx'); +Verifications + .CreditCard + .identify('xxxx-xxxx-xxxx-xxxx'); +// returns identity of the card/issuer ``` -returns type of the card +##### Type ```javascript -var isCreditCardValid = Verifications - .CreditCard - .type('xxxx-xxxx-xxxx-xxxx'); +Verifications + .CreditCard + .type('xxxx-xxxx-xxxx-xxxx'); +// returns type of the card ``` -returns issuer of the card +##### Issuer ```javascript -var isCreditCardValid = Verifications - .CreditCard - .issuer('xxxx-xxxx-xxxx-xxxx'); +Verifications + .CreditCard + .issuer('xxxx-xxxx-xxxx-xxxx'); +// returns issuer of the card ``` **Luhn verification algorithm (almost all credit cards around the globe)** @@ -95,4 +110,40 @@ var isCreditCardValid = Verifications - 9 card/issuer types - 46 active issuers are supported -***** +#### Phone + +##### Identify + +```javascript +Verifications + .Phone + .identify('+xx (xxx) xxx xxxx'); +// returns the identity of the number or undefined +``` + +##### Country + +```javascript +Verifications + .Phone + .country('+xx (xxx) xxx xxxx'); +// returns the country of the number or undefined +``` + +##### Fancy + +```javascript +Verifications + .Phone + .fancy('00xx xx x xxx x x xx'); +// returns the beautified format if supported or the given number +``` + +##### Normalize + +```javascript +Verifications + .Phone + .normalize('+xx (xxx) xxx xxxx'); +// returns numbers only -> xxxxxxxxxxxx +``` diff --git a/package.json b/package.json index c7814d6..4310654 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,10 @@ { "name": "verifications", - "version": "0.2.1", + "version": "0.3.0", "description": "universal verification library for developers", "main": "dist/index.js", "scripts": { - "prepare": "./scripts/prepare.js", + "prepare": "node ./scripts/prepare.js", "build": "tsc", "test": "echo \"Error: no test specified\" && exit 1" }, diff --git a/src/CreditCard/CreditCard.ts b/src/CreditCard/CreditCard.ts index e6f2f07..98c6d3c 100644 --- a/src/CreditCard/CreditCard.ts +++ b/src/CreditCard/CreditCard.ts @@ -1,3 +1,27 @@ +// +// MIT License +// +// Copyright (c) 2017 Ardalan Amini +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// + import * as issuers from './issuers.json' import * as types from './types.json' import { range } from '../utils' diff --git a/src/CreditCard/index.ts b/src/CreditCard/index.ts index 62777a9..a073f83 100644 --- a/src/CreditCard/index.ts +++ b/src/CreditCard/index.ts @@ -1 +1,25 @@ +// +// MIT License +// +// Copyright (c) 2017 Ardalan Amini +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// + export { default } from './CreditCard' diff --git a/src/NationalID/IR.ts b/src/NationalID/IR.ts index b942f50..672cd02 100644 --- a/src/NationalID/IR.ts +++ b/src/NationalID/IR.ts @@ -1,3 +1,29 @@ +// +// MIT License +// +// Copyright (c) 2017 Ardalan Amini +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// + +// National Identification Number (کد ملی) + export function verify(code: string) { code = code.replace(/\D/g, '') // just in case diff --git a/src/NationalID/NationalID.ts b/src/NationalID/NationalID.ts index a73f99d..0df49b3 100644 --- a/src/NationalID/NationalID.ts +++ b/src/NationalID/NationalID.ts @@ -1,11 +1,38 @@ +// +// MIT License +// +// Copyright (c) 2017 Ardalan Amini +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// + import * as IR from './IR' +import * as UK from './UK' import * as US from './US' const locales: { [key: string]: any } = { IR, + UK, US } +// @see https://en.wikipedia.org/wiki/National_identification_number export default class NationalId { static get locales() { return Object.keys(locales) diff --git a/src/NationalID/UK.ts b/src/NationalID/UK.ts new file mode 100644 index 0000000..48a41ba --- /dev/null +++ b/src/NationalID/UK.ts @@ -0,0 +1,39 @@ +// +// MIT License +// +// Copyright (c) 2017 Ardalan Amini +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// + +// National Insurance Number - NI Number (NINO) + +export function verify(code: string) { + code = code.replace(/_|\W/g, '') // just in case + + if (code.length !== 9) return false + + code = code.toUpperCase() + + if (!/^[ABCEGHJKLMNOPRSTWXYZ]{1}[ABCEGHJKLMNPRSTWXYZ]{1}\d{6}[A-D]{1}$/.test(code)) return false + + if (/^(BG|GB|NK|KN|TN|NT|ZZ)/.test(code)) return false + + return true +} diff --git a/src/NationalID/US.ts b/src/NationalID/US.ts index df0304a..39a6dd8 100644 --- a/src/NationalID/US.ts +++ b/src/NationalID/US.ts @@ -1,3 +1,29 @@ +// +// MIT License +// +// Copyright (c) 2017 Ardalan Amini +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// + +// Social Security Number (SSN) + const blackList = [ '078-05-1120' ] diff --git a/src/NationalID/index.ts b/src/NationalID/index.ts index 42a0e7d..1bb8615 100644 --- a/src/NationalID/index.ts +++ b/src/NationalID/index.ts @@ -1 +1,25 @@ +// +// MIT License +// +// Copyright (c) 2017 Ardalan Amini +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// + export { default } from './NationalID' diff --git a/src/Phone/IR/IR.ts b/src/Phone/IR/IR.ts new file mode 100644 index 0000000..7a5b482 --- /dev/null +++ b/src/Phone/IR/IR.ts @@ -0,0 +1,94 @@ +// +// MIT License +// +// Copyright (c) 2017 Ardalan Amini +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// + +import Phone from '../Phone' +import { range } from '../../utils' +import * as landlines from './landlines.json' +import * as mobiles from './mobiles.json' + +const isMobile = (number: string) => /^9/.test(number) + +const normalize = (number: string) => Phone.normalize(number).slice(2) + +export function fancy(number: string) { + number = normalize(number) + + if (number.length !== 10) return number + + if (isMobile(number)) return `(${number.substr(0, 3)}) ${number.substr(3, 3)} ${number.substr(6)}` + + return `(${number.substr(0, 2)}) ${number.substr(2, 4)} ${number.substr(6, 3)}` +} + +export function identify(number: string) { + number = normalize(number) + + if (number.length !== 10) return undefined + + if (isMobile(number)) return identifyMobile(number) + + return identifyLandline(number) +} + +function identifyLandline(number: string) { + let identity = { + type: 'Landline' + } + + for (let code in landlines) { + if ((new RegExp(`^${code}`)).test(number)) return { + ...identity, + province: landlines[code] + } + } + + return undefined +} + +function identifyMobile(number: string) { + let providers: { [key: string]: any } = {} + let identity = { + type: 'Mobile' + } + + mobiles.map((mobile: { [key: string]: any }) => { + mobile.identifiers.map((identifier: string) => { + if (/~/g.test(identifier)) { + range(...identifier.split('~').map((number: string) => +number)) + .map((number: number) => providers[`${number}`] = mobile.provider) + } else { + providers[identifier] = mobile.provider + } + }) + }) + + for (let code of Object.keys(providers).sort((a, b) => +b - +a)) { + if ((new RegExp(`^${code}`)).test(number)) return { + ...identity, + provider: providers[code] + } + } + + return undefined +} diff --git a/src/Phone/IR/index.ts b/src/Phone/IR/index.ts new file mode 100644 index 0000000..effda1d --- /dev/null +++ b/src/Phone/IR/index.ts @@ -0,0 +1,25 @@ +// +// MIT License +// +// Copyright (c) 2017 Ardalan Amini +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// + +export * from './IR' diff --git a/src/Phone/IR/landlines.json b/src/Phone/IR/landlines.json new file mode 100644 index 0000000..116b782 --- /dev/null +++ b/src/Phone/IR/landlines.json @@ -0,0 +1,33 @@ +{ + "11": "Mazandaran", + "13": "Gilan", + "17": "Golestan", + "21": "Tehran", + "23": "Semnan", + "24": "Zanjan", + "25": "Qom", + "26": "Alborz", + "28": "Qazvin", + "31": "Esfahan", + "34": "Kerman", + "35": "Yazd", + "38": "Chahar Mahall and Bakhtiari", + "41": "East Azarbaijan", + "44": "West Azarbaijan", + "45": "Ardebil", + "51": "Razavi Khorasan", + "54": "Sistan and Baluchestan", + "56": "South Khorasan", + "58": "North Khorasan", + "61": "Khuzestan", + "66": "Lorestan", + "71": "Fars", + "74": "Kohgiluyeh and Buyer Ahmad", + "76": "Hormozgan", + "77": "Bushehr", + "81": "Hamadan", + "83": "Kermanshah", + "84": "Ilam", + "86": "Markazi", + "87": "Kordestan" +} diff --git a/src/Phone/IR/mobiles.json b/src/Phone/IR/mobiles.json new file mode 100644 index 0000000..1f0152c --- /dev/null +++ b/src/Phone/IR/mobiles.json @@ -0,0 +1,66 @@ +[ + { + "provider": "Shatel mobile", + "identifiers": [ + "9981" + ] + }, + { + "provider": "UpTell", + "identifiers": [ + "9991" + ] + }, + { + "provider": "SamanTel", + "identifiers": [ + "99998~99999" + ] + }, + { + "provider": "RighTel", + "identifiers": [ + "920~922" + ] + }, + { + "provider": "MTN Irancell", + "identifiers": [ + "901~903", + "935~939", + "9301364307", + "933" + ] + }, + { + "provider": "MTCE", + "identifiers": [ + "931" + ] + }, + { + "provider": "Taliya", + "identifiers": [ + "932" + ] + }, + { + "provider": "MCE", + "identifiers": [ + "91", + "990" + ] + }, + { + "provider": "TeleKish", + "identifiers": [ + "9" + ] + }, + { + "provider": "PH.Lotus", + "identifiers": [ + "9990" + ] + } +] diff --git a/src/Phone/Phone.ts b/src/Phone/Phone.ts new file mode 100644 index 0000000..34cdb03 --- /dev/null +++ b/src/Phone/Phone.ts @@ -0,0 +1,82 @@ +// +// MIT License +// +// Copyright (c) 2017 Ardalan Amini +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// + +import * as IR from './IR' +import * as countries from './countries.json' + +const locales: { [key: string]: any } = { + IR +} + +const getCountry = (number: string) => { + for (let code of Object.keys(countries).sort((a, b) => +b - +a)) { + if ((new RegExp(`^${code}`)).test(number)) return countries[code] + } + + return undefined +} + +export default class Phone { + static identify(number: string) { + number = Phone.normalize(number) + + let country = getCountry(number) + + if (country) { + let locale = locales[country.alias] + + if (locale) { + let identity = locale.identify(number) + + if (identity) return { ...identity, country } + } + } + + return undefined + } + + static fancy(number: string) { + number = Phone.normalize(number) + + let country = getCountry(number) + + if (country && locales[country.alias]) return `+${number.substr(0, 2)} ${locales[country.alias].fancy(number)}` + + return number + } + + static country(number: string) { + number = Phone.normalize(number) + + return getCountry(number) + } + + static normalize(number: string) { + number = number.replace(/\D/g, '') + + if (/^0{2}/.test(number)) number = number.slice(2) + + return number + } +} diff --git a/src/Phone/countries.json b/src/Phone/countries.json new file mode 100644 index 0000000..40f41ab --- /dev/null +++ b/src/Phone/countries.json @@ -0,0 +1,14 @@ +{ + "1": { + "name": "United States", + "alias": "US" + }, + "44": { + "name": "United Kingdom", + "alias": "UK" + }, + "98": { + "name": "Iran, Islamic Republic of", + "alias": "IR" + } +} diff --git a/src/Phone/index.ts b/src/Phone/index.ts new file mode 100644 index 0000000..7577496 --- /dev/null +++ b/src/Phone/index.ts @@ -0,0 +1,25 @@ +// +// MIT License +// +// Copyright (c) 2017 Ardalan Amini +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// + +export { default } from './Phone' diff --git a/src/Verifications.ts b/src/Verifications.ts index a45d4bc..937de37 100644 --- a/src/Verifications.ts +++ b/src/Verifications.ts @@ -1,7 +1,27 @@ -import NationalID from './NationalID' -import CreditCard from './CreditCard' +// +// MIT License +// +// Copyright (c) 2017 Ardalan Amini +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// -export default class Verifications { - static NationalID = NationalID - static CreditCard = CreditCard -} +export {default as NationalID} from './NationalID' +export {default as CreditCard} from './CreditCard' +export {default as Phone} from './Phone' diff --git a/src/index.ts b/src/index.ts index bb5dbab..f81747f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1 +1,25 @@ -export { default } from './Verifications' +// +// MIT License +// +// Copyright (c) 2017 Ardalan Amini +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// + +export * from './Verifications' diff --git a/src/typings.d.ts b/src/typings.d.ts index 05cd5df..d14cf4c 100644 --- a/src/typings.d.ts +++ b/src/typings.d.ts @@ -1,7 +1,35 @@ -declare module '*.json' { - const value: { - [key: string]: any - } | any +// +// MIT License +// +// Copyright (c) 2017 Ardalan Amini +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// - export = value -} +declare module 'require-all' + +declare module '*.json' + +// declare module '*.json' { +// const value: { +// [key: string]: any +// } | any +// +// export = value +// } diff --git a/src/utils.ts b/src/utils.ts index 0144e2a..6d67355 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,3 +1,27 @@ +// +// MIT License +// +// Copyright (c) 2017 Ardalan Amini +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// + export function range(start: number = 0, stop: number = start, step: number = 1) { const length = Math.max(Math.ceil((stop - start) / step), 0) let range = []