Unique array

This commit is contained in:
Luke Hagar
2024-01-18 17:22:39 -06:00
parent 38e00bdc8a
commit 74f9339713
2 changed files with 26 additions and 5 deletions

View File

@@ -77,12 +77,14 @@ export function convertNumber(number: number, config: Config) {
export function convertArray(array: any[], config: Config) {
const output: Output = {};
const items = [];
const exampleArray = [];
for (const entry of array) {
const map = convertObject(entry, config);
if (items.filter((item) => item.type === map.type && item.format === map.format).length < 1) {
items.push(map);
exampleArray.push(entry);
}
}
@@ -93,7 +95,7 @@ export function convertArray(array: any[], config: Config) {
output.items = [...items];
}
if (config.includeExamples) output.example = array;
if (config.includeExamples) output.example = exampleArray;
return output;
}
@@ -140,6 +142,8 @@ export function convertObject(input: any, config: Config) {
output.type = 'boolean';
output.example = input;
return output;
} else if (input === undefined) {
throw new Error(`undefined cannot be converted to OAS`);
} else {
throw new Error(`Invalid Swagger type for type ${typeof input}:${input}`);
}

View File

@@ -1,15 +1,16 @@
<script lang="ts">
import { example, convertObjectToOAS } from '$lib/converter';
import { example, convertObjectToOAS, type Config } from '$lib/converter';
import { clipboard } from '@skeletonlabs/skeleton';
import { onMount } from 'svelte';
import { stringify } from 'yaml';
import { JSONEditor } from 'svelte-jsoneditor';
import { config, yamlOut } from '$lib/store';
let content: { text?: string | undefined; json: object } = {
let content: { text?: string; json?: object } = {
text: undefined, // can be used to pass a stringified JSON document instead
json: example
};
let outSwagger: string = '';
onMount(() => {
let tempJSON = localStorage.getItem('inputJSON');
@@ -28,7 +29,23 @@
}
}
$: outSwagger = format(convertObjectToOAS(content.json, $config), $yamlOut);
function run(
content: { text?: string; json?: object | undefined },
config: Config,
yamlOut: boolean
) {
if (content.json !== undefined && content.text !== undefined) return;
let data;
if (content.text !== undefined) {
data = JSON.parse(content.text);
} else if (content.json !== undefined) {
data = content.json;
}
outSwagger = format(convertObjectToOAS(data, config), yamlOut);
}
$: run(content, $config, $yamlOut);
</script>
<div class="flex flex-row flex-wrap justify-between px-2 gap-2 overflow-hidden">
@@ -37,7 +54,7 @@
Input all of your JSON formatted Data, Typically API response bodies
</p>
<div class="card my-json-editor jse-theme-dark overflow-hidden h-[85vh]">
<JSONEditor bind:content />
<JSONEditor onChange={() => run(content, $config, $yamlOut)} bind:content />
</div>
</div>
<div class="grow">