Nefarious Pomegranate Magnate

    protobuf-fieldmask

    1.1.0 • Public • Published

    protobuf-fieldmask

    npm version npm downloads Coverage Status

    Library for generating and applying protobuf FieldMask

    Usage

    generateFieldMask

    /**
     * Generates field mask that includes all non-function own properties on specified object
     * @param {*} object - object to generate field mask from
     * @returns {string[]} - generated field mask
     */
    function generateFieldMask(object)
    

    The name of the properties containing . or \ characters are escaped.

    For an example, running this function with this input:

    {
      f: {
        a: 22,
        b: {
          d: 1
        },
        'b.d': 33,
        'x\\y': 44
      }
    }
    

    generates this output:

    ['f.a', 'f.b.d', 'f.b\\.d', "f.x\\\\y"]
    

    applyFieldMask

    /**
     * Creates a new object that copies fields present in field mask from specified source object
     * @param {*} sourceObject - object to apply field mask to
     * @param {string[]} fieldMask
     * @returns {*} - new object created by applying field mask on source object or original entity if source is not an object
     */
    function applyFieldMask(sourceObject, fieldMask)
    

    Respects the escaping of the property names in the fieldMask.

    For an example, running this function with this input:

    {
      f: {
        a: 22,
        b: {
          d: 1,
          x: 2
        },
        'b.d': 33,
        y: 13
      },
      z: 8
    },
    ['f.a', 'f.b.d', 'f.b\\.d']
    

    generates this output:

    {
      f: {
        a: 22,
        b: {
          d: 1
        },
        'b.d': 33
      }
    }
    

    Install

    npm i protobuf-fieldmask

    DownloadsWeekly Downloads

    1,251

    Version

    1.1.0

    License

    MIT

    Unpacked Size

    7.52 kB

    Total Files

    6

    Last publish

    Collaborators

    • kibertoad