remotealex / std-fns

Cleaner and easier to reason JS/TS for all

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

std-fns

A very simple set of (fixed arity) functions which are transparent wrappers around the standard Javascript built-in object operators.

Why?

To write software with Javascript you rely on functions. Unfortunately, many of the utilities built in to the Javascript language are housed as instance methods which as a Javascript developer yourself, you will seldom write.

This means that your code will be littered with different function call syntax.

For example, have a read of this typically trivial "beginner" funtion.

How many ways to invoke a function1 can you count?

function greet(name: string) {
  const nameLowerCase = name.toLowerCase();

  if (nameLowerCase.includes("world")) {
    return "Hello, Mr. Predictable";
  }

  return "Hello, " + name;
}

console.log(greet("Mr. World"));

Answer: Three! It's much easier to write/read code which has a single, standard way to call functions and pass in arguments. This tiny suite of tools aims to make Javacsript easier for us all.

Using std-fns we can make this code much easier to read and explain:

function greet(name: string) {
  const nameLowerCase = Str.toLowerCase(name);

  if (Str.includes(nameLowerCase, "world")) {
    return "Hello, Mr. Predictable";
  }

  return Str.concat("Hello, ", name);
}

console.log(greet("Mr. World")); // "Hello, Mr. Predictable"

It also makes parameter partial application much easier:

import R from "ramda";

const includesWorld = R.partialRight(Str.includes, ["world"]);

function greet(name: string) {
  const nameLowerCase = Str.toLowerCase(name);

  if (includesWorld(nameLowerCase)) {
    return "Hello, Mr. Predictable";
  }

  return Str.concat("Hello, ", name);
}

console.log(greet("Mr. World")); // "Hello, Mr. Predictable"

Test drive

To install dependencies (with bun):

bun install

To run:

bun run index.ts

Footnotes

  1. Functions, instance and static methods will be used interchangeable here

About

Cleaner and easier to reason JS/TS for all


Languages

Language:TypeScript 100.0%