protobuf-fieldmask
TypeScript icon, indicating that this package has built-in type declarations

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

Special thanks to Jorge Yero Salazar for providing TS typings!

Package Sidebar

Install

npm i protobuf-fieldmask

Weekly Downloads

2,171

Version

2.0.0

License

MIT

Unpacked Size

8.8 kB

Total Files

7

Last publish

Collaborators

  • kibertoad