npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jsbuffer

v2.0.12

Published

## Installation

Downloads

122

Readme

jsbuffer

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

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.

Demo

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

jsb: Support for other languages

The new jsb command-line tool supports generating code for additional languages, currently, the CLI offer experimental support for the following languages:

  • C99
  • C++17

Usage

npm i -g jsbuffer@^2
jsb -h

Examples

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