Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
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
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 propertiesToYaml } from './properties-to-yaml';
import { tool as pdfSignatureChecker } from './pdf-signature-checker';
import { tool as numeronymGenerator } from './numeronym-generator';
import { tool as macAddressGenerator } from './mac-address-generator';
Expand Down Expand Up @@ -100,6 +101,7 @@ export const toolsByCategory: ToolCategory[] = [
listConverter,
tomlToJson,
tomlToYaml,
propertiesToYaml,
],
},
{
Expand Down
12 changes: 12 additions & 0 deletions src/tools/properties-to-yaml/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { AlignJustified } from '@vicons/tabler';
import { defineTool } from '../tool';

export const tool = defineTool({
name: 'Properties to YAML',
path: '/properties-to-yaml',
description: '',
keywords: ['properties', 'yaml', 'convert', 'spring', 'spring-boot', 'parse'],
component: () => import('./properties-to-yaml.vue'),
icon: AlignJustified,
createdAt: new Date('2023-12-24'),
});
65 changes: 65 additions & 0 deletions src/tools/properties-to-yaml/properties-to-yaml.e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { expect, test } from '@playwright/test';

test.describe('Tool - Properties to YAML', () => {
test.beforeEach(async ({ page }) => {
await page.goto('/properties-to-yaml');
});

test('Has correct title', async ({ page }) => {
await expect(page).toHaveTitle('Properties to YAML - IT Tools');
});

test('', async ({ page }) => {
await page.getByTestId('input').fill(`
spring.mvc.async.request-timeout=-1
spring.output.ansi.enabled=NEVER
spring.config.import=optional:file:.env[.properties]
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=\${DATABASE_URI}
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.open-in-view=false
management.endpoints.web.base-path=/internal
management.endpoints.web.exposure.include[0]=health
management.endpoints.web.exposure.include[1]=info
management.endpoint.info.enabled=true
management.endpoint.health=
`.trim());

const generatedJson = await page.getByTestId('area-content').innerText();

expect(generatedJson.trim()).toEqual(`
spring:
mvc:
async:
request-timeout: "-1"
output:
ansi:
enabled: NEVER
config:
import: optional:file:.env[.properties]
datasource:
driverClassName: com.mysql.cj.jdbc.Driver
url: \${DATABASE_URI}
jpa:
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
database-platform: org.hibernate.dialect.MySQLDialect
open-in-view: "false"
management:
endpoints:
web:
base-path: /internal
exposure:
include:
- health
- info
endpoint:
info:
enabled: "true"
health: ""
`.trim(),
);
});
});
19 changes: 19 additions & 0 deletions src/tools/properties-to-yaml/properties-to-yaml.service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { describe, expect, it } from 'vitest';
import { isValidProperties } from './properties-to-yaml.service';

describe('isValidProperties', () => {
it('should return true for valid properties', () => {
const properties = 'key1=value1\nkey2=value2';
expect(isValidProperties(properties)).toBe(true);
});

it('should return false for properties with duplicate keys', () => {
const properties = 'key1=value1\nkey1=value2';
expect(isValidProperties(properties)).toBe(false);
});

it('should return false for properties with incorrect format', () => {
const properties = 'key1\nkey2=value2';
expect(isValidProperties(properties)).toBe(false);
});
});
45 changes: 45 additions & 0 deletions src/tools/properties-to-yaml/properties-to-yaml.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import _ from 'lodash';

export { isValidProperties, parseProperties };

function isValidProperties(properties: string): boolean {
const lines = properties.split('\n');
const keys: Set<string> = new Set();

for (const line of lines) {
if (line.trim().startsWith('#') || line.trim() === '') {
continue;
}

if (!line.includes('=')) {
return false;
}

const [key, _value] = line.split('=');

if (!key) {
return false;
}

if (keys.has(key.trim())) {
return false;
}

keys.add(key.trim());
}

return true;
}

// Thanks to https://github.com/sdoxsee/env-gen
function parseProperties(properties: string): Record<string, string> {
const result = properties
.split('\n')
.filter(Boolean) // removes empty lines
.reduce((acc, line) => {
const pair = line.split('=');
_.set(acc, pair[0], pair[1]);
return acc;
}, {});
return result;
}
25 changes: 25 additions & 0 deletions src/tools/properties-to-yaml/properties-to-yaml.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<script setup lang="ts">
import { stringify as stringifyToYaml } from 'yaml';
import { withDefaultOnError } from '../../utils/defaults';
import { isValidProperties, parseProperties } from './properties-to-yaml.service';
import type { UseValidationRule } from '@/composable/validation';

const transformer = (value: string) => value.trim() === '' ? '' : withDefaultOnError(() => stringifyToYaml(parseProperties(value)), '');
const rules: UseValidationRule<string>[] = [
{
validator: isValidProperties,
message: 'Invalid .properties given.',
},
];
</script>

<template>
<format-transformer
input-label="Your Properties"
input-placeholder="Paste your Properties here..."
output-label="YAML from your Properties"
output-language="yaml"
:input-validation-rules="rules"
:transformer="transformer"
/>
</template>