Skip to content

Commit

Permalink
feat(parser): convert "true" and "false" to booleans
Browse files Browse the repository at this point in the history
BREAKING CHANGE: This will convert strings "true" and "false" to
booleans, they will no longer be evaluated as strings.
  • Loading branch information
Hans Kristian Flaatten committed Feb 10, 2016
1 parent 5f1d089 commit e5e5a63
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ useful when building an API and accepting various user specificed queries.
* `$nin`
* `$exists`
* `$regex`
* Parse string integers and floats to numbers
* Parse string boolean to ture/false booleans

| operation | query string | query object |
|-----------|---------------|--------------|
Expand Down
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ module.exports.prototype.customAfter = function(field) {
};

module.exports.prototype.parseString = function(string) {
if (!isNaN(string)) {
if (string.toLowerCase() === 'true') {
return true;
} else if (string.toLowerCase() === 'false') {
return false;
} else if (!isNaN(string)) {
return parseFloat(string, 10);
} else {
return string;
Expand Down
48 changes: 48 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,32 @@ describe('customAfter()', function() {
});
});

describe('parseString()', function() {
it('returns boolean true for "true" string', function() {
assert.equal(qs.parseString('true'), true);
});

it('returns boolean false for "flase" string', function() {
assert.equal(qs.parseString('false'), false);
});

it('returns number for parseable integer', function() {
assert.equal(qs.parseString('100'), 100);
});

it('returns number for zero padded parseable integer', function() {
assert.equal(qs.parseString('000100'), 100);
});

it('returns number for parseable float', function() {
assert.equal(qs.parseString('10.123'), 10.123);
});

it('returns number for zero padded parseable float', function() {
assert.equal(qs.parseString('00010.123'), 10.123);
});
});

describe('parse()', function() {
describe('parsing', function() {
describe('key value validation', function() {
Expand Down Expand Up @@ -143,6 +169,17 @@ describe('parse()', function() {
});
});

it('return string boolean as boolean', function() {
query = qs.parse({
foo: 'true',
bar: 'false'
});
assert.deepEqual(query, {
foo: true,
bar: false
});
});

it('returns string integer as number', function() {
query = qs.parse({
navn: '10'
Expand Down Expand Up @@ -185,6 +222,17 @@ describe('parse()', function() {
});
});

it('return string boolean as boolean', function() {
query = qs.parse({
foo: '!true',
bar: '!false'
});
assert.deepEqual(query, {
foo: {$ne: true},
bar: {$ne: false}
});
});

it('returns string integer as number', function() {
query = qs.parse({
navn: '!10'
Expand Down

0 comments on commit e5e5a63

Please sign in to comment.