Options
All
  • Public
  • Public/Protected
  • All
Menu

lightdash

A lightweight TypeScript utility library to complement lodash.

Introduction

Lightdash is an extensive object of utility functions to complement lodash written in TypeScript, designed to be lightweight and modern.

Docs

Versions

>= v11.x

An utility library built on top of lodash, complementing it.

v10.x

A standalone utility library to replace lodash.

Usage

Installation:

npm install lightdash

When Using ES Modules:

import { groupMapBy } from "lightdash";

groupMapBy([1, 2, 3, 4, 5], val => val % 2);

When using CommonJS (Node):

const { groupMapBy } = require("lightdash");

groupMapBy([1, 2, 3, 4, 5], val => val % 2);

When using in the browser (you have to load lodash before lightdash):

const { groupMapBy } = l_;

groupMapBy([1, 2, 3, 4, 5], val => val % 2);

Updating

v10.x -> v11.x

Breaking:

  • Made the library complement lodash rather than replace it. All duplicate methods have been removed.

v9.x -> v10.x

Breaking:

  • changed entry iteration from key,val,index,object to val,key,object
  • removed isArray and strFromCamelCase (use Array.isArray and strFromPascalCase instead)
  • removed "immediate" param for fn* methods

v8.x -> v9.x

Breaking:

  • Merged arrRemoveItem and arrRemoveItemFirst
  • Removed isArguments and isNull
  • Removed numSum, numMedian and numAverage
  • Removed fnCurry
  • Removed hasPath

Contributing

Contributions are always welcome, no matter if you have a requests, an idea, found a bug, or spotted a typo: Feel free to create a PR or open an issue!

Index

Array Functions

Is Functions

Lang Functions

Object Functions

Search Functions

String Functions

Array Functions

Const countMapBy

  • countMapBy<TValue, UKey>(collection: List<TValue>, keyFn: ListIterator<TValue, UKey>): Map<UKey, number>
  • Counts the values of an array in a map, using the return value of the function as key.

    since

    12.0.0

    example

    countMapBy([1, 2, 4, 2, 4, 4], val => val) // => Map{1: 1, 2: 2, 4: 3}

    Type parameters

    • TValue

    • UKey

    Parameters

    • collection: List<TValue>

      Collection to count.

    • keyFn: ListIterator<TValue, UKey>

      Function to use for key generation.

    Returns Map<UKey, number>

    Count map.

Const groupMapBy

  • groupMapBy<TValue, UKey>(collection: List<TValue>, keyFn: ListIterator<TValue, UKey>): Map<UKey, TValue[]>
  • Collects the values of an array in a map as array values, using the return value of the function as key.

    since

    6.1.0

    example

    groupMapBy([1, 2, 3, 4, 5], val => val % 2) // => Map{0: [2, 4], 1: [1, 3, 5]}

    Type parameters

    • TValue

    • UKey

    Parameters

    • collection: List<TValue>

      Collection to group.

    • keyFn: ListIterator<TValue, UKey>

      Function to use for grouping.

    Returns Map<UKey, TValue[]>

    Grouped map.

Const groupMapReducingBy

  • groupMapReducingBy<TValue, UKey, VMerged>(collection: List<TValue>, keyProducer: ListIterator<TValue, UKey>, initializer: ListIterator<TValue, VMerged>, reducer: (current: VMerged, value: TValue, index: number, collection: List<TValue>) => VMerged): Map<UKey, VMerged>
  • Collects elements in an array into a an array of merged elements.

    since

    11.0.0

    example

    groupMapReducingBy( ["foo", "bar", "fizz", "buzz"], val => val.charAt(0), () => { return { count: 0, matches: [] }; }, (current, val) => { current.count++; current.matches.push(val); return current; } ) // => Map{"f": {count: 2, matches: ["foo", "fizz"]}, "b": {count: 2, matches: ["bar", "buzz"]}}

    Type parameters

    • TValue

    • UKey

    • VMerged

    Parameters

    • collection: List<TValue>

      Collection to group.

    • keyProducer: ListIterator<TValue, UKey>

      Function returning the key for the value.

    • initializer: ListIterator<TValue, VMerged>

      Function initializing a new mergable object.

    • reducer: (current: VMerged, value: TValue, index: number, collection: List<TValue>) => VMerged

      Consumer mutating the existing object with the new data.

        • (current: VMerged, value: TValue, index: number, collection: List<TValue>): VMerged
        • Parameters

          • current: VMerged
          • value: TValue
          • index: number
          • collection: List<TValue>

          Returns VMerged

    Returns Map<UKey, VMerged>

    Grouped and merged map.

Const removeIndex

  • removeIndex<TValue>(collection: List<TValue>, targetIndex: number): TValue[]
  • Returns a new array with the item at the given index removed.

    since

    2.8.0

    example

    removeIndex(["foo", "bar", "fizz"], 1) // => ["foo", "fizz"]

    Type parameters

    • TValue

    Parameters

    • collection: List<TValue>

      Collection to use.

    • targetIndex: number

      Index to remove.

    Returns TValue[]

    Collection with the index removed.

Const removeItem

  • removeItem<TValue>(collection: List<TValue>, targetValue: TValue, removeAll?: boolean): List<TValue>
  • Returns a new collection with instances of the given item removed.

    since

    2.8.0

    example

    removeItem(["foo", "bar", "fizz", "bar"], "bar") // => ["foo", "fizz"]

    removeItem(["foo", "bar", "fizz", "bar"], "bar", false) // => ["foo", "fizz", "bar"]

    Type parameters

    • TValue

    Parameters

    • collection: List<TValue>

      Collection to use.

    • targetValue: TValue

      Item to remove.

    • Default value removeAll: boolean = true

      If all occurrences should be removed, otherwise just the first occurrence will.

    Returns List<TValue>

    Collection with the item removed.

Const step

  • step<TValue>(collection: List<TValue>, n: number): List<TValue>
  • Returns a new collection with every n-th item from the input array.

    since

    1.0.0

    example

    step([1, 2, 3, 4, 5, 6], 2) // => [1, 3, 5]

    Type parameters

    • TValue

    Parameters

    • collection: List<TValue>

      Collection to use.

    • n: number

      Step to use.

    Returns List<TValue>

    Stepped collection.

Is Functions

Const isBlank

  • isBlank(str: string): boolean
  • Checks if the string is blank (no non-space content).

    since

    11.0.0

    example

    isBlank("") // => true

    isBlank(" ") // => true

    isBlank(" foo ") // => false

    Parameters

    • str: string

      String to use.

    Returns boolean

    If the file is blank.

Const isPromise

  • isPromise(value: any): value is Promise<any>
  • Checks if a value is a promise.

    since

    3.0.0

    example

    isPromise(new Promise((resolve, reject) => resolve("foo"))); // => true

    isPromise(() => "foo"); // => false

    Parameters

    • value: any

      Value to check.

    Returns value is Promise<any>

    If the value is a promise.

Lang Functions

Const toMap

  • toMap<TValue, UKey, VInitialValue>(object: Dictionary<VInitialValue> | NumericDictionary<VInitialValue>, keyMapper?: (key: PropertyKey) => UKey, valueMapper?: (value: VInitialValue) => TValue): Map<UKey, TValue>
  • Creates a map from an objects entries.

    since

    1.0.0

    example

    toMap({a: 1, b: 4, c: 5}) // => Map{"a": 1, "b": 4, "c": 5}

    toMap({a: 1, b: 4, c: 5}, key => { return { key }; }, value => value * 2) // => Map{{key: "a"}: 2, {key: "b"}: 8, {key: "a"}: 10}

    Type parameters

    • TValue

    • UKey

    • VInitialValue

    Parameters

    • object: Dictionary<VInitialValue> | NumericDictionary<VInitialValue>

      Object to use.

    • Default value keyMapper: (key: PropertyKey) => UKey = identity

      Function mapping keys, defaulting to identity.

        • (key: PropertyKey): UKey
        • Parameters

          • key: PropertyKey

          Returns UKey

    • Default value valueMapper: (value: VInitialValue) => TValue = identity

      Function mapping values, defaulting to identity.

        • (value: VInitialValue): TValue
        • Parameters

          • value: VInitialValue

          Returns TValue

    Returns Map<UKey, TValue>

    Map created from the object.

Object Functions

Const decycle

  • decycle<TValue>(collection: Collection<TValue>, replacer?: CollectionIterator<TValue | Collection<TValue>, any>, references?: WeakSet<any>): Collection<any>
  • Replaces every circular reference in an object with a value, defaulting to null.

    Can take a custom replacer function.

    since

    6.0.0

    example

    const a = {a: {}, b: 1, c: 2};

    a.a = a;

    decycle(a) // => {a: null, b: 1, c: 2}

    decycle(a, key => _${key}) // => {a: "_a", b: 1, c: 2}

    Type parameters

    • TValue

    Parameters

    • collection: Collection<TValue>

      Object to decycle.

    • Default value replacer: CollectionIterator<TValue | Collection<TValue>, any> = (): null =>null

      Circular reference value replacer function

    • Default value references: WeakSet<any> = new WeakSet()

      WeakSet prefilled with encountered references, not recommended to provide this manually.

    Returns Collection<any>

    Decycled object.

Const deepFreeze

  • deepFreeze(target: any): void
  • Recursively freezes objects, useful for constant objects.

    This function mutates the input value and calls Object.freeze() recursively on all sub-objects.

    since

    12.0.0

    example

    const a = {a: {b: 2}, b: [1, {foo: "foo"}], c: 2};

    deepFreeze(a) // => object and all sub-objects are frozen.

    Parameters

    • target: any

      Object to recursively freeze.

    Returns void

Const deepSeal

  • deepSeal(target: any): void
  • Recursively seals objects, useful for constant objects.

    This function mutates the input value and calls Object.seal() recursively on all sub-objects.

    since

    12.0.0

    example

    const a = {a: {b: 2}, b: [1, {foo: "foo"}], c: 2};

    deepSeal(a) // => object and all sub-objects are sealed.

    Parameters

    • target: any

      Object to recursively seal.

    Returns void

Const name

  • name(value: any): string | null
  • Gets name of a value.

    If the value has a name or description property, the value of that is returned. If the value is a string, it is returned as is. Otherwise null is returned.

    since

    10.2.0

    example

    name(class Foo{}) // => "Foo"

    name(function bar(){}) // => "bar"

    name(Symbol("abc")) // => "abc"

    name("foo") // => "foo"

    name(1) // => null

    Parameters

    • value: any

      Value to check.

    Returns string | null

    The name of the value.

Search Functions

Const binarySearch

  • binarySearch(arr: List<number>, search: number): number | null
  • Binary-search implementation.

    since

    5.0.0

    example

    binarySearch([0, 1, 2], 1) // => 1

    binarySearch([0, 1, 2], 100) // => null

    Parameters

    • arr: List<number>

      Array to search in.

    • search: number

      Value to search.

    Returns number | null

    Index of the value in the array, or null if none is found.

String Functions

Const distance

  • distance(str1: string, str2: string): number
  • Returns the levenshtein string distance of two strings.

    since

    6.3.0

    example

    distance("Kitten", "Sitting") // => 3

    distance("String", "Stribng") // => 1

    distance("foo", "foo") // => 0

    Parameters

    • str1: string

      First string to compare.

    • str2: string

      Second string to compare.

    Returns number

    Distance between the two strings.

Const matchAll

  • matchAll(str: string, pattern: RegExp): RegExpExecArray[]
  • Finds all regex matches in a string. Meant to be used with a global regex.

    since

    11.0.0

    example

    matchAll("Kitten", /t/g) // => [[0: "t"], [1: "t"]]

    matchAll("Kitten", /f/g) // => []

    Parameters

    • str: string

      String to match against.

    • pattern: RegExp

      SRegex pattern to match.

    Returns RegExpExecArray[]

    Array of all matches.

Const pascalCase

  • pascalCase(str: string): string
  • Creates a PascalCase string from a string.

    since

    6.2.0

    example

    pascalCase("fooBar") // => "FooBar"

    pascalCase("foo_bar") // => "FizzBuzzBazz"

    Parameters

    • str: string

      String to use.

    Returns string

    PascalCase string of the input string.

Const similar

  • similar(str: string, collection: List<string>, returnFull?: boolean): List<string> | Map<number, List<string>>
  • Returns strings similar to the input based its levenshtein distance to the values in the list given.

    since

    6.3.0

    example

    similar("Fob", ["Foo", "Bar"]) // => ["Foo"]

    similar("cmmit", ["init", "commit", "push"]) // => ["commit"]

    similar("Kitten", ["Sitten", "Sitting", "Bitten"]) // => ["Sitten", "Bitten"]

    similar("cmmit", ["init", "commit", "push"], true) // => Map<number, string[]>{1: ["commit"], 3: ["init"], 5: ["push"]}

    Parameters

    • str: string

      String to check.

    • collection: List<string>

      Array of values to compare the string to.

    • Default value returnFull: boolean = false

      If the full map should be returned, rather than just the closest matches.

    Returns List<string> | Map<number, List<string>>

    Array of the closest matches, or the map if returnFull is true.

Legend

Generated using TypeDoc