Skip to content

Commit 435eeec

Browse files
committed
Accepting integer with a leading zero in query string
- Adding some unit tests for filters; - Adjusting filters.treatValue function to return integer only if string can be converted to integer;
1 parent 4390a11 commit 435eeec

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

lib/mapper/filters.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ function treatValue(value) {
4848
else if (value.includes('*')) return buildRegEx(value)
4949
else if (value.includes(':')) return getCompareOperator(value)
5050
else if (value === 'now') return normalizeDate(dateToString(new Date()), false)
51-
else if (/^\d*$/.test(value) && (parseInt(value).toString().length !== value.length)) return value
52-
else if (/^\d*$/.test(value)) return parseInt(value)
51+
else if (parseInt(value).toString() === value) return parseInt(value)
5352
return value
5453
}
5554

test/unit/filters.spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,24 @@ describe('QueryString: Filters', function () {
1111
})
1212
})
1313

14+
context('when query filters contains an integer with a leading zero', function () {
15+
it('should return a JSON with filters params, keeping the leading zero of the number as string', function (done) {
16+
const VALUE = '009'
17+
const result = filter.filters({ value: VALUE }, default_options)
18+
expect(result).to.have.property('value', VALUE)
19+
done()
20+
})
21+
})
22+
23+
context('when query filters contains an integer that exceeds max safe integer', function () {
24+
it('should return a JSON with filters params containing the number as string', function (done) {
25+
const VALUE = '9007199254740999'
26+
const result = filter.filters({ value: VALUE }, default_options)
27+
expect(result).to.have.property('value', VALUE)
28+
done()
29+
})
30+
})
31+
1432
context('when query filters its a object', function () {
1533
it('should ignore the object param and returns another filters', function () {
1634
verify(filter.filters({job: {first: 'developer'}, name: 'lucas', age: '30'}, default_options))

0 commit comments

Comments
 (0)