npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@seiyab/do-expr

v1.0.1

Published

do expression as a package inspired by https://tc39.es/proposal-do-expressions/

Downloads

13

Readme

package of do expressions

This package is inspired by proposal-do-expressions

  • https://github.com/tc39/proposal-do-expressions
  • https://tc39.github.io/proposal-do-expressions/

Motivation

  • expression-oriented programming one of the great advances of FP
  • expressions plug together like legos, making more malleable programming experience in-the-small

Installation

npm install @seiyab/do-expr

Examples

Write in an expression-oriented style, scoping variables as locally as possible:

import { do_ } from "@seiyab/do-expr";

let x = do_(() => {
  let tmp = f();
  return tmp * tmp + 1
});

Use conditional statements as expressions, instead of awkward nested ternaries:

let x = do_(() => {
  if (foo()) { return f() }
  else if (bar()) { return g() }
  else { return h() }
});

Especially nice for templating languages like JSX:

return (
  <nav>
    <Home />
    {do_(() => {
      if (loggedIn) return <LogoutButton />;
      return <LoginButton />;
    })}
  </nav>
)

Edge cases

var declarations

var declarations are allowed, with the hoisting to the parameter function's scope. This behavior differs from proposal-do-expressions.

Empty do

do_(() => {}) is almost equivalent to undefined.

Some lint tool might blame it.

await

Works fine. To use await, you need an await before do_ and an async before ().

const result = await do_(async () => {
  const x = await fetchSomeData();
  if (x.isSuccess) return "ok";
  return "oops";
});

throw

Works fine. Does what you expect.

break/continue/yield

These won't work like proposal-do-expressions.

The major reason is technical one. And it's also for readability. Allowing these might induce confusion. These don't fit the motivation "expression-oriented programming".

return

It has a different functionality from proposal-do-expressions.

return is used for declaring result of do_, rather than returning from outer function.

Conflict with do-while

No problem at all.

Comparison

vs. IIFE

Yes, do-expr is just IIFE as you noticed. do-expr still has some advantages.

The main advantage is readability. We need to read last part of the expressions to know whether expressions are IIFEs or not.

// plain IIFE
const x = (() => {
//         ^^^^^ is x a function?
  if (f()) return g();
  return h();
})()
//^^ Finally I got it. this is an IIFE.

// do_
const x = do_(() => {
//        ^^^ OK, this behaves like a do expression.
  if (f()) return g();
  return h();
})

As another idea, you can declare _do argument to indicate it behaves like do expression.

const x = ((_do) => {
//          ^^^ OK, this behave like a do expression.
  if (f()) return g();
  return h();
})()

In this case, you need to write coding guideline documents and notify colleagues. Because this is not idiomatic yet. If readers don't know this idiom, they are confused by a mysterious _do argument and might consider that it is a function declaration until they reach the last ().

On the other hand, do_ already has README.md that you read now. If readers don't know do_, they are likely to google @seiyab/do-expr and find this README.

vs. proposal-do-expressions

Functionality

Some features like break / continue / yield doesn't work with @seiyab/do-expr.

The reasons are not only technical ones. It is an intended interface design. The main motivation of @seiyab/do-expr is expression-oriented programming. Though the features are perhaps helpful, they don't fit the original motivation. They will induce unnecessary complexity.

The following issue reports proposal-do-expressions vs IIFEs: https://github.com/tc39/proposal-do-expressions/issues/34

Availability

proposal-do-expressions is still stage 1 of the TC39 process. But a babel plugin @babel/plugin-proposal-do-expression is available.

@seiyab/do-expr already has stable release.