Skip to content

String matching syntax is not idiomatic with JS #2

@juanojeda

Description

@juanojeda

Hey,
This looks great! Only comment I would add is that the when("_", ...) syntax feels a bit weird. For instance, what if you actually care if the value is "_"? It's a small edge case, but I would probably feel more comfortable with an explicit default function or keyword somehow.

In Scala, the default case _ is treated as syntax, but in the case of your library it could be useful to have it as something like:

match(person)
  .when("case 1", value1)
  .when("case 2", value2)
  .default(value)

or you could implement the default keyword (named entity) to use instead, and use it like:

import match, { _ } from "match-when-value"

match(person)
  .when("1", value1)
  .when(_, valueDefault)

The same thinking would apply to the Pick and Skip functionality provided. Matching against strings is tricky, especially for those not familiar with the origins of the ~, ~; etc notation (like me!)). One way you could make this more accessible is by providing a named entity against which to match, like so:

import match, { _, pickOne, skipRest } from "match-when-value"

match(person)
  .when([1,skipRest,pickOne], value1)
  .when(_, valueDefault)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions