npm

@prairielearn/run
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

@prairielearn/run

A small utility for emulating expression-oriented programming, Inspired by https://maxgreenwald.me/blog/do-more-with-run.

Usage

import { run } from '@prairielearn/run';

const x = run(() => {
  if (foo()) return 1;
  if (bar()) return 2;
  return 3;
});

This is equivalent to the following:

let x;

if (foo()) {
  x = 1;
} else if (bar()) {
  x = 2;
} else {
  x = 3;
}

It's also equivalent to this:

const x = foo() ? 1 : bar() ? 2 : 3;

While the nested ternary is more concise in this example, it's more difficult to read and maintain as the number and complexity of conditions grows.

Why?

Max Greenwald has a great blog post that explains the rationale for this pattern: https://maxgreenwald.me/blog/do-more-with-run

Why is this one-liner a package? Mostly so the documentation and rationale can be kept in one place.

Readme

Keywords

none

Package Sidebar

Install

npm i @prairielearn/run

Weekly Downloads

7

Version

1.0.1

License

none

Unpacked Size

4.04 kB

Total Files

14

Last publish

Collaborators

  • nwalters512
  • mwest1066