Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: parseNumbers support includes & excludes options #369

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion base.d.ts
Expand Up @@ -127,6 +127,7 @@ export type ParseOptions = {

/**
Parse the value as a number type instead of string type if it's a number.
Note: will always return a string if it exceeds the maximum safe integer in JavaScript

@default false

Expand All @@ -136,9 +137,15 @@ export type ParseOptions = {

queryString.parse('foo=1', {parseNumbers: true});
//=> {foo: 1}

queryString.parse('foo=1&bar=2', {parseNumbers: {includes: ['foo']}});
//=> {foo: 1, bar: '2'}

queryString.parse('foo=1&bar=2', {parseNumbers: {excludes: ['foo']}});
//=> {foo: '1', bar: 2}
```
*/
readonly parseNumbers?: boolean;
readonly parseNumbers?: boolean | {includes?: string[]; excludes?: string[]};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels too limiting. I would prefer a filter method that receives the key and value and returns true whether it should be parsed as a number. But the ideal solution would be something like #210

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. I'll look at #249 remaining unfinished parts

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you decide to work no #249, I would recommend posting a proposal on what to do in #249 before writing any code to make sure it's the most optimal solution.


/**
Parse the value as a boolean type instead of string type if it's a boolean.
Expand Down
41 changes: 28 additions & 13 deletions base.js
Expand Up @@ -300,11 +300,31 @@ function getHash(url) {
return hash;
}

function parseValue(value, options) {
if (options.parseNumbers && !Number.isNaN(Number(value)) && (typeof value === 'string' && value.trim() !== '')) {
value = Number(value);
} else if (options.parseBooleans && value !== null && (value.toLowerCase() === 'true' || value.toLowerCase() === 'false')) {
value = value.toLowerCase() === 'true';
function getNumberValue(value) {
const numberValue = Number(value);

return Number.isNaN(numberValue) || numberValue > Number.MAX_SAFE_INTEGER ? value : numberValue;
}

function parseValue(key, value, options, returnValue) {
if (options.parseBooleans && value !== null && (value.toLowerCase() === 'true' || value.toLowerCase() === 'false')) {
return value.toLowerCase() === 'true';
}

if (options.parseNumbers && (typeof value === 'string' && value.trim() !== '')) {
if (typeof options.parseNumbers === 'object') {
const {includes = Object.keys(returnValue), excludes = []} = options.parseNumbers;

if (!includes.includes(key)) {
return value;
}

if (excludes.includes(key)) {
return value;
}
}

return getNumberValue(value);
}

return value;
Expand Down Expand Up @@ -370,10 +390,10 @@ export function parse(query, options) {
for (const [key, value] of Object.entries(returnValue)) {
if (typeof value === 'object' && value !== null) {
for (const [key2, value2] of Object.entries(value)) {
value[key2] = parseValue(value2, options);
value[key2] = parseValue(key2, value2, options, returnValue);
}
} else {
returnValue[key] = parseValue(value, options);
returnValue[key] = parseValue(key, value, options, returnValue);
}
}

Expand All @@ -385,12 +405,7 @@ export function parse(query, options) {
// eslint-disable-next-line unicorn/no-array-reduce
return (options.sort === true ? Object.keys(returnValue).sort() : Object.keys(returnValue).sort(options.sort)).reduce((result, key) => {
const value = returnValue[key];
if (Boolean(value) && typeof value === 'object' && !Array.isArray(value)) {
// Sort object keys, not values
result[key] = keysSorter(value);
} else {
result[key] = value;
}
result[key] = Boolean(value) && typeof value === 'object' && !Array.isArray(value) ? keysSorter(value) : value;

return result;
}, Object.create(null));
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -55,7 +55,7 @@
"deep-equal": "^2.1.0",
"fast-check": "^3.4.0",
"tsd": "^0.25.0",
"xo": "^0.53.1"
"xo": "^0.54.2"
},
"tsd": {
"compilerOptions": {
Expand Down
11 changes: 9 additions & 2 deletions readme.md
Expand Up @@ -197,18 +197,25 @@ Supports both `Function` as a custom sorting function or `false` to disable sort

##### parseNumbers

Type: `boolean`\
Type: `boolean | { includes?: string[], excludes?: string[] }`\
Default: `false`

```js
import queryString from 'query-string';

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

queryString.parse('foo=1&bar=2', {parseNumbers: {includes: ['foo']}});
//=> {foo: 1, bar: '2'}

queryString.parse('foo=1&bar=2', {parseNumbers: {excludes: ['foo']}});
//=> {foo: '1', bar: 2}
```
Parse the value as a number type instead of string type if it's a number.

> Note: will always return a string if it exceeds the maximum safe integer in JavaScript

##### parseBooleans

Type: `boolean`\
Expand Down
25 changes: 25 additions & 0 deletions test/parse.js
Expand Up @@ -324,6 +324,31 @@ test('NaN value returns as string if option is set', t => {
t.deepEqual(queryString.parse('foo= &bar=', {parseNumbers: true}), {foo: ' ', bar: ''});
});

test('exceed JavaScript\'s maximum safe integer value returns as string if option is set', t => {
// 9007199254740991 is Number.MAX_SAFE_INTEGER
t.deepEqual(queryString.parse('foo=9007199254740991', {parseNumbers: true}), {foo: Number.MAX_SAFE_INTEGER});
t.deepEqual(queryString.parse('foo=9007199254740992', {parseNumbers: true}), {foo: '9007199254740992'});
t.deepEqual(queryString.parse('foo=9007199254740992', {parseNumbers: {includes: ['foo']}}), {foo: '9007199254740992'});
t.deepEqual(queryString.parse('foo=9007199254740992', {parseNumbers: {excludes: ['foo']}}), {foo: '9007199254740992'});
});

test('only keys that match the includes configuration in parseNumbers will return number if parseNumbers.includes is set', t => {
t.deepEqual(queryString.parse('foo=1&bar=2', {parseNumbers: {includes: ['foo', 'bar']}}), {foo: 1, bar: 2});
t.deepEqual(queryString.parse('foo=1&bar=2', {parseNumbers: {includes: ['foo']}}), {foo: 1, bar: '2'});
t.deepEqual(queryString.parse('foo=null&bar=2', {parseNumbers: {includes: ['foo']}}), {foo: 'null', bar: '2'});
t.deepEqual(queryString.parse('foo=1&bar=2&baz=3', {parseNumbers: {includes: []}}), {foo: '1', bar: '2', baz: '3'});
t.deepEqual(queryString.parse('foo=1&bar=2&baz=3', {parseNumbers: {includes: ['foo'], excludes: ['baz']}}), {foo: 1, bar: '2', baz: '3'});
t.deepEqual(queryString.parse('foo=1&bar=2&baz=3', {parseNumbers: {includes: []}}), {foo: '1', bar: '2', baz: '3'});
});

test('only keys that does not match the excludes configuration in parseNumbers will return number if parseNumbers.excludes is set', t => {
t.deepEqual(queryString.parse('foo=1&bar=2', {parseNumbers: {excludes: ['foo', 'bar']}}), {foo: '1', bar: '2'});
t.deepEqual(queryString.parse('foo=1&bar=2', {parseNumbers: {excludes: ['foo']}}), {foo: '1', bar: 2});
t.deepEqual(queryString.parse('foo=1&bar=2', {parseNumbers: {excludes: []}}), {foo: 1, bar: 2});
t.deepEqual(queryString.parse('foo=1&bar=null', {parseNumbers: {excludes: ['foo']}}), {foo: '1', bar: 'null'});
t.deepEqual(queryString.parse('foo=1&bar=2&baz=3', {parseNumbers: {excludes: ['baz']}}), {foo: 1, bar: 2, baz: '3'});
});

test('parseNumbers works with arrayFormat', t => {
t.deepEqual(queryString.parse('foo[]=1&foo[]=2&foo[]=3&bar=1', {parseNumbers: true, arrayFormat: 'bracket'}), {foo: [1, 2, 3], bar: 1});
t.deepEqual(queryString.parse('foo=1,2,a', {parseNumbers: true, arrayFormat: 'comma'}), {foo: [1, 2, 'a']});
Expand Down