From 0ba0303ed092a813532d34140276d9b7f568faa5 Mon Sep 17 00:00:00 2001 From: Benoit Durand Date: Mon, 22 Apr 2024 22:58:09 +0200 Subject: [PATCH 1/3] Update json-format extension - Add format to JSONLines - Initial commit --- extensions/json-format/package.json | 9 ++- .../json-format/src/formatJsonLines.tsx | 57 +++++++++++++++++++ extensions/json-format/src/utils.ts | 18 ++++++ 3 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 extensions/json-format/src/formatJsonLines.tsx diff --git a/extensions/json-format/package.json b/extensions/json-format/package.json index 5314fcfd52b8..4094af5813ed 100644 --- a/extensions/json-format/package.json +++ b/extensions/json-format/package.json @@ -9,7 +9,8 @@ "Aayush9029", "devlos", "lachero", - "alexs" + "alexs", + "duranbe" ], "categories": [ "Data", @@ -28,6 +29,12 @@ "title": "Format JSON", "description": "Formats a JSON/JS Object", "mode": "view" + }, + { + "name": "formatJsonLines", + "title": "Format Array of JSON to JSONLines", + "description": "Formats an array of JSON/JS Object to JSONLines", + "mode": "view" } ], "preferences": [ diff --git a/extensions/json-format/src/formatJsonLines.tsx b/extensions/json-format/src/formatJsonLines.tsx new file mode 100644 index 000000000000..24c19f64adf6 --- /dev/null +++ b/extensions/json-format/src/formatJsonLines.tsx @@ -0,0 +1,57 @@ +import { Form, ActionPanel, Action } from "@raycast/api"; +import { useState } from "react"; + +import { formatToJSONLines } from './utils'; + +interface FormInput { + input: string; + result: string; +} + +export default function Command() { + const [input, setInput] = useState(''); + const [result, setResult] = useState(''); + + return ( +
+ { + let input: string = values.input; + const output = formatToJSONLines(input); + if (output) { + setResult(output); + } + }} + /> + { + let input: string = values.input; + const output = formatToJSONLines(input); + if (output) { + setResult(output); + } + }} + /> + + } + > + + + + ); +} \ No newline at end of file diff --git a/extensions/json-format/src/utils.ts b/extensions/json-format/src/utils.ts index ee796f79b840..87945459c3b3 100644 --- a/extensions/json-format/src/utils.ts +++ b/extensions/json-format/src/utils.ts @@ -90,6 +90,20 @@ function parse(input: string) { } } +export function formatToJSONLines(input: string) { + if (!isJson(input) || !isArray(input)) { + showToast({ + style: Toast.Style.Failure, + title: 'Please enter a valid JSON/JS Array Object', + }); + return; + } + + let jsonVal = JSON.parse(`{"data":${input}}`); + return jsonVal.data.map(JSON.stringify).join('\n'); +} + + function isJson(str: string) { try { JSON.parse(str); @@ -98,3 +112,7 @@ function isJson(str: string) { } return true; } + +function isArray(str: string) { + return str.startsWith('[') && str.endsWith(']'); +} From 7a6b20e5143b7bccd9172f6f8258ffc33bdcc35e Mon Sep 17 00:00:00 2001 From: Benoit Durand Date: Mon, 22 Apr 2024 23:10:31 +0200 Subject: [PATCH 2/3] Fix ESLint and update CHANGELOG --- extensions/json-format/CHANGELOG.md | 4 ++++ extensions/json-format/src/formatJsonLines.tsx | 4 ++-- extensions/json-format/src/utils.ts | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/extensions/json-format/CHANGELOG.md b/extensions/json-format/CHANGELOG.md index 7bb4f207cc59..2c75d527f963 100644 --- a/extensions/json-format/CHANGELOG.md +++ b/extensions/json-format/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## [Adds support for JSONLines formatting] - 2024-04-22 + +- Add formatting from JSON/JS Object Array to JSONLines + ## [Fix json () bug] - 2024-02-20 - Fixed bug where json with "()"" was not being recognized, for example `{"color": "rgba(0, 0, 0, 0.5)"}` diff --git a/extensions/json-format/src/formatJsonLines.tsx b/extensions/json-format/src/formatJsonLines.tsx index 24c19f64adf6..12694c2f1604 100644 --- a/extensions/json-format/src/formatJsonLines.tsx +++ b/extensions/json-format/src/formatJsonLines.tsx @@ -19,7 +19,7 @@ export default function Command() { { - let input: string = values.input; + const input: string = values.input; const output = formatToJSONLines(input); if (output) { setResult(output); @@ -29,7 +29,7 @@ export default function Command() { { - let input: string = values.input; + const input: string = values.input; const output = formatToJSONLines(input); if (output) { setResult(output); diff --git a/extensions/json-format/src/utils.ts b/extensions/json-format/src/utils.ts index 87945459c3b3..9b3d0f38d41b 100644 --- a/extensions/json-format/src/utils.ts +++ b/extensions/json-format/src/utils.ts @@ -99,7 +99,7 @@ export function formatToJSONLines(input: string) { return; } - let jsonVal = JSON.parse(`{"data":${input}}`); + const jsonVal = JSON.parse(`{"data":${input}}`); return jsonVal.data.map(JSON.stringify).join('\n'); } From 7977690972941e10d17c7000b4a58291bb21835c Mon Sep 17 00:00:00 2001 From: Benoit Durand Date: Mon, 22 Apr 2024 23:15:24 +0200 Subject: [PATCH 3/3] Format with Prettier --- .../json-format/src/formatJsonLines.tsx | 99 ++++++++++--------- extensions/json-format/src/utils.ts | 1 - 2 files changed, 50 insertions(+), 50 deletions(-) diff --git a/extensions/json-format/src/formatJsonLines.tsx b/extensions/json-format/src/formatJsonLines.tsx index 12694c2f1604..b4f3128def28 100644 --- a/extensions/json-format/src/formatJsonLines.tsx +++ b/extensions/json-format/src/formatJsonLines.tsx @@ -1,57 +1,58 @@ -import { Form, ActionPanel, Action } from "@raycast/api"; -import { useState } from "react"; +import { Form, ActionPanel, Action } from '@raycast/api'; +import { useState } from 'react'; import { formatToJSONLines } from './utils'; interface FormInput { - input: string; - result: string; + input: string; + result: string; } export default function Command() { - const [input, setInput] = useState(''); - const [result, setResult] = useState(''); + const [input, setInput] = useState(''); + const [result, setResult] = useState(''); - return ( -
- { - const input: string = values.input; - const output = formatToJSONLines(input); - if (output) { - setResult(output); - } - }} - /> - { - const input: string = values.input; - const output = formatToJSONLines(input); - if (output) { - setResult(output); - } - }} - /> - - } - > - - - - ); -} \ No newline at end of file + return ( +
+ { + const input: string = values.input; + const output = formatToJSONLines(input); + if (output) { + setResult(output); + } + }} + /> + { + const input: string = values.input; + const output = formatToJSONLines(input); + if (output) { + setResult(output); + } + }} + /> + + } + > + + + + ); +} diff --git a/extensions/json-format/src/utils.ts b/extensions/json-format/src/utils.ts index 9b3d0f38d41b..07989816dd88 100644 --- a/extensions/json-format/src/utils.ts +++ b/extensions/json-format/src/utils.ts @@ -103,7 +103,6 @@ export function formatToJSONLines(input: string) { return jsonVal.data.map(JSON.stringify).join('\n'); } - function isJson(str: string) { try { JSON.parse(str);