compress-json
Advanced tools
Comparing version 3.0.0 to 3.0.1
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.test = exports.bool_to_s = exports.s_to_bool = void 0; | ||
function s_to_bool(s) { | ||
@@ -4,0 +5,0 @@ switch (s) { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.config = void 0; | ||
exports.config = { | ||
sort_key: false, | ||
}; |
import { Key, Value } from './memory'; | ||
export declare type Values = Value[]; | ||
export declare type Compressed = [Values, Key]; | ||
export type Values = Value[]; | ||
export type Compressed = [Values, Key]; | ||
export declare function compress(o: object): Compressed; | ||
export declare function decode(values: Values, key: Key): any; | ||
export declare function decompress(c: Compressed): any; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.decompress = exports.decode = exports.compress = void 0; | ||
const debug_1 = require("./debug"); | ||
@@ -7,5 +8,5 @@ const encode_1 = require("./encode"); | ||
function compress(o) { | ||
const mem = memory_1.makeInMemoryMemory(); | ||
const root = memory_1.addValue(mem, o, undefined); | ||
const values = memory_1.memToValues(mem); | ||
const mem = (0, memory_1.makeInMemoryMemory)(); | ||
const root = (0, memory_1.addValue)(mem, o, undefined); | ||
const values = (0, memory_1.memToValues)(mem); | ||
return [values, root]; | ||
@@ -53,3 +54,3 @@ } | ||
} | ||
const id = encode_1.decodeKey(key); | ||
const id = (0, encode_1.decodeKey)(key); | ||
const v = values[id]; | ||
@@ -68,14 +69,14 @@ if (v === null) { | ||
case 'b|': | ||
return encode_1.decodeBool(v); | ||
return (0, encode_1.decodeBool)(v); | ||
case 'o|': | ||
return decodeObject(values, v); | ||
case 'n|': | ||
return encode_1.decodeNum(v); | ||
return (0, encode_1.decodeNum)(v); | ||
case 'a|': | ||
return decodeArray(values, v); | ||
default: | ||
return encode_1.decodeStr(v); | ||
return (0, encode_1.decodeStr)(v); | ||
} | ||
} | ||
return debug_1.throwUnknownDataType(v); | ||
return (0, debug_1.throwUnknownDataType)(v); | ||
} | ||
@@ -82,0 +83,0 @@ exports.decode = decode; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.throwUnknownDataType = exports.getType = void 0; | ||
function getType(o) { | ||
@@ -4,0 +5,0 @@ return Object.prototype.toString.call(o); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.decodeStr = exports.encodeStr = exports.decodeBool = exports.encodeBool = exports.decodeKey = exports.decodeNum = exports.encodeNum = void 0; | ||
const number_1 = require("./number"); | ||
function encodeNum(num) { | ||
const a = 'n|' + number_1.num_to_s(num); | ||
const a = 'n|' + (0, number_1.num_to_s)(num); | ||
return a; | ||
@@ -13,7 +14,7 @@ // let b = num.toString() | ||
s = s.replace('n|', ''); | ||
return number_1.s_to_num(s); | ||
return (0, number_1.s_to_num)(s); | ||
} | ||
exports.decodeNum = decodeNum; | ||
function decodeKey(key) { | ||
return typeof key === 'number' ? key : number_1.s_to_int(key); | ||
return typeof key === 'number' ? key : (0, number_1.s_to_int)(key); | ||
} | ||
@@ -20,0 +21,0 @@ exports.decodeKey = decodeKey; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.trimUndefinedRecursively = exports.trimUndefined = void 0; | ||
function trimUndefined(object) { | ||
@@ -4,0 +5,0 @@ for (const key in object) { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.trimUndefinedRecursively = exports.trimUndefined = exports.addValue = exports.decode = exports.decompress = exports.compress = void 0; | ||
/* for direct usage */ | ||
var core_1 = require("./core"); | ||
exports.compress = core_1.compress; | ||
exports.decompress = core_1.decompress; | ||
Object.defineProperty(exports, "compress", { enumerable: true, get: function () { return core_1.compress; } }); | ||
Object.defineProperty(exports, "decompress", { enumerable: true, get: function () { return core_1.decompress; } }); | ||
/* for custom wrapper */ | ||
var core_2 = require("./core"); | ||
exports.decode = core_2.decode; | ||
Object.defineProperty(exports, "decode", { enumerable: true, get: function () { return core_2.decode; } }); | ||
var memory_1 = require("./memory"); | ||
exports.addValue = memory_1.addValue; | ||
Object.defineProperty(exports, "addValue", { enumerable: true, get: function () { return memory_1.addValue; } }); | ||
/* to remove undefined object fields */ | ||
var helpers_1 = require("./helpers"); | ||
exports.trimUndefined = helpers_1.trimUndefined; | ||
exports.trimUndefinedRecursively = helpers_1.trimUndefinedRecursively; | ||
Object.defineProperty(exports, "trimUndefined", { enumerable: true, get: function () { return helpers_1.trimUndefined; } }); | ||
Object.defineProperty(exports, "trimUndefinedRecursively", { enumerable: true, get: function () { return helpers_1.trimUndefinedRecursively; } }); |
@@ -1,4 +0,4 @@ | ||
declare type Parent = any[] | object; | ||
export declare type Key = string; | ||
export declare type Value = string | null; | ||
type Parent = any[] | object; | ||
export type Key = string; | ||
export type Value = string | null; | ||
/** | ||
@@ -5,0 +5,0 @@ * potential implementation of store are: |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.addValue = exports.makeInMemoryMemory = exports.makeInMemoryCache = exports.makeInMemoryStore = exports.memToValues = void 0; | ||
const config_1 = require("./config"); | ||
@@ -82,3 +83,3 @@ const debug_1 = require("./debug"); | ||
const id = mem.keyCount++; | ||
const key = number_1.num_to_s(id); | ||
const key = (0, number_1.num_to_s)(id); | ||
mem.store.add(value); | ||
@@ -143,10 +144,10 @@ mem.cache.setValue(value, key); | ||
case 'boolean': | ||
return getValueKey(mem, encode_1.encodeBool(o)); | ||
return getValueKey(mem, (0, encode_1.encodeBool)(o)); | ||
case 'number': | ||
return getValueKey(mem, encode_1.encodeNum(o)); | ||
return getValueKey(mem, (0, encode_1.encodeNum)(o)); | ||
case 'string': | ||
return getValueKey(mem, encode_1.encodeStr(o)); | ||
return getValueKey(mem, (0, encode_1.encodeStr)(o)); | ||
} | ||
return debug_1.throwUnknownDataType(o); | ||
return (0, debug_1.throwUnknownDataType)(o); | ||
} | ||
exports.addValue = addValue; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.s_to_num = exports.int_str_to_s = exports.num_to_s = exports.big_int_to_s = exports.int_to_s = exports.s_to_big_int = exports.s_to_int = void 0; | ||
let i_to_s = ''; | ||
@@ -4,0 +5,0 @@ for (let i = 0; i < 10; i++) { |
{ | ||
"name": "compress-json", | ||
"version": "3.0.0", | ||
"version": "3.0.1", | ||
"description": "convert JSON data to space efficient format", | ||
@@ -17,12 +17,16 @@ "keywords": [ | ||
"types": "dist/index.d.ts", | ||
"browser": "./bundle.js", | ||
"unpkg": "./bundle.js", | ||
"scripts": { | ||
"test": "npm run rawtest", | ||
"test": "run-s format build test-case", | ||
"clean": "rimraf dist", | ||
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", | ||
"postformat": "tslint -p . --fix", | ||
"prebuild": "npm run clean", | ||
"build": "tsc -p tsconfig.build.json", | ||
"pretest": "npm run format && npm run build", | ||
"rawtest": "ts-node test/index.ts", | ||
"prepublishOnly": "npm run rawtest && npm run build" | ||
"format": "run-s prettier tslint", | ||
"prettier": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", | ||
"tslint": "tslint -p . --fix", | ||
"build": "run-s clean tsc bundle minify", | ||
"tsc": "tsc -p tsconfig.build.json", | ||
"bundle": "esbuild --bundle src/browser.ts --outfile=bundle.js", | ||
"minify": "esbuild --bundle --minify src/browser.ts --outfile=bundle.min.js", | ||
"test-case": "ts-node test/index.ts", | ||
"prepublishOnly": "run-s test-case build" | ||
}, | ||
@@ -35,11 +39,12 @@ "directories": { | ||
], | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"@beenotung/tslib": "^16.20.1", | ||
"@beenotung/tslib": "^23.4.0", | ||
"@types/node": "*", | ||
"compressed-json": "^1.0.15", | ||
"esbuild": "^0.20.2", | ||
"jsonpack": "^1.1.5", | ||
"npm-run-all": "^4.1.5", | ||
"prettier": "^2.0.5", | ||
"rimraf": "^3.0.2", | ||
"ts-node": "^8.10.1", | ||
"ts-node": "^10.9.2", | ||
"tslint": "^6.1.2", | ||
@@ -49,3 +54,3 @@ "tslint-config-prettier": "^1.18.0", | ||
"tslint-etc": "^1.10.1", | ||
"typescript": "^3.8.3" | ||
"typescript": "^5.4.3" | ||
}, | ||
@@ -52,0 +57,0 @@ "repository": { |
153
README.md
@@ -6,2 +6,3 @@ # compress-json | ||
[![npm Package Version](https://img.shields.io/npm/v/compress-json.svg)](https://www.npmjs.com/package/compress-json) | ||
[![Minified Package Size](https://img.shields.io/bundlephobia/min/compress-json)](https://bundlephobia.com/package/compress-json) | ||
[![npm Package Downloads](https://img.shields.io/npm/dm/compress-json)](https://www.npmtrends.com/compress-json) | ||
@@ -16,2 +17,3 @@ | ||
## Features | ||
- Supports all JSON types | ||
@@ -22,9 +24,10 @@ - Object key order is preserved | ||
- Support multiple storage backend | ||
- in-memory object / array / Map | ||
- localStorage | ||
- lmdb | ||
- leveldb (sync mode) | ||
- custom adapter | ||
- in-memory object / array / Map | ||
- localStorage | ||
- lmdb | ||
- leveldb (sync mode) | ||
- custom adapter | ||
## All Implementations | ||
- Javascript/Typescript: [source](https://github.com/beenotung/compress-json) / [package](https://www.npmjs.com/package/compress-json) | ||
@@ -36,2 +39,5 @@ - PHP: [source](https://github.com/inkrot/php-compress-json) / [package](https://packagist.org/packages/inkrot/php-compress-json) | ||
## Installation | ||
You can install `compress-json` from npm: | ||
```bash | ||
@@ -41,7 +47,53 @@ npm i -S compress-json | ||
Then import from typescript using named import or star import: | ||
```typescript | ||
import { compress, decompress } from 'compress-json' | ||
import * as compressJSON from 'compress-json' | ||
``` | ||
Or import from javascript as commonjs module: | ||
```javascript | ||
var compressJSON = require('compress-json') | ||
``` | ||
You can also load `compress-json` directly in html via CDN: | ||
```html | ||
<script src="https://cdn.jsdelivr.net/npm/compress-json@3/bundle.js"></script> | ||
<script> | ||
console.log(compressJSON) | ||
/* | ||
{ | ||
// for direct usage | ||
compress, | ||
decompress, | ||
// for custom wrapper | ||
decode, | ||
addValue, | ||
// to remove undefined object fields | ||
trimUndefined, | ||
trimUndefinedRecursively, | ||
} | ||
*/ | ||
</script> | ||
``` | ||
If you do not intend to inspect the source of `compress-json`, you can load the minified version for smaller file size: | ||
```html | ||
<script src="https://cdn.jsdelivr.net/npm/compress-json@3/bundle.min.js"></script> | ||
``` | ||
Details see [index.ts](./src/index.ts) | ||
## Usage | ||
```typescript | ||
import { compress, decompress } from 'compress-json' | ||
let data = { | ||
let data = { | ||
user: 'Alice', | ||
@@ -54,3 +106,3 @@ // more fields of any json values (string, number, array, object, e.t.c.) | ||
method: 'post', | ||
body: JSON.stringify(compressed) // convert into string if needed | ||
body: JSON.stringify(compressed), // convert into string if needed | ||
}) | ||
@@ -64,3 +116,5 @@ | ||
## Format | ||
**Sample data**: | ||
```typescript | ||
@@ -78,10 +132,11 @@ let longStr = 'A very very long string, that is repeated' | ||
arr2: [42, longStr], // identical values will be deduplidated, including array and object | ||
obj: { // nested values are supported | ||
obj: { | ||
// nested values are supported | ||
id: 123, | ||
name: 'Alice', | ||
role: [ 'Admin', 'User', 'Guest' ], | ||
role: ['Admin', 'User', 'Guest'], | ||
longStr: 'A very very long string, that is repeated', | ||
longNum: 9876543210.123455 | ||
longNum: 9876543210.123455, | ||
}, | ||
escape: [ 's|str', 'n|123', 'o|1', 'a|1', 'b|T', 'b|F' ] | ||
escape: ['s|str', 'n|123', 'o|1', 'a|1', 'b|T', 'b|F'], | ||
} | ||
@@ -91,6 +146,8 @@ ``` | ||
**Compressed data**: | ||
```typescript | ||
// [ encoded value array, root value index ] | ||
let compressed = [ | ||
[ // encoded value array | ||
[ | ||
// encoded value array | ||
'int', // string | ||
@@ -133,5 +190,5 @@ 'float', | ||
'a|U|V|W|X|Y|Z', | ||
'o|B|C|D|E|F|G|H|I|J|J|T|a' | ||
'o|B|C|D|E|F|G|H|I|J|J|T|a', | ||
], | ||
'b' // root value index | ||
'b', // root value index | ||
] | ||
@@ -141,2 +198,3 @@ ``` | ||
## Helper Functions | ||
```typescript | ||
@@ -167,3 +225,4 @@ import { compress } from 'compress-json' | ||
Algorithms in comparison: | ||
- JSON (`JSON.stringify` without indentation) | ||
- JSON (`JSON.stringify` without indentation) | ||
- compressed-json | ||
@@ -176,40 +235,44 @@ - jsonpack | ||
### Compressed Size | ||
| sample | JSON | compressed-json | jsonpack | **compress-json** | | ||
|---|---|---|---|---| | ||
| all | 263M | 199M | - | 176M | | ||
| 100,000 | 235M | 178M | - | 158M | | ||
| 50,000 | 70M | 55M | - | 50M | | ||
| 10,000 | 34M | 26M | - | 23M | | ||
| 2,000 | 6.6M | 5.0M | 5.3M | 4.4M | | ||
| 1,000 | 4.8M | 3.7M | 3.8M | 3.3M | | ||
| 100 | 335K | 265K | 271K | 243K | | ||
| 10 | 4.0K | 3.3K | 3.0K | 3.2K | | ||
| ------- | ---- | --------------- | -------- | ----------------- | | ||
| all | 263M | 199M | - | 176M | | ||
| 100,000 | 235M | 178M | - | 158M | | ||
| 50,000 | 70M | 55M | - | 50M | | ||
| 10,000 | 34M | 26M | - | 23M | | ||
| 2,000 | 6.6M | 5.0M | 5.3M | 4.4M | | ||
| 1,000 | 4.8M | 3.7M | 3.8M | 3.3M | | ||
| 100 | 335K | 265K | 271K | 243K | | ||
| 10 | 4.0K | 3.3K | 3.0K | 3.2K | | ||
### Compression Time | ||
| sample | JSON | compressed-json | jsonpack | **compress-json** | | ||
|---|---|---|---|---| | ||
| all | 1,654ms | 12,674ms | timeout* | 15,788ms | | ||
| 100,000 | 1,500ms | 10,921ms | timeout* | 12,715ms | | ||
| 50,000 | 462ms | 3,047ms | timeout* | 3,935ms | | ||
| 10,000 | 146ms | 1,278ms | timeout* | 1,733ms | | ||
| 2,000 | 35ms | 328ms | 21,018ms | 456ms | | ||
| 1,000 | 20ms | 270ms | 12,960ms | 390ms | | ||
| 100 | 1ms | 18ms | 47ms | 37ms | | ||
| 10 | 0.3ms | 1.8ms | 1.6ms | 1.9ms | | ||
*timeout: excess 1 minute | ||
| sample | JSON | compressed-json | jsonpack | **compress-json** | | ||
| ------- | ------- | --------------- | --------- | ----------------- | | ||
| all | 1,654ms | 12,674ms | timeout\* | 15,788ms | | ||
| 100,000 | 1,500ms | 10,921ms | timeout\* | 12,715ms | | ||
| 50,000 | 462ms | 3,047ms | timeout\* | 3,935ms | | ||
| 10,000 | 146ms | 1,278ms | timeout\* | 1,733ms | | ||
| 2,000 | 35ms | 328ms | 21,018ms | 456ms | | ||
| 1,000 | 20ms | 270ms | 12,960ms | 390ms | | ||
| 100 | 1ms | 18ms | 47ms | 37ms | | ||
| 10 | 0.3ms | 1.8ms | 1.6ms | 1.9ms | | ||
\*timeout: excess 1 minute | ||
### Decompress Time | ||
| sample | JSON | compressed-json | jsonpack | **compress-json** | | ||
|---|---|---|---|---| | ||
| all | 1,908ms | 4,611ms | - | 9,225ms | | ||
| 100,000 | 1,744ms | 3,740ms | - | 7,576ms | | ||
| 50,000 | 558ms | 1,066ms | - | 2,452ms | | ||
| 10,000 | 173ms | 460ms | - | 1,055ms | | ||
| 2,000 | 47ms | 108ms | 189ms | 317ms | | ||
| 1,000 | 34ms | 90ms | 160ms | 263ms | | ||
| 100 | 2ms | 11ms | 16ms | 19ms | | ||
| 10 | 2.9ms | 2.1ms | 1.1ms | 1.3ms | | ||
| sample | JSON | compressed-json | jsonpack | **compress-json** | | ||
| ------- | ------- | --------------- | -------- | ----------------- | | ||
| all | 1,908ms | 4,611ms | - | 9,225ms | | ||
| 100,000 | 1,744ms | 3,740ms | - | 7,576ms | | ||
| 50,000 | 558ms | 1,066ms | - | 2,452ms | | ||
| 10,000 | 173ms | 460ms | - | 1,055ms | | ||
| 2,000 | 47ms | 108ms | 189ms | 317ms | | ||
| 1,000 | 34ms | 90ms | 160ms | 263ms | | ||
| 100 | 2ms | 11ms | 16ms | 19ms | | ||
| 10 | 2.9ms | 2.1ms | 1.1ms | 1.3ms | | ||
## License | ||
[BSD 2-Clause License](./LICENSE) (Free Open Sourced Software) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
29783
23
630
266
14