Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update json-format extension #11938

Merged
merged 3 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(']');
}