Files
core/hwe/ts/util/mergeKVArray.ts
T
Hide_D 28d71b8ce2 js2ts: install_db.php
- jqValidateForm 구현
  - async-validator + 수작업
- common.js 없이 ventor -> 타겟 ts 직접
2021-08-16 23:50:12 +09:00

35 lines
930 B
TypeScript

import { isString } from "lodash";
interface NameValuePair {
name: string,
value: string
}
export function mergeKVArray(array : NameValuePair[]):Record<string, string|string[]>{
const result:Record<string, string|string[]> = {};
for(const {name, value} of array){
if(!isString(name)){
throw new TypeError(`${name} is not string`);
}
if(!isString(value)){
throw new TypeError(`${value} is not string`);
}
if(name === '' || name === '[]'){
continue;
}
if(name.length > 2 && name.slice(-2) == '[]'){
const keyHead = name.slice(0, -2);
if(!(keyHead in result)){
result[keyHead] = [value];
}
else{
(result[keyHead] as string[]).push(value);
}
continue;
}
result[name] = value;
}
return result;
}