0.1.2 • Published 7 years ago

node-select v0.1.2

Weekly downloads
2
License
ISC
Repository
github
Last release
7 years ago

node-select

Select is syntactic sugar to help maintain a functional approach to assigning to variables. It lets you avoid reassigning, so you can keep things const-y, if you're into that sort of thing. 🤷🏽‍

Please don't look at the source code, as it's embarassingly small. This is a grower, not a shower, ok?

Usage

Lets say you have something like this.

  let o = null;
  if (someCondition) {
    o = someConstructorMaybe();
  } else if (someOtherCondition) {
    o = somethingElse;
  }

Maybe you, like me, don't really like the look of that snippet, and would prefer it if you could assign directly to o with a single statement, and keep it as a const. You could do something like this instead:

  const o = (() => {
    if (someCondition) {
      return someConstructorMaybe();
    } else if (someOtherCondition) {
      return somethingElse;
    } else {
      return null;
    }
  })();

Good old self-invocing function pattern. Well, if you're still not quite happy, you can use select. Like this:

  const o = select(criteriaObject, (criteria) => {
    if (criteria.someCondition) {
      return someConstructorMaybe();
    } else if (criteria.someOtherCondition) {
      return somethingElse;
    } else {
      return null;
    }
  });

Plans for world domination

In my future, ECMAScript will support this syntax:

  const o = select (criteriaObject) {
    if (this.someCondition) {
      return someConstructorMaybe();
    } else if (this.someOtherCondition) {
      return somethingElse;
    } else {
      return null;
    }
  }

One can always dream...