Giter Club home page Giter Club logo

cdr-ts's Introduction

CDR-TS

CDR-TS is a library to parse Common Data Representation (CDR) data in TypeScript objects. Compared to other libraries, CDR-TS uses schema-Definitions to define the structure of the data. This allows for a more type-safe and structured way to work with CDR data.

Install

npm install @mono424/cdr-ts
yarn install @mono424/cdr-ts
pnpm install @mono424/cdr-ts

Usage

1. Define Schema

import {
  CDRSchemaField,
  CDRSchemaIntValue,
  CDRSchemaSequenceValue,
  CDRSchemaStringValue,
  CDRSchemaUintValue,
} from "cdr-ts";

type TestMessageSchema = {
  send_at: CDRSchemaField & { value: CDRSchemaIntValue };
  barbershop_name: CDRSchemaField & { value: CDRSchemaStringValue };
  customers_count: CDRSchemaField & { value: CDRSchemaUintValue };
  customers: CDRSchemaField & {
    value: CDRSchemaSequenceValue & {
      itemSchema: {
        customer_name: CDRSchemaField & { value: CDRSchemaStringValue };
        customer_id: CDRSchemaField & { value: CDRSchemaStringValue };
      };
    };
  };
};

const testMessageSchema: TestMessageSchema = {
  send_at: { index: 0, value: { type: "int", len: 32 } },
  barbershop_name: { index: 1, value: { type: "uint", len: 32 } },
  customers_count: { index: 2, value: { type: "uint", len: 32 } },
  customers: {
    index: 3,
    value: {
      type: "sequence",
      itemSchema: {
        customer_name: { index: 0, value: { type: "string" } },
        customer_id: { index: 1, value: { type: "string" } },
      }
    },
  },
};

2. Decode Binary Data

import {
  decodeCDR
} from "cdr-ts";

const testPayload = "<base64-data>";

const { header, payload } = decodeCDR(testPayload, testMessageSchema);
console.log(payload);

cdr-ts's People

Contributors

mono424 avatar

Watchers

 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.