From 3e40c7ea602a1781eb44540b43e9bd810c82e984 Mon Sep 17 00:00:00 2001 From: harris-miller Date: Mon, 23 Dec 2024 23:56:50 -0700 Subject: [PATCH 1/5] WIP --- types/mapKeys.d.ts | 1 + types/rebuild.d.ts | 1 + 2 files changed, 2 insertions(+) create mode 100644 types/mapKeys.d.ts create mode 100644 types/rebuild.d.ts diff --git a/types/mapKeys.d.ts b/types/mapKeys.d.ts new file mode 100644 index 0000000..5aef541 --- /dev/null +++ b/types/mapKeys.d.ts @@ -0,0 +1 @@ +declare 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..73f6ac2 --- /dev/null +++ b/types/rebuild.d.ts @@ -0,0 +1 @@ +export function rebuild(fn: (kvp: [string, A]) => [string, B][], obj: Record): Record; From ef2642cc113cc6cc6d039d33bf8c4fedce9b6b19 Mon Sep 17 00:00:00 2001 From: harris-miller Date: Wed, 23 Apr 2025 18:41:29 -0600 Subject: [PATCH 2/5] curry them --- types/mapKeys.d.ts | 9 ++++++++- types/rebuild.d.ts | 7 +++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/types/mapKeys.d.ts b/types/mapKeys.d.ts index 5aef541..f89dcb7 100644 --- a/types/mapKeys.d.ts +++ b/types/mapKeys.d.ts @@ -1 +1,8 @@ -declare function mapKeys(fn: (key: string) => string, obj: Record): Record; +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 index 73f6ac2..dd5b40b 100644 --- a/types/rebuild.d.ts +++ b/types/rebuild.d.ts @@ -1 +1,8 @@ +import { Placeholder } from './util/tools'; + +// rebuild(fn)(obj) +export function rebuild(fn: (kvp: [string, A]) => [string, B][]): (obj: Record) => Record; +// rebuild(__, obj)(fn) +export function rebuild(__: Placeholder, obj: Record): (fn: (kvp: [string, A]) => [string, B][]) => Record; +// rebuild(fn, obj) export function rebuild(fn: (kvp: [string, A]) => [string, B][], obj: Record): Record; From e586197773239067f8801a205cfb1a19bcea7e6c Mon Sep 17 00:00:00 2001 From: harris-miller Date: Wed, 2 Jul 2025 20:32:01 -0600 Subject: [PATCH 3/5] rebuild --- test/rebuild.test.ts | 24 ++++++++++++++++++++++++ types/rebuild.d.ts | 6 +++--- 2 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 test/rebuild.test.ts diff --git a/test/rebuild.test.ts b/test/rebuild.test.ts new file mode 100644 index 0000000..79bd4b7 --- /dev/null +++ b/test/rebuild.test.ts @@ -0,0 +1,24 @@ +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); diff --git a/types/rebuild.d.ts b/types/rebuild.d.ts index dd5b40b..d8a5815 100644 --- a/types/rebuild.d.ts +++ b/types/rebuild.d.ts @@ -1,8 +1,8 @@ import { Placeholder } from './util/tools'; // rebuild(fn)(obj) -export function rebuild(fn: (kvp: [string, A]) => [string, B][]): (obj: Record) => Record; +export function rebuild(fn: (kvp: [keyof Obj, Obj[keyof Obj]]) => [string, OutValue][]): (obj: Obj) => Record; // rebuild(__, obj)(fn) -export function rebuild(__: Placeholder, obj: Record): (fn: (kvp: [string, A]) => [string, B][]) => Record; +export function rebuild(__: Placeholder, obj: Obj): (fn: (kvp: [keyof Obj, Obj[keyof Obj]]) => [string, OutValue][]) => Record; // rebuild(fn, obj) -export function rebuild(fn: (kvp: [string, A]) => [string, B][], obj: Record): Record; +export function rebuild(fn: (kvp: [keyof Obj, Obj[keyof Obj]]) => [string, OutValue][], obj: Obj): Record; From dfde053191a1eb9b8d4fe3f6f6d98fc2c8dc83d5 Mon Sep 17 00:00:00 2001 From: harris-miller Date: Wed, 2 Jul 2025 21:39:38 -0600 Subject: [PATCH 4/5] simplify rebuild types --- types/rebuild.d.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/types/rebuild.d.ts b/types/rebuild.d.ts index d8a5815..d8c92e3 100644 --- a/types/rebuild.d.ts +++ b/types/rebuild.d.ts @@ -1,8 +1,8 @@ import { Placeholder } from './util/tools'; // rebuild(fn)(obj) -export function rebuild(fn: (kvp: [keyof Obj, Obj[keyof Obj]]) => [string, OutValue][]): (obj: Obj) => Record; +export function rebuild(fn: (kvp: [keyof T, T[keyof T]]) => [string, V][]): (obj: T) => Record; // rebuild(__, obj)(fn) -export function rebuild(__: Placeholder, obj: Obj): (fn: (kvp: [keyof Obj, Obj[keyof Obj]]) => [string, OutValue][]) => Record; +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 Obj, Obj[keyof Obj]]) => [string, OutValue][], obj: Obj): Record; +export function rebuild(fn: (kvp: [keyof T, T[keyof T]]) => [string, V][], obj: T): Record; From 5a0aded4d44fd155d5d46333d4986c478bf27d50 Mon Sep 17 00:00:00 2001 From: harris-miller Date: Wed, 2 Jul 2025 21:52:30 -0600 Subject: [PATCH 5/5] tests and other updates --- test/rebuild.test.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/rebuild.test.ts b/test/rebuild.test.ts index 79bd4b7..ce0be71 100644 --- a/test/rebuild.test.ts +++ b/test/rebuild.test.ts @@ -22,3 +22,8 @@ const newObj3 = rebuild(([k, v]) => { }, oldObj); expectType>>(newObj3); + +const diffValueTypes = { foo: 123, bar: 'blah' }; + +const updated = rebuild(([k, v]) =>[[k, v]], diffValueTypes); +expectType>(updated);