@trenskow/json-compressor 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,16 @@
1
+ {
2
+ // Use IntelliSense to learn about possible attributes.
3
+ // Hover to view descriptions of existing attributes.
4
+ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5
+ "version": "0.2.0",
6
+ "configurations": [
7
+ {
8
+ "type":"node",
9
+ "request":"launch",
10
+ "name":"Run tests",
11
+ "runtimeExecutable":"_mocha",
12
+ "cwd":"${workspaceFolder}",
13
+ "args":[]
14
+ }
15
+ ]
16
+ }
package/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ Copyright 2025 Kristian Trenskow
2
+
3
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
+
5
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
+
7
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+
9
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md ADDED
@@ -0,0 +1,29 @@
1
+ @trenskow/json-compressor
2
+ ----
3
+
4
+ A simple JSON to JSON compressor for JavaScript.
5
+
6
+ It also preserves circular references – useful for stringifying complex object graphs.
7
+
8
+ # Usage
9
+
10
+ Below is an example of how to use.
11
+
12
+ ````javascript
13
+ import { inflate, deflate } from '@trenskow/json-compressor';
14
+
15
+ const myObject = {
16
+ hello: 'world'
17
+ };
18
+
19
+ myObject.circular = myObject;
20
+
21
+ const json = JSON.stringify(deflate(myObject));
22
+
23
+ inflate(JSON.parse(JSON)); // Returns copy of original instance (with circular reference preserved).
24
+ ````
25
+
26
+ # License
27
+
28
+ See license in LICENSE.
29
+
@@ -0,0 +1,55 @@
1
+ import globals from 'globals';
2
+ import path from 'node:path';
3
+ import { fileURLToPath } from 'node:url';
4
+ import js from '@eslint/js';
5
+ import { FlatCompat } from '@eslint/eslintrc';
6
+
7
+ const __filename = fileURLToPath(import.meta.url);
8
+ const __dirname = path.dirname(__filename);
9
+ const compat = new FlatCompat({
10
+ baseDirectory: __dirname,
11
+ recommendedConfig: js.configs.recommended,
12
+ allConfig: js.configs.all
13
+ });
14
+
15
+ export default [...compat.extends('eslint:recommended'), {
16
+ languageOptions: {
17
+ globals: {
18
+ ...globals.node,
19
+ ...globals.mocha,
20
+ },
21
+
22
+ ecmaVersion: 'latest',
23
+ sourceType: 'module',
24
+ },
25
+
26
+ rules: {
27
+ indent: ['error', 'tab', {
28
+ SwitchCase: 1,
29
+ }],
30
+
31
+ 'linebreak-style': ['error', 'unix'],
32
+ quotes: ['error', 'single'],
33
+ semi: ['error', 'always'],
34
+
35
+ 'no-console': ['error', {
36
+ allow: ['warn', 'error', 'info'],
37
+ }],
38
+
39
+ 'no-unused-vars': ['error', {
40
+ argsIgnorePattern: '^_',
41
+ }],
42
+
43
+ 'no-empty': ['error', {
44
+ allowEmptyCatch: true,
45
+ }],
46
+
47
+ 'no-trailing-spaces': ['error', {
48
+ ignoreComments: true,
49
+ }],
50
+
51
+ 'require-atomic-updates': 'off',
52
+ 'no-implicit-globals': ['error'],
53
+ 'eol-last': ['error', 'always'],
54
+ },
55
+ }];
package/index.js ADDED
@@ -0,0 +1,9 @@
1
+ //
2
+ // index.js
3
+ // @trenskow/json-compressor
4
+ //
5
+ // Created by Kristian Trenskow on 2025/12/19
6
+ // For license see LICENSE.
7
+ //
8
+
9
+ export * from './lib/index.js';
package/lib/index.js ADDED
@@ -0,0 +1,196 @@
1
+ //
2
+ // index.js
3
+ // @trenskow/json-compressor
4
+ //
5
+ // Created by Kristian Trenskow on 2025/12/19
6
+ // For license see LICENSE.
7
+ //
8
+
9
+ import equals from '@trenskow/equals';
10
+
11
+ const literals = {
12
+ [undefined]: -1,
13
+ [null]: -2,
14
+ [true]: -3,
15
+ [false]: -4,
16
+ };
17
+
18
+ const indices = {
19
+ [-1]: undefined,
20
+ [-2]: null,
21
+ [-3]: true,
22
+ [-4]: false,
23
+ };
24
+
25
+ const _deflate = (json, output, seen) => {
26
+
27
+ const referenceIndex = seen.findIndex((item) => typeof item === 'object' && json === item);
28
+
29
+ if (referenceIndex !== -1) {
30
+ return `"${referenceIndex}"`;
31
+ }
32
+
33
+ const index = seen.findIndex((item) => equals(json, item));
34
+
35
+ if (index !== -1) {
36
+ return index;
37
+ }
38
+
39
+ if (json in literals) {
40
+ return literals[json];
41
+ }
42
+
43
+ seen.push(json);
44
+
45
+ if (typeof json === 'object') {
46
+
47
+ if (Array.isArray(json)) {
48
+ return _deflate.array(json, output, seen);
49
+ }
50
+
51
+ return _deflate.object(json, output, seen);
52
+
53
+ }
54
+
55
+ output.push(json);
56
+
57
+ return output.length - 1;
58
+
59
+ };
60
+
61
+ _deflate.array = (json, output, seen) => {
62
+
63
+ const result = ['a'];
64
+
65
+ output.push(result);
66
+
67
+ const index = output.length - 1;
68
+
69
+ for (let i = 0; i < json.length; i++) {
70
+ result.push(_deflate(json[i], output, seen));
71
+ }
72
+
73
+ return index;
74
+
75
+ };
76
+
77
+ _deflate.object = (json, output, seen) => {
78
+
79
+ const result = ['o'];
80
+
81
+ output.push(result);
82
+
83
+ const index = output.length - 1;
84
+
85
+ for (const key in json) {
86
+ result.push(_deflate(key, output, seen));
87
+ result.push(_deflate(json[key], output, seen));
88
+ }
89
+
90
+ return index;
91
+
92
+ };
93
+
94
+ const deflate = (json) => {
95
+
96
+ const output = [];
97
+
98
+ _deflate(json, output, []);
99
+
100
+ if (output.length < 2) {
101
+ return json;
102
+ }
103
+
104
+ return output;
105
+
106
+ };
107
+
108
+ const _inflate = (data, index, resolved) => {
109
+
110
+ if (index in indices) {
111
+ return indices[index];
112
+ }
113
+
114
+ if (typeof index === 'string' && index.startsWith('"') && index.endsWith('"')) {
115
+ return resolved[parseInt(index.slice(1, -1), 10)];
116
+ }
117
+
118
+ const value = data[index];
119
+
120
+ if (Array.isArray(value)) {
121
+
122
+ if (value[0] === 'a') {
123
+ return _inflate.array(data, index, value.slice(1), resolved);
124
+ }
125
+
126
+ if (value[0] === 'o') {
127
+ return _inflate.object(data, index, value.slice(1), resolved);
128
+ }
129
+
130
+ return data;
131
+
132
+ }
133
+
134
+ return value;
135
+
136
+ };
137
+
138
+ _inflate.array = (data, index, value, resolved) => {
139
+
140
+ const result = [];
141
+
142
+ resolved[index] = result;
143
+
144
+ for (let i = 0; i < value.length; i++) {
145
+ result.push(_inflate(data, value[i], resolved));
146
+ }
147
+
148
+ return result;
149
+
150
+ };
151
+
152
+ _inflate.object = (data, index, value, resolved) => {
153
+
154
+ if (value.length % 2 !== 0) {
155
+ return value;
156
+ }
157
+
158
+ const result = {};
159
+
160
+ resolved[index] = result;
161
+
162
+ for (let i = 0; i < value.length; i += 2) {
163
+ result[_inflate(data, value[i], resolved)] = _inflate(data, value[i + 1], resolved);
164
+ }
165
+
166
+ return result;
167
+
168
+ };
169
+
170
+ const inflate = (data) => {
171
+
172
+ if (!Array.isArray(data)) {
173
+ return data;
174
+ }
175
+
176
+ if (data.length < 2) {
177
+ return data;
178
+ }
179
+
180
+ if (!Array.isArray(data[0])) {
181
+ return data;
182
+ }
183
+
184
+ return _inflate(data, 0, {});
185
+
186
+ };
187
+
188
+ export {
189
+ deflate,
190
+ inflate
191
+ };
192
+
193
+ export default {
194
+ deflate,
195
+ inflate
196
+ };
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@trenskow/json-compressor",
3
+ "version": "0.1.0",
4
+ "description": "A simple JSON to JSON compressor for JavaScript.",
5
+ "keywords": [
6
+ "json",
7
+ "compressor",
8
+ "serialization"
9
+ ],
10
+ "homepage": "https://github.com/trenskow/json-compressor#readme",
11
+ "bugs": {
12
+ "url": "https://github.com/trenskow/json-compressor/issues"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/trenskow/json-compressor.git"
17
+ },
18
+ "license": "BSD-2-Clause",
19
+ "author": "Kristian Trenskow <this.is@kristians.email>",
20
+ "type": "module",
21
+ "main": "index.js",
22
+ "scripts": {
23
+ "test": "node ./node_modules/mocha/bin/mocha.js ./test/index.js"
24
+ },
25
+ "devDependencies": {
26
+ "@eslint/eslintrc": "^3.3.3",
27
+ "@eslint/js": "^9.39.2",
28
+ "chai": "^6.2.1",
29
+ "eslint": "^9.39.2",
30
+ "globals": "^16.5.0",
31
+ "mocha": "^11.7.5"
32
+ },
33
+ "dependencies": {
34
+ "@trenskow/equals": "^0.1.2"
35
+ }
36
+ }
package/test/index.js ADDED
@@ -0,0 +1,89 @@
1
+ //
2
+ // test.js
3
+ // @trenskow/json-compressor
4
+ //
5
+ // Created by Kristian Trenskow on 2025/12/19
6
+ // For license see LICENSE.
7
+ //
8
+
9
+ import { expect } from 'chai';
10
+
11
+ import { deflate, inflate } from '../lib/index.js';
12
+
13
+ describe('@trenskow/json-compressor', () => {
14
+
15
+ describe('deflate()', () => {
16
+
17
+ it ('should come back with `[-1]`.', () => {
18
+ expect(deflate(undefined)).to.eql(undefined);
19
+ });
20
+
21
+ it ('should come back with `[-2]`.', () => {
22
+ expect(deflate(null)).to.eql(null);
23
+ });
24
+
25
+ it ('should come back with `[-3]`.', () => {
26
+ expect(deflate(true)).to.eql(true);
27
+ });
28
+
29
+ it ('should come back with `[-4]`.', () => {
30
+ expect(deflate(false)).to.eql(false);
31
+ });
32
+
33
+ it ('should come back with `[[\'o\', 1, 2, 2, 1, 3, -2], \'hello\', \'world\', \'none\']`.', () => {
34
+ expect(deflate({ hello: 'world', world: 'hello', none: null })).to.eql([['o', 1, 2, 2, 1, 3, -2], 'hello', 'world', 'none']);
35
+ });
36
+
37
+ it ('should come back with `[[\'a\', 1, 2, 2, -2], \'hello\', \'world\']`.', () => {
38
+ expect(deflate(['hello', 'world', 'world', null])).to.eql([['a', 1, 2, 2, -2], 'hello', 'world']);
39
+ });
40
+
41
+ it ('should come back with circular reference.', () => {
42
+
43
+ const arr = ['hello'];
44
+ arr.push(arr);
45
+
46
+ expect(deflate(arr)).to.eql([['a', 1, '"0"'], 'hello']);
47
+
48
+ });
49
+
50
+ });
51
+
52
+ describe('inflate()', () => {
53
+
54
+ it ('should come back with `undefined`.', () => {
55
+ expect(inflate(undefined)).to.eql(undefined);
56
+ });
57
+
58
+ it ('should come back with `null`.', () => {
59
+ expect(inflate(null)).to.eql(null);
60
+ });
61
+
62
+ it ('should come back with `true`.', () => {
63
+ expect(inflate(true)).to.eql(true);
64
+ });
65
+
66
+ it ('should come back with `false`.', () => {
67
+ expect(inflate(false)).to.eql(false);
68
+ });
69
+
70
+ it ('should come back with `{ hello: \'world\', world: \'hello\', none: null }`.', () => {
71
+ expect(inflate([['o', 1, 2, 2, 1, 3, -2], 'hello', 'world', 'none'])).to.eql({ hello: 'world', world: 'hello', none: null });
72
+ });
73
+
74
+ it ('should come back with `[\'hello\', \'world\', \'world\', null]`.', () => {
75
+ expect(inflate([['a', 1, 2, 2, -2], 'hello', 'world'])).to.eql(['hello', 'world', 'world', null]);
76
+ });
77
+
78
+ it ('should come back with circular reference.', () => {
79
+
80
+ const inflated = inflate([['a', 1, '"0"'], 'hello']);
81
+
82
+ expect(inflated[0]).to.eql('hello');
83
+ expect(inflated[1]).to.eql(inflated);
84
+
85
+ });
86
+
87
+ });
88
+
89
+ });