Skip to content

Commit

Permalink
Merge pull request #1 from gustavochavarria/feature/get-municipalitie…
Browse files Browse the repository at this point in the history
…s-from-dni

Feature/get municipalities from dni
  • Loading branch information
gustavochavarria authored Nov 26, 2019
2 parents 72b2738 + 432c64e commit 2f3cd19
Show file tree
Hide file tree
Showing 10 changed files with 1,292 additions and 1,353 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: CI

on: [push]
on: [push, pull_request]

jobs:
tests:
Expand Down
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"singleQuote": true,
"bracketSpacing": false,
"bracketSpacing": false
}
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"peacock.color": "#0067c6",
"workbench.colorCustomizations": {
"activityBar.background": "#0082f9",
"activityBar.activeBorder": "#ff9fd1",
"activityBar.foreground": "#e7e7e7",
"activityBar.inactiveForeground": "#e7e7e799",
"activityBarBadge.background": "#ff9fd1",
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Dnic - Javascript
Validador de Numero de Documento Nacional de Identidad (DNI/Cedula) Nicaragüense.

Validador de Numero de Documento Nacional de Identidad (DNI/Cedula) Nicaragüense.

Esctructura del DNI:

Expand All @@ -12,3 +12,6 @@ Esctructura del DNI:

<img src="https://i.ibb.co/YX8Tk1n/descarga.jpg" />

## Uso

import dnic from '@nerdify/dnic'
19 changes: 13 additions & 6 deletions __tests__/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import test from 'ava';

import dnic, {dnicWithDateValidation} from '../src';
import dnic, {validate, validateDate} from '../src';

const VALID_DNI = '001-280592-0025N';
const INVALID_DATE = '001-310292-0000N';

test('shoud pass a valid dni', t => {
t.true(dnic(VALID_DNI));
test('should pass a valid dni (default)', t => {
t.true(dnic.isValid(VALID_DNI));
});

test('validate date', t => {
t.true(dnicWithDateValidation(VALID_DNI));
t.false(dnicWithDateValidation(INVALID_DATE));
test('should pass a valid dni', t => {
t.true(validate(VALID_DNI));
});

test('valid dni (date)', t => {
t.true(validateDate(VALID_DNI));
});

test('valid a wrong date', t => {
t.false(validateDate(INVALID_DATE));
});
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"module": "dist/index.es.js",
"source": "src/index.js",
"scripts": {
"build": "microbundle",
"build": "xo && ava && microbundle",
"test": "xo && ava",
"prepare": "npm run build",
"release": "np"
Expand All @@ -31,7 +31,7 @@
"homepage": "https://github.com/nerdify/dnic#readme",
"devDependencies": {
"ava": "2.4.0",
"microbundle": "0.11.0",
"microbundle": "0.12.0-next.6",
"np": "5.1.0",
"xo": "0.25.3"
},
Expand All @@ -47,4 +47,4 @@
"prettier": true,
"space": true
}
}
}
35 changes: 25 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import {municipalities} from './municipalities';
import {validateDate} from './utils';
import {isValidDate} from './utils';

const dnic = dni => {
/**
* Basic Dni Validation
*
* @param {*} dni
*/
export const validate = dni => {
if (!dni) {
return false;
}
Expand All @@ -11,14 +16,12 @@ const dnic = dni => {
return regex.test(dni);
};

export {dnic as default};

/**
*
* Dni Validation with date validation
* @param {*} dni
*/
export const dnicWithDateValidation = dni => {
if (!dnic(dni)) {
export const validateDate = dni => {
if (!validate(dni)) {
return false;
}

Expand All @@ -27,15 +30,16 @@ export const dnicWithDateValidation = dni => {
const month = date.slice(2, 4);
const year = date.slice(4, 6);

return validateDate(day, month, year);
return isValidDate(day, month, year);
};

/**
* Get the regions from dni
*
* @param {*} dni
*/
export const getRegionFromDni = dni => {
if (!dnic(dni)) {
export const getMunicipalities = dni => {
if (!validate(dni)) {
return false;
}

Expand All @@ -44,3 +48,14 @@ export const getRegionFromDni = dni => {

return foundRegion[region];
};

/**
* Export default.
*/

const exportDefault = {
isValid: dni => validate(dni),
isValidDate: dni => validateDate(dni)
};

export {exportDefault as default};
218 changes: 211 additions & 7 deletions src/municipalities.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,222 @@
const atlanticoNorte = [
{'607': 'Puerto Cabezas'},
{'608': 'Waspán'},
{'610': 'Siuna'},
{'611': 'Bonanza'},
{'612': 'Rosita'},
{'615': 'Bocana Paiwás'},
{'454': 'Waslala'},
{'602': 'Corn Island'}
];

const atlanticoSur = [
{'601': 'Bluefields'},
{'603': 'El Rama'},
{'604': 'Muelle De Los Buelles'},
{'605': 'La Cruz De Rio Grande'},
{'606': 'Prinzapolka'},
{'616': 'Nueva Guinea'},
{'619': 'Tortuguero'},
{'624': 'Kukra Hill'},
{'626': 'Laguna De Perlas'},
{'627': 'Desembocadura Rio Grande'},
{'628': 'El Ayote'}
];

const boaco = [
{'361': 'Boaco'},
{'362': 'Camoapa'},
{'363': 'Santa Lucía'},
{'364': 'San José Del Remate'},
{'365': 'San Lorenzo'},
{'366': 'Teustepe'}
];

const carazo = [
{'041': 'Jinotepe'},
{'042': 'Diriamba'},
{'043': 'San Marcos'},
{'044': 'Santa Teresa'},
{'045': 'Dolores'},
{'046': 'La Paz Carazo'},
{'047': 'El Rosario'},
{'048': 'La Conquista'}
];

const chinandega = [
{'081': 'Chinandega'},
{'082': 'Corinto'},
{'083': 'El Realejo'},
{'084': 'Chichigalpa'},
{'085': 'Posoltega'},
{'086': 'El Viejo'},
{'087': 'Puerto Morazán'},
{'088': 'Somotillo'},
{'089': 'Villa Nueva'},
{'090': 'Santo Tomás del Norte'},
{'091': 'Cinco Pinos'},
{'092': 'San Francisco Del Norte'},
{'093': 'San Pedro Del Norte'}
];

const chontales = [
{'121': 'Juigalpa'},
{'122': 'Acoyapa'},
{'123': 'Santo Tomás'},
{'124': 'Villa Sandino'},
{'125': 'San Pedro de Lóvago'},
{'126': 'La Libertad'},
{'127': 'Santo Domingo'},
{'128': 'Comalapa'},
{'129': 'San Francisco Cuapa'},
{'130': 'El Coral'}
];

const esteli = [
{'161': 'Estelí'},
{'162': 'Pueblo Nuevo'},
{'163': 'Condega'},
{'164': 'San Juan Limay'},
{'165': 'La Trinidad'},
{'166': 'San Nicolás'}
];

const granada = [
{'201': 'Granada'},
{'202': 'Nandaime'},
{'203': 'Diriomo'},
{'204': 'Diriá'}
];

const jinotega = [
{'241': 'Jinotega'},
{'242': 'San Rafael Del Norte'},
{'243': 'San Sebastián Yalí'},
{'244': 'La Concordia'},
{'245': 'San José De Bocay'},
{'246': 'El Cuá Bocay'},
{'247': 'Santa María Pantasma'}
];

const leon = [
{'281': 'Leon'},
{'283': 'El Jicaral'},
{'284': 'La Paz Centro'},
{'285': 'Santa Rosa Del Peñón'},
{'286': 'Quetzalguaque'},
{'287': 'Nagarote'},
{'288': 'El Sauce'},
{'289': 'Achuapa'},
{'290': 'Telica'},
{'291': 'Larreynaga Malpaisillo'}
];

const madriz = [
{'321': 'Somoto'},
{'322': 'Telpaneca'},
{'323': 'Sn Juan Rio Coco'},
{'324': 'Palacagüina'},
{'325': 'Yalagüina'},
{'326': 'Totogalpa'},
{'327': 'San Lucas'},
{'328': 'La Sabanas'},
{'329': 'San José De Cusmapa'}
];

const managua = [
{'001': 'Managua'},
{'002': 'San Rafael Del Sur'},
{'003': 'Tipitapa'},
{'004': 'Villa Carlos Fonseca'},
{'005': 'San Francisco Libre'},
{'006': 'Mateare'}
{'006': 'Mateare'},
{'007': 'Ticuantepe'},
{'008': 'Ciudad Sandino'},
{'009': 'El Crucero'}
];

const leon = [{'281': 'Leon'}];
const masaya = [
{'401': 'Masaya'},
{'402': 'Nindirí'},
{'403': 'Tisma'},
{'404': 'Catarina'},
{'405': 'San Juan Oriente'},
{'406': 'Niquinohomo'},
{'407': 'Nandasmo'},
{'408': 'Masatepe'},
{'409': 'La Concepción'}
];

const chinandega = [
{'081': 'Chinandega'},
{'082': 'Corinto'},
{'083': 'El Realejo'}
const matagalpa = [
{'441': 'Matagalpa'},
{'442': 'San Ramón'},
{'443': 'Matiguás'},
{'444': 'Muy Muy'},
{'445': 'Esquipulas'},
{'446': 'San Dionisio'},
{'447': 'San Isidro'},
{'448': 'Sébaco'},
{'449': 'Ciudad Darío'},
{'450': 'Terrabona'},
{'451': 'Rio Blanco'},
{'452': 'Tuma La Dalia'},
{'453': 'Rancho Grande'}
];

export const municipalities = [...managua, ...leon, ...chinandega];
const nuevaSegovia = [
{'481': 'Ocotal'},
{'482': 'Santa María'},
{'483': 'Macuelizo'},
{'484': 'Dipilto'},
{'485': 'Ciudad Antigua'},
{'486': 'Mozonte'},
{'487': 'San Fernando'},
{'488': 'El Jícaro'},
{'489': 'Jalapa'},
{'490': 'Murra'},
{'491': 'Quilalí'},
{'492': 'Wiwilí'},
{'493': 'Wiwilí Nueva Segovia'}
];

const rioSanJuan = [
{'521': 'San Carlos'},
{'522': 'El Castillo'},
{'523': 'San Miguelito'},
{'524': 'Morrito'},
{'525': 'San Juan del Norte'},
{'526': 'El Almendro'}
];

const rivas = [
{'561': 'Rivas'},
{'562': 'San Jorge'},
{'563': 'Buenos Aires'},
{'564': 'Potosí'},
{'565': 'Belén'},
{'566': 'Tola'},
{'567': 'San Juan Sur'},
{'568': 'Cárdenas'},
{'569': 'Moyogalpa'},
{'570': 'Altagracia'}
];

export const municipalities = [
...atlanticoNorte,
...atlanticoSur,
...boaco,
...carazo,
...chinandega,
...chontales,
...granada,
...jinotega,
...leon,
...madriz,
...managua,
...masaya,
...matagalpa,
...nuevaSegovia,
...rioSanJuan,
...rivas,
...esteli
];
2 changes: 1 addition & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const validateDate = (day, month, year) => {
export const isValidDate = (day, month, year) => {
const pDay = parseInt(day, 10);
const pMonth = parseInt(month, 10);
const pYear = parseInt(year, 10);
Expand Down
Loading

0 comments on commit 2f3cd19

Please sign in to comment.