devkits-yaml-json 1.0.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/README.md +131 -0
- package/index.js +202 -0
- package/package.json +47 -0
- package/test.js +60 -0
package/README.md
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# devkits-yaml-json
|
|
2
|
+
|
|
3
|
+
> Convert between YAML and JSON formats
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install -g devkits-yaml-json
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Or use without installation:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npx devkits-yaml-json [args]
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
# JSON to YAML
|
|
21
|
+
dk-yaml -j '{"name": "test", "value": 123}'
|
|
22
|
+
|
|
23
|
+
# YAML to JSON
|
|
24
|
+
dk-yaml -y "name: test"
|
|
25
|
+
|
|
26
|
+
# From stdin (auto-detects format)
|
|
27
|
+
echo '{"key": "value"}' | dk-yaml
|
|
28
|
+
echo "key: value" | dk-yaml
|
|
29
|
+
|
|
30
|
+
# Pretty print JSON
|
|
31
|
+
echo "key: value" | dk-yaml --pretty
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Output Examples
|
|
35
|
+
|
|
36
|
+
### JSON to YAML
|
|
37
|
+
```bash
|
|
38
|
+
$ dk-yaml -j '{"name": "test", "count": 5}'
|
|
39
|
+
|
|
40
|
+
name: "test"
|
|
41
|
+
count: 5
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### YAML to JSON
|
|
45
|
+
```bash
|
|
46
|
+
$ dk-yaml -y "name: test"
|
|
47
|
+
|
|
48
|
+
{"name":"test"}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Why @devkits/yaml-json?
|
|
52
|
+
|
|
53
|
+
- ✅ Fast - Instant conversion
|
|
54
|
+
- ✅ Zero dependencies - Pure Node.js
|
|
55
|
+
- ✅ Offline - Works without internet
|
|
56
|
+
- ✅ Free - Open source (MIT)
|
|
57
|
+
- ✅ CLI - Use in your terminal
|
|
58
|
+
- ✅ Auto-detect from stdin
|
|
59
|
+
|
|
60
|
+
## Web Version
|
|
61
|
+
|
|
62
|
+
Prefer a GUI? Check out the web version: **https://devkits-tools.surge.sh/tools/yaml-json**
|
|
63
|
+
|
|
64
|
+
- 84 developer tools in one PWA
|
|
65
|
+
- Works offline
|
|
66
|
+
- No signup required
|
|
67
|
+
|
|
68
|
+
## Pro Features
|
|
69
|
+
|
|
70
|
+
Upgrade to Pro for advanced features:
|
|
71
|
+
|
|
72
|
+
- 📦 Batch Processing
|
|
73
|
+
- 🔗 API Access (1000 req/day free)
|
|
74
|
+
- ☁️ Cloud Sync
|
|
75
|
+
- 🎨 Custom Themes
|
|
76
|
+
- 📊 Advanced Analytics
|
|
77
|
+
- 💬 Priority Support
|
|
78
|
+
|
|
79
|
+
**Price:** $9 one-time payment
|
|
80
|
+
**Upgrade:** https://devkits-tools.surge.sh/pro
|
|
81
|
+
**Discount:** Use code `EARLYBIRD-2026` for 20% off
|
|
82
|
+
|
|
83
|
+
## See Also
|
|
84
|
+
|
|
85
|
+
Part of the **[DevKits Tools](https://devkits-tools.surge.sh)** collection — 80+ free developer tools:
|
|
86
|
+
|
|
87
|
+
### Popular Tools
|
|
88
|
+
|
|
89
|
+
| Tool | npm Package | Description |
|
|
90
|
+
|------|-------------|-------------|
|
|
91
|
+
| **[Base64](https://devkits-tools.surge.sh/tools/base64)** | `@devkits/base64` | Encode/decode Base64 |
|
|
92
|
+
| **[JSON Formatter](https://devkits-tools.surge.sh/tools/json-formatter)** | `@devkits/json-formatter` | Format and validate JSON |
|
|
93
|
+
| **[Color Converter](https://devkits-tools.surge.sh/tools/color-converter)** | `@devkits/color-converter` | HEX/RGB/HSL conversion |
|
|
94
|
+
| **[UUID Generator](https://devkits-tools.surge.sh/tools/uuid-generator)** | `@devkits/uuid-generator` | Generate unique UUIDs |
|
|
95
|
+
| **[Hash Generator](https://devkits-tools.surge.sh/tools/hash-generator)** | `@devkits/hash-generator` | MD5, SHA1, SHA256, SHA512 |
|
|
96
|
+
| **[Regex Tester](https://devkits-tools.surge.sh/tools/regex-tester)** | `@devkits/regex-tester` | Test regex patterns |
|
|
97
|
+
|
|
98
|
+
### Other DevKits Tools
|
|
99
|
+
|
|
100
|
+
- **[HTML Tools](https://devkits-tools.surge.sh/tools/html-entities)** — HTML entity encode/decode
|
|
101
|
+
- **[CSS Tools](https://devkits-tools.surge.sh/tools/css-minifier)** — CSS minify/format
|
|
102
|
+
- **[Cron Parser](https://devkits-tools.surge.sh/tools/cron-parser)** — Parse cron expressions
|
|
103
|
+
- **[Case Convert](https://devkits-tools.surge.sh/tools/text-case)** — camelCase, snake_case, etc.
|
|
104
|
+
- **[Slugify](https://devkits-tools.surge.sh/tools/slug-generator)** — Create URL-friendly slugs
|
|
105
|
+
- **[Lorem Ipsum](https://devkits-tools.surge.sh/tools/lorem-ipsum)** — Generate placeholder text
|
|
106
|
+
- **[Password Generator](https://devkits-tools.surge.sh/tools/password-generator)** — Secure passwords
|
|
107
|
+
- **[Text Counter](https://devkits-tools.surge.sh/tools/text-counter)** — Word/char counter
|
|
108
|
+
|
|
109
|
+
---
|
|
110
|
+
|
|
111
|
+
### More from DevKits
|
|
112
|
+
|
|
113
|
+
- **[Invoicely](https://invoicely-app.surge.sh)** — Free invoice generator for freelancers
|
|
114
|
+
- **[SnapOG](https://snapog.surge.sh)** — Free OG image generator with 20+ templates
|
|
115
|
+
- **[API Monitor](https://api-monitor-saas.surge.sh)** — Real-time API monitoring
|
|
116
|
+
|
|
117
|
+
---
|
|
118
|
+
|
|
119
|
+
👉 **Explore all 80+ developer tools at [DevKits Tools](https://devkits-tools.surge.sh)**
|
|
120
|
+
|
|
121
|
+
## License
|
|
122
|
+
|
|
123
|
+
MIT © [DevKits Team](https://devkits-tools.surge.sh)
|
|
124
|
+
|
|
125
|
+
---
|
|
126
|
+
|
|
127
|
+
## 🚀 API Monitoring for Developers
|
|
128
|
+
|
|
129
|
+
Build better APIs with **API Monitor SaaS** — real-time monitoring, alerting, and analytics.
|
|
130
|
+
|
|
131
|
+
👉 **Early Access: $1 pre-order (50% off for life)** → https://api-monitor-saas.surge.sh?utm_source=npm&utm_medium=readme&utm_campaign=phase0
|
package/index.js
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @devkits/yaml-json - Convert between YAML and JSON formats
|
|
5
|
+
*
|
|
6
|
+
* Usage:
|
|
7
|
+
* dk-yaml -j '{"key": "value"}' # JSON to YAML
|
|
8
|
+
* dk-yaml -y "key: value" # YAML to JSON
|
|
9
|
+
* cat file.yaml | dk-yaml -j # File conversion
|
|
10
|
+
*
|
|
11
|
+
* Web version: https://devkits-tools.surge.sh/tools/yaml-json
|
|
12
|
+
* Pro features: https://devkits-tools.surge.sh/pro
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
// Simple YAML parser (handles basic YAML)
|
|
16
|
+
function parseYaml(yaml) {
|
|
17
|
+
const result = {};
|
|
18
|
+
const lines = yaml.split('\n');
|
|
19
|
+
let currentKey = null;
|
|
20
|
+
let currentObj = result;
|
|
21
|
+
const indentStack = [{ obj: result, indent: -1 }];
|
|
22
|
+
|
|
23
|
+
for (const line of lines) {
|
|
24
|
+
// Skip empty lines and comments
|
|
25
|
+
if (!line.trim() || line.trim().startsWith('#')) continue;
|
|
26
|
+
|
|
27
|
+
const match = line.match(/^(\s*)(-?)\s*([^:]+):\s*(.*)$/);
|
|
28
|
+
if (match) {
|
|
29
|
+
const [, indent, isArray, key, value] = match;
|
|
30
|
+
const indentLevel = indent.length;
|
|
31
|
+
const cleanKey = key.trim().replace(/^- /, '');
|
|
32
|
+
let cleanValue = value.trim();
|
|
33
|
+
|
|
34
|
+
// Remove quotes from value
|
|
35
|
+
if ((cleanValue.startsWith('"') && cleanValue.endsWith('"')) ||
|
|
36
|
+
(cleanValue.startsWith("'") && cleanValue.endsWith("'"))) {
|
|
37
|
+
cleanValue = cleanValue.slice(1, -1);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Find the right parent based on indentation
|
|
41
|
+
while (indentStack.length > 1 && indentStack[indentStack.length - 1].indent >= indentLevel) {
|
|
42
|
+
indentStack.pop();
|
|
43
|
+
}
|
|
44
|
+
currentObj = indentStack[indentStack.length - 1].obj;
|
|
45
|
+
|
|
46
|
+
// Handle array items
|
|
47
|
+
if (isArray) {
|
|
48
|
+
if (!Array.isArray(currentObj)) {
|
|
49
|
+
// This is a simplified handler
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Parse value type
|
|
54
|
+
if (cleanValue === '' || cleanValue === 'null' || cleanValue === '~') {
|
|
55
|
+
currentObj[cleanKey] = null;
|
|
56
|
+
} else if (cleanValue === 'true') {
|
|
57
|
+
currentObj[cleanKey] = true;
|
|
58
|
+
} else if (cleanValue === 'false') {
|
|
59
|
+
currentObj[cleanKey] = false;
|
|
60
|
+
} else if (/^-?\d+$/.test(cleanValue)) {
|
|
61
|
+
currentObj[cleanKey] = parseInt(cleanValue, 10);
|
|
62
|
+
} else if (/^-?\d+\.\d+$/.test(cleanValue)) {
|
|
63
|
+
currentObj[cleanKey] = parseFloat(cleanValue);
|
|
64
|
+
} else {
|
|
65
|
+
currentObj[cleanKey] = cleanValue;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
indentStack.push({ obj: currentObj[cleanKey], indent: indentLevel });
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return result;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Simple JSON to YAML converter
|
|
76
|
+
function jsonToYaml(obj, indent = 0) {
|
|
77
|
+
let result = '';
|
|
78
|
+
const prefix = ' '.repeat(indent);
|
|
79
|
+
|
|
80
|
+
if (typeof obj !== 'object' || obj === null) {
|
|
81
|
+
return String(obj);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (Array.isArray(obj)) {
|
|
85
|
+
for (const item of obj) {
|
|
86
|
+
if (typeof item === 'object' && item !== null) {
|
|
87
|
+
const firstKey = Object.keys(item)[0];
|
|
88
|
+
result += `${prefix}- ${firstKey}: ${jsonToYaml(item[firstKey], indent + 1)}\n`;
|
|
89
|
+
} else {
|
|
90
|
+
result += `${prefix}- ${item}\n`;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
} else {
|
|
94
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
95
|
+
if (typeof value === 'object' && value !== null) {
|
|
96
|
+
result += `${prefix}${key}:\n${jsonToYaml(value, indent + 1)}`;
|
|
97
|
+
} else {
|
|
98
|
+
const str = typeof value === 'string' ? `"${value}"` : value;
|
|
99
|
+
result += `${prefix}${key}: ${str}\n`;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return result;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function showHelp() {
|
|
108
|
+
console.log(`
|
|
109
|
+
@devkits/yaml-json - Convert between YAML and JSON
|
|
110
|
+
|
|
111
|
+
Usage:
|
|
112
|
+
dk-yaml -j '{"key": "value"}' # JSON to YAML
|
|
113
|
+
dk-yaml -y "key: value" # YAML to JSON
|
|
114
|
+
cat file.yaml | dk-yaml -j # YAML file to JSON
|
|
115
|
+
cat file.json | dk-yaml -y # JSON file to YAML
|
|
116
|
+
|
|
117
|
+
Options:
|
|
118
|
+
-j, --to-json <str> Convert JSON string to YAML
|
|
119
|
+
-y, --to-yaml <str> Convert YAML string to JSON
|
|
120
|
+
--from-file <file> Read from file (auto-detect format)
|
|
121
|
+
-p, --pretty Pretty print JSON output
|
|
122
|
+
-h, --help Show this help message
|
|
123
|
+
|
|
124
|
+
Examples:
|
|
125
|
+
dk-yaml -j '{"name": "test"}'
|
|
126
|
+
echo '{"name": "test"}' | dk-yaml -j
|
|
127
|
+
dk-yaml -y "name: test"
|
|
128
|
+
echo "name: test" | dk-yaml -y
|
|
129
|
+
|
|
130
|
+
Web version: https://devkits-tools.surge.sh/tools/yaml-json
|
|
131
|
+
Pro features: https://devkits-tools.surge.sh/pro
|
|
132
|
+
`);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Main entry point
|
|
136
|
+
const args = process.argv.slice(2);
|
|
137
|
+
|
|
138
|
+
if (!args.length || args.includes('-h') || args.includes('--help')) {
|
|
139
|
+
showHelp();
|
|
140
|
+
process.exit(0);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Parse options
|
|
144
|
+
let toJson = null;
|
|
145
|
+
let toYaml = null;
|
|
146
|
+
let pretty = false;
|
|
147
|
+
|
|
148
|
+
for (let i = 0; i < args.length; i++) {
|
|
149
|
+
if (args[i] === '-j' || args[i] === '--to-json') {
|
|
150
|
+
toJson = args[++i];
|
|
151
|
+
} else if (args[i] === '-y' || args[i] === '--to-yaml') {
|
|
152
|
+
toYaml = args[++i];
|
|
153
|
+
} else if (args[i] === '-p' || args[i] === '--pretty') {
|
|
154
|
+
pretty = true;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Read from stdin if no input provided
|
|
159
|
+
const processInput = (input, isJson) => {
|
|
160
|
+
try {
|
|
161
|
+
if (isJson) {
|
|
162
|
+
// JSON to YAML
|
|
163
|
+
const obj = JSON.parse(input);
|
|
164
|
+
console.log(jsonToYaml(obj).trim());
|
|
165
|
+
} else {
|
|
166
|
+
// YAML to JSON
|
|
167
|
+
const obj = parseYaml(input);
|
|
168
|
+
console.log(pretty ? JSON.stringify(obj, null, 2) : JSON.stringify(obj));
|
|
169
|
+
}
|
|
170
|
+
} catch (err) {
|
|
171
|
+
console.error(`Error: ${err.message}`);
|
|
172
|
+
process.exit(1);
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
if (toJson !== null) {
|
|
177
|
+
processInput(toJson, true);
|
|
178
|
+
} else if (toYaml !== null) {
|
|
179
|
+
processInput(toYaml, false);
|
|
180
|
+
} else if (!process.stdin.isTTY) {
|
|
181
|
+
let stdin = '';
|
|
182
|
+
process.stdin.setEncoding('utf8');
|
|
183
|
+
process.stdin.on('readable', () => {
|
|
184
|
+
let chunk;
|
|
185
|
+
while ((chunk = process.stdin.read()) !== null) {
|
|
186
|
+
stdin += chunk;
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
process.stdin.on('end', () => {
|
|
190
|
+
if (stdin.trim()) {
|
|
191
|
+
// Auto-detect format
|
|
192
|
+
const isJson = stdin.trim().startsWith('{') || stdin.trim().startsWith('[');
|
|
193
|
+
processInput(stdin, isJson);
|
|
194
|
+
} else {
|
|
195
|
+
showHelp();
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
} else {
|
|
199
|
+
console.error('Error: Please provide input with -j or -y flag, or pipe from stdin');
|
|
200
|
+
showHelp();
|
|
201
|
+
process.exit(1);
|
|
202
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "devkits-yaml-json",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Convert between YAML and JSON formats",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"dk-yaml": "./index.js",
|
|
8
|
+
"yaml-json": "./index.js"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"test": "node test.js"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"yaml",
|
|
15
|
+
"json",
|
|
16
|
+
"convert",
|
|
17
|
+
"parser",
|
|
18
|
+
"devkits",
|
|
19
|
+
"developer-tools",
|
|
20
|
+
"cli",
|
|
21
|
+
"command-line",
|
|
22
|
+
"nodejs",
|
|
23
|
+
"utility",
|
|
24
|
+
"productivity",
|
|
25
|
+
"dev-tools",
|
|
26
|
+
"free-tools"
|
|
27
|
+
],
|
|
28
|
+
"author": "DevKits Team <devkits-auto@protonmail.com>",
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"repository": {
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "https://github.com/devkits/tools"
|
|
33
|
+
},
|
|
34
|
+
"homepage": "https://devkits-tools.surge.sh/tools/yaml-json",
|
|
35
|
+
"bugs": {
|
|
36
|
+
"url": "https://github.com/devkits/tools/issues"
|
|
37
|
+
},
|
|
38
|
+
"engines": {
|
|
39
|
+
"node": ">=14.0.0"
|
|
40
|
+
},
|
|
41
|
+
"files": [
|
|
42
|
+
"index.js",
|
|
43
|
+
"README.md",
|
|
44
|
+
"test.js"
|
|
45
|
+
],
|
|
46
|
+
"dependencies": {}
|
|
47
|
+
}
|
package/test.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Tests for @devkits/yaml-json
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const { execSync } = require('child_process');
|
|
8
|
+
|
|
9
|
+
const tests = [
|
|
10
|
+
{
|
|
11
|
+
name: 'JSON to YAML',
|
|
12
|
+
cmd: 'node index.js -j \'{"name": "test"}\'',
|
|
13
|
+
check: (result) => result.includes('name:') && result.includes('test')
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
name: 'YAML to JSON',
|
|
17
|
+
cmd: 'node index.js -y "name: test"',
|
|
18
|
+
check: (result) => result.includes('name') && result.includes('test')
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
name: 'JSON with number to YAML',
|
|
22
|
+
cmd: 'node index.js -j \'{"count": 5}\'',
|
|
23
|
+
check: (result) => result.includes('count:') && result.includes('5')
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
name: 'YAML with number to JSON',
|
|
27
|
+
cmd: 'node index.js -y "count: 5"',
|
|
28
|
+
check: (result) => result.includes('count') && result.includes('5')
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
name: 'Pretty JSON output',
|
|
32
|
+
cmd: 'node index.js -y "name: test" --pretty',
|
|
33
|
+
check: (result) => result.includes('{\n') || result.split('\n').length > 1
|
|
34
|
+
},
|
|
35
|
+
];
|
|
36
|
+
|
|
37
|
+
let passed = 0;
|
|
38
|
+
let failed = 0;
|
|
39
|
+
|
|
40
|
+
console.log('Running @devkits/yaml-json tests...\n');
|
|
41
|
+
|
|
42
|
+
for (const test of tests) {
|
|
43
|
+
try {
|
|
44
|
+
const result = execSync(test.cmd, { encoding: 'utf8' }).trim();
|
|
45
|
+
if (test.check(result)) {
|
|
46
|
+
console.log(`✅ ${test.name}`);
|
|
47
|
+
passed++;
|
|
48
|
+
} else {
|
|
49
|
+
console.log(`❌ ${test.name}`);
|
|
50
|
+
console.log(` Got: ${result}`);
|
|
51
|
+
failed++;
|
|
52
|
+
}
|
|
53
|
+
} catch (err) {
|
|
54
|
+
console.log(`❌ ${test.name} - Error: ${err.message}`);
|
|
55
|
+
failed++;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
console.log(`\n${passed}/${tests.length} tests passed`);
|
|
60
|
+
process.exit(failed > 0 ? 1 : 0);
|