Skip to content

Commit

Permalink
Update json-format extension (#11938)
Browse files Browse the repository at this point in the history
  • Loading branch information
duranbe authored Apr 30, 2024
1 parent e73dd16 commit ef8b589
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 1 deletion.
4 changes: 4 additions & 0 deletions extensions/json-format/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)"}`
Expand Down
9 changes: 8 additions & 1 deletion extensions/json-format/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"Aayush9029",
"devlos",
"lachero",
"alexs"
"alexs",
"duranbe"
],
"categories": [
"Data",
Expand All @@ -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": [
Expand Down
58 changes: 58 additions & 0 deletions extensions/json-format/src/formatJsonLines.tsx
Original file line number Diff line number Diff line change
@@ -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<string>('');
const [result, setResult] = useState<string>('');

return (
<Form
actions={
<ActionPanel>
<Action.SubmitForm
title="Format"
onSubmit={async (values: FormInput) => {
const input: string = values.input;
const output = formatToJSONLines(input);
if (output) {
setResult(output);
}
}}
/>
<Action.SubmitForm
title="View Result"
onSubmit={async (values: FormInput) => {
const input: string = values.input;
const output = formatToJSONLines(input);
if (output) {
setResult(output);
}
}}
/>
</ActionPanel>
}
>
<Form.TextArea
id="input"
title="Input"
placeholder="Paste JSON Array here…"
value={input}
onChange={setInput}
/>
<Form.TextArea
id="result"
title="Result"
placeholder="Command + Shift + Enter to view result..."
value={result}
onChange={setResult}
/>
</Form>
);
}
17 changes: 17 additions & 0 deletions extensions/json-format/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -98,3 +111,7 @@ function isJson(str: string) {
}
return true;
}

function isArray(str: string) {
return str.startsWith('[') && str.endsWith(']');
}

0 comments on commit ef8b589

Please sign in to comment.