[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 as key=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' }>>,
]

 

posted @   Zhentiw  阅读(25)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2021-12-08 [AWS] CloudFormation: create EC2 with SecurityGroupIds for custom VPC
2020-12-08 [Javascript] Broadcaster + Operator + Listener pattern -- 24. Design choice, ifElse or merge
2020-12-08 [Git] Revert to a old commit
2020-12-08 [Tools] Dendron
2019-12-08 [Algorithm] 46. Permutations
2019-12-08 [React] Write a generic React Suspense Resource factory
2019-12-08 [React] Fetch Data with React Suspense
点击右上角即可分享
微信分享提示