Skip to content

Commit

Permalink
feat(parser): add string.toBoolean and string.toNumber options
Browse files Browse the repository at this point in the history
  • Loading branch information
Hans Kristian Flaatten committed Feb 10, 2016
1 parent e5e5a63 commit eb60e36
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ var MongoQS = require('mongo-querystring');
* `object` blacklist - blacklisted query params
* `object` whitelist - whitelisted query params
* `object` custom - custom query params
* `object` string - string parsing
* `boolean` toBoolean - parse `"true"`, `"false"` string to booleans
* `boolean` toNumber - parse string integer and float values to numbers

#### Bult in custom queries

Expand Down
12 changes: 9 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ module.exports = function MongoQS(opts) {
this.whitelist = opts.whitelist || {};
this.custom = opts.custom || {};

// String Value Parsing
opts.string = opts.string || {};
this.string = opts.string || {};
this.string.toBoolean = opts.string.toBoolean || true;
this.string.toNumber = opts.string.toNumber || true;

this.keyRegex = opts.keyRegex || /^[a-zæøå0-9-_.]+$/i;
this.arrRegex = opts.arrRegex || /^[a-zæøå0-9-_.]+(\[\])?$/i;

Expand Down Expand Up @@ -101,11 +107,11 @@ module.exports.prototype.customAfter = function(field) {
};

module.exports.prototype.parseString = function(string) {
if (string.toLowerCase() === 'true') {
if (this.string.toBoolean && string.toLowerCase() === 'true') {
return true;
} else if (string.toLowerCase() === 'false') {
} else if (this.string.toBoolean && string.toLowerCase() === 'false') {
return false;
} else if (!isNaN(string)) {
} else if (this.string.toNumber && !isNaN(string)) {
return parseFloat(string, 10);
} else {
return string;
Expand Down
15 changes: 15 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,29 @@ describe('parseString()', function() {
assert.equal(qs.parseString('true'), true);
});

it('returns string "true" when boolean parsing is disabled', function() {
qs.string.toBoolean = false;
assert.equal(qs.parseString('true'), 'true');
});

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

it('returns string "false" when boolean parsing is disabled', function() {
qs.string.toBoolean = false;
assert.equal(qs.parseString('false'), 'false');
});

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

it('returns string number when number parsing is disabled', function() {
qs.string.toNumber = false;
assert.equal(qs.parseString('100'), '100');
});

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

0 comments on commit eb60e36

Please sign in to comment.