ljharb / qs

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

use charset with decoder #419

Closed ultralabsgev closed 3 years ago

ultralabsgev commented 3 years ago

when I'm parsing my URL decoder ignoring charset. os I decoding charsets manually

qs.parse(search, { charset: 'utf-8', ignoreQueryPrefix: true, decoder: customDecoder, })

const customDecoder = ( str: string, defaultDecoder: (str: string, decoder?: any, charset?: string) => string, charset: string, type: 'key' | 'value' ) => { if (type === 'key') { return str; } return [str]; };

this is my problem what I need dateEnd: ["2021-08-18T20:00:00.000Z"] dateStart: ["2021-08-02T20:00:00.000Z"]

this is what I get with my decoder dateEnd: ["2021-08-18T20%3A00%3A00.000Z"] dateStart: ["2021-08-02T20%3A00%3A00.000Z"]

this is my fix but it's BS I add it inside my decoder const validStr = str.replace(/%20/g, ' ').replace(/%3A/g, ':');

ljharb commented 3 years ago

I'm not sure what you're asking - you need the colons unencoded? Why?

Can you provide search so i can try to reproduce this?

ultralabsgev commented 3 years ago

openingStatuses=Submitted&dateStart=2021-08-02T20%3A00%3A00.000Z&dateEnd=2021-08-19T20%3A00%3A00.000Z %3A = :

ljharb commented 3 years ago
> qs.parse('openingStatuses=Submitted&dateStart=2021-08-02T20%3A00%3A00.000Z&dateEnd=2021-08-19T20%3A00%3A00.000Z', { charset: 'utf-8' })
{
  openingStatuses: 'Submitted',
  dateStart: '2021-08-02T20:00:00.000Z',
  dateEnd: '2021-08-19T20:00:00.000Z'
}
> qs.parse('openingStatuses=Submitted&dateStart=2021-08-02T20%3A00%3A00.000Z&dateEnd=2021-08-19T20%3A00%3A00.000Z', {
  charset: 'utf-8',
  decoder(str, defaultDecoder, charset, type) {
    var result = defaultDecoder(str); return type === 'key' ? result : [result];
  }
});
{
  openingStatuses: [ 'Submitted' ],
  dateStart: [ '2021-08-02T20:00:00.000Z' ],
  dateEnd: [ '2021-08-19T20:00:00.000Z' ]
}
ultralabsgev commented 3 years ago

f*** I forgot about the default decoder thx