protobuf-fieldmask

Library for generating and applying FieldMask

Usage no npm install needed!

<script type="module">
  import protobufFieldmask from 'https://cdn.skypack.dev/protobuf-fieldmask';
</script>

README

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
  }
}