Skip to content

Commit

Permalink
feat: add docs setup
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Dec 11, 2016
1 parent 9f9fa2e commit 8164bc2
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules
test/setup/tmp-disposable-nodes-addrs.json
dist
coverage
docs
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules
coverage

test
docs
17 changes: 7 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,23 @@ TODO
## Example

```javascript
const cbor = require(borc)
const cbor = require('borc')
const assert = require('assert')

const encoded = cbor.encode(true) // returns <Buffer f5>
const decoded = cbor.decodeFirst(encoded)
// decoded is the unpacked object
assert.ok(obj === true);
assert.ok(obj === true)

// Use integers as keys?
var m = new Map();
m.set(1, 2);
encoded = cbor.encode(m); // <Buffer a1 01 02>
// Use integers as keys
var m = new Map()
m.set(1, 2)
encoded = cbor.encode(m) // <Buffer a1 01 02>
```

## API

- `.encode(any): Buffer`
- `.decodeFirst(string|Buffer): any`
- `.decodeAll(string|Buffer): Array<any>`
- `.diagnose(string|Buffer): string`
See https://dignifiedquire.github.io/borc for details

The sync encoding and decoding are exported as a
[leveldb encoding](https://github.com/Level/levelup#custom_encodings), as
Expand Down
6 changes: 6 additions & 0 deletions documentation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
toc:
- Encoder
- Decoder
- Diagnose
- Tagged
- Simple
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
"test:browser": "aegir-test --env browser",
"test:node": "aegir-test --env node",
"lint": "aegir-lint",
"release": "aegir-release",
"release-minor": "aegir-release --type minor",
"release-major": "aegir-release --type major",
"docs": "aegir-docs",
"release": "aegir-release --docs",
"release-minor": "aegir-release --type minor --docs",
"release-major": "aegir-release --type major --docs",
"build": "aegir-build",
"coverage": "aegir-coverage",
"coverage-publish": "aegir-coverage publish",
Expand Down
21 changes: 21 additions & 0 deletions src/decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ const Simple = require('./simple')
const Tagged = require('./tagged')
const url = require('url')

/**
* Transform binary cbor data into JavaScript objects.
*/
class Decoder {
/**
* @param {Object} [opts={}]
* @param {number} [opts.size=65536] - Size of the allocated heap.
*/
constructor (opts) {
opts = opts || {}

Expand Down Expand Up @@ -574,6 +581,13 @@ class Decoder {
return this._res
}

/**
* Decode the first cbor object.
*
* @param {Buffer|string} input
* @param {string} [enc='hex'] - Encoding used if a string is passed.
* @returns {*}
*/
static decode (input, enc) {
if (typeof input === 'string') {
input = new Buffer(input, enc || 'hex')
Expand All @@ -583,6 +597,13 @@ class Decoder {
return dec.decodeFirst(input)
}

/**
* Decode all cbor objects.
*
* @param {Buffer|string} input
* @param {string} [enc='hex'] - Encoding used if a string is passed.
* @returns {Array<*>}
*/
static decodeAll (input, enc) {
if (typeof input === 'string') {
input = new Buffer(input, enc || 'hex')
Expand Down
12 changes: 12 additions & 0 deletions src/encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ function toType (obj) {
*
*/
class Encoder {
/**
* @param {Object} [options={}]
* @param {function(Buffer)} options.stream
*/
constructor (options) {
options = options || {}

Expand Down Expand Up @@ -366,6 +370,9 @@ class Encoder {

/**
* Alias for `.pushAny`
*
* @param {*} obj
* @returns {undefind}
*/
write (obj) {
this.pushAny(obj)
Expand Down Expand Up @@ -486,6 +493,11 @@ class Encoder {
this.offset = 0
}

/**
* Encode the given value
* @param {*} o
* @returns {Buffer}
*/
static encode (o) {
const enc = new Encoder()
enc.pushAny(o)
Expand Down
3 changes: 2 additions & 1 deletion src/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ class Simple {
*
* @returns {string} simple(value)
*/
inspect (depth, opts) {
inspect () {
return 'simple(' + this.value + ')'
}

/**
* Push the simple value onto the CBOR stream
*
* @param {cbor.Encoder} gen The generator to push onto
* @returns {number}
*/
encodeCBOR (gen) {
return gen._pushInt(this.value, MT.SIMPLE_FLOAT)
Expand Down
1 change: 1 addition & 0 deletions src/tagged.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Tagged {
* Push the simple value onto the CBOR stream
*
* @param {cbor.Encoder} gen The generator to push onto
* @returns {number}
*/
encodeCBOR (gen) {
gen._pushTag(this.tag)
Expand Down

0 comments on commit 8164bc2

Please sign in to comment.