Options
All
  • Public
  • Public/Protected
  • All
Menu

deep-obj-utils

Index

Type aliases

Path

An path that describes an value in an object or array

PathSegment

PathSegment: string | number

A segment describing an key or index in an object or array

Pattern

An pattern that describes a number of paths that fulfills the pattern regarding an specific object or array

PatternSegment

PatternSegment: string | number | RegExp

A segment describing an key, index, matcher or wildcard in an object or array

Functions

expand

  • expand(pattern: Pattern, object: any | any[]): Path[]
  • Expands a pattern to all matching conrete paths over an object

    example
    const o = {
      foo: {
        bar: "bar",
        baz: "baz",
      },
      foo2: [
        "one",
        "two",
      ]
    }
    let result = expand(["*"], o)
    // result === [ ["foo"], ["foo2"] ]
    let result = expand(["*", "*"], o)
    // result === [ ["foo", "bar"], ["foo", "baz"], ["foo2", 0], ["foo2", 1] ]
    let result = expand(["foo", "*"], o)
    // result ===  [ ["foo", "bar"], ["foo", "baz"] ]
    

    Parameters

    • pattern: Pattern

      the pattern to expand

    • object: any | any[]

      the object to expand onto

    Returns Path[]

get

  • get<T>(path: Path, obj: T): any
  • Retrieves a value in an object described by an path

    example
    const object = { foo: [null, { bar: "BAR" }] }
    const result = get(["foo", 1, "bar"], object)
    // result === "BAR"
    

    Type parameters

    • T

    Parameters

    • path: Path

      the path at which the objects value should be retrieved

    • obj: T

      the object to retrieve a value from

    Returns any

hasNumberlikeStrings

  • hasNumberlikeStrings(p: Path): boolean
  • Checks if an path contains number-like strings

    Parameters

    • p: Path

      the path to check

    Returns boolean

    true, if {@code p} contains a number-like string

isPath

  • isPath(p: any): boolean
  • Checks if an object is a valid path

    Parameters

    • p: any

      the object to check

    Returns boolean

    true if {@code p} is a valid path

isPattern

  • isPattern(p: any): boolean
  • Checks if an object is a valid pattern

    Parameters

    • p: any

      the object to check

    Returns boolean

    true if {@code p} is a valid pattern

set

  • set<T>(path: Path, obj: T, value: any): T
  • Creates an object that is similiar to a given object but differs at a specific path

    example
    const object = { foo: [null, { bar: "BAR" }] }
    
    let result = set(["foo", 0, "baz"], object, "baz")
    // result === { foo: ["baz", { bar: "BAR" }] }
    // object === { foo: [null, { bar: "BAR" }] } !
    
    // with autovivication
    const object = {}
    
    let result = set(["foo", 0, "baz"], object, "baz")
    // result === { foo: ["baz", { bar: "BAR" }] }
    // *missing* segments will be created on the fly
    // if `typeof segment === "string"`  an empty object will be created at the non-existing path
    // if `typeof segment === "number"`  an empty array will be created at the non-existing path
    

    Type parameters

    • T

    Parameters

    • path: Path

      the path at which the the created object shall be different

    • obj: T

      the object to use as blueprint

    • value: any

      the value to insert at the given path

    Returns T

update

  • update<T>(path: Path, obj: T, updater: function): T
  • Creates an object that is similiar to a given object but differs at a specific path

    example
    const object = { foo: [null, { bar: "bar" }] }
    
    let result = update(["foo", 1, "bar"], v => v.toUpperCase())
    // result === { foo: [null, { bar: "BAR" }] }
    
    // with autovivication
    const object = {}
    
    let result = set(["foo", 1, "bar"], object, v => v.toUpperCase())
    // result === { foo: [null, { bar: "BAR" }] }
    // *missing* segments will be created on the fly
    // if `typeof segment === "string"`  an empty object will be created at the non-existing path
    // if `typeof segment === "number"`  an empty array will be created at the non-existing path
    

    Type parameters

    • T

    Parameters

    • path: Path

      the path at which the the created object shall be different

    • obj: T

      the object to use as blueprint

    • updater: function

      a function that transforms the given objects value at path to a new value

        • (o: any): any
        • Parameters

          • o: any

          Returns any

    Returns T

values

  • values(object: any | any[]): Array<any>
  • Retrieves all primitive value from an object

    The order of the returned values is not guarenteed to be sorted in any way

    example
    const object = { foo: { bar: "bar", baz: [true, 0] }}
    const result = values(object)
    // result === [ "bar", true, 0 ]
    

    Parameters

    • object: any | any[]

      the object to retrieve all primitives values from

    Returns Array<any>

    an array of all primitive values

valuesAndPaths

  • valuesAndPaths(object: any | any[]): Array<object>
  • Retrieves all primitive value and their corresponding paths from an object

    The order of the returned values is not guarenteed to be sorted in any way

    example
    const object = { foo: { bar: "bar", baz: [true, 0] }}
    const result = values(object)
    // result === [
      { path: [ "foo", "bar", ], value: "bar" },
      { path: [ "foo", "baz", 0 ], value: true },
      { path: [ "foo", "baz", 1 ], value: 0 }
    ]
    

    Parameters

    • object: any | any[]

      the object to retrieve all primitives values from

    Returns Array<object>

    an array of all primitive values and their corresponding paths

Generated using TypeDoc