7.0.0 • Published 3 years ago

@bcg-ts/ts-toolkit v7.0.0

Weekly downloads
28
License
MIT
Repository
github
Last release
3 years ago

ts-toolkit

Helper Code For Typescript Projects (not fully documented, see code for a better understanding of available features).

Function type aliases

type Fn0<R> = () => R;
type Fn<P1, R> = (p1: P1) => R
// Fn2
// Fn3
// Fn4
// Fn5
// Fn6
// Fn7
// Fn8

type Consumer<T> = Fn<T, void>;
type Supplier<T> = Fn0<T>;
type Predicate<T> = Fn<T, boolean>;
type Reducer<T> = Fn2<T, T, T>;
type Comparator<T> = Fn2<T, T, -1|0|1>;
type Callback = Fn0<void>;

compose

Can compose up to 10 functions

import {Fn, compose} from "@bcg-ts/ts-toolkit";

const fn1: Fn<number, string> = (val: number) => val.toString();
const fn2: Fn<string, number> = (val: string) => val.length;
const fn3: Fn<number, number> = compose(fn1, fn2);

TypeUtils

import {TypeUtils} from "@bcg-ts/ts-toolkit";

TypeUtils.boolean(false); // true
TypeUtils.boolean(1); // false
// TypeUtils.function( ... )
// TypeUtils.nonNull( ... )
// TypeUtils.nonEmptyString( ... )
// TypeUtils.null( ... )
// TypeUtils.undefined( ... )
// TypeUtils.number( ... )
// TypeUtils.object( ... )
// TypeUtils.string( ... )
// TypeUtils.symbol( ... )
// TypeUtils.unknown ( ... )

TypeUtils.field({ value: 1 }, "value", TypeUtils.number) // true

Pipeline

uses "compose" internally to avoid intermediate values

import {Fn, Pipeline} from "@bcg-ts/ts-toolkit";

const numDigits: Fn<number, number> = Pipeline
  .identity<number>()
  .map((val) => val.toString())
  .map((str) => str.length)
  .callable;

const numDigitsStr: Fn<number, string> = Pipeline
  .fromCallable(numDigits)
  .map((val) => val.toString())
  .callable;

Optional

import {Optional} from "@bcg-ts/ts-toolkit";

const result: number = Optional.some(1) // number
  .map((val) => val + 1)                // number
  .filter((val) => val > 0)             // number | undefined
  .orElse(0)                            // number
  .value;                               // number
7.0.0

3 years ago

6.0.1

3 years ago

6.0.0

3 years ago

5.2.0

3 years ago

5.1.0

3 years ago

5.0.0

3 years ago

4.1.0

4 years ago

4.0.0

5 years ago

3.2.1

5 years ago

3.1.1

5 years ago

3.1.0

5 years ago

3.0.0

5 years ago

2.6.2

5 years ago

2.6.1

5 years ago

2.6.0

5 years ago

2.5.1

5 years ago

2.5.0

5 years ago

2.4.0

5 years ago

2.3.1

5 years ago

2.3.0

5 years ago

2.2.0

5 years ago

2.1.0

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.1.0

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago