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

Add 'remove diacritics' option #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion src/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ class Base {
triggerEventName = false,
elements = false,
success: callbackSuccess,
error: callbackError
error: callbackError,
removeDiacritics = false
} = object;

let {
Expand All @@ -24,6 +25,7 @@ class Base {
this.stateEl = stateEl;

this.triggerEventName = triggerEventName;
this.removeDiacritics = removeDiacritics;
this.callbackSuccess = callbackSuccess || false;
this.callbackError = callbackError || false;
}
Expand Down
21 changes: 20 additions & 1 deletion src/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,30 @@ class Search {
fillForm(data) {
this.streetEl.value = `${data.tipoDeLogradouro} ${data.logradouro}`;
this.neighborhoodEl.value = data.bairro;
this.cityEl.value = data.cidade;
var cityValue = data.cidade;
if (this.removeDiacritics) {
cityValue = this.replaceDiacritics(cityValue);
}
this.cityEl.value = cityValue;
this.stateEl.value = data.estado;

this.numberEl.focus();
}

replaceDiacritics(str) {
str = str.replace(/[ÁÀÂÃÄ]/g, 'A');
str = str.replace(/[áàâãä]/g, 'a');
str = str.replace(/[ÉÈÊË]/g, 'E');
str = str.replace(/[éèêë]/g, 'e');
str = str.replace(/[ÍÌÎÏ]/g, 'I');
str = str.replace(/[íìîï]/g, 'i');
str = str.replace(/[ÓÒÔÕÖ]/g, 'O');
str = str.replace(/[óòôõö]/g, 'o');
str = str.replace(/[ÚÙÛÜ]/g, 'U');
str = str.replace(/[úùûü]/g, 'u');
str = str.replace(/[Ç]/g, 'C');
return str.replace(/[ç]/g, 'c');
}
}

module.exports = Search;
20 changes: 20 additions & 0 deletions test/unit/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,5 +208,25 @@ describe('Base', () => {
expect(base.searchEl({ el })).to.not.equal(undefined);
});
});

describe('when removeDiacritics is empty', () => {
beforeEach(() => {
base = new Base();
});

it('removeDiacritics is false', () => {
expect(base.removeDiacritics).to.equal(false);
});
});

describe('when removeDiacritics exists', () => {
beforeEach(() => {
base = new Base({ removeDiacritics: true });
});

it('returns the value passed on removeDiacritics option', () => {
expect(base.removeDiacritics).to.equal(true);
});
});
});
});
40 changes: 33 additions & 7 deletions test/unit/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,43 @@ describe('Search', () => {
fooInstance.search();
});

it('when success has been fill form', () => {
let fooInstance = new Foo();
describe('with removeDiacritics option set to false', () => {
it('fills the form with diacritics', () => {
let fooInstance = new Foo({ removeDiacritics: false });

fooInstance.fillForm({ tipoDeLogradouro: 'Rua', logradouro: 'logradouro', bairro: 'bairro', cidade: 'cidade', estado: 'estado' });
fooInstance.fillForm({ tipoDeLogradouro: 'Rua', logradouro: 'logradouro', bairro: 'bairro', cidade: 'cidáde', estado: 'estado' });

expect(fooInstance.streetEl.value).to.equal('Rua logradouro');
expect(fooInstance.neighborhoodEl.value).to.equal('bairro');
expect(fooInstance.cityEl.value).to.equal('cidade');
expect(fooInstance.stateEl.value).to.equal('estado');
expect(fooInstance.streetEl.value).to.equal('Rua logradouro');
expect(fooInstance.neighborhoodEl.value).to.equal('bairro');
expect(fooInstance.cityEl.value).to.equal('cidáde');
expect(fooInstance.stateEl.value).to.equal('estado');
});
});
describe('with removeDiacritics option set to true', () => {
it('fills the form without diacritics', () => {
let fooInstance = new Foo({ removeDiacritics: true });

fooInstance.fillForm({ tipoDeLogradouro: 'Rua', logradouro: 'logradouro', bairro: 'bairro', cidade: 'cidáde', estado: 'estado' });

expect(fooInstance.streetEl.value).to.equal('Rua logradouro');
expect(fooInstance.neighborhoodEl.value).to.equal('bairro');
expect(fooInstance.cityEl.value).to.equal('cidade');
expect(fooInstance.stateEl.value).to.equal('estado');
});
});
});
});
});

describe('replaceDiacritics function', () => {
beforeEach(() => {
fooInstance = new Foo();
});

it('replaces diacritics with ascii characters', () => {
var input = 'AÁÀÂÃÄaáàâãäEÉÈÊËeéèêëIÍÌÎÏiíìîïOÓÒÔÕÖoóòôõöUÚÙÛÜuúùûüCÇcç';
var expected = 'AAAAAAaaaaaaEEEEEeeeeeIIIIIiiiiiOOOOOOooooooUUUUUuuuuuCCcc';
expect(fooInstance.replaceDiacritics(input)).to.equal(expected);
});
});
});