/*! * * Copyright 2017 - acrazing * * @author acrazing joking.young@gmail.com * @since 2017-12-15 13:35:11 * @version 1.0.0 * @desc as-map.ts */ import { AMap, NMap, SMap } from './map'; /** * create a Record according to input type, cases can get in spec file * @param source * @param key */ export function asMap( source: T[], key: K, ): K extends undefined ? T extends number ? NMap : T extends string ? SMap : never : K extends keyof T ? T[K] extends number ? NMap : T[K] extends string ? SMap : never : never { if (!source) { return {} as any; } const map: AMap = {}; if (!key) { source.forEach((item) => { map[item as any] = item; }); } else { source.forEach((item) => { map[item[key as Exclude] as any] = item; }); } return map as any; }