Typed functional tools
Find a file
2021-08-27 01:01:08 +02:00
doc Add defined() and options() 2021-08-27 01:01:08 +02:00
.editorconfig Updated testing tools 2020-05-13 11:46:10 +02:00
.gitignore Removed vscode config 2020-05-12 00:49:11 +02:00
comparison.test.ts Add defined() and options() 2021-08-27 01:01:08 +02:00
comparison.ts Switched to deno 2020-05-12 00:34:57 +02:00
composition.test.ts Add defined() and options() 2021-08-27 01:01:08 +02:00
composition.ts Switched to deno 2020-05-12 00:34:57 +02:00
deps.testing.ts Add defined() and options() 2021-08-27 01:01:08 +02:00
iterables.test.ts Add defined() and options() 2021-08-27 01:01:08 +02:00
iterables.ts Switched to deno 2020-05-12 00:34:57 +02:00
mod.ts Add defined() and options() 2021-08-27 01:01:08 +02:00
objects.test.ts Add defined() and options() 2021-08-27 01:01:08 +02:00
objects.ts Add defined() and options() 2021-08-27 01:01:08 +02:00
predicates.test.ts Add defined() and options() 2021-08-27 01:01:08 +02:00
predicates.ts Switched to deno 2020-05-12 00:34:57 +02:00
README.md Add defined() and options() 2021-08-27 01:01:08 +02:00
run Add defined() and options() 2021-08-27 01:01:08 +02:00
tsconfig.json Switched to deno 2020-05-12 00:34:57 +02:00
typing.test.ts Add defined() and options() 2021-08-27 01:01:08 +02:00
typing.ts Switched to deno 2020-05-12 00:34:57 +02:00

typescript/functional

Build Status

About

Provides some common helpers for functional-style programming.

Import

In deno:

import { bool } from "https://js.thunderk.net/functional/mod.ts";

In browser:

<script type="module">
import { bool } from "https://js.thunderk.net/functional/mod.js";
</script>

Use

always and never return true and false respectively

always(); // => true
never(); // => false

and, or and not allow to combine or negate predicates

const a = and((x: number) => x > 2, (x: number) => x < 5);
a(2); // => false
a(3); // => true
a(4); // => true
a(5); // => false
const o = or((x: number) => x < 2, (x: number) => x > 3);
o(1); // => true
o(2); // => false
o(3); // => false
o(4); // => true
const n = not((x: number) => x == 1);
n(0); // => true
n(1); // => false
n(2); // => true

at gets an array's item by position:

const getthird = at(2);
getthird([2, 4, 8, 16]); // => 8
getthird([2, 4]); // => undefined
const getsecondlast = at(-2);
getsecondlast([1, 2, 3, 4]); // => 3
getsecondlast([1]); // => undefined

attr gets an object's attribute:

const getx = attr("x");
getx({ x: 3 }); // => 3

bool checks for boolean equivalence (in a broader sense than !(!(val))):

bool(undefined); // => false
bool(null); // => false

bool(-1); // => true
bool(0); // => false
bool(1); // => true

bool(""); // => false
bool(" "); // => true
bool("abc"); // => true

bool([]); // => false
bool([1, 2, 3]); // => true

bool({}); // => false
bool({ x: 1 }); // => true

cmp simplifies the use of array sorting:

[8, 3, 5].sort(cmp()) // => [3, 5, 8]
[8, 3, 5].sort(cmp({ reverse: true })) // => [8, 5, 3]
[-2, 8, -7].sort(cmp({ key: Math.abs })); // => [-2, -7, 8]

defined removes undefined values from an object:

defined({ a: 1, b: undefined }); // => {a: 1}

first and last return the first or last item of an array:

first([1, 2, 3]); // => 1
first([]); // => undefined
last([1, 2, 3]); // => 3
last([]); // => undefined

identity returns its argument untouched:

a === identity(a); // => true

is and isinstance checks for strict equality and inheritance:

const f = is(8);
f(8); // => true
f(5 + 3); // => true
f("8"); // => false
f(null); // => false

class A {}
class A1 extends A {}
class A2 extends A {}
class B {}
const f: any[] = [5, null, undefined, new A(), new A1(), new B(), new A2()];
const result: A[] = f.filter(isinstance(A)); // => [f[3], f[4], f[6]]

nn, nu and nnu checks at run-time for null or undefined:

nn(undefined); // => undefined
nn(null); // => Error
nn(1); // => 1

nu(undefined); // => Error
nu(null); // => null
nu(1); // => 1

nnu(undefined); // => Error
nnu(null); // => Error
nnu(1); // => 1

nop does nothing (useful for some callbacks):

new ConstructorWithMandatoryCallback(nop);

options merges options over default values:

options({ a: 1, b: 2, c: 3, d: 4 }, { a: 11, c: 33 }, { b: 22 });
// => {a: 11, b: 22, c: 33, d: 4}

partial applies a partial configuration object as first argument of compatible functions:

const sum = (args: { a: number; b: number }) => args.a + args.b;
const plus1 = partial({ a: 1 }, sum);
plus1({ b: 8 }); // => 9

pipe chains two functions as one:

const f = pipe((x: number) => x * 2, (x: number) => x + 1);
f(3); // => 7 ((3 * 2) + 1)