GithubHelp home page GithubHelp logo

matias-la / borc Goto Github PK

View Code? Open in Web Editor NEW

This project forked from exodusmovement/borc

0.0 0.0 0.0 3.3 MB

Assimilate your JavaScript objects into cbor

Home Page: https://dignifiedquire.github.io/borc

License: MIT License

JavaScript 100.00%

borc's Introduction

borc

Coverage Status Dependency Status Travis CI

Assimilate all your JavaScript objects into the Concise Binary Object Representation (CBOR) data format (RFC7049) as fast as possible.

About

This library is a fork of the awesome node-cbor. It borrows a lot of the interface, but drops all streaming and async processing in favor of a minimal syn api and being as fast as possible.

Installation

$ npm install --save borc

Benchmarks

TODO

Example

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(decoded === true)

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

API

See https://dignifiedquire.github.io/borc for details

The sync encoding and decoding are exported as a leveldb encoding, as cbor.leveldb.

Supported types

The following types are supported for encoding:

  • boolean
  • number (including -0, NaN, and ±Infinity)
  • string
  • Array, Set (encoded as Array)
  • Object (including null), Map
  • undefined
  • Buffer
  • Date,
  • RegExp
  • url.URL
  • bignumber

Decoding supports the above types, including the following CBOR tag numbers:

Tag Generated Type
0 Date
1 Date
2 bignumber
3 bignumber
4 bignumber
5 bignumber
32 url.URL
35 RegExp

Customizations

Borc supports custom tags as well as custom input types.

Encode Custom Types

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
  encodeCBOR (gen) {
    return gen.pushAny('mytype:' + this.val)
  }
}

cbor.encode([new MyType('hello')])

Encode Custom Tags

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

Decode Custom Tags

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

License

MIT

borc's People

Contributors

hildjj avatar dignifiedquire avatar paroga avatar hugomrdias avatar chalker avatar sake avatar achingbrain avatar alanshaw avatar kartben avatar danfinlay avatar kimpers avatar rynop avatar rklaehn avatar greenkeeperio-bot avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.