[Typescript] 131. Extreme - Query String Parser
You're required to implement a type-level parser to parse URL query string into a object literal type.
Some detailed requirements:
- Value of a key in query string can be ignored but still be parsed to
true
. For example,'key'
is without value, so the parser result is{ key: true }
. - Duplicated keys must be merged into one. If there are different values with the same key, values must be merged into a tuple type.
- When a key has only one value, that value can't be wrapped into a tuple type.
- If values with the same key appear more than once, it must be treated as once. For example,
key=value&key=value
must be treated askey=value
only.
/* _____________ Your Code Here _____________ */
type ToKeyValPair<T extends string> = T extends `${infer K extends string}=${infer V}` ? {[Key in K]: V}: {[Key in T]: true};
type MapToKeyValPair<T extends string[]> = T extends [infer F extends string, ...infer RT extends string[]] ? [ToKeyValPair<F>, ...MapToKeyValPair<RT>]: [];
type SplitQuery<T extends string> = T extends `${infer Q}&${infer RT}` ? [Q, ...SplitQuery<RT>]: [T];
type MergeObject<T> = {
[P in keyof T]: T[P]
}
type Grouping<U, ACC extends Record<PropertyKey, unknown>= {}> = U extends object[]
? U extends [infer F, ...infer RT]
? Grouping<RT, MergeObject<({
[Key in keyof ACC as Key extends keyof F ? never: Key]: ACC[Key]
} & {
[Key in keyof F]: Key extends keyof ACC
? ACC[Key] extends any[]
? [...ACC[Key], F[Key]]
: Equal<F[Key], ACC[Key]> extends true ? F[Key]: [ACC[Key], F[Key]]
: F[Key]
})>>
: ACC
:ACC
type ParseQueryString<T extends string> = T extends '' ? {}: Grouping<MapToKeyValPair<SplitQuery<T>>>
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<ParseQueryString<''>, {}>>,
Expect<Equal<ParseQueryString<'k1'>, { k1: true }>>,
Expect<Equal<ParseQueryString<'k1&k1'>, { k1: true }>>,
Expect<Equal<ParseQueryString<'k1&k2'>, { k1: true; k2: true }>>,
Expect<Equal<ParseQueryString<'k1=v1'>, { k1: 'v1' }>>,
Expect<Equal<ParseQueryString<'k1=v1&k1=v2'>, { k1: ['v1', 'v2'] }>>,
Expect<Equal<ParseQueryString<'k1=v1&k2=v2'>, { k1: 'v1'; k2: 'v2' }>>,
Expect<Equal<ParseQueryString<'k1=v1&k2=v2&k1=v2'>, { k1: ['v1', 'v2']; k2: 'v2' }>>,
Expect<Equal<ParseQueryString<'k1=v1&k2'>, { k1: 'v1'; k2: true }>>,
Expect<Equal<ParseQueryString<'k1=v1&k1=v1'>, { k1: 'v1' }>>,
Expect<Equal<ParseQueryString<'k1=v1&k2=v2&k1=v2&k1=v3'>, { k1: ['v1', 'v2', 'v3']; k2: 'v2' }>>,
]