ljharb / qs

A querystring parser with nesting support
BSD 3-Clause "New" or "Revised" License
8.5k stars 729 forks source link

default encoder support filter specified characters? #416

Closed zwwtj2014 closed 3 years ago

zwwtj2014 commented 3 years ago

https://github.com/ljharb/qs/blob/master/lib/utils.js#L157

        if (
            ...
            || fliterCharacter(c)
        ) {
            out += string.charAt(i);
            continue;
        }

hi, default encoder can support filter specified characters?

maybe implement as above?

fliterCharacter is a function used to determine character c whether escape is required, and passed by the user

ljharb commented 3 years ago

The format defines that for the default encoder - can you elaborate on your use case, and why passing your own encoder doesn’t solve it?

zwwtj2014 commented 3 years ago

can you elaborate on your use case, and why passing your own encoder doesn’t solve it?

passing my own encoder can solve it, but it cannot reuse existing features of the default encoder.


my use case:

qs.stringify({ aaa: '=+/' })
// now: aaa=%3D%2B%2F
// but i want: aaa=%3D+%2F

So in my use case, i want all the other features of the default encoder expect for a few special characters , such as the + above. If i self-implement the encoder, i have two ideas:

  1. use defaultEncoder first, then decode %2B to +
  2. encode all character by myself

And i do not think the two ideas above are better than giving a hook 😄

ljharb commented 3 years ago

but it cannot reuse existing features of the default encoder.

Sure you can. Your custom encoder gets the default encoder as its second argument.

The first example seems like the best approach at the moment. I agree that adding a hook is more convenient for your use case, but I'm still not clear on what it is - why would you want the + unencoded on the default format? is this alternate format something standard - or something that somebody besides you would need?

zwwtj2014 commented 3 years ago

I'm still not clear on what it is - why would you want the + unencoded on the default format? is this alternate format something standard - or something that somebody besides you would need?

yep,maybe server side unencode use non-standard lib, and i have asked them to check

The first example seems like the best approach at the moment.

ok, thanks.. and i will use first example to polyfill until they check done.