Skip to content

Commit fd96b6a

Browse files
committed
feat: add a JSON-to-JavaScript converter
1 parent 1b4201d commit fd96b6a

File tree

5 files changed

+67
-0
lines changed

5 files changed

+67
-0
lines changed

locales/en/lib-converters-commands.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"javaScript": "JavaScript format",
1010
"json": "JSON format",
1111
"jsonToCsv": "JSON to CSV",
12+
"jsonToJavaScript": "JSON to JavaScript",
1213
"jsonToYaml": "JSON to YAML",
1314
"jwt": "JWT",
1415
"less": "LESS format",

locales/en/lib-converters-results.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"javaScript": "Formatted JavaScript",
1010
"json": "Formatted JSON",
1111
"jsonToCsv": "Formatted CSV",
12+
"jsonToJavaScript": "Formatted JavaScript",
1213
"jsonToYaml": "Formatted YAML",
1314
"jwt": "Decoded JWT",
1415
"less": "Formatted LESS",
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { ConverterOptions } from '@lib/types'
2+
import { output as javaScriptOutput } from '@lib/outputs/JavaScriptOutput'
3+
import { input as jsonInput } from '@lib/inputs/JsonInput'
4+
5+
/**
6+
* A string which uniquely identifies this operation.
7+
*/
8+
export const id = 'jsonToJavaScript'
9+
10+
/**
11+
* A string which uniquely identifies the output component used by
12+
* this converter.
13+
*/
14+
export const outputId = 'javaScript'
15+
16+
/**
17+
* An operation that formats the given JSON input string as JavaScript.
18+
*
19+
* @param input - the string to convert.
20+
* @param options - options that control the conversion process.
21+
*
22+
* @returns the converted string.
23+
*/
24+
export const operation = (
25+
input: string,
26+
options: ConverterOptions = {},
27+
): string => {
28+
const obj = jsonInput(input)
29+
if (!obj) {
30+
return ''
31+
}
32+
33+
return javaScriptOutput(`const data = ${JSON.stringify(obj)}`, options)
34+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { JsonToJavaScriptConverter } from '@lib/converters'
2+
import { expectOutput } from './_helpers'
3+
4+
describe('converters', () => {
5+
describe('JsonToJavaScriptConverter', () => {
6+
it('converts JSON data to JavaScript', async () => {
7+
await expectOutput(
8+
JsonToJavaScriptConverter,
9+
`[
10+
{
11+
"a": "1",
12+
"b": "2",
13+
"c": "3"
14+
},
15+
{
16+
"a": "4",
17+
"b": "5",
18+
"c": "6"
19+
}
20+
]`,
21+
'javascript-output',
22+
`const data = [
23+
{ a: '1', b: '2', c: '3' },
24+
{ a: '4', b: '5', c: '6' },
25+
];
26+
`,
27+
)
28+
})
29+
})
30+
})

src/lib/converters/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export * as HtmlFormatter from '@lib/converters/HtmlFormatter'
1010
export * as JavaScriptFormatter from '@lib/converters/JavaScriptFormatter'
1111
export * as JsonFormatter from '@lib/converters/JsonFormatter'
1212
export * as JsonToCsvConverter from '@lib/converters/JsonToCsvConverter'
13+
export * as JsonToJavaScriptConverter from '@lib/converters/JsonToJavaScriptConverter'
1314
export * as JsonToYamlConverter from '@lib/converters/JsonToYamlConverter'
1415
export * as JwtDecoder from '@lib/converters/JwtDecoder'
1516
export * as LessFormatter from '@lib/converters/LessFormatter'

0 commit comments

Comments
 (0)