From ef8b5891a6802681aa0e93bdc35d722929fb5335 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Durand?= Date: Tue, 30 Apr 2024 12:28:41 +0200 Subject: [PATCH] Update json-format extension (#11938) --- extensions/json-format/CHANGELOG.md | 4 ++ extensions/json-format/package.json | 9 ++- .../json-format/src/formatJsonLines.tsx | 58 +++++++++++++++++++ extensions/json-format/src/utils.ts | 17 ++++++ 4 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 extensions/json-format/src/formatJsonLines.tsx 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/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..b4f3128def28 --- /dev/null +++ b/extensions/json-format/src/formatJsonLines.tsx @@ -0,0 +1,58 @@ +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 ( +
+ { + 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 ee796f79b840..07989816dd88 100644 --- a/extensions/json-format/src/utils.ts +++ b/extensions/json-format/src/utils.ts @@ -90,6 +90,19 @@ 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; + } + + const jsonVal = JSON.parse(`{"data":${input}}`); + return jsonVal.data.map(JSON.stringify).join('\n'); +} + function isJson(str: string) { try { JSON.parse(str); @@ -98,3 +111,7 @@ function isJson(str: string) { } return true; } + +function isArray(str: string) { + return str.startsWith('[') && str.endsWith(']'); +}