Giter Site home page Giter Site logo

Comments (4)

JUSTIVE avatar JUSTIVE commented on July 17, 2024 1

I've just found O.all from ts-belt 4.0.0.

https://github.com/mobily/ts-belt/blob/c825d9709de1e5d15b1b362429e0cee56c96c516/src/Option/index.ts#L213C1-L215C32

const el1: O.Option<string> = O.Some("3");
const el2: O.Option<boolean> = O.Some(true);
const el3: O.Option<number> = O.Some(4);
const el4: O.Option<string[]> = O.Some(["hello"]);

const result = 
  pipe(
    [el1,el2,el3,el4],
    O.all,
    O.map(([el1,el2,el3,el4])=> /* your function goes here */), 
  )

This is probably the right function for you.

from ts-belt.

JUSTIVE avatar JUSTIVE commented on July 17, 2024

I'd prefer ts-pattern for these scenarios.

const el1: O.Option<string> = O.Some("3");
const el2: O.Option<boolean> = O.Some(true);
const el3: O.Option<number> = O.Some(4);
const el4: O.Option<string[]> = O.Some(["hello"]);

const optionsResult = 
  match([el1,el2,el3,el4])
  .with([P.not(P.nullish),P.not(P.nullish),P.not(P.nullish),P.not(P.nullish)],
    ([el1,el2,el3,el4])=>({el1,el2,el3,el4}))
  .otherwise(F.always(O.None))

I've experienced the same dx issue in the rescript. When handling >3 option types, using pattern matching was much better to read.

in other languages such as purescript or haskell, they have do-notation for this but unfortunately, javascript does not.

from ts-belt.

araera111 avatar araera111 commented on July 17, 2024

I am also a fan of ts-pattern.

I did as you described and it seems to be working well.
Thanks for your help, I appreciate it.

I thought of a utility function during my lunch break.
The following is not type safe as it uses as assertion.

const el1: O.Option<string> = O.Some("hello");
const el2: O.Option<boolean> = O.Some(true);
const el3: O.Option<number> = O.Some(4);
const el4: O.Option<string[]> = O.Some(["hello", "world"]);

type OptionObject<T> = {
  [K in keyof T]: Option<T[K]>;
};

const zipAllOption = <T>(
  options: OptionObject<T>,
): Option<{ [K in keyof T]: T[K] }> =>
  pipe(
    options,
    D.toPairs,
    A.every(([_, v]) => O.isSome(v)),
    B.ifElse(
      () => options as { [K in keyof T]: T[K] },
      () => O.None,
    ),
  );

const optionResult = zipAllOption({ el1, el2, el3, el4 });

from ts-belt.

araera111 avatar araera111 commented on July 17, 2024

Thanks!

from ts-belt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.