diff --git a/test/rebuild.test.ts b/test/rebuild.test.ts new file mode 100644 index 0000000..ce0be71 --- /dev/null +++ b/test/rebuild.test.ts @@ -0,0 +1,29 @@ +import { expectType } from 'tsd'; + +import { rebuild } from '../es'; + +const oldObj = { foo: '123-456', bar: '678' }; + +const newObj = rebuild(([k, v]) => { + return v.split('-').map((n, i) => [`${k}${i}`, n]); +}, oldObj); + +expectType>(newObj); + +const newObj2 = rebuild(([k, v]) => { + return [[k, v.split('-')]]; +}, oldObj); + +expectType>(newObj2); + +const newObj3 = rebuild(([k, v]) => { + const innerObj = Object.fromEntries(v.split('-').map((n, i) => [i, n])); + return [[k, innerObj]]; +}, oldObj); + +expectType>>(newObj3); + +const diffValueTypes = { foo: 123, bar: 'blah' }; + +const updated = rebuild(([k, v]) =>[[k, v]], diffValueTypes); +expectType>(updated); diff --git a/types/mapKeys.d.ts b/types/mapKeys.d.ts new file mode 100644 index 0000000..f89dcb7 --- /dev/null +++ b/types/mapKeys.d.ts @@ -0,0 +1,8 @@ +import { Placeholder } from './util/tools'; + +// mapKeys(fn)(obj) +export function mapKeys(fn: (key: string) => string): (obj: Record) => Record; +// mapKeys(__, obj)(fn) +export function mapKeys(__: Placeholder, obj: Record): (fn: (key: string) => string) => Record; +// mapKeys(fn, obj) +export function mapKeys(fn: (key: string) => string, obj: Record): Record; diff --git a/types/rebuild.d.ts b/types/rebuild.d.ts new file mode 100644 index 0000000..d8c92e3 --- /dev/null +++ b/types/rebuild.d.ts @@ -0,0 +1,8 @@ +import { Placeholder } from './util/tools'; + +// rebuild(fn)(obj) +export function rebuild(fn: (kvp: [keyof T, T[keyof T]]) => [string, V][]): (obj: T) => Record; +// rebuild(__, obj)(fn) +export function rebuild(__: Placeholder, obj: T): (fn: (kvp: [keyof T, T[keyof T]]) => [string, V][]) => Record; +// rebuild(fn, obj) +export function rebuild(fn: (kvp: [keyof T, T[keyof T]]) => [string, V][], obj: T): Record;