-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0035bcd
commit be4631d
Showing
6 changed files
with
341 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.npmignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
# @joaopedrogomes/nif-portugal | ||
|
||
Validar e obter mais informações dos números de identificação fiscal portugueses | ||
|
||
## Compreendendo o conceito de NIF | ||
|
||
### O que é um NIF? | ||
O acrónimo NIF significa **Número de Identificação Fiscal**. Este número também é chamada de Número de Contribuinte. | ||
|
||
### Quem pode obter um NIF? | ||
O **NIF é um número único** e pode ser atribuído a quem **necessite de pagar impostos em Portugal**, quer sejam pessoas individuais, quer sejam pessoas coletivas (como empresas, sociedades, etc.). Este número também pode ser atribuído a estrangeiros não residentes. | ||
|
||
### Tipos de NIF / Pessoa fiscal: NIF e NIPC | ||
Existem dois tipos de NIF, sendo que os dois primeiros dígitos do número definem isso: | ||
|
||
- **Entre 10 e 49**: NIF - Persona singular | ||
- **Entre 50 e 99**: NIPC - Pessoa Colectiva | ||
|
||
Coloquialmente, a denominação NIF aplica-se a todos os números de identificação fiscal. Contudo, legalmente, os números de identificação fiscal coletivos são chamados de NIPC - Número de Identificação de Pessoa Coletiva. | ||
|
||
## Installation | ||
|
||
```bash | ||
npm install --save-dev @joaopedrogomes/nif-portugal | ||
``` | ||
|
||
## Utilização | ||
|
||
Podes obter todas as informações em um objeto com a função NIF (nif). No entanto, se desejares obter apenas uma das keys do objeto, podes chamar a função desejada diretamente: _categorize_, _designate_ or _validate_. | ||
|
||
### NIF - nif(value: string) | ||
|
||
#### Utilização | ||
|
||
```js | ||
import { nif } from '@joaopedrogomes/nif-portugal'; | ||
|
||
const data = nif('123456789'); | ||
``` | ||
|
||
#### Resposta | ||
|
||
```js | ||
{ | ||
category: string, | ||
designation: string, | ||
valid: boolean | ||
} | ||
``` | ||
|
||
| Category : string | Designation : string| Valid : boolean | | ||
|---|---|---| | ||
| pessoa singular | NIF | true | | ||
| pessoa colectiva | NIPC | false | | ||
| administração pública | | | | ||
| pessoa singular não residente | | | | ||
| herança indivisa | | | | ||
| pessoa colectiva não residente | | | | ||
| fundos de investimento | | | | ||
| atribuição oficiosa | | | | ||
| regime excepcional | | | | ||
| condominios e sociedades irregulares | | | | ||
| não residentes | | | | ||
| sociedades civis | | | | ||
|
||
### Category - categorize(value: string) | ||
|
||
#### Utilização | ||
|
||
```js | ||
import { categorize } from '@joaopedrogomes/nif-portugal'; | ||
|
||
const data = nif('123456789'); | ||
``` | ||
|
||
#### Resposta | ||
|
||
```js | ||
string | ||
``` | ||
|
||
### Designation - designate(value: string) | ||
|
||
#### Utilização | ||
|
||
```js | ||
import { designate } from '@joaopedrogomes/nif-portugal'; | ||
|
||
const data = nif('123456789'); | ||
``` | ||
|
||
#### Resposta | ||
|
||
```js | ||
string | ||
``` | ||
|
||
### Valid - validate(value: string) | ||
|
||
#### Utilização | ||
|
||
```js | ||
import { validate } from '@joaopedrogomes/nif-portugal'; | ||
|
||
const data = nif('123456789'); | ||
``` | ||
|
||
#### Resposta | ||
|
||
```js | ||
boolean | ||
``` | ||
|
||
|
||
## Créditos | ||
|
||
- [João Pedro Gomes](https://joaopedrogomes.dev) | ||
|
||
## Licença | ||
|
||
[MIT](./license) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
# @joaopedrogomes/nif-pt | ||
|
||
Validate and get more information from portuguese tax ID numbers | ||
|
||
## Understanding the NIF concept | ||
|
||
The Portuguese tax system is really complex, so let's check some concepts first: | ||
|
||
### What is a NIF? | ||
|
||
NIF stands for _Número de Identificação Fiscal_, which translates to Tax ID Number. | ||
|
||
### Who can get a NIF? | ||
|
||
The **NIF is a unique ID** and can be attributed to individuals or to collective persons like companies, societies, etc., that **need to pay taxes in Portugal**. The number can be attributed to non-resident foreigners as well. | ||
|
||
### Types of NIF: NIF and NIPC | ||
|
||
There are two types of NIF, and the first two digits of the number define it: | ||
|
||
- **Between 10 and 49**: NIF - individuals | ||
- **Betweeen 50 and 99**: NIPC - collective | ||
|
||
So, while colloquially the term NIF applies to all tax IDs, legally, the collective tax IDs are referred to as NIPC. The acronym NIPC stands for _Número de Identificação de Pessoa Colectiva_, which translates to Collective Person ID Number. | ||
|
||
|
||
## Installation | ||
|
||
```bash | ||
npm install --save-dev @joaopedrogomes/nif-pt | ||
``` | ||
|
||
## Usage | ||
|
||
You can get all the information in an object with the NIF function (_nif_). However, if you only want to get one of the keys of the object, you can also call the desired function directly: _categorize_, _designate_, or _validate_. | ||
|
||
### NIF - nif(value: string) | ||
|
||
#### Usage | ||
|
||
```js | ||
import { nif } from '@joaopedrogomes/nif-pt'; | ||
|
||
const data = nif('123456789'); | ||
``` | ||
|
||
#### Return | ||
|
||
```js | ||
{ | ||
category: string, | ||
designation: string, | ||
valid: boolean | ||
} | ||
``` | ||
|
||
| Category : string | Designation : string| Valid : boolean | | ||
|---|---|---| | ||
| pessoa singular | NIF | true | | ||
| pessoa colectiva | NIPC | false | | ||
| administração pública | | | | ||
| pessoa singular não residente | | | | ||
| herança indivisa | | | | ||
| pessoa colectiva não residente | | | | ||
| fundos de investimento | | | | ||
| atribuição oficiosa | | | | ||
| regime excepcional | | | | ||
| condominios e sociedades irregulares | | | | ||
| não residentes | | | | ||
| sociedades civis | | | | ||
|
||
### Category only - categorize(value: string) | ||
|
||
#### Usage | ||
|
||
```js | ||
import { categorize } from '@joaopedrogomes/nif-pt'; | ||
|
||
const data = nif('123456789'); | ||
``` | ||
|
||
#### Return | ||
|
||
```js | ||
string | ||
``` | ||
|
||
### Designation only - designate(value: string) | ||
|
||
#### Usage | ||
|
||
```js | ||
import { designate } from '@joaopedrogomes/nif-pt'; | ||
|
||
const data = nif('123456789'); | ||
``` | ||
|
||
#### Return | ||
|
||
```js | ||
string | ||
``` | ||
|
||
### Valid only - validate(value: string) | ||
|
||
#### Usage | ||
|
||
```js | ||
import { validate } from '@joaopedrogomes/nif-pt'; | ||
|
||
const data = nif('123456789'); | ||
``` | ||
|
||
#### Return | ||
|
||
```js | ||
boolean | ||
``` | ||
|
||
|
||
## Credits | ||
|
||
- [João Pedro Gomes](https://joaopedrogomes.dev) | ||
|
||
## License | ||
|
||
[MIT](./license) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const categorize = (value = '') => { | ||
if (!value || typeof value !== "string" || value.length !== 9) return null | ||
const area = { | ||
oneNumber: { | ||
'1': 'pessoa singular', | ||
'2': 'pessoa singular', | ||
'3': 'pessoa singular', | ||
'5': 'pessoa colectiva', | ||
'6': 'administração pública' | ||
}, | ||
twoNumbers: { | ||
'45': 'pessoa singular não residente', | ||
'70': 'herança indivisa', | ||
'71': 'pessoa colectiva não residente', | ||
'72': 'fundos de investimento', | ||
'77': 'atribuição oficiosa', | ||
'79': 'regime excepcional', | ||
'90': 'condominios e sociedades irregulares', | ||
'91': 'condominios e sociedades irregulares', | ||
'98': 'não residentes', | ||
'99': 'sociedades civis' | ||
} | ||
} | ||
return area.oneNumber[value.substring(0, 1)] || area.twoNumbers[value.substring(0, 2)] || null | ||
} | ||
|
||
const designate = (value = '') => { | ||
if (!value || typeof value !== "string" || value.length !== 9) return null; | ||
if (parseInt(value.substring(0,1)) > 4) return 'NIPC'; | ||
else return 'NIF'; | ||
} | ||
|
||
const validate = (value = '') => { | ||
if (!value || typeof value !== "string" || value.length !== 9) return false; | ||
|
||
const firstEightDigits = value.slice(0, value.length - 1); | ||
let validationSum = 0; | ||
for (let i = 9; i >= 2; i--) validationSum += i * (parseInt(value[value.length - i]) || 0); | ||
|
||
const module = validationSum % 11; | ||
if (module === 0 || module === 1) return `${firstEightDigits}0` === value; | ||
else return `${firstEightDigits}${11 - module}` === value; | ||
} | ||
|
||
const nif = (value = '') => { | ||
return { | ||
category: categorize(value), | ||
designation: designate(value), | ||
valid: validate(value) | ||
} | ||
} | ||
|
||
module.exports = { categorize, designate, nif, validate } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
MIT License | ||
|
||
Copyright (c) João Pedro Gomes <[email protected]> (https://joaopedrogomes.dev) | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"name": "@joaopedrogomes/nif-pt", | ||
"description": "Validate and get more information from portuguese tax ID numbers ", | ||
"version": "1.0.0", | ||
"author": "João Pedro Gomes", | ||
"license": "MIT", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/joaopedrogomes/nif-pt.git" | ||
}, | ||
"keywords": [ | ||
"nif", | ||
"financas", | ||
"portugal", | ||
"nipc", | ||
"validador", | ||
"nif", | ||
"numero", | ||
"identificacao", | ||
"fiscal" | ||
], | ||
"bugs": { | ||
"url": "https://github.com/joaopedrogomes/nif-pt/issues" | ||
}, | ||
"homepage": "https://github.com/joaopedrogomes/nif-pt#readme" | ||
} |