Options
All
  • Public
  • Public/Protected
  • All
Menu

Module "setter"

Index

Functions

Const compS

  • compS(k: NumOrString, f: (o: any, v: any) => any): (Anonymous function)
  • Helper for defSetter. Returns setter for a single step.

    internal

    Parameters

    • k: NumOrString

      -

    • f: (o: any, v: any) => any

      -

        • (o: any, v: any): any
        • Parameters

          • o: any
          • v: any

          Returns any

    Returns (Anonymous function)

Const copy

  • copy(x: any): any
  • Creates a shallow copy of given array, typed array or plain object.

    Parameters

    • x: any

    Returns any

defSetter

  • defSetter<T>(path: Path0): Fn2<T, T, T>
  • defSetter<T, A>(path: Path1<T, A>): Fn2<T, PathVal<T, [A]>, T>
  • defSetter<T, A, B>(path: Path2<T, A, B>): Fn2<T, PathVal<T, [A, B]>, T>
  • defSetter<T, A, B, C>(path: Path3<T, A, B, C>): Fn2<T, PathVal<T, [A, B, C]>, T>
  • defSetter<T, A, B, C, D>(path: Path4<T, A, B, C, D>): Fn2<T, PathVal<T, [A, B, C, D]>, T>
  • defSetter<T, A, B, C, D, E>(path: Path5<T, A, B, C, D, E>): Fn2<T, PathVal<T, [A, B, C, D, E]>, T>
  • defSetter<T, A, B, C, D, E, F>(path: Path6<T, A, B, C, D, E, F>): Fn2<T, PathVal<T, [A, B, C, D, E, F]>, T>
  • defSetter<T, A, B, C, D, E, F, G>(path: Path7<T, A, B, C, D, E, F, G>): Fn2<T, PathVal<T, [A, B, C, D, E, F, G]>, T>
  • defSetter<T, A, B, C, D, E, F, G, H>(path: Path8<T, A, B, C, D, E, F, G, H>): Fn2<T, PathVal<T, [A, B, C, D, E, F, G, H]>, T>
  • defSetter<T, A, B, C, D, E, F, G, H>(path: DeepPath<T, A, B, C, D, E, F, G, H>): Fn2<T, any, any>
  • Type checked version of defSetterUnsafe. Only the first 8 path levels are type checked.

    remarks

    Due to the higher-order nature of this function, generics for path validation must be given and so this function is more verbose than setIn (where the generics can usually be fully inferred).

    example
    type State = { a: { b: number } };
    
    const setB = defSetter<State, "a", "b">(["a", "b"]);
    
    setB({ a: { b: 1 } }, 2); // ok!
    setB({ a: { b: 1 } }, "2"); // error!
    example
    type State = { a: { b: number } };
    
    const path = <const>["a","b"];
    
    const setB = defSetter<State, typeof path[0], typeof path[1]>(path);

    Type parameters

    • T

    Parameters

    • path: Path0

      -

    Returns Fn2<T, T, T>

  • Type parameters

    • T

    • A

    Parameters

    • path: Path1<T, A>

    Returns Fn2<T, PathVal<T, [A]>, T>

  • Type parameters

    • T

    • A

    • B

    Parameters

    • path: Path2<T, A, B>

    Returns Fn2<T, PathVal<T, [A, B]>, T>

  • Type parameters

    • T

    • A

    • B

    • C

    Parameters

    • path: Path3<T, A, B, C>

    Returns Fn2<T, PathVal<T, [A, B, C]>, T>

  • Type parameters

    • T

    • A

    • B

    • C

    • D

    Parameters

    • path: Path4<T, A, B, C, D>

    Returns Fn2<T, PathVal<T, [A, B, C, D]>, T>

  • Type parameters

    • T

    • A

    • B

    • C

    • D

    • E

    Parameters

    • path: Path5<T, A, B, C, D, E>

    Returns Fn2<T, PathVal<T, [A, B, C, D, E]>, T>

  • Type parameters

    • T

    • A

    • B

    • C

    • D

    • E

    • F

    Parameters

    • path: Path6<T, A, B, C, D, E, F>

    Returns Fn2<T, PathVal<T, [A, B, C, D, E, F]>, T>

  • Type parameters

    • T

    • A

    • B

    • C

    • D

    • E

    • F

    • G

    Parameters

    • path: Path7<T, A, B, C, D, E, F, G>

    Returns Fn2<T, PathVal<T, [A, B, C, D, E, F, G]>, T>

  • Type parameters

    • T

    • A

    • B

    • C

    • D

    • E

    • F

    • G

    • H

    Parameters

    • path: Path8<T, A, B, C, D, E, F, G, H>

    Returns Fn2<T, PathVal<T, [A, B, C, D, E, F, G, H]>, T>

  • Type parameters

    • T

    • A

    • B

    • C

    • D

    • E

    • F

    • G

    • H

    Parameters

    • path: DeepPath<T, A, B, C, D, E, F, G, H>

    Returns Fn2<T, any, any>

Const defSetterUnsafe

  • defSetterUnsafe<T>(path: Path): Fn2<any, T, any>
  • Composes a setter function for given nested update path. Optimized fast execution paths are provided for path lengths less up to 4.

    remarks

    Supports both arrays and objects and creates intermediate shallow copies at each level of the path. Thus provides structural sharing with the original data for any branches not being updated by the setter.

    The type parameter T can be used to indicate the type of the nested value to be updated (default: any).

    If path is given as string, it will be split using .. Returns function which accepts single object and when called, immutably updates value at given path, i.e. produces a partial deep copy of obj up until given path.

    If any intermediate key is not present in the given obj, creates a plain empty object for that key and descends further.

    If path is an empty string or array, the returned setter will simply return the new value.

    Only keys in the path will be modified, all other keys present in the given object retain their original values to provide efficient structural sharing / re-use.

    example
    s = defSetterUnsafe("a.b.c");
    // or
    s = defSetterUnsafe(["a", "b", "c"]);
    
    s({ a: { b: { c: 23} } }, 24)
    // { a: { b: { c: 24} } }
    
    s({ x: 23 }, 24)
    // { x: 23, a: { b: { c: 24 } } }
    
    s(null, 24)
    // { a: { b: { c: 24 } } }
    example
    s = defSetterUnsafe("a.b.c");
    
    a = { x: { y: { z: 1 } } };
    b = s(a, 2);
    // { x: { y: { z: 1 } }, a: { b: { c: 2 } } }
    
    a.x === b.x // true
    a.x.y === b.x.y // true

    Type parameters

    • T

    Parameters

    • path: Path

      -

    Returns Fn2<any, T, any>

Generated using TypeDoc