@trenskow/json-compressor 0.1.0 → 0.1.2
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.
- package/README.md +1 -2
- package/index.d.ts +10 -0
- package/lib/index.js +17 -17
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -20,10 +20,9 @@ myObject.circular = myObject;
|
|
|
20
20
|
|
|
21
21
|
const json = JSON.stringify(deflate(myObject));
|
|
22
22
|
|
|
23
|
-
inflate(JSON.parse(
|
|
23
|
+
inflate(JSON.parse(json)); // Returns copy of original instance (with circular reference preserved).
|
|
24
24
|
````
|
|
25
25
|
|
|
26
26
|
# License
|
|
27
27
|
|
|
28
28
|
See license in LICENSE.
|
|
29
|
-
|
package/index.d.ts
ADDED
package/lib/index.js
CHANGED
|
@@ -105,7 +105,7 @@ const deflate = (json) => {
|
|
|
105
105
|
|
|
106
106
|
};
|
|
107
107
|
|
|
108
|
-
const _inflate = (
|
|
108
|
+
const _inflate = (json, index, resolved) => {
|
|
109
109
|
|
|
110
110
|
if (index in indices) {
|
|
111
111
|
return indices[index];
|
|
@@ -115,19 +115,19 @@ const _inflate = (data, index, resolved) => {
|
|
|
115
115
|
return resolved[parseInt(index.slice(1, -1), 10)];
|
|
116
116
|
}
|
|
117
117
|
|
|
118
|
-
const value =
|
|
118
|
+
const value = json[index];
|
|
119
119
|
|
|
120
120
|
if (Array.isArray(value)) {
|
|
121
121
|
|
|
122
122
|
if (value[0] === 'a') {
|
|
123
|
-
return _inflate.array(
|
|
123
|
+
return _inflate.array(json, index, value.slice(1), resolved);
|
|
124
124
|
}
|
|
125
125
|
|
|
126
126
|
if (value[0] === 'o') {
|
|
127
|
-
return _inflate.object(
|
|
127
|
+
return _inflate.object(json, index, value.slice(1), resolved);
|
|
128
128
|
}
|
|
129
129
|
|
|
130
|
-
return
|
|
130
|
+
return json;
|
|
131
131
|
|
|
132
132
|
}
|
|
133
133
|
|
|
@@ -135,21 +135,21 @@ const _inflate = (data, index, resolved) => {
|
|
|
135
135
|
|
|
136
136
|
};
|
|
137
137
|
|
|
138
|
-
_inflate.array = (
|
|
138
|
+
_inflate.array = (json, index, value, resolved) => {
|
|
139
139
|
|
|
140
140
|
const result = [];
|
|
141
141
|
|
|
142
142
|
resolved[index] = result;
|
|
143
143
|
|
|
144
144
|
for (let i = 0; i < value.length; i++) {
|
|
145
|
-
result.push(_inflate(
|
|
145
|
+
result.push(_inflate(json, value[i], resolved));
|
|
146
146
|
}
|
|
147
147
|
|
|
148
148
|
return result;
|
|
149
149
|
|
|
150
150
|
};
|
|
151
151
|
|
|
152
|
-
_inflate.object = (
|
|
152
|
+
_inflate.object = (json, index, value, resolved) => {
|
|
153
153
|
|
|
154
154
|
if (value.length % 2 !== 0) {
|
|
155
155
|
return value;
|
|
@@ -160,28 +160,28 @@ _inflate.object = (data, index, value, resolved) => {
|
|
|
160
160
|
resolved[index] = result;
|
|
161
161
|
|
|
162
162
|
for (let i = 0; i < value.length; i += 2) {
|
|
163
|
-
result[_inflate(
|
|
163
|
+
result[_inflate(json, value[i], resolved)] = _inflate(json, value[i + 1], resolved);
|
|
164
164
|
}
|
|
165
165
|
|
|
166
166
|
return result;
|
|
167
167
|
|
|
168
168
|
};
|
|
169
169
|
|
|
170
|
-
const inflate = (
|
|
170
|
+
const inflate = (json) => {
|
|
171
171
|
|
|
172
|
-
if (!Array.isArray(
|
|
173
|
-
return
|
|
172
|
+
if (!Array.isArray(json)) {
|
|
173
|
+
return json;
|
|
174
174
|
}
|
|
175
175
|
|
|
176
|
-
if (
|
|
177
|
-
return
|
|
176
|
+
if (json.length < 2) {
|
|
177
|
+
return json;
|
|
178
178
|
}
|
|
179
179
|
|
|
180
|
-
if (!Array.isArray(
|
|
181
|
-
return
|
|
180
|
+
if (!Array.isArray(json[0])) {
|
|
181
|
+
return json;
|
|
182
182
|
}
|
|
183
183
|
|
|
184
|
-
return _inflate(
|
|
184
|
+
return _inflate(json, 0, {});
|
|
185
185
|
|
|
186
186
|
};
|
|
187
187
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trenskow/json-compressor",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "A simple JSON to JSON compressor for JavaScript.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"json",
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
"author": "Kristian Trenskow <this.is@kristians.email>",
|
|
20
20
|
"type": "module",
|
|
21
21
|
"main": "index.js",
|
|
22
|
+
"types": "index.d.ts",
|
|
22
23
|
"scripts": {
|
|
23
24
|
"test": "node ./node_modules/mocha/bin/mocha.js ./test/index.js"
|
|
24
25
|
},
|