xw-devtool-cli 1.0.13 → 1.0.14

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 CHANGED
@@ -72,15 +72,16 @@ xw-devtool
72
72
  7. Unicode Encode/Decode
73
73
  8. HTML Entity Encode/Decode
74
74
  9. Variable Format Converter
75
- a. Chinese to Pinyin
76
- b. Time Format / Timestamp
77
- c. Time Calculation (Diff/Offset)
78
- d. Color Converter (Hex <-> RGB)
79
- e. Get UUID
80
- f. Hash Calculator (MD5/SHA/SM3)
81
- g. Mock Text
82
- h. Special Characters (Symbols)
83
- i. Emoji Picker
75
+ a. JSON Format (Minify/Prettify)
76
+ b. Chinese to Pinyin
77
+ c. Time Format / Timestamp
78
+ d. Time Calculation (Diff/Offset)
79
+ e. Color Converter (Hex <-> RGB)
80
+ f. Get UUID
81
+ g. Hash Calculator (MD5/SHA/SM3)
82
+ h. Mock Text
83
+ i. Special Characters (Symbols)
84
+ j. Emoji Picker
84
85
  0. Exit
85
86
  =================================
86
87
  ```
@@ -165,50 +166,68 @@ i. Emoji Picker
165
166
  - 自动展示 CamelCase, PascalCase, SnakeCase, KebabCase, ConstantCase 五种格式。
166
167
  - 选择一种格式自动复制。
167
168
 
168
- ### 10. 中文转拼音
169
+ ### 10. JSON 格式化
169
170
  - 选择 `a` 进入。
171
+ - 支持三种模式:
172
+ - **Minify**: 压缩 JSON (去除空格和换行)。
173
+ - **Prettify (2 spaces)**: 美化 JSON (2 空格缩进)。
174
+ - **Prettify (4 spaces)**: 美化 JSON (4 空格缩进)。
175
+ - 支持自动修复简单的非标准 JSON (如 key 缺少引号的 JS 对象)。
176
+ - 支持从剪贴板、文件或手动输入读取。
177
+ - **注意**:为防止长文本刷屏,结果**仅自动复制到剪贴板**,不会在终端打印。
178
+
179
+ ### 11. 中文转拼音
180
+ - 选择 `b` 进入。
170
181
  - 输入中文字符串(支持直接回车读取剪贴板)。
171
182
  - 输出对应的拼音(无声调)。
172
183
 
173
- ### 11. 时间格式化 (Time Format)
174
- - 选择 `b` 进入。
184
+ ### 12. 时间格式化 (Time Format)
185
+ - 选择 `c` 进入。
175
186
  - 支持时间戳转日期字符串,或日期字符串转时间戳。
176
187
  - 直接回车可获取当前时间戳。
177
188
 
178
- ### 12. 时间计算 (Time Calculation)
179
- - 选择 `c` 进入。
189
+ ### 13. 时间计算 (Time Calculation)
190
+ - 选择 `d` 进入。
180
191
  - **Diff**: 计算两个日期之间的时间差 (天、小时、分、秒)。
192
+ - **操作步骤**:
193
+ 1. 输入 **开始日期** (支持 `YYYY-MM-DD HH:mm:ss` 等多种格式,默认为当前时间)。
194
+ 2. 输入 **结束日期** (默认为当前时间)。
195
+ 3. 查看时间差结果 (显示总天数、总小时数及详细的 `x天x小时x分x秒`)。
181
196
  - **Offset**: 计算指定偏移量后的日期。
182
- - 支持单位:Years, Months, Weeks, Days, Hours, Minutes, Seconds。
183
- - 支持操作:Add (+), Subtract (-)。
197
+ - **操作步骤**:
198
+ 1. 输入 **基准日期** (默认为当前时间)。
199
+ 2. 选择 **时间单位** (Days, Hours, Minutes, Seconds, etc.)。
200
+ 3. 选择 **操作类型** (Add / Subtract)。
201
+ 4. 输入 **数量** (Amount)。
202
+ 5. 查看计算后的新日期。
184
203
  - 示例:计算 "当前时间 3 天后" 或 "2025-01-01 5 小时前"。
185
204
 
186
- ### 13. 颜色转换 (Hex <-> RGB)
187
- - 选择 `d` 进入。
205
+ ### 14. 颜色转换 (Hex <-> RGB)
206
+ - 选择 `e` 进入。
188
207
  - 输入 Hex 颜色值 (如 #333) 或 RGB 值 (如 rgb(51,51,51))。
189
208
  - 自动转换并显示对应格式。
190
209
 
191
- ### 14. Get UUID
192
- - 选择 `e` 进入。
210
+ ### 15. Get UUID
211
+ - 选择 `f` 进入。
193
212
  - 自动生成一个 UUID v4 并复制到剪贴板。
194
213
 
195
- ### 15. 哈希计算 (Encryption)
196
- - 选择 `f` 进入。
214
+ ### 16. 哈希计算 (Encryption)
215
+ - 选择 `g` 进入。
197
216
  - 输入文本(支持直接回车读取剪贴板)。
198
217
  - 同时计算并显示 MD5, SHA1, SHA256, SHA512, SM3 哈希值。
199
218
 
200
- ### 16. Mock 数据生成
201
- - 选择 `g` 进入。
219
+ ### 17. Mock 数据生成
220
+ - 选择 `h` 进入。
202
221
  - 提供多种数据类型:英文段落、中文字符、身份证号、邮箱、手机号等。
203
222
  - 支持指定生成数量。
204
223
 
205
- ### 17. 特殊符号大全
206
- - 选择 `h` 进入。
224
+ ### 18. 特殊符号大全
225
+ - 选择 `i` 进入。
207
226
  - 网格化展示常用特殊符号。
208
227
  - 输入符号对应的编号即可一键复制。
209
228
 
210
- ### 18. Emoji 输入
211
- - 选择 `i` 进入。
229
+ ### 19. Emoji 输入
230
+ - 选择 `j` 进入。
212
231
  - 分类展示常用 Emoji (表情、手势、动物、食物等)。
213
232
  - 输入编号选择并复制 Emoji 到剪贴板。
214
233
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xw-devtool-cli",
3
- "version": "1.0.13",
3
+ "version": "1.0.14",
4
4
  "type": "module",
5
5
  "description": "基于node的开发者助手cli",
6
6
  "main": "index.js",
@@ -0,0 +1,83 @@
1
+ import inquirer from 'inquirer';
2
+ import { copy } from '../utils/clipboard.js';
3
+ import { selectFromMenu } from '../utils/menu.js';
4
+ import fs from 'fs';
5
+
6
+ export async function jsonFormatHandler() {
7
+ const mode = await selectFromMenu('JSON Format', [
8
+ { name: 'Minify (Compact)', value: 'minify' },
9
+ { name: 'Prettify (2 spaces)', value: 'prettify-2' },
10
+ { name: 'Prettify (4 spaces)', value: 'prettify-4' }
11
+ ]);
12
+
13
+ const inputSource = await selectFromMenu('Select Input Source', [
14
+ { name: 'Clipboard (Paste automatically)', value: 'clipboard' },
15
+ { name: 'Manual Input', value: 'manual' },
16
+ { name: 'File', value: 'file' }
17
+ ]);
18
+
19
+ let input = '';
20
+ if (inputSource === 'clipboard') {
21
+ const clipboardy = (await import('clipboardy')).default;
22
+ input = await clipboardy.read();
23
+ } else if (inputSource === 'file') {
24
+ const { filePath } = await inquirer.prompt([
25
+ {
26
+ type: 'input',
27
+ name: 'filePath',
28
+ message: 'Enter file path:'
29
+ }
30
+ ]);
31
+ try {
32
+ input = fs.readFileSync(filePath.replace(/"/g, ''), 'utf-8');
33
+ } catch (e) {
34
+ console.error('Error reading file:', e.message);
35
+ return;
36
+ }
37
+ } else {
38
+ const { manualInput } = await inquirer.prompt([
39
+ {
40
+ type: 'input',
41
+ name: 'manualInput',
42
+ message: 'Enter JSON string:'
43
+ }
44
+ ]);
45
+ input = manualInput;
46
+ }
47
+
48
+ try {
49
+ // Try to parse relaxed JSON (like JS objects) if possible, but standard JSON.parse is safer for strict JSON
50
+ // We can try to evaluate if it fails parsing, but that's risky.
51
+ // Let's stick to standard JSON parsing first.
52
+ let jsonObj;
53
+ try {
54
+ jsonObj = JSON.parse(input);
55
+ } catch (e) {
56
+ // If strict parse fails, try to be more lenient if it's a JS object string
57
+ // e.g. { a: 1 } instead of { "a": 1 }
58
+ // Using Function constructor is safer than eval but still risky.
59
+ // For a dev tool, it might be acceptable if warned.
60
+ // Let's try a simple regex fix for common cases (keys without quotes)
61
+ try {
62
+ // This is a very basic attempt to fix unquoted keys
63
+ const fixed = input.replace(/([{,]\s*)([a-zA-Z0-9_]+?)\s*:/g, '$1"$2":');
64
+ jsonObj = JSON.parse(fixed);
65
+ } catch (e2) {
66
+ throw new Error('Invalid JSON format');
67
+ }
68
+ }
69
+
70
+ let output = '';
71
+ if (mode === 'minify') {
72
+ output = JSON.stringify(jsonObj);
73
+ } else if (mode === 'prettify-2') {
74
+ output = JSON.stringify(jsonObj, null, 2);
75
+ } else {
76
+ output = JSON.stringify(jsonObj, null, 4);
77
+ }
78
+
79
+ await copy(output);
80
+ } catch (e) {
81
+ console.error('Error processing JSON:', e.message);
82
+ }
83
+ }
package/src/index.js CHANGED
@@ -12,6 +12,7 @@ import { uuidHandler } from './commands/uuid.js';
12
12
  import { pinyinHandler } from './commands/pinyin.js';
13
13
  import { colorHandler } from './commands/color.js';
14
14
  import { variableFormatHandler } from './commands/variableFormat.js';
15
+ import { jsonFormatHandler } from './commands/jsonFormat.js';
15
16
  import { hashingHandler } from './commands/hashing.js';
16
17
  import { qrcodeHandler } from './commands/qrcode.js';
17
18
  import { specialCharsHandler } from './commands/specialChars.js';
@@ -47,6 +48,7 @@ const features = [
47
48
  { name: 'Unicode Encode/Decode', value: 'unicode' },
48
49
  { name: 'HTML Entity Encode/Decode', value: 'htmlEntities' },
49
50
  { name: 'Variable Format Converter', value: 'variableFormat' },
51
+ { name: 'JSON Format (Minify/Prettify)', value: 'jsonFormat' },
50
52
  { name: 'Chinese to Pinyin', value: 'pinyin' },
51
53
 
52
54
  // Utils (Time, Color, UUID, Hash)
@@ -174,6 +176,9 @@ async function handleAction(action) {
174
176
  case 'variableFormat':
175
177
  await variableFormatHandler();
176
178
  break;
179
+ case 'jsonFormat':
180
+ await jsonFormatHandler();
181
+ break;
177
182
  case 'hashing':
178
183
  await hashingHandler();
179
184
  break;