Skip to content
This repository was archived by the owner on May 1, 2025. It is now read-only.

Commit e5e5a63

Browse files
author
Hans Kristian Flaatten
committed
feat(parser): convert "true" and "false" to booleans
BREAKING CHANGE: This will convert strings "true" and "false" to booleans, they will no longer be evaluated as strings.
1 parent 5f1d089 commit e5e5a63

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ useful when building an API and accepting various user specificed queries.
2323
* `$nin`
2424
* `$exists`
2525
* `$regex`
26+
* Parse string integers and floats to numbers
27+
* Parse string boolean to ture/false booleans
2628

2729
| operation | query string | query object |
2830
|-----------|---------------|--------------|

index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,11 @@ module.exports.prototype.customAfter = function(field) {
101101
};
102102

103103
module.exports.prototype.parseString = function(string) {
104-
if (!isNaN(string)) {
104+
if (string.toLowerCase() === 'true') {
105+
return true;
106+
} else if (string.toLowerCase() === 'false') {
107+
return false;
108+
} else if (!isNaN(string)) {
105109
return parseFloat(string, 10);
106110
} else {
107111
return string;

test.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,32 @@ describe('customAfter()', function() {
100100
});
101101
});
102102

103+
describe('parseString()', function() {
104+
it('returns boolean true for "true" string', function() {
105+
assert.equal(qs.parseString('true'), true);
106+
});
107+
108+
it('returns boolean false for "flase" string', function() {
109+
assert.equal(qs.parseString('false'), false);
110+
});
111+
112+
it('returns number for parseable integer', function() {
113+
assert.equal(qs.parseString('100'), 100);
114+
});
115+
116+
it('returns number for zero padded parseable integer', function() {
117+
assert.equal(qs.parseString('000100'), 100);
118+
});
119+
120+
it('returns number for parseable float', function() {
121+
assert.equal(qs.parseString('10.123'), 10.123);
122+
});
123+
124+
it('returns number for zero padded parseable float', function() {
125+
assert.equal(qs.parseString('00010.123'), 10.123);
126+
});
127+
});
128+
103129
describe('parse()', function() {
104130
describe('parsing', function() {
105131
describe('key value validation', function() {
@@ -143,6 +169,17 @@ describe('parse()', function() {
143169
});
144170
});
145171

172+
it('return string boolean as boolean', function() {
173+
query = qs.parse({
174+
foo: 'true',
175+
bar: 'false'
176+
});
177+
assert.deepEqual(query, {
178+
foo: true,
179+
bar: false
180+
});
181+
});
182+
146183
it('returns string integer as number', function() {
147184
query = qs.parse({
148185
navn: '10'
@@ -185,6 +222,17 @@ describe('parse()', function() {
185222
});
186223
});
187224

225+
it('return string boolean as boolean', function() {
226+
query = qs.parse({
227+
foo: '!true',
228+
bar: '!false'
229+
});
230+
assert.deepEqual(query, {
231+
foo: {$ne: true},
232+
bar: {$ne: false}
233+
});
234+
});
235+
188236
it('returns string integer as number', function() {
189237
query = qs.parse({
190238
navn: '!10'

0 commit comments

Comments
 (0)