const UNSAFE_KEYS = new Set(['__proto__', 'constructor', 'prototype']); function decode(text: string): string { const value = text.replace(/\+/g, ' '); try { return decodeURIComponent(value); } catch { return value; } } /** `a[b][]` → `['a', 'b', '']`; levels past `depth` stay together in the last key. */ function splitKey(key: string, depth: number): string[] { const open = key.indexOf('['); if (open <= 0) { return [key]; } const keys = [key.slice(0, open)]; const segment = /\[([^[\]]*)\]/g; segment.lastIndex = open; let end = open; let match: RegExpExecArray | null; while (keys.length <= depth && (match = segment.exec(key)) && match.index === end) { keys.push(match[1]); end = segment.lastIndex; } if (end < key.length) { keys.push(key.slice(end)); } return keys; } function isIndex(key: string, arrayLimit: number): boolean { return /^\d+$/.test(key) && Number(key) <= arrayLimit; } /** An array that is about to get a named key becomes an object with its indexes as keys. */ function toObject(value: unknown[]): Record { const object: Record = {}; value.forEach((item, i) => { if (item !== undefined) { object[i] = item; } }); return object; } function assign(target: any, keys: string[], value: string, arrayLimit: number): any { const [key, ...rest] = keys; if (key === undefined) { return target === undefined ? value : Array.isArray(target) ? [...target, value] : [target, value]; } if (Array.isArray(target)) { if (key === '') { target.push(assign(undefined, rest, value, arrayLimit)); return target; } if (!isIndex(key, arrayLimit)) { target = toObject(target); } } if (target === undefined || typeof target !== 'object') { if (key === '' || isIndex(key, arrayLimit)) { target = []; if (key === '') { target.push(assign(undefined, rest, value, arrayLimit)); return target; } } else { target = {}; } } target[key] = assign(target[key], rest, value, arrayLimit); return target; } function compact(value: any): any { if (Array.isArray(value)) { return value.filter(item => item !== undefined).map(compact); } if (value && typeof value === 'object') { for (const key of Object.keys(value)) { value[key] = compact(value[key]); } } return value; } export namespace FormUtil { export interface ParseOptions { /** Most parameters; more throw a `RangeError`. */ parameterLimit?: number; /** Most levels of nesting. */ depth?: number; /** Largest index that makes an array: `a[200]=x` makes `{ 200: 'x' }`. */ arrayLimit?: number; } /** * `application/x-www-form-urlencoded` with the brackets of PHP and qs: `a[b]=1` → `{ a: { b: '1' } }`, * `a[]=1&a[]=2` and `a=1&a=2` → `{ a: ['1', '2'] }`. `__proto__`, `constructor` and `prototype` keys are dropped. */ export function parse(text: string, { parameterLimit = 1000, depth = 32, arrayLimit = 100 }: ParseOptions = {}): Record { const result: Record = {}; if (!text) { return result; } const pairs = text.split('&'); if (pairs.length > parameterLimit) { throw new RangeError('too many parameters'); } for (const pair of pairs) { if (!pair) { continue; } const equals = pair.indexOf('='); const key = decode(equals < 0 ? pair : pair.slice(0, equals)); if (!key) { continue; } const value = equals < 0 ? '' : decode(pair.slice(equals + 1)); const keys = splitKey(key, depth); if (keys.some(k => UNSAFE_KEYS.has(k))) { continue; } const [first, ...rest] = keys; result[first] = assign(result[first], rest, value, arrayLimit); } return compact(result); } }