Skip to content

Create an binary encoder/decoder for Node's build in types like, json, utf-8, hex.

License

Notifications You must be signed in to change notification settings

mafintosh/codecs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

codecs

Create an binary encoder/decoder for Node's build in types like, json, utf-8, hex.

npm install codecs

Useful to support value encodings similar to leveldb's.

Usage

const codecs = require('codecs')
const json = codecs('json')

console.log(json.encode({ hello: 'world' })) // Buffer.from('{"hello":"world"}')
console.log(json.decode(new Buffer('{"hello":"world"}'))) // { hello: 'world' }

API

const codec = codecs(type, [fallback])

Create a new codec.

Supported types are

  • utf8
  • json
  • ndjson
  • binary
  • hex
  • ascii
  • base64
  • ucs2
  • ucs-2
  • utf16le
  • utf-16le
  • binary

If an unknown type is passed-in, the fallback is used if given, else binary. If you want to use a custom codec you can pass in an object containing a an encode and decode method and that will be returned. If you pass a compact encoder, then it will be converted into a codec for you and returned.

const buf = codec.encode(value)

Encode a value to a buffer.

const value = codec.decode(buf)

Decode a buffer to a value.

License

MIT

About

Create an binary encoder/decoder for Node's build in types like, json, utf-8, hex.

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors 3

  •  
  •  
  •