unsplash / pipe-ts

32 stars 4 forks source link

Support falsy paramaters ? #14

Open illiaChaban opened 3 months ago

illiaChaban commented 3 months ago

I love your pipe-ts package, thanks for creating that! There was one use case that I wish it supported - falsy parameters

Example

const data = pipeWith(   
  apiData,   
  ...,  
  someOtherData && doSomethingToData(someOtherData)
)

In theory, some helper function like iif(condition, doThis, doThat?) could be used, but it doesn't work well in cases where condition part needs to be used to compose doThis function.

I have made it working locally with full TS inference and I was wondering if you think it could be a useful update to this package. If so, i could put up a pull request

If you'd rather keep the original package simple, I could fork your package and make my own

Thanks!

illiaChaban commented 3 months ago

Here's maybe a better example

const finalData = pipeWith(array1, array2 && concat(array2))

Without using function approach & followiing immutability principles, this would look like this

const finalData = [...array1]
if (array2) finalData.push(...array2)

// Or without unnecessarily cloning the first array

let finalData = array1
if (array2) finalData = [...finalData, ...array2]

IMO the first example is a lot more readable, shorter and there's no mutable / immutable (+ const / let) overhead

Or another one

const data = pipeWith(array, featureEnabled && addSomethingToTheList)