1.1.10 • Published 7 months ago

api-data-compressor v1.1.10

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

api-data-compressor

This is a simple data compressor for JSON data in API responses. It will analyze the JSON structure and transform the JS objects into arrays to reduce the size of the response.

Note: This compressor is designed for JSON structured optimizations, not a real compression algorithm like gzip.

Installation

npm install api-data-compressor

Usage

Compressing Data

import { compress } from 'api-data-compressor';

const data = [{
  id: 1,
  name: 'John Doe',
  address: {
    street: '123 Main St',
    city: 'Springfield',
    state: 'IL',
    zip: '62701'
  },
  phone: '555-555-5555',
  email: 'john.doe@google.com'
}, {
  id: 2,
  name: 'Jane Smith',
  address: {
    street: '456 Elm St',
    city: 'Springfield',
    state: 'IL',
    zip: '62701'
  },
  phone: '555-555-5555',
  email: 'jane.smith@google.com'
}, {
  id: 3,
  name: 'Bob Johnson',
  address: {
    street: '789 Oak St',
    city: 'Springfield',
    state: 'IL',
    zip: '62701'
  },
  phone: '555-555-5555',
  email: 'bob.johnson@google.com'
}, {
  id: 4,
  name: 'Alice Brown',
  address: {
    street: '1012 Pine St',
    city: 'Springfield',
    state: 'IL',
    zip: '62701'
  },
  phone: '555-555-5555',
  email: 'alice.brown@google.com'
}];

const compressedData = compress(data);

The compress function will return an object with two properties: struct and data. The struct property contains the structure of the original JSON data, and the data property contains the transformed data.

{
  "struct": [
    {
      "id": "n",
      "name": "s",
      "address": { "street": "s", "city": "s", "state": "s", "zip": "s" },
      "phone": "s",
      "email": "s"
    }
  ],
  "data": [
    [
      1,
      "John Doe",
      ["123 Main St", "Springfield", "IL", "62701"],
      "555-555-5555",
      "john.doe@google.com"
    ],
    [
      2,
      "Jane Smith",
      ["456 Elm St", "Springfield", "IL", "62701"],
      "555-555-5555",
      "jane.smith@google.com"
    ],
    [
      3,
      "Bob Johnson",
      ["789 Oak St", "Springfield", "IL", "62701"],
      "555-555-5555",
      "bob.johnson@google.com"
    ],
    [
      4,
      "Alice Brown",
      ["1012 Pine St", "Springfield", "IL", "62701"],
      "555-555-5555",
      "alice.brown@google.com"
    ]
  ]
}

In this example, the original size of the JSON data is 668 bytes, and the compressed size is 520 bytes, which is a 22% reduction in size. The actual reduction is even more pronounced in larger datasets.

Note: When the data is very small or consists of non-repeating structures, the compressed data may be larger than the original data because of the compressed data will preserve the structure of the original data. See the examples below.

Decompressing Data

import { decompress } from 'api-data-compressor';

const originalData = decompress(compressedData);

The decompress function will return the original JSON data from the compressed data returned by the compress function.

Other Examples

"Hello, World!"
{ "struct": "s", "data": "Hello, World!" }
[ 1, 2, 3, 4, 5]
{ "struct": ["n"], "data": [1, 2, 3, 4, 5] }
{ "id": 1, "name": "John Doe" }
{
  "struct": { "id": "n", "name": "s" },
  "data": [1, "John Doe"]
}
{
  "id": 1,
  "name": "John Doe",
  "address": {
    "street": "123 Main St",
    "city": "Springfield",
    "state": "IL",
    "zip": "62701"
  },
  "phone": "555-555-5555",
  "email": "john.doe@gmail.com"
}
{
  "struct": {
    "id": "n",
    "name": "s",
    "address": {
      "street": "s", "city": "s",
      "state": "s", "zip": "s"
    },
    "phone": "s",
    "email": "s"
  },
  "data": [
    1,
    "John Doe",
    ["123 Main St", "Springfield", "IL", "62701"],
    "555-555-5555",
    "john.doe@gmail.com"
  ]
}
[
  { "id": 1, "name": "John Doe" },
  { "id": 2, "name": "Jane Smith" },
  { "id": 3, "name": "Bob Johnson" },
  { "id": 4, "name": "Alice Brown" },
  { "id": 5, "name": "Charlie Davis" },
  { "id": 6, "name": "Eve Wilson" },
  { "id": 7, "name": "Grace Lee" },
  { "id": 8, "name": "Henry Young" },
  { "id": 9, "name": "Ivy King" },
  { "id": 10, "name": "Jack Wright" },
  { "id": 11, "name": "Kelly Lopez" },
  { "id": 12, "name": "Morgan Hill" },
  { "id": 13, "name": "Nora Green" },
  { "id": 14, "name": "Oscar Adams" },
  { "id": 15, "name": "Penny Baker" },
  { "id": 16, "name": "Quinn Carter" },
  { "id": 17, "name": "Riley Clark" },
  { "id": 18, "name": "Sammy Davis" },
  { "id": 19, "name": "Terry Evans" },
  { "id": 20, "name": "Ursula Fisher" },
  { "id": 21, "name": "Vivian Gray" },
  { "id": 22, "name": "Walter Harris" },
  { "id": 23, "name": "Xavier Irwin" },
  { "id": 24, "name": "Yvonne Johnson" },
  { "id": 25, "name": "Zachary King" },
  { "id": 26, "name": "Amanda Lee" },
  { "id": 27, "name": "Brian Miller" },
  { "id": 28, "name": "Cindy Nelson" },
  { "id": 29, "name": "David Olson" },
  { "id": 30, "name": "Ella Peterson" },
]
{
  "struct": [{ "id": "n", "name": "s" }],
  "data": [
    [1, "John Doe"],
    [2, "Jane Smith"],
    [3, "Bob Johnson"],
    [4, "Alice Brown"],
    [5, "Charlie Davis"],
    [6, "Eve Wilson"],
    [7, "Grace Lee"],
    [8, "Henry Young"],
    [9, "Ivy King"],
    [10, "Jack Wright"],
    [11, "Kelly Lopez"],
    [12, "Morgan Hill"],
    [13, "Nora Green"],
    [14, "Oscar Adams"],
    [15, "Penny Baker"],
    [16, "Quinn Carter"],
    [17, "Riley Clark"],
    [18, "Sammy Davis"],
    [19, "Terry Evans"],
    [20, "Ursula Fisher"],
    [21, "Vivian Gray"],
    [22, "Walter Harris"],
    [23, "Xavier Irwin"],
    [24, "Yvonne Johnson"],
    [25, "Zachary King"],
    [26, "Amanda Lee"],
    [27, "Brian Miller"],
    [28, "Cindy Nelson"],
    [29, "David Olson"],
    [30, "Ella Peterson"]
  ]
}
[
  { "a": 1, "b": 2 },
  { "b": 3, "c": 4 },
  { "c": 5, "d": 6 },
  { "d": 6, "b": 3 },
  { "c": 4, "a": 1 },
]
{
  "struct": [{
    "a": "n", "b": "n",
    "c": "n", "d": "n"
  }],
  "data": [
    [1, 2],
    [null, 3, 4],
    [null, null, 5, 6],
    [null, 3, null, 6],
    [1, null, 4]
  ]
}
[
  1,
  {"foo":"bar"},
  [2, [{"foo":"bar"}, 4], 5],
  6,
  [{"foo":[{"a": 1, "b": 2}, [1, 2, 3]]}]
]
{
  "struct": [
    {
      "foo": "s",
      "": {
        "": { "foo": "s" },
        "foo": [ { "a": "n", "b": "n", "": "n" } ]
      }
    }
  ],
  "data": [
    1,
    [ "bar" ],
    [ {}, 2, [ [ "bar" ], 4 ], 5 ],
    6,
    [
      {},
      [ {}, [ [ 1, 2 ], [ {}, 1, 2, 3 ] ] ]
    ]
  ]
}

License

MIT

1.1.9

7 months ago

1.1.8

7 months ago

1.1.7

7 months ago

1.1.6

7 months ago

1.1.10

7 months ago

1.1.5

7 months ago

1.1.1

8 months ago

1.1.4

8 months ago

1.1.3

8 months ago

1.1.2

8 months ago

1.1.0

8 months ago

1.0.13

8 months ago

1.0.12

8 months ago

1.0.11

8 months ago

1.0.10

8 months ago

1.0.9

8 months ago

1.0.8

8 months ago

1.0.7

8 months ago

1.0.6

8 months ago

1.0.5

8 months ago

1.0.4

8 months ago

1.0.3

8 months ago

1.0.2

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago