1
\$\begingroup\$

How would you merge these two functions into a single more generic function?

// concatStringsFromObject :: Object, Array, String -> Strings const concatStringsFromObject = (o = {}, a = [], j = ' ') => { return a.filter( x => o.hasOwnProperty(x) ) .map( x => o[x] ) .join(j); }; // concatStringsFromArrayOfObject :: Array, String, String -> Strings const concatStringsFromArrayOfObject = (a = [], s = '', j = ', ') => { return a.filter( x => x.hasOwnProperty(s) ) .map( x => x[s] ) .join(j); }; 
\$\endgroup\$
0

    1 Answer 1

    1
    \$\begingroup\$

    The best functions have functional cohesion, meaning they do one thing, and do it well. I don't see a reason to merge these functions, and I don't see how that would be an improvement.

    The first function takes Object, Array, String arguments, the second one takes Array, String, String. If you need something that works with both Object and Array, then you can make it check the underlying type of its parameter, and delegate to the appropriate method. So it will be a 3rd function, using the other two under the hood.

    I need to be easy to test.

    There's nothing special here to make testing hard. You can either test the two main worker methods directly, or test everything through the 3rd method.

    \$\endgroup\$
    0

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.