# pb-util

> Utilities for working with common protobuf types

Latest version **1.0.3** (published 2022-01-12) · ISC license · 0 weekly downloads

## Install

```sh
npm install pb-util
pnpm add pb-util
yarn add pb-util
bun add pb-util
```

## Health

**Score 25/100 (F)** — status: abandoned.

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2022-01-12 |
| First published | 2019-03-28 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 19.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 34 |
| Author | Dave Gramlich |
| Maintainers | callmehiphop |
| Keywords | protobuf, utility, util, value, list, struct |

## Links

- npm: https://www.npmjs.com/package/pb-util
- Repository: https://github.com/callmehiphop/pb-util
- Homepage: https://github.com/callmehiphop/pb-util#readme
- Issues: https://github.com/callmehiphop/pb-util/issues
- npm.io page: https://npm.io/package/pb-util

## Alternatives

- [lodash.assign](https://npm.io/package/lodash.assign.md) — 2.3M weekly downloads
- [lodash.chunk](https://npm.io/package/lodash.chunk.md) — 1.8M weekly downloads
- [react-native-ios-utilities](https://npm.io/package/react-native-ios-utilities.md) — 138.5K weekly downloads
- [@technically/lodash](https://npm.io/package/@technically/lodash.md) — 50.9K weekly downloads
- [@fluid-topics/ft-icon](https://npm.io/package/@fluid-topics/ft-icon.md) — 20.6K weekly downloads

## Recent versions

- 1.0.3 (latest) — 2022-01-12
- 1.0.2 — 2021-11-10
- 1.0.1 — 2021-06-03
- 1.0.0 — 2021-06-01
- 0.1.3 — 2020-03-09
- 0.1.2 — 2019-08-08
- 0.1.1 — 2019-06-14
- 0.1.0 — 2019-03-28

## README

# pb-util

> Utilities for working with common protobuf types.

## Installing

```sh
$ npm i --save pb-util
```

## API

### value.encode(val)

Encodes a JSON value into a [`google.protobuf.Value`][pb-value].

#### val

Type: `string` `number` `boolean` `null` `object` `array`

```js
const {value} = require('pb-util');

const stringValue = value.encode('hello!');
// => {
//   kind: 'stringValue',
//   stringValue: 'hello!'
// }
```

### value.decode(protoValue)

Decodes a [`google.protobuf.Value`][pb-value] into a JSON value.

#### protoValue

Type: [`google.protobuf.Value`][pb-value]

```js
const {value} = require('pb-util');

const str = value.decode({
  kind: 'stringValue',
  stringValue: 'beep boop'
});
// => 'beep boop'
```

### struct.encode(json)

Encodes a JSON object into a [`google.protobuf.Struct`][pb-struct].

#### json

Type: `object`

```js
const {struct} = require('pb-util');

const structValue = struct.encode({foo: 'bar'});
// => {
//   fields: {
//     foo: {
//       kind: 'stringValue',
//       stringValue: 'bar'
//     }
//   }
// }
```

### struct.decode(structValue)

Decodes a [`google.protobuf.Struct`][pb-struct] into a JSON object.

#### structValue

Type: [`google.protobuf.ListValue`][pb-list]

```js
const {struct} = require('pb-util');

const obj = struct.decode({
  fields: {
    foo: {
      kind: 'stringValue',
      stringValue: 'bar'
    },
    yes: {
      kind: 'boolValue',
      boolValue: true
    }
  }
});
// => {
//   foo: 'bar',
//   yes: true
// }
```

### list.encode(array)

Encodes an array of JSON values into a [`google.protobuf.ListValue`][pb-list].

#### array

Type: `array`

```js
const {list} = require('pb-util');

const listValue = list.encode(['foo', 'bar']);
// => {
//   values: [
//     {
//       kind: 'stringValue',
//       stringValue: 'foo'
//     },
//     {
//       kind: 'stringValue',
//       stringValue: 'bar'
//     }
//   ]
// }
```

### list.decode(listValue)

Decodes a [`google.protobuf.ListValue`][pb-list] into an array of JSON values.

#### listValue

Type: [`google.protobuf.ListValue`][pb-list]

```js
const {list} = require('pb-util');

const arr = list.decode({
  values: [
    {
      kind: 'stringValue',
      stringValue: 'foo'
    },
    {
      kind: 'numberValue',
      numberValue: 10
    }
  ]
});
// => ['foo', 10]
```

## License

ISC

[pb-value]: https://developers.google.com/protocol-buffers/docs/reference/google.protobuf#google.protobuf.Value
[pb-list]: https://developers.google.com/protocol-buffers/docs/reference/google.protobuf#listvalue
[pb-struct]: https://developers.google.com/protocol-buffers/docs/reference/google.protobuf#struct

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