Skip to content

Commit

Permalink
docs(readme): add customization examples
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Jan 31, 2017
1 parent 0704dbc commit be834b9
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,45 @@ Decoding supports the above types, including the following CBOR tag numbers:
| 35 | RegExp |


## Customizations

Borc supports custom tags as well as custom input types.

### Encode Custom Types

```js
class MyType {
constructor (val) {
this.val = val
}

// Gets called when encoding this object
// gen - instance of the encoder
// obj - the object being encoded
//
// should return true on success and false otherwise
toCBOR (gen, obj) {
return gen.pushAny('mytype:' + this.val.toString())
}
}
```

### Encode Custom Tags

```js
cbor.encode([new cbor.Tagged(42, 'hello')])
```

### Decode Custom Tags

```js
const decoder = new cbor.Decoder({
tags: {
42: (val) => val + ' world'
}
})
```

## License

MIT

0 comments on commit be834b9

Please sign in to comment.