Skip to content

How to merge object methods by composing them? #31

Answered by RebeccaStevens
batusai513 asked this question in Q&A
Discussion options

You must be logged in to vote

Here's how you would customize deepmerge-ts to compose the functions:

import { deepmergeCustom } from "deepmerge-ts";

export const myDeepmerge = deepmergeCustom({
  mergeOthers: (values, utils) => {
    if (areAllFunctions(values)) {
      return composeFunctions(values);
    }
    return utils.defaultMergeFunctions.mergeOthers(values, utils);
  },
});

function areAllFunctions(
  values: readonly unknown[]
): values is ReadonlyArray<(...args: unknown[]) => unknown> {
  return values.every((value) => typeof value === "function");
}

function composeFunctions(fns: ReadonlyArray<(...args: unknown[]) => unknown>) {
  return (param: unknown) =>
    fns.reduceRight((accumulator, fn) => fn(acc…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@batusai513
Comment options

@RebeccaStevens
Comment options

@batusai513
Comment options

Answer selected by batusai513
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants