1.0.34 • Published 6 days ago

jsbuffer v1.0.34

Weekly downloads
-
License
MIT
Repository
-
Last release
6 days ago

jsbuffer

Description

jsbuffer is the implementation of a type language. We offer tools for you to generate TypeScript interfaces and functions to encode and decode your data, and more.

Usage

Example with command-line tool

Create a schema/main file:

type User {
  int id;
  string name;
}

Run your terminal:

npx jsbuffer schema/main -o src/schema

Help

npx jsbuffer -h

Or

npx jsbuffer --help

TypeScript interfaces

So this:

type user {
  int id;
  string name;
}

Generate a TypeScript interface:

interface user {
  id: number;
  name: string;
}

Codec functions

This:

type user {
  int id;
  string name;
}

Generate an encode function:

export function encodeUser(s: ISerializer, value: user) {
  s.writeInt32(-399411702);
  /**
   * encoding param: id
   */
  const __pv0 = value['id'];
  s.writeInt32(__pv0);
  /**
   * encoding param: name
   */
  const __pv1 = value['name'];
  s.writeString(__pv1);
}

And a decode function:

export function decodeUser(__d: IDeserializer): user | null {
  const __id = __d.readInt32();
  /**
   * decode header
   */
  if (__id !== -399411702) return null;
  let id: number;
  let name: string;
  /**
   * decoding param: id
   */
  id = __d.readInt32();
  /**
   * decoding param: name
   */
  name = __d.readString();
  return {
    _name: 'schema.user',
    id,
    name,
  };
}

Default functions

Functions that are supposed to initialize these interfaces with default data in it:

This:

type user {
  int id;
  string name;
}

Generate this function:

export function defaultUser(params: Partial<userInputParams> = {}): user {
  return user({
    id: 0,
    name: '',
    ...params,
  });
}

Deep comparison functions

This:

type user {
  int id;
  string name;
}

Generate a comparison function:

export function compareUser(__a: user, __b: user) {
  return (
    /**
     * compare parameter id
     */
    __a['id'] === __b['id'] &&
    /**
     * compare parameter name
     */
    __a['name'] === __b['name']
  );
}

Update functions

Generated update functions uses the deep comparison expressions to make sure the reference of the input object is never changed, if there is no change in the changes argument even if you're using complex objects. To give you an example, let's say you have the following type definition:

type testMap2 {
  map<optional<string>,string> a;
  map<optional<string>,tuple<string,map<int,int>>> b;
}

The code generator will create an update function with the following signature:

function updateTestMap2(
  value: testMap2,
  changes: Partial<testMap2InputParams>
): testMap2;

So the following test case would pass without errors:

import assert from 'assert';
import { testMap2, updateTestMap2 } from '../out/schema';

const a1 = testMap2({
  a: new Map([
    ['a', '1'],
    ['b', '2'],
    ['c', '3'],
  ]),
  b: new Map([['a', ['', new Map([[1, 2]])]]]),
});

assert.strict.equal(updateTestMap2(a1, {}), a1);
assert.strict.equal(
  updateTestMap2(a1, {
    b: new Map([['a', ['', new Map([[1, 2]])]]]),
  }),
  a1
);
assert.strict.notEqual(
  updateTestMap2(a1, {
    b: new Map([['a', ['', new Map([[1, 3]])]]]),
  }),
  a1
);
assert.strict.deepEqual(
  updateTestMap2(a1, {
    b: new Map([['a', ['', new Map([[1, 3]])]]]),
  }),
  testMap2({
    a: new Map([
      ['a', '1'],
      ['b', '2'],
      ['c', '3'],
    ]),
    b: new Map([['a', ['', new Map([[1, 3]])]]]),
  })
);

Traits

trait User {}
type user : User {
  ulong id;
}
type userDeleted : User {
  ulong deletedAt;
}

Becomes this:

export type User = userDeleted | user;

Complex type structures

To me, the most cool part of jsbuffer, is that you can create all sort of complex type structures that involve many parts and we will resolve and generate the code and files accordingly:

import {refUser} from "./Ref";
type comment {
  int id;
  refUser author;
  string comment;
}
type post {
  int id;
  refUser author;
  string title;
  string contents;
  vector<comment> comments;
}
type user {
  int id;
  string firstName;
  vector<post> posts;
}

Data types

  • set<t>
  • map<k,v>
  • vector<t>
  • tuple<a,b,c,d,e,f,...>
  • null_terminated_string
  • string
  • int
  • int32
  • uint32
  • ulong
  • long
  • int16
  • uint16
  • int8
  • uint8

Demo

You can try jsbuffer online in the online playground, still in very early progress, but it works.

Installation

yarn add jsbuffer
npm i jsbuffer

Usage

npm install -g jsbuffer
jsbuffer schema/src -o schema
jsbuffer schema/src -o schema --extends tsconfig.base.json
1.0.34

6 days ago

1.0.32

14 days ago

1.0.31

14 days ago

1.0.30

14 days ago

1.0.29

4 months ago

1.0.17

7 months ago

1.0.3

7 months ago

1.0.28

7 months ago

1.0.2

9 months ago

1.0.1

9 months ago

1.0.0

9 months ago

0.0.44

12 months ago

0.0.45

12 months ago

0.0.46

12 months ago

0.0.47

12 months ago

0.0.73

10 months ago

0.0.75

10 months ago

0.0.76

10 months ago

0.0.77

10 months ago

0.0.70

10 months ago

0.0.71

10 months ago

0.0.72

10 months ago

0.0.62

10 months ago

0.0.63

10 months ago

0.0.65

10 months ago

0.0.66

10 months ago

0.0.69

10 months ago

0.0.60

10 months ago

0.0.61

10 months ago

0.0.59

10 months ago

0.0.51

12 months ago

0.0.53

12 months ago

0.0.55

10 months ago

0.0.48

12 months ago

0.0.49

12 months ago

0.0.43

1 year ago

0.0.42

1 year ago

0.0.41

1 year ago

0.0.40

1 year ago

0.0.39

1 year ago

0.0.38

1 year ago

0.0.37

1 year ago

0.0.36

1 year ago

0.0.33

1 year ago

0.0.32

1 year ago

0.0.31

1 year ago

0.0.30

1 year ago

0.0.29

1 year ago

0.0.28

1 year ago

0.0.27

1 year ago

0.0.26

1 year ago

0.0.25

1 year ago

0.0.24

1 year ago

0.0.23

1 year ago

0.0.22

1 year ago

0.0.21

1 year ago

0.0.20

1 year ago

0.0.19

1 year ago

0.0.18

1 year ago

0.0.17

1 year ago

0.0.16

1 year ago

0.0.15

1 year ago

0.0.14

1 year ago

0.0.13

1 year ago

0.0.12

1 year ago

0.0.11

1 year ago

0.0.10

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago