Legend for query parameters

When I have a url:

http://server/site?firstname=Jack&lastname=Daniels

      

I understand that in my JavaScript the query for firstname should return "Jack".

But what should the query return for firstname in the following cases:

http://server/site?lastname=Daniels
http://server/site?firstname&lastname=Daniels
http://server/site?firstname=&lastname=Daniels

      

[Edit] To answer some of the comments: all of the above are legal queries, my question is not how to get the parameters, but how to interpret them.

For the record, I'm parsing queries with the following regular expression, which covers all cases:

/([^?=&;]+)(?:=([^&;]*))?/g

      

There seems to be a very popular question out there about how to get the querystring parameters , but the answer is wrong (or at least doesn't address edge cases).

[Update] My choice is based on @Bergi and @zzzzBov answers:

http://server/site?lastname=Daniels                => firstname: undefined
http://server/site?firstname&lastname=Daniels      => firstname: true
http://server/site?firstname=&lastname=Daniels     => firstname: ""
http://server/site?firstname=Jack&lastname=Daniels => firstname: "Jack"

      

A side effect is that I had to slightly modify my regex as the above rules must capture the character =

:

/([^?=&;]+)(=[^&;]*)?/g

      

+3


source to share


2 answers


But what should the request return

Is it your decision, what do you need? There are several query evaluation functions with varying degrees of complexity. Some of them may deal with repetitive, nested syntax values ​​in parentheses, while others may not. The known ones can be found in How do I get query string values ​​in JavaScript? and his recurring questions.

However, you requested agreements:

http://server/site?lastname=Daniels

The result must be something false to represent the absence firstName

. You can choose null

or undefined

.



http://server/site?firstname&lastname=Daniels

This is often some true

kind of boolean parameter, so a return would be legal. However, I am sure there are libraries out there that completely ignore this format.

http://server/site?firstname=&lastname=Daniels

This is obviously an empty line ""

.

0


source


But what should the query return for firstname in the following cases:

http://server/site?lastname=Daniels
http://server/site?firstname&lastname=Daniels
http://server/site?firstname=&lastname=Daniels

      

Query strings can be easily represented by object notation in JavaScript. Just set only the keys that are present in the query string of the object.

This means that for the ?lastname=Daniels

created object must be:

{
    lastname: 'Daniels'
}

      

In cases where a key is present but no value is specified (no equal sign), the key must be set with a value null

that represents "no value".

This means that for the ?firstname&lastname=Daniels

created object must be:

{
    firstname: null,
    lastname: 'Daniels'
}

      

In cases where a key is present and the supplied value is empty (equal to sign), it is effectively an empty string.

This means that for the ?firstname=&lastname=Daniels

created object must be:

{
    firstname: '',
    lastname: 'Daniels'
}

      

There is also an often forgotten case where the same key is used multiple times. In traditional query string syntax (not PHP), keys can be used multiple times, as-is, to represent an array.

This means that for the ?foo=bar&foo=baz

created object must be:

{
    foo: [
        'bar',
        'baz'
    ]
}

      



<& GT side;

In the PHP scope, the keys used for arrays are labeled []

:

`?foo[]=bar&foo[]=baz`

      

PHP will automatically convert the server-side query string so that:

$_GET['foo'] = array('bar', 'baz')

      

</ & towards GT;

Returning to the original question, this implies the following:

  • ?lastname=Daniels

    matters undefined

    for the key firstname

    .
  • ?firstname&lastname=Daniels

    matters null

    for the key firstname

    .
  • ?firstname=&lastname=Daniels

    matters ''

    for the key firstname

    .

In cases where a key is used as a boolean based on its presence, you should check to see if the object has a set of keys (and ignores the value entirely, as it doesn't matter).

This is usually done with obj.hasOwnProperty('key')

, but since the object is indeed larger than the hash, it Object.prototype.hasOwnProperty.call(obj, 'key')

is preferable
that it can be abstracted into a function has

:

has(obj, key) {
    return Object.prototype.hasOwnProperty.call(obj, key);
}

      

If you are using underscore.js you can use _.has(obj, key)

+1


source







All Articles