Skip to content

Commit

Permalink
feat(new tool): JSON string converter
Browse files Browse the repository at this point in the history
  • Loading branch information
gitmotion committed Oct 17, 2024
1 parent f962c41 commit 7344e3f
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
4 changes: 4 additions & 0 deletions locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,10 @@ tools:
title: JSON minify
description: Minify and compress your JSON by removing unnecessary whitespace.

json-string-converter:
title: JSON string converter
description: Convert your plain text or JavaScript objects into JSON string format and vice versa.

ulid-generator:
title: ULID generator
description: Generate random Universally Unique Lexicographically Sortable Identifier (ULID).
Expand Down
2 changes: 2 additions & 0 deletions src/tools/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { tool as base64FileConverter } from './base64-file-converter';
import { tool as base64StringConverter } from './base64-string-converter';
import { tool as basicAuthGenerator } from './basic-auth-generator';
import { tool as jsonStringConverter } from './json-string-converter';
import { tool as emailNormalizer } from './email-normalizer';

import { tool as asciiTextDrawer } from './ascii-text-drawer';
Expand Down Expand Up @@ -151,6 +152,7 @@ export const toolsByCategory: ToolCategory[] = [
crontabGenerator,
jsonViewer,
jsonMinify,
jsonStringConverter,
jsonToCsv,
sqlPrettify,
chmodCalculator,
Expand Down
12 changes: 12 additions & 0 deletions src/tools/json-string-converter/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Braces } from '@vicons/tabler';
import { defineTool } from '../tool';

export const tool = defineTool({
name: 'Json string converter',
path: '/json-string-converter',
description: '',
keywords: ['json', 'string', 'converter'],
component: () => import('./json-string-converter.vue'),
icon: Braces,
createdAt: new Date('2024-10-17'),
});
11 changes: 11 additions & 0 deletions src/tools/json-string-converter/json-string-converter.e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { test, expect } from '@playwright/test';

Check failure on line 1 in src/tools/json-string-converter/json-string-converter.e2e.spec.ts

View workflow job for this annotation

GitHub Actions / ci

Member 'expect' of the import declaration should be sorted alphabetically

test.describe('Tool - Json string converter', () => {
test.beforeEach(async ({ page }) => {
await page.goto('/json-string-converter');
});

test('Has correct title', async ({ page }) => {
await expect(page).toHaveTitle('Json string converter - IT Tools');
});
});
56 changes: 56 additions & 0 deletions src/tools/json-string-converter/json-string-converter.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<script setup lang="ts">
import type { UseValidationRule } from '@/composable/validation';
import { withDefaultOnError } from '@/utils/defaults';
const defaultValue = '{\n\t"hello": [\n\t\t"world"\n\t]\n}';
// Define a reactive variable to track the selected transformation mode
const selectedMode = ref('stringify');
// Create functions for both stringification and parsing
const stringifyTransformer = (value: string) => withDefaultOnError(() => JSON.stringify(value), '');
const parseTransformer = (value: string) => withDefaultOnError(() => JSON.parse(value).toString(), '');
// Dynamically select the transformer based on the selected mode
const transformer = computed(() => {
if (selectedMode.value === 'stringify') {
return stringifyTransformer;
}
else {
return parseTransformer;
}
});
const rules: UseValidationRule<string>[] = [
{
validator: (v: string) => v === '' || (selectedMode.value === 'stringify' ? JSON.stringify(v) : JSON.parse(v)),
message: 'Provided text is not valid. (Make sure your JSON is in double quotes)',
},
];
// Dropdown options
const dropdownOptions = computed(() => [
{ label: 'JSON Stringify', value: 'stringify' },
{ label: 'JSON Parse', value: 'parse' },
]);
</script>

<template>
<c-card>
<c-select
v-model:value="selectedMode"
label="Transformation Mode"
:options="dropdownOptions"
/>
</c-card>
<div />
<format-transformer
input-label="Your text / JSON string"
:input-default="defaultValue"
input-placeholder="Paste your text here..."
output-label="JSON string conversion of your input"
output-language="string"
:input-validation-rules="rules"
:transformer="transformer"
/>
</template>

0 comments on commit 7344e3f

Please sign in to comment.