# protobuf-fieldmask

> Library for generating and applying FieldMask

Latest version **2.0.0** (published 2024-02-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install protobuf-fieldmask
pnpm add protobuf-fieldmask
yarn add protobuf-fieldmask
bun add protobuf-fieldmask
```

## Health

**Score 35/100 (D)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2024-02-01 |
| First published | 2018-07-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=8 |
| Dependencies | 1 |
| Unpacked size | 8.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 12 |
| Maintainers | kibertoad |
| Keywords | protocol, buffers, protobuf, field, mask, json, grpc, fieldmask |

## Links

- npm: https://www.npmjs.com/package/protobuf-fieldmask
- Repository: https://github.com/kibertoad/protobuf-fieldmask
- Homepage: http://github.com/kibertoad/protobuf-fieldmask
- Issues: https://github.com/kibertoad/protobuf-fieldmask/issues
- npm.io page: https://npm.io/package/protobuf-fieldmask

## Dependencies (1)

- [lodash](https://npm.io/package/lodash.md) ^4.17.21

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 2.0.0 (latest) — 2024-02-01
- 1.2.0 — 2023-07-13
- 1.1.0 — 2021-04-20
- 1.0.2 — 2020-12-28
- 1.0.1 — 2018-07-10
- 1.0.0 — 2018-07-10

## README

# protobuf-fieldmask

[![npm version](http://img.shields.io/npm/v/protobuf-fieldmask.svg)](https://npmjs.org/package/protobuf-fieldmask)
[![npm downloads](https://img.shields.io/npm/dm/protobuf-fieldmask.svg)](https://npmjs.org/package/protobuf-fieldmask)
![](https://github.com/kibertoad/protobuf-fieldmask/workflows/unit-tests/badge.svg)
[![Coverage Status](https://coveralls.io/repos/kibertoad/protobuf-fieldmask/badge.svg?branch=master)](https://coveralls.io/r/kibertoad/protobuf-fieldmask?branch=master)

Library for generating and applying protobuf [FieldMask](https://developers.google.com/protocol-buffers/docs/reference/csharp/class/google/protobuf/well-known-types/field-mask)

## 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!

---
_Source: https://npm.io/package/protobuf-fieldmask · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
