0.2.0 • Published 7 years ago

@endeo/specials v0.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
7 years ago

@endeo/specials

Build Status Dependency Status npm version Coverage Status

Build "object spec" for compact object encoding.

A flexible specification allowing progressive improvements for greater performance.

Features:

  1. knows a list of property names for an object to allow encoding without them which avoids all those bytes.
  2. optionally knows defaults to avoid encoding those values.
  3. optionally knows type to avoid the value's type check every time for faster encoding and decoding.
  4. optionally uses special keys to trigger automatic encoding/decoding changes for compression (or any reason). You may define your own special keys and how they are used.

See packages:

  1. endeo
  2. enbyte
  3. debyte
  4. unstring

Install

npm install --save @endeo/specials

Usage

// get the Specials builder
var build = require('@endeo/specials')

// the types we'd like to refer to by name.
// i'm using some default ones.
var types = require('@endeo/types')

// build it
var specials = build({ types: types })

// add more types:
specials.addType('someId', {
  // optionally provide one or both
  encode: function(enbyte, value, output) {},
  decode: function(debyte, input) {}
})

// add a type which is always one of a set of values
specials.addType('fruit', {
  // basically encodes the index of the value
  select: [ 'apple', 'banana', 'orange' ]
})

// a "creator" function makes an object
// representing the object with all its keys
// and default values.
function createThing() {
  return {
    key1: 'someDefault',
    key2: 2,
    fruit: 'banana'
  }
}

// the "enhancers" are extra info you can supply
var enhancers = {
  key1: 'name of a type',
  key2: {
    // optionally ref a known type still
    type: 'name of a type',

    // optionally provide encode() / decode()
    encode: function(enbyte, value, output) {},
    decode: function(debyte, input) {}
  },
  fruit: 'fruit' // use type 'fruit' above
}

// create an "object spec" (Special)
var spec = specials.build(1, createThing, enhancers)

// set the spec's ID on your objects.

// on a class:
function SomeClass() {}
spec.imprint(SomeClass.prototype)

// on an object:
var someObject = {}
spec.imprint(someObject)

MIT License

0.2.0

7 years ago

0.1.0

7 years ago