Function safeSet

  • Provides a way to safely set an object's data / entries using either a dotted path string or an array of exact property keys. Array indexes may only be accessed by number through the array property-key form.

    Parameters

    • data: object

      An object to access entry data.

    • path: PropertyPath

      A dotted string path or an array of exact string, number, or symbol property keys.

      The string keys __proto__, prototype, and constructor are rejected to prevent prototype-pollution access paths. ECMAScript well-known symbols, such as Symbol.toStringTag, Symbol.iterator, and Symbol.toPrimitive, are also rejected because they modify built-in language protocols and object behavior. User-created symbols and symbols from Symbol.for remain valid.

    • value: any

      A new value to set if an entry for path is found.

    • Optionaloptions: {
          createMissing?: boolean;
          operation?: "add" | "set" | "div" | "sub" | "mult" | "set-undefined";
      }

      Options.

      • OptionalcreateMissing?: boolean

        If true missing path entries will be created as objects automatically; default: false.

      • Optionaloperation?: "add" | "set" | "div" | "sub" | "mult" | "set-undefined"

        Operation to perform including: add, div, mult, set, set-undefined, sub; default: set.

    Returns boolean

    True if successful.