Skip to content

Commit

Permalink
Add parseBooleans option to .parse() (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
felixoi authored and sindresorhus committed Jun 15, 2019
1 parent 3ccd68d commit f958329
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
15 changes: 14 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,23 @@ export interface ParseOptions {
```
*/
readonly parseNumbers?: boolean;

/**
Parse the value as a boolean type instead of string type if it's a boolean.
@default false
@example
```
queryString.parse('foo=true', {parseBooleans: true});
//=> {foo: true}
```
*/
readonly parseBooleans?: boolean;
}

export interface ParsedQuery {
readonly [key: string]: string | number | Array<string | number> | null | undefined;
readonly [key: string]: string | number | boolean | Array<string | number | boolean> | null | undefined;
}

/**
Expand Down
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ function parse(input, options) {
decode: true,
sort: true,
arrayFormat: 'none',
parseNumbers: false
parseNumbers: false,
parseBooleans: false
}, options);

const formatter = parserForArrayFormat(options);
Expand Down Expand Up @@ -205,6 +206,10 @@ function parse(input, options) {
value = Number(value);
}

if (options.parseBooleans && value !== null && (value.toLowerCase() === 'true' || value.toLowerCase() === 'false')) {
value = value.toLowerCase() === 'true';
}

formatter(decode(key, options), value, ret);
}

Expand Down
10 changes: 8 additions & 2 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ expectType<queryString.ParsedQuery>(
expectType<queryString.ParsedQuery>(
queryString.parse('?foo=1', {parseNumbers: true})
);
expectType<queryString.ParsedQuery>(
queryString.parse('?foo=true', {parseBooleans: true})
);

// Parse URL
expectType<queryString.ParsedUrl>(queryString.parseUrl('?foo=bar'));
Expand All @@ -73,8 +76,11 @@ expectType<queryString.ParsedUrl>(
expectType<queryString.ParsedUrl>(
queryString.parseUrl('?foo=bar', {arrayFormat: 'comma'})
);
expectType<queryString.ParsedQuery>(
queryString.parse('?foo=1', {parseNumbers: true})
expectType<queryString.ParsedUrl>(
queryString.parseUrl('?foo=1', {parseNumbers: true})
);
expectType<queryString.ParsedUrl>(
queryString.parseUrl('?foo=true', {parseBooleans: true})
);

// Extract
Expand Down
12 changes: 12 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,18 @@ queryString.parse('foo=1', {parseNumbers: true});

Parse the value as a number type instead of string type if it's a number.

##### parseBooleans

Type: `boolean`<br>
Default: `false`

```js
queryString.parse('foo=true', {parseBooleans: true});
//=> {foo: true}
```

Parse the value as a boolean type instead of string type if it's a boolean.

### .stringify(object, [options])

Stringify an object into a query string and sorting the keys.
Expand Down
9 changes: 9 additions & 0 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,12 @@ test('NaN value returns as string if option is set', t => {
t.deepEqual(queryString.parse('foo=undefined', {parseNumbers: true}), {foo: 'undefined'});
t.deepEqual(queryString.parse('foo=100a&bar=100', {parseNumbers: true}), {foo: '100a', bar: 100});
});

test('boolean value returns as string by default', t => {
t.deepEqual(queryString.parse('foo=true'), {foo: 'true'});
});

test('boolean value returns as boolean if option is set', t => {
t.deepEqual(queryString.parse('foo=true', {parseBooleans: true}), {foo: true});
t.deepEqual(queryString.parse('foo=false&bar=true', {parseBooleans: true}), {foo: false, bar: true});
});

0 comments on commit f958329

Please sign in to comment.