import { interleave } from './Collection'; import { records } from './Table'; import { divider, defined, FilterFunction } from './Filters'; type Item = { [key: string]: unknown }; type MapperFunction = (value: T | string | unknown) => T; type Mapper = { [K in keyof T]: MapperFunction }; type Params = Parameters; type Table = { (...args: Params): Array }; type TableOf = { (...args: Params): Array }; /** * Create a new template literal function parsing tables with a custom set of filters * * @export * @param {...Array} filters * @returns {(...args: Params) => Array} */ export function create( ...filters: Array ): (...args: Params) => Array { return (...args: Params) => records(interleave(...args), ...filters) as Array; } /** * Table template literal parser, exludes any divider ro, preserves rows * consisting of only undefined values * * * @template T * @param {TemplateStringsArray} strings * @param {...Array} values * @returns {Array} */ export const empty = create(divider); /** * Table template literal parser, exludes both any divider row and * rows that contain only undefined values * * @template T * @param {TemplateStringsArray} strings * @param {...Array} values * @returns {Array} */ export const table =
( Object.assign(create(divider, defined), { empty }) ); /** * Create a new table template literaral parser, mapping specified keys * * @export * @template T * @param {Mapper} mapper * @return {Table} */ export function mapper(mapper: Partial>): TableOf { const mapping = >Object.keys(mapper).reduce( (carry, key) => (item: T) => carry({ ...item, [key]: (mapper as Mapper)[key](item[key]) }), (item: T): T => item, ); return (...args: Params): Array => table(...args).map(mapping); }