开发者社区> 镜像站> NPM> money-encoder
money-encoder
Money encoder/decoder for [protocol buffers](https://developers.google.com/protocol-buffers/)
Last updated 4 years ago by thlorenz .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install money-encoder 
SYNC missed versions from official npm registry.

money-encoder build status

Money encoder/decoder for protocol buffers

money

Example using mafintosh/protocoll-buffers

// traveller.proto
syntax = "proto2";

/* 
 * money isn't a real protobuf type, but protocol-buffers
 * knows how to encode/decode it since we pass it the money-encoder
 */

message wallet {
  optional money dollars = 0;
  optional money pesos = 1;
}

message traveller {
  required wallet wallet;
}
var protobuf = require('protocol-buffers')
var fs = require('fs')
var money = require('wallet-encoder')
var proto = fs.readFileSync(__dirname + '/wallet.proto')

// initialize protobuf and teach it how to encode/decode money
var messages = protobuf(proto, { encodings: { money: money } })

var traveller = {
  wallet: {
      dollars: 20.45
    , pesos: 235.67
  }
}

var buf = messages.entry.encode(traveller)
console.log(buf)

var w = messages.entry.decode(buf)
console.log(w)

Output

<Buffer 02 0a 00 fd 07 00 00 00 10 5c 00 00>
{ wallet: { dollars: 235.68, pesos: 235.67 } }

Installation

npm install money-encoder

API

decode(buf, offset) → {number}

Decodes a money value from the given buffer starting at the given offset.

Parameters:
Name Type Description
buf Buffer

buffer that contains the value to be decoded

offset number

offset at which to start reading the encoded value

Source:
Returns:

the money value which as two decimal places

Type
number

encode(val, buf, offset) → {Buffer}

Encodes the given money value into the provided buffer at the given offset. If no buffer is provided one is created for you. Encoding preserves exactly two decimal places of the value.

Parameters:
Name Type Argument Description
val number

the value to encode

buf Buffer <optional>

buffer into which to save the result of the encoding

offset number <optional>

offset at which to save encoded result into the buffer

Source:
Returns:

the buffer that was passed in

Type
Buffer

generated with docme

License

MIT

Current Tags

  • 0.1.0                                ...           latest (4 years ago)

1 Versions

  • 0.1.0                                ...           4 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (1)
Dependents (0)
None