slimjson 1.0.4 → 1.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.
package/compress-file.js CHANGED
@@ -1,41 +1,41 @@
1
- /**
2
- * 用法: node compress-file.js <输入.json> [输出]
3
- *
4
- * 默认输出文件名: <输入名>.json.slim
5
- */
6
- const fs = require('fs');
7
- const path = require('path');
8
- const { compress, stringify } = require('./compress');
9
-
10
- const input = process.argv[2];
11
- if (!input) {
12
- console.error('用法: node compress-file.js <输入.json> [输出]');
13
- process.exit(1);
14
- }
15
- if (!fs.existsSync(input)) {
16
- console.error(`文件不存在: ${input}`);
17
- process.exit(1);
18
- }
19
-
20
- const output = (process.argv[3] || input).replace(/\.json$/i, '') + '.json.slim';
21
-
22
- let data;
23
- try {
24
- data = JSON.parse(fs.readFileSync(input, 'utf8'));
25
- } catch (e) {
26
- console.error(`JSON 解析失败: ${e.message}`);
27
- process.exit(1);
28
- }
29
-
30
- const compressed = compress(data);
31
- const text = stringify(compressed);
32
-
33
- fs.writeFileSync(output, text, 'utf8');
34
-
35
- const originalSize = Buffer.byteLength(JSON.stringify(data), 'utf8');
36
- const newSize = Buffer.byteLength(text, 'utf8');
37
- const ratio = ((originalSize - newSize) / originalSize * 100).toFixed(2);
38
-
39
- console.log(`输入: ${path.basename(input)} (${(originalSize / 1024).toFixed(2)} KB)`);
40
- console.log(`输出: ${path.basename(output)} (${(newSize / 1024).toFixed(2)} KB)`);
41
- console.log(`压缩率: ${ratio}%`);
1
+ /**
2
+ * 用法: node compress-file.js <输入.json> [输出]
3
+ *
4
+ * 默认输出文件名: <输入名>.json.slim
5
+ */
6
+ const fs = require('fs');
7
+ const path = require('path');
8
+ const { compress, stringify } = require('./compress');
9
+
10
+ const input = process.argv[2];
11
+ if (!input) {
12
+ console.error('用法: node compress-file.js <输入.json> [输出]');
13
+ process.exit(1);
14
+ }
15
+ if (!fs.existsSync(input)) {
16
+ console.error(`文件不存在: ${input}`);
17
+ process.exit(1);
18
+ }
19
+
20
+ const output = (process.argv[3] || input).replace(/\.json$/i, '') + '.json.slim';
21
+
22
+ let data;
23
+ try {
24
+ data = JSON.parse(fs.readFileSync(input, 'utf8'));
25
+ } catch (e) {
26
+ console.error(`JSON 解析失败: ${e.message}`);
27
+ process.exit(1);
28
+ }
29
+
30
+ const compressed = compress(data);
31
+ const text = stringify(compressed);
32
+
33
+ fs.writeFileSync(output, text, 'utf8');
34
+
35
+ const originalSize = Buffer.byteLength(JSON.stringify(data), 'utf8');
36
+ const newSize = Buffer.byteLength(text, 'utf8');
37
+ const ratio = ((originalSize - newSize) / originalSize * 100).toFixed(2);
38
+
39
+ console.log(`输入: ${path.basename(input)} (${(originalSize / 1024).toFixed(2)} KB)`);
40
+ console.log(`输出: ${path.basename(output)} (${(newSize / 1024).toFixed(2)} KB)`);
41
+ console.log(`压缩率: ${ratio}%`);
package/compress-ratio.js CHANGED
@@ -1,70 +1,70 @@
1
- /**
2
- * 用法: node compress-ratio.js <json文件路径>
3
- *
4
- * 读取 JSON 文件压缩,输出压缩率。
5
- */
6
- const fs = require('fs');
7
- const path = require('path');
8
- const { compress, stringify } = require('./compress');
9
-
10
- function getByteSize(obj) {
11
- return Buffer.byteLength(JSON.stringify(obj), 'utf8');
12
- }
13
-
14
- function formatBytes(bytes) {
15
- if (bytes < 1024) return `${bytes} B`;
16
- if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(2)} KB`;
17
- return `${(bytes / (1024 * 1024)).toFixed(2)} MB`;
18
- }
19
-
20
- // ---------- 参数解析 ----------
21
- const filePath = process.argv[2];
22
- if (!filePath) {
23
- console.error('用法: node compress-ratio.js <json文件>');
24
- process.exit(1);
25
- }
26
- if (!fs.existsSync(filePath)) {
27
- console.error(`文件不存在: ${filePath}`);
28
- process.exit(1);
29
- }
30
-
31
- // ---------- 读取并解析 ----------
32
- let raw;
33
- try {
34
- raw = fs.readFileSync(filePath, 'utf8');
35
- } catch (e) {
36
- console.error(`读取文件失败: ${e.message}`);
37
- process.exit(1);
38
- }
39
-
40
- let data;
41
- try {
42
- data = JSON.parse(raw);
43
- } catch (e) {
44
- console.error(`JSON 解析失败: ${e.message}`);
45
- process.exit(1);
46
- }
47
-
48
- const originalSize = Buffer.byteLength(JSON.stringify(data), 'utf8');
49
-
50
- // ---------- 压缩 ----------
51
- const compressed = compress(data);
52
- const compressedStr = stringify(compressed);
53
-
54
- const compressedSize = Buffer.byteLength(compressedStr, 'utf8');
55
-
56
- const savings = originalSize - compressedSize;
57
- const ratio = originalSize === 0 ? 0 : (savings / originalSize * 100);
58
-
59
- // ---------- 输出 ----------
60
- const fileName = path.basename(filePath);
61
-
62
- console.log(`\n文件: ${fileName}`);
63
- console.log(`原始大小: ${formatBytes(originalSize)}`);
64
- console.log(`压缩后: ${formatBytes(compressedSize)}`);
65
- console.log(`节省: ${formatBytes(savings)} (${ratio.toFixed(2)}%)`);
66
-
67
- // 如果是数组,额外输出元素数量
68
- if (Array.isArray(data)) {
69
- console.log(`元素数量: ${data.length}`);
70
- }
1
+ /**
2
+ * 用法: node compress-ratio.js <json文件路径>
3
+ *
4
+ * 读取 JSON 文件压缩,输出压缩率。
5
+ */
6
+ const fs = require('fs');
7
+ const path = require('path');
8
+ const { compress, stringify } = require('./compress');
9
+
10
+ function getByteSize(obj) {
11
+ return Buffer.byteLength(JSON.stringify(obj), 'utf8');
12
+ }
13
+
14
+ function formatBytes(bytes) {
15
+ if (bytes < 1024) return `${bytes} B`;
16
+ if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(2)} KB`;
17
+ return `${(bytes / (1024 * 1024)).toFixed(2)} MB`;
18
+ }
19
+
20
+ // ---------- 参数解析 ----------
21
+ const filePath = process.argv[2];
22
+ if (!filePath) {
23
+ console.error('用法: node compress-ratio.js <json文件>');
24
+ process.exit(1);
25
+ }
26
+ if (!fs.existsSync(filePath)) {
27
+ console.error(`文件不存在: ${filePath}`);
28
+ process.exit(1);
29
+ }
30
+
31
+ // ---------- 读取并解析 ----------
32
+ let raw;
33
+ try {
34
+ raw = fs.readFileSync(filePath, 'utf8');
35
+ } catch (e) {
36
+ console.error(`读取文件失败: ${e.message}`);
37
+ process.exit(1);
38
+ }
39
+
40
+ let data;
41
+ try {
42
+ data = JSON.parse(raw);
43
+ } catch (e) {
44
+ console.error(`JSON 解析失败: ${e.message}`);
45
+ process.exit(1);
46
+ }
47
+
48
+ const originalSize = Buffer.byteLength(JSON.stringify(data), 'utf8');
49
+
50
+ // ---------- 压缩 ----------
51
+ const compressed = compress(data);
52
+ const compressedStr = stringify(compressed);
53
+
54
+ const compressedSize = Buffer.byteLength(compressedStr, 'utf8');
55
+
56
+ const savings = originalSize - compressedSize;
57
+ const ratio = originalSize === 0 ? 0 : (savings / originalSize * 100);
58
+
59
+ // ---------- 输出 ----------
60
+ const fileName = path.basename(filePath);
61
+
62
+ console.log(`\n文件: ${fileName}`);
63
+ console.log(`原始大小: ${formatBytes(originalSize)}`);
64
+ console.log(`压缩后: ${formatBytes(compressedSize)}`);
65
+ console.log(`节省: ${formatBytes(savings)} (${ratio.toFixed(2)}%)`);
66
+
67
+ // 如果是数组,额外输出元素数量
68
+ if (Array.isArray(data)) {
69
+ console.log(`元素数量: ${data.length}`);
70
+ }