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

acos-json-delta

v1.0.4

Published

JSON delta and merge used by ACOS.games

Downloads

282

Readme

ACOS JSON Delta for Websocket Networking

Generate a delta of your JSON data of any changes, reducing your bandwidth bytes transmitted to players. When JSON delta is received at receiver, merge it with the previous JSON data.

This package is used in combination with the acos-json-encoder to maximize bandwidth reduction.

Installation

npm i acos-json-delta

Usage

CommonJS usage

const { delta, merge, hidden } = require("acos-json-delta");

ES6 usage

import {delta, merge, hidden} = require("acos-json-delta");

Delta and Merge example

//example JSON data
let defaultGame = {
  state: {
    board: [
      [0, 2, 0, 2, 0, 2, 0, 2], //white
      [2, 0, 2, 0, 2, 0, 2, 0],
      [0, 2, 0, 2, 0, 2, 0, 2],
      [0, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0],
      [1, 0, 1, 0, 1, 0, 1, 0],
      [0, 1, 0, 1, 0, 1, 0, 1],
      [1, 0, 1, 0, 1, 0, 1, 0], //black
    ],
    cells: ["", "", "", "", "", "", "", "", ""],
    startPlayer: "",
  },
  players: {},
  rules: {
    bestOf: 5,
    maxPlayers: 2,
  },
  next: {},
  events: [],
};

// mutate the JSON data
let changed = JSON.parse(JSON.stringify(defaultGame));
changed.state.board[0][0] = 1;
changed.state.cells[0] = "x";
changed.state.cells[1] = "x";
changed.state.cells[2] = "o";
changed.players["joe"] = { name: "Joe", type: "x" };
delete changed.state.startPlayer;

// build a delta of the mutated JSON
let diff = delta(defaultGame, changed, {});

// print delta
console.log("------------------");
console.log("delta1: \n", JSON.stringify(diff, null, 2));

// merge the delta with previous
let merged = merge(defaultGame, diff);

// mutate the new JSON data again
changed = JSON.parse(JSON.stringify(merged));
delete changed.players.joe.type;

// build another delta of the JSON
diff = delta(merged, changed, {});

// print delta
console.log("------------------");
console.log("delta2: \n", JSON.stringify(diff, null, 2));

// merge the delta from mutated JSON with previous
merged = merge(merged, diff);
Example Output

The array changes appear complex, but when combined with acos-json-encoder they are efficiently optimized to use up minimum bytes. You can identify array deltas when their key is prefixed with #.

Deletions of an object key are added to a key named $ with array of keys to be deleted on merge. See delta2 example below.

------------------
delta1:
 {
  "state": {
    "#board": [
      {
        "index": 0,
        "type": "nested",
        "value": [
          {
            "index": 0,
            "type": "setvalue",
            "value": 1
          }
        ]
      }
    ],
    "#cells": [
      {
        "index": 0,
        "type": "setvalue",
        "value": "x"
      },
      {
        "index": 1,
        "type": "setvalue",
        "value": "x"
      },
      {
        "index": 2,
        "type": "setvalue",
        "value": "o"
      }
    ],
    "$": [
      "startPlayer"
    ]
  },
  "players": {
    "joe": {
      "name": "Joe",
      "type": "x"
    }
  }
}
------------------
delta2:
 {
  "players": {
    "joe": {
      "$": [
        "type"
      ]
    }
  }
}

Methods

delta(from, to, result)

Generate a delta between two JSON data

Parameters
  • from (JSON object or array) - Data before mutation
  • to (JSON object or array) - Data after mutation
  • result (JSON object or array) - Data after merging the from/to
Returns

result is returned redundantly, for readability.

.

merge(from, delta)

Merge JSON data with a JSON delta

Parameters
  • from (JSON object or array) - Data previously saved
  • delta (JSON object or array) - Delta JSON generated using the delta function
Returns

JSON data merged between the from and the delta.

.

hidden(json)

Parameters
  • json (JSON object or array) - Data that will be mutated to delete objects

Deletes and extracts the keys marked as hidden. Keys with prefix _ are marked hidden.

Returns

Extracted hidden keys and values.