query-string icon indicating copy to clipboard operation
query-string copied to clipboard

Support predefining types

Open goodwin64 opened this issue 4 years ago • 8 comments

(related to https://github.com/sindresorhus/query-string/issues/201)

Consider 2 examples:

const search1 = '?phoneNumber=%2B380951234567&subtopicId=2&topicId=1';
const result1 = queryString.parse(search, {
  parseBooleans: true,
  parseNumbers: true,
});

const search2 = '?phoneNumber=a%2B380951234567&subtopicId=2&topicId=1';
const result2 = queryString.parse(search, {
  parseBooleans: true,
  parseNumbers: true,
});

console.log('result1', result1);
// {
//   phoneNumber: 380951234567,
//   subtopicId: 2,
//   topicId: 1,
// };
console.log('result2', result2);
// {
//   phoneNumber: 'a+380951234567',
//   subtopicId: 2,
//   topicId: 1,
// };

In the 1st example, "+" sign (%2B escaped) is ignored thus reducing to the number parsing, which is not what it supposed to be.

The main goal is to keep parsing numbers for parameters which values starts on the number and parse as plain string otherwise.

Does it look feasible to make a PR on this or should I not use parseNumbers option in that case?

goodwin64 avatar Sep 11 '19 10:09 goodwin64

+380951234567 is a valid number though. You would expect '-380951234567' to be parsed as a number, right? That being said, I agree it's not a optimal behavior.

Regardless of what we decide to do with this, we should better document the behavior of what's considered a number.

// @yaodingyd

sindresorhus avatar Oct 02 '19 17:10 sindresorhus

What should be the optimal behavior? Here it's using Number() function to convert to number.

yaodingyd avatar Oct 02 '19 17:10 yaodingyd

I meant if I store the number "+123" in the URL and then I parse it with "parseNumbers" option I get lost the plus sign (+) and the parsed value will be just "123".

Meanwhile I want to store some number-like parameters in URL so removing the "parseNumbers" option is not the right solution for me.

goodwin64 avatar Oct 03 '19 05:10 goodwin64

So when I prepend the value with the prefix "a" it's just a workaround not to lost the plus sign :)

"a+123" get parsed as is and then I just skip the "a" letter, which causes a lot of questions what is "a"

goodwin64 avatar Oct 03 '19 05:10 goodwin64

I think some kinda schema support here could solve the ambiguity. You could then explicitly define the type for this one key. See: https://github.com/sindresorhus/query-string/issues/228#issuecomment-575197530

sindresorhus avatar Jan 16 '20 15:01 sindresorhus

Hi, I would like to help you to solve this issue, I was thinking about you said about schema.

What do you think about something like this

const search2 = '?phoneNumber=a%2B380951234567&subtopicId=2&topicId=1';
const result2 = queryString.parse(search, {
    parseBooleans: true,
    parseNumbers: true,
    schema: {
        phoneNumber: String, // StringConstructor
    }
});

and when you are trying to parse always check the schema and see if have a property with the same name of the URL param, with this you will be able to parse correctly any primitive type.

and in the lib, maybe we can pass down the propertyName of the value that will be parsed to make something like

function parseValue(value, options, propertyName) {
const { schema } = options;
const type = schema[propertyName];
// if have type use in the function, if not keep the default behaviour 
}

iagolaguna avatar Feb 14 '20 12:02 iagolaguna

I would prefer something like this:

const search2 = '?phoneNumber=a%2B380951234567&subtopicId=2&topicId=1';
const result2 = queryString.parse(search, {
    parseBooleans: true,
    parseNumbers: true,
    types: {
        phoneNumber: 'string',
        subtopicId: value => Number(value)
    }
});

The type could also be a function to let the user handle the converting it from a string to a custom type. Don't forget that it also needs to be able to handle arrays.

sindresorhus avatar Feb 14 '20 14:02 sindresorhus

If anyone wants to work on this, see the feedback given in https://github.com/sindresorhus/query-string/pull/249.

sindresorhus avatar Jun 03 '20 14:06 sindresorhus