I have a function that takes a string transformation function and a an object and applies the transformation to all of the keys, returning the resulting object.
In particular I'm using Ramda
Here is my function (untyped for clarity).
const renameBy = R.curry(
(fn, obj) => R.pipe(
R.toPairs,
R.map([k, v] => [fn(k), v]),
R.fromPairs
)(obj)
);
I'm well aware of the issues currying can cause, so I'm really only looking for solutions addressing the core function (and optionally the second unary function, renameBy(fn)) and resolving a nicer return type than the current R.Dictionary, which basically never matches anything.
I've been looking here, dealing with renaming specific keys of an object and here, dealing with recursive, deep nested-object renaming but neither seems to quite be what I'm looking for.
Example of usage
const input = {
foo_bar: "foobar",
bar: "bar",
bar_foo: "barfoo"
}
const camelize = (paramName) =>
paramName.replace(
/_(.?)/g,
$1 => $1.substr(1).toUpperCase()
);
renameBy(camelize, input)
const expectedOutput = {
fooBar: "foobar",
bar: "bar",
barFoo: "barfoo"
}
It's possible I could use something like
function renameBy<
F extends (str: string) => string,
N extends string[],
M extends Record<string, any>
>(fn: F, obj: M): { [P in N]: M[keyof M] }
{...}
But I can't quite get it to work.