CmpStr - v3.3.0
    Preparing search index...

    DeepMerge class provides static methods for deep merging objects and manipulating nested properties using path strings.

    Index

    Constructors

    Properties

    BRACKET_PATTERN: RegExp = ...

    Regular expression to match bracket notation in paths

    PATH_CACHE: Map<string, (string | number)[]> = ...

    Path cache for efficient parsing

    Methods

    • Deeply get a value from an object by a path string.

      Type Parameters

      • T extends Record<string, any>

        The type of the object to get the value from

      • R = any

        The return type of the value

      Parameters

      • t: T

        The object to get the value from

      • path: string

        The path string, e.g. a.b.c

      • Optionalfb: R

        The default value to return if the path does not exist

      Returns R | undefined

      • The value at the specified path, otherwise the default value
    • Check if a path exists in an object.

      Type Parameters

      • T extends Record<string, any>

        The type of the object to get the value from

      Parameters

      • t: T

        The object to check

      • path: string

        The path string, e.g. a.b.c

      Returns boolean

      • True if the path exists, otherwise false
    • Deeply merge two objects, where the second object overrides the first.

      Type Parameters

      • T extends Record<string, any>

        The type of the object to get the value from

      Parameters

      • t: T | undefined = ...

        The target object to merge into

      • o: T | undefined = ...

        The source object to merge from

      • OptionalmergeUndefined: boolean = false

        Whether to merge undefined values

      Returns T

      • The merged object
    • Parse a path string into an array of keys.

      Parameters

      • p: string

        The path string, e.g. a.b.c or a[0].b

      Returns (string | number)[]

      • An array of keys, e.g. ['a', 'b', 'c'] or ['a', 0, 'b']
    • Delete a value at a specified path in an object.

      Type Parameters

      • T extends Record<string, any>

        The type of the object to get the value from

      Parameters

      • t: T

        The object to delete the value from

      • path: string

        The path string, e.g. a.b.c

      • OptionalpreserveEmpty: boolean = false

        Whether to preserve empty objects/arrays

      Returns T

      • The modified object with the value deleted at the specified path
    • Deeply set a value in an object by a path string.

      Type Parameters

      • T extends Record<string, any>

        The type of the object to get the value from

      Parameters

      • t: T

        The object to set the value in

      • path: string

        The path string, e.g. a.b.c

      • value: any

        The value to set at the specified path

      Returns T

      • The modified object with the value set at the specified path
      • If the path is invalid or if a non-object value is encountered along the path
    • Walk through an object using an array of keys and return whether the path exists and its value.

      Parameters

      • obj: any

        The object to walk through

      • keys: (string | number)[]

        An array of keys representing the path to walk

      Returns { exists: false } | { exists: true; value: any }

      • An object indicating whether the path exists and its value if it does