@yehn/cbor
TypeScript icon, indicating that this package has built-in type declarations

3.0.41 • Public • Published

Yehn™

This repository is part of the source code of Yehn. You can find more information at yehn.io or by contacting opensource@yehn.io.

You can find the published source code at github.com/yehn/yehn.

For licensing information, see the attached LICENSE file and the list of third-party licenses at yehn.io/legal/licenses/.

CBOR

Installation

yarn add @yehn/cbor

Usage

Browser

TypeScript

import * as CBOR from '@yehn/cbor';

const payload: Uint8Array = new Uint8Array([
  255,
  18,
  15,
  34,
  210,
  168,
  165,
  188,
  81,
  33,
  34,
  40,
  73,
  61,
  149,
  198,
  154,
  54,
  128,
  76,
  191,
  161,
  58,
  176,
  45,
  75,
  1,
  33,
  80,
  157,
  28,
  89,
]);

// Encoding
const encoder: CBOR.Encoder = new CBOR.Encoder();
const encoded: ArrayBuffer = encoder
  .object(1)
  .u8(0)
  .bytes(payload)
  .get_buffer();

// Decoding
let decoded: Uint8Array;
const decoder: CBOR.Decoder = new CBOR.Decoder(encoded);
const properties: number = decoder.object();
for (let i = 0; i < properties; i++) {
  switch (decoder.u8()) {
    case 0:
      decoded = new Uint8Array(decoder.bytes());
      break;
    default:
      decoder.skip();
  }
}

// Comparison
const isEqual = decoded.every((row, index) => {
  return decoded[index] === payload[index];
});

console.log(isEqual); // true

Readme

Keywords

none

Package Sidebar

Install

npm i @yehn/cbor

Weekly Downloads

1

Version

3.0.41

License

GPL-3.0

Unpacked Size

37.1 kB

Total Files

3

Last publish

Collaborators

  • yehn-owner