Options
All
  • Public
  • Public/Protected
  • All
Menu

@appsweet-co/utils

Index

Functions

Const Either

  • Either(__namedParameters: { left: (val: any) => any; right: (val: any) => any; test: (val: any) => boolean }): (Anonymous function)
  • A Monad-like function that tests a value against a predicate.

    Calls right if the test passes or left if the test fails.

    import { Either } from '@appsweet-co/utils';
    
    const moreThan6 = Either({
      test: x => x > 6,
      left: x => onFail(x),
      right: x => onSuccess(x)
    });
    
    moreThan6(8);
    // => onSuccess(8)
    
    moreThan6(4);
    // => onFail(4)

    Parameters

    • __namedParameters: { left: (val: any) => any; right: (val: any) => any; test: (val: any) => boolean }
      • left: (val: any) => any
          • (val: any): any
          • Parameters

            • val: any

            Returns any

      • right: (val: any) => any
          • (val: any): any
          • Parameters

            • val: any

            Returns any

      • test: (val: any) => boolean
          • (val: any): boolean
          • Parameters

            • val: any

            Returns boolean

    Returns (Anonymous function)

Const Maybe

  • Maybe<T>(val?: T): { chain: any; else: any; filter: any; map: any; pipe: any; value: any }
  • A monad for working with values that may or may not be null or undefined.

    Inspired by James Sinclair, Pratica and Eric Elliott.

    import { Maybe } from '@appsweet-co/utils';

    Type parameters

    • T = any

    Parameters

    • Optional val: T

    Returns { chain: any; else: any; filter: any; map: any; pipe: any; value: any }

    • chain: function
      • chain(fn: (arg: T) => MaybeType<unknown>): any
      • Return a new monad from inside the first. This is a clean way to work with a second value that may or may not be null or undefined.

        Maybe(6).chain(x => Maybe(null))
        // => Maybe(null)

        Chain as many monads as you need:

        Maybe(6)
         .chain(x => Maybe(x + 1)) // => Maybe(7)
         .chain(y => Maybe(y + 1)) // => Maybe(8)
         // etc...

        Parameters

        Returns any

    • else: function
      • else(fallback: T)
      • Return a monad with the fallback value. This is a clean way to guarantee a default value.

        Maybe(null).else(6)
        // => Maybe(6)

        Parameters

        • fallback: T
    • filter: function
      • filter(...fns: any[]): any
      • Test the value against one or more predicates. Returns a monad with the value if and only if all predicates return true.

        Functions are composed left-to-right.

        Maybe(6).filter(x => x % 2 === 0, x < 10)
        // => Maybe(6)
        
        Maybe(6).filter(x => x % 2 === 0, x < 5)
        // => Maybe(undefined)

        Parameters

        • Rest ...fns: any[]

        Returns any

    • map: function
      • map(...fns: any[])
      • Transform the value inside the monad using one or more transformation functions.

        Functions are composed left-to-right.

        Maybe(6).map(x => x * 2, y => y + 5)
        // => Maybe(17)

        Parameters

        • Rest ...fns: any[]
    • pipe: function
      • pipe(...fns: any[]): any
      • Send the value to one or more functions and return the original value. This is a clean way to cause side effects.

        Functions are composed left-to-right.

        Maybe(6)
         .pipe(x => x * 2, console.log) // => 12
         .map(x => x + 2)
         .pipe(console.log) // => 8

        Parameters

        • Rest ...fns: any[]

        Returns any

    • value: function
      • value(): T
      • Unwrap the value from the monad and returns the value in its raw form.

        Maybe(6).value();
        // => 6

        Returns T

Const add

  • add(a: number, b?: number): number
  • Adds two numbers. Same as a + b.

    Inspired by Ramda.js

    import { add } from '@appsweet-co/utils';
    
    add(2, 3);
    // => 5
    
    const add2 = add(2);
    
    add2(3);
    // => 5

    The following are equivalent:

    add(a)(b);
    add(a, b);

    Parameters

    • a: number
    • Optional b: number

    Returns number

Const all

  • all(fn: (...args: unknown[]) => unknown, list: unknown[]): boolean
  • Returns true if and only if all elements of the list match the predicate. Same as list.every(fn).

    Inspired by Ramda.js.

    import { all } from '@appsweet-co/utils';
    
    all(x => x === 2, [2, 2, 2]);
    // => true
    
    const all2 = all(x => x ===2);
    
    all2([2, 2, 10]);
    // => false

    The following are equivalent:

    all(fn)(list);
    all(fn, list);

    Parameters

    • fn: (...args: unknown[]) => unknown
        • (...args: unknown[]): unknown
        • Parameters

          • Rest ...args: unknown[]

          Returns unknown

    • list: unknown[]

    Returns boolean

Const any

  • any(fn: (...args: unknown[]) => unknown, list: unknown[]): boolean
  • Returns true if at least one element in the list matches the predicate. Same as list.some(fn).

    Inspired by Ramda.js.

    import { any } from '@appsweet-co/utils';
    
    any(x => x === 2, [1, 2, 3]);
    // => true
    
    const any2 = any(x => x === 2);
    
    any2([1, 3, 5]);
    // => false

    The following are equivalent:

    any(fn)(list);
    any(fn, list);

    Parameters

    • fn: (...args: unknown[]) => unknown
        • (...args: unknown[]): unknown
        • Parameters

          • Rest ...args: unknown[]

          Returns unknown

    • list: unknown[]

    Returns boolean

Const bind

  • bind(fn: (...args: unknown[]) => unknown, ...args: unknown[]): unknown
  • Binds arguments to any function. Same as fn.bind(null, ...args).

    Inspired by Ramda.js.

    import { bind } from '@appsweet-co/utils';
    
    const add = (a, b) => a + b;
    
    const add2 = bind(add, 2);
    
    add2(3);
    // => 5

    Parameters

    • fn: (...args: unknown[]) => unknown
        • (...args: unknown[]): unknown
        • Parameters

          • Rest ...args: unknown[]

          Returns unknown

    • Rest ...args: unknown[]

    Returns unknown

Const clamp

  • clamp(...args: [min: number, max?: number, val?: number]): number
  • Returns a number between min and max values.

    Inspired by Ionic.

    import { clamp } from '@appsweet-co/utils';
    
    clamp(3, 5, 1);
    => 3
    
    clamp(3, 5, 4);
    // => 4
    
    clamp(3, 5, 12);
    // => 5
    
    const range = clamp(3, 5);
    
    range(1);
    // => 3
    
    range(4);
    // => 4
    
    range(12);
    // => 5

    The following are equivalent:

    clamp(min)(max)(val);
    clamp(min)(max, val);
    clamp(min, max)(val);
    clamp(min, max, val);

    Parameters

    • Rest ...args: [min: number, max?: number, val?: number]

    Returns number

Const clone

  • clone(val: any): any
  • Returns a deep copy of an object of any depth, if able. Returns val unchanged if val is not an object.

    Inspired by Hey, JavaScript.

    import { clone } from '@appsweet-co/utils';
    
    const copy = clone(complexObj);

    Parameters

    • val: any

    Returns any

Const curry

  • curry<T>(fn: (...args: any[]) => any, ...args: any[]): T
  • Transforms a method of any arity into a curried function.

    Inspired by William Vincent.

    import { curry } from '@appsweet-co/utils';
    
    const add = (a, b, c) => a + b + c;
    
    const add9 = curry(add, 3, 6);
    
    add9(5)
    // => 14

    curry() accepts an optional generic return type:

    const add9 = curry<number>(add, 3, 6);

    Type parameters

    • T = any

    Parameters

    • fn: (...args: any[]) => any
        • (...args: any[]): any
        • Parameters

          • Rest ...args: any[]

          Returns any

    • Rest ...args: any[]

    Returns T

Const dec

  • dec(val: number): number
  • Decrements a value by one. Same as val - 1.

    Inspried by Ramda.js

    import { dec } from '@appsweet-co/utils';
    
    dec(10);
    // => 9

    Parameters

    • val: number

    Returns number

Const divide

  • divide(a: number, b?: number): number
  • Divide the first argument by the second. Same as a / b.

    Inspired by Ramda.js.

    import { divide } from '@appsweet-co/utils';
    
    divide(10, 2);
    // => 5
    
    const div10 = divide(10);
    
    div10(2);
    // => 5

    The following are equivalent:

    divide(a)(b);
    divide(a, b);

    Parameters

    • a: number
    • Optional b: number

    Returns number

Const filter

  • filter(...fns: ((arg: any) => any)[]): (Anonymous function)
  • Applies one or more predicates to each item in a list.

    Functions are composed left-to-right.

    import { filter } from '@appsweet-co/utils';
    
    const evens = filter(x => x !== 0, y => y % 2 ===0);
    
    evens([1, 2, 3, 0, 4, 5]);
    // => [2, 4]

    This function works very well with pipe.

    Parameters

    • Rest ...fns: ((arg: any) => any)[]

    Returns (Anonymous function)

Const first

  • first(n: number, list?: any): any
  • Returns the first n values in a list.

    Inspried by Real World Haskell.

    import { first } from '@appsweet-co/utils';
    
    first(3, [1, 2, 3, 0, 4, 5]);
    // => [1, 2, 3]
    
    const first3 = first(3)
    
    first3([1, 2, 3, 0, 4, 5]);
    // => [1, 2, 3]
    

    The following are equivalent:

    first(n)(list);
    first(n, list);

    Parameters

    • n: number
    • Optional list: any

    Returns any

Const inc

  • inc(val: number): number
  • Increments a value by one. Same as val + 1.

    Inspried by Ramda.js

    import { inc } from '@appsweet-co/utils';
    
    inc(10);
    // => 11

    Parameters

    • val: number

    Returns number

Const last

  • last(n: number, list?: any): any
  • Returns the last n values in a list.

    Inspried by Real World Haskell.

    import { last } from '@appsweet-co/utils';
    
    last(3, [1, 2, 3, 0, 4, 5]);
    // => [0, 4, 5]
    
    const last3 = last(3)
    
    last3([1, 2, 3, 0, 4, 5]);
    // => [0, 4, 5]
    

    The following are equivalent:

    last(n)(list);
    last(n, list);

    Parameters

    • n: number
    • Optional list: any

    Returns any

Const map

  • map(...fns: ((arg: any) => any)[]): (Anonymous function)
  • Applies one or more transformation functions to each item in a list.

    Functions are composed left-to-right.

    Inspired by Ramda.js.

    import { map } from '@appsweet-co/utils';
    
    const add4 = map(x => x + 1, y => y + 3);
    
    add4([1, 2, 3])
    // => [5, 6, 7]

    This function works very well with pipe.

    Parameters

    • Rest ...fns: ((arg: any) => any)[]

    Returns (Anonymous function)

Const max

  • max(a: number, ...more: number[]): number
  • Returns the largest of zero or more numbers given as input. Same as Math.max().

    Return a curried function by passing only one parameter in the initial call.

    import { max } from '@appsweet-co/utils';
    
    max(3, 6, 9);
    // => 9
    
    const max9 = max(9);
    
    max9(3, 12);
    // => 12
    
    max9(3, 6);
    // => 9

    The following are equivalent:

    max(a)(...more);
    max(a, ...more);

    Parameters

    • a: number
    • Rest ...more: number[]

    Returns number

Const min

  • min(a: number, ...more: number[]): number
  • Returns the largest of zero or more numbers given as input. Same as Math.min().

    Return a curried function by passing only one parameter in the initial call.

    import { min } from '@appsweet-co/utils';
    
    min(3, 6, 9);
    // => 3
    
    const min9 = min(9);
    
    min9(3, 12);
    // => 3

    The following are equivalent:

    min(a)(...more);
    min(a, ...more);

    Parameters

    • a: number
    • Rest ...more: number[]

    Returns number

Const neg

  • neg(arg: number | number[]): number | number[]
  • Negates a value or an array of values.

    Inspired by Ramda.js.

    import { neg } from '@appsweet-co/utils';
    
    neg(9);
    // => -9
    
    neg([9, -13]);
    // => [-9, 13]

    Parameters

    • arg: number | number[]

    Returns number | number[]

Const nil

  • nil(val: any): boolean
  • Returns true if the value is null or undefined.

    import { nil } from '@appsweet-co/utils';
    
    nil([1, 2, 3, 0, 4, 5]);
    // => false
    
    nil(null);
    // => true

    Parameters

    • val: any

    Returns boolean

Const none

  • none(fn: (...args: unknown[]) => unknown, list: unknown[]): boolean
  • Returns true if and only if no elements of of the list match the predicate Same as !list.every(fn).

    Inspired by Ramda.js.

    import { none } from '@appsweet-co/utils';
    
    none(x => x === 2, [1, 2, 3]);
    // => false
    
    const none2 = none(x => x ===2);
    
    none2([1, 3, 5]);
    // => true

    The following are equivalent:

    none(fn)(list);
    none(fn, list);

    Parameters

    • fn: (...args: unknown[]) => unknown
        • (...args: unknown[]): unknown
        • Parameters

          • Rest ...args: unknown[]

          Returns unknown

    • list: unknown[]

    Returns boolean

Const or

  • or(...args: [fallback: any, val?: any]): any
  • Returns val if and only if:

    1. val is not null or undefined; and
    2. val and fallback are of the same type.

    Returns fallback otherwise.

    import { or } from '@appsweet-co/utils';
    
    or([], [1, 2, 3, 0, 4, 5]);
    // => [1, 2, 3, 0, 4, 5]
    
    const orArray = or([]);
    
    orArray(true);
    // => [];
    
    The following are equivalent:
    

    or(fallback)(val); or(fallback, val);

    Parameters

    • Rest ...args: [fallback: any, val?: any]

    Returns any

Const parse

  • parse(val: any): string
  • Parse a JSON string, or return the original.

    import { parse } from '@appsweet-co/utils';
    
    parse('foobar');
    // => 'foobar'
    
    parse('{"hello": "world"}');
    // => { hello: 'world' }

    Parameters

    • val: any

    Returns string

Const percent

  • percent(...args: [min: number, max?: number, val?: number]): number
  • Get the numerical percentage of a value compared against min and max values.

    import { percent } from '@appsweet-co/utils';
    
    percent(0, 10, 5);
    // => 50
    
    const scale = percentC(0, 10);
    
    scale(5);
    // => 50

    The following are equivalent:

    percent(min)(max)(val);
    percent(min)(max, val);
    percent(min, max)(val);
    percent(min, max, val);

    Parameters

    • Rest ...args: [min: number, max?: number, val?: number]

    Returns number

Const pipe

  • pipe(...fns: any[]): (Anonymous function)
  • Compose functions left-to-right.

    Inspired by Eric Elliott and Ramda.js.

    import { pipe } from '@appsweet-co/utils';
    
    const roundedLog = pipe(Math.log, Math.round);
    
    roundedLog(123.45)
    // => 5

    Parameters

    • Rest ...fns: any[]

    Returns (Anonymous function)

Const prop

  • prop(key: string | number, obj?: {}): unknown
  • Returns the value of the supplied property. Same as obj[key].

    Inspired by Krzysztof Czernek and Ramda.js.

    import { prop } from '@appsweet-co/utils';
    
    prop('bananas', { bananas: 7 });
    // => 7
    
    const bananaCount = prop('bananas');
    
    bananaCount({ bananas: 7 });
    // => 7

    The following are equivalent:

    prop(key)(obj);
    prop(key, obj);

    Parameters

    • key: string | number
    • Optional obj: {}

    Returns unknown

Const ratio

  • ratio(...args: [min: number, max?: number, val?: number]): number
  • Get the decimal fraction of a value compared against min and max values.

    import { ratio } from '@appsweet-co/utils';
    
    ratio(0, 10, 5);
    // => 0.5
    
    const range = ratio(0, 10);
    
    range(5);
    // => 0.5

    The following are equivalent:

    ratio(min)(max)(val);
    ratio(min)(max, val);
    ratio(min, max)(val);
    ratio(min, max, val);

    Parameters

    • Rest ...args: [min: number, max?: number, val?: number]

    Returns number

Const reduce

  • reduce(fn: (acc: unknown, cur: unknown) => unknown, list?: unknown[]): unknown
  • Executes a reducer function on each element of an array and returns a single output value. Same as list.reduce(fn, init).

    Inspired by Mozilla.

    import { reduce } from '@appsweet-co/utils';
    
    reduce((acc, cur) => acc + cur, [1, 2, 3]);
    // => 6
    
    const sum = reduce((acc, cur) => acc + cur);
    
    sum([1, 2, 3]);
    // => 6

    The following are equivalent:

    reduce(fn)(list);
    reduce(fn, list);

    Parameters

    • fn: (acc: unknown, cur: unknown) => unknown
        • (acc: unknown, cur: unknown): unknown
        • Parameters

          • acc: unknown
          • cur: unknown

          Returns unknown

    • Optional list: unknown[]

    Returns unknown

Const stringify

  • stringify(val: any): string
  • Stringify a value, or return the original.

    import { stringify } from '@appsweet-co/utils';
    
    stringify('foobar');
    // => 'foobar'
    
    stringify(123);
    // => '123'

    Parameters

    • val: any

    Returns string

Const subtract

  • subtract(a: number, b?: number): number
  • Subtract the first argument from the second. Same as b - a.

    Inspired by Ramda.js

    import { subtract } from '@appsweet-co/utils';
    
    subtract(5, 9);
    // => 4
    
    const sub5 = subtract(5);
    
    sub5(9);
    // => 4

    The following are equivalent:

    subtract(a)(b);
    subtract(a, b);

    Parameters

    • a: number
    • Optional b: number

    Returns number

Const timeout

  • timeout(delay: number, fn: TimerHandler): any
  • Sets a timer that executes a function once the timer expires. Same as setTimeout(fn, delay).

    import { timeout } from '@appsweet-co/utils';
    
    timeout(3000, () => { console.log('click')});
    // => 'click'
    
    const click = timeout(3000);
    
    click(() => { console.log('click')});
    // => 'click'

    The following are equivalent:

    click(delay)(fn);
    click(delay, fn);

    Parameters

    • delay: number
    • fn: TimerHandler

    Returns any

Const type

  • type(kind: string, val?: unknown): boolean
  • Compares a value against a given type. Same as typeof val === kind.

    Inspried by Ramda.js.

    import { type } from '@appsweet-co/utils';
    
    type('string', 'foobar');
    // => true
    
    const isNumber = type('number');
    
    isNumber('foobar');
    // => false

    The following are equivalent:

    type(kind)(val);
    type(kind, val);

    Parameters

    • kind: string
    • Optional val: unknown

    Returns boolean

Const unzip

  • unzip(fn: (value: any) => any, list: any[]): unknown
  • Returns an array of arrays. The first element is a list of items that pass the predicate. The second element is a list of items that fail.

    import { unzip } from '@appsweet-co/utils';
    
    unzip(x => x % 2 === 0, [1, 2, 3, 0, 4, 5])
    // => [[2, 0, 4], [1, 3, 5]]
    
    const evens = unzip(x => x % 2 === 0)
    
    evens([1, 2, 3, 0, 4, 5])
    // => [[2, 0, 4], [1, 3, 5]]

    The following are equivalent:

    unzip(fn)(list);
    unzip(fn, list);

    Parameters

    • fn: (value: any) => any
        • (value: any): any
        • Parameters

          • value: any

          Returns any

    • list: any[]

    Returns unknown

Generated using TypeDoc