Skip to content

Commit

Permalink
fix: remove empty fields from query param and update regex creation u…
Browse files Browse the repository at this point in the history
…nit test
  • Loading branch information
PauloGoncalvesBH committed Jan 22, 2024
1 parent ca33b37 commit 00d44a1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/utils/alterarValoresParaRegex.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ const permittedKeys = ['nome', 'password', 'descricao']

module.exports = queryString => {
Object.keys(queryString).forEach(key => {
if (permittedKeys.includes(key) && queryString[key] != null) {
const isFieldEmpty = !queryString[key]
/* istanbul ignore next */
if (isFieldEmpty) {
delete queryString[key]
} else if (permittedKeys.includes(key)) {
try {
// Add 'i' flag for case-insensitive matching
queryString[key] = new RegExp(escapeStringRegexp(queryString[key]), 'i')
Expand Down
13 changes: 12 additions & 1 deletion test/unit/alterarValoresParaRegex.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ describe('alterarValoresParaRegex', () => {
}

const result = alterarValoresParaRegex(queryString)
console.log(result)

expect(result.nome).to.be.instanceOf(RegExp)
expect(result.password).to.be.instanceOf(RegExp)
Expand All @@ -34,4 +33,16 @@ describe('alterarValoresParaRegex', () => {
expect(result.password.toString()).to.equal('/12\\*34/i')
expect(result.descricao.toString()).to.equal('/des\\|cription/i')
})

it('should remove field without value', () => {
const queryString = {
nome: 'test',
password: '',
empty: ''
}

const result = alterarValoresParaRegex(queryString)

expect(result).to.be.deep.equal({ nome: /test/i })
})
})

0 comments on commit 00d44a1

Please sign in to comment.