-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
feat(new tool): percentage calculator #456
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Percentage } from '@vicons/tabler'; | ||
import { defineTool } from '../tool'; | ||
|
||
export const tool = defineTool({ | ||
name: 'Percentage calculator', | ||
path: '/percentage-calculator', | ||
description: 'Easily calculate percentages from a value to another value, or from a percentage to a value.', | ||
keywords: ['percentage', 'calculator', 'calculate', 'value', 'number', '%'], | ||
component: () => import('./percentage-calculator.vue'), | ||
icon: Percentage, | ||
createdAt: new Date('2023-06-18'), | ||
}); |
36 changes: 36 additions & 0 deletions
36
src/tools/percentage-calculator/percentage-calculator.e2e.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { expect, test } from '@playwright/test'; | ||
|
||
test.describe('Tool - Percentage calculator', () => { | ||
test.beforeEach(async ({ page }) => { | ||
await page.goto('/percentage-calculator'); | ||
}); | ||
|
||
test('Has correct title', async ({ page }) => { | ||
await expect(page).toHaveTitle('Percentage calculator - IT Tools'); | ||
}); | ||
|
||
test('Correctly works out percentages', async ({ page }) => { | ||
await page.getByTestId('percentageX').locator('input').fill('123'); | ||
await page.getByTestId('percentageY').locator('input').fill('456'); | ||
await expect(page.getByTestId('percentageResult').locator('input')).toHaveValue('560.88'); | ||
|
||
await page.getByTestId('numberX').locator('input').fill('123'); | ||
await page.getByTestId('numberY').locator('input').fill('456'); | ||
await expect(page.getByTestId('numberResult').locator('input')).toHaveValue('26.973684210526315'); | ||
|
||
await page.getByTestId('numberFrom').locator('input').fill('123'); | ||
await page.getByTestId('numberTo').locator('input').fill('456'); | ||
await expect(page.getByTestId('percentageIncreaseDecrease').locator('input')).toHaveValue('270.7317073170732'); | ||
}); | ||
|
||
test('Displays empty results for incomplete input', async ({ page }) => { | ||
await page.getByTestId('percentageX').locator('input').fill('123'); | ||
await expect(page.getByTestId('percentageResult').locator('input')).toHaveValue(''); | ||
|
||
await page.getByTestId('numberY').locator('input').fill('456'); | ||
await expect(page.getByTestId('numberResult').locator('input')).toHaveValue(''); | ||
|
||
await page.getByTestId('numberFrom').locator('input').fill('123'); | ||
await expect(page.getByTestId('percentageIncreaseDecrease').locator('input')).toHaveValue(''); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<script setup lang="ts"> | ||
const percentageX = ref(); | ||
const percentageY = ref(); | ||
const percentageResult = computed(() => { | ||
if (percentageX.value === undefined || percentageY.value === undefined) { | ||
return ''; | ||
} | ||
return (percentageX.value / 100 * percentageY.value).toString(); | ||
}); | ||
|
||
const numberX = ref(); | ||
const numberY = ref(); | ||
const numberResult = computed(() => { | ||
if (numberX.value === undefined || numberY.value === undefined) { | ||
return ''; | ||
} | ||
const result = 100 * numberX.value / numberY.value; | ||
return (!Number.isFinite(result) || Number.isNaN(result)) ? '' : result.toString(); | ||
}); | ||
|
||
const numberFrom = ref(); | ||
const numberTo = ref(); | ||
const percentageIncreaseDecrease = computed(() => { | ||
if (numberFrom.value === undefined || numberTo.value === undefined) { | ||
return ''; | ||
} | ||
const result = (numberTo.value - numberFrom.value) / numberFrom.value * 100; | ||
return (!Number.isFinite(result) || Number.isNaN(result)) ? '' : result.toString(); | ||
}); | ||
</script> | ||
|
||
<template> | ||
<div style="flex: 0 0 100%"> | ||
<div style="margin: 0 auto; max-width: 600px"> | ||
<c-card mb-3> | ||
<div mb-3 sm:hidden> | ||
What is | ||
</div> | ||
<div flex gap-2> | ||
<div hidden pt-1 sm:block style="min-width: 48px;"> | ||
What is | ||
</div> | ||
<n-input-number v-model:value="percentageX" data-test-id="percentageX" placeholder="X" /> | ||
<div min-w-fit pt-1> | ||
% of | ||
</div> | ||
<n-input-number v-model:value="percentageY" data-test-id="percentageY" placeholder="Y" /> | ||
<input-copyable v-model:value="percentageResult" data-test-id="percentageResult" readonly placeholder="Result" style="max-width: 150px;" /> | ||
</div> | ||
</c-card> | ||
|
||
<c-card mb-3> | ||
<div mb-3 sm:hidden> | ||
X is what percent of Y | ||
</div> | ||
<div flex gap-2> | ||
<n-input-number v-model:value="numberX" data-test-id="numberX" placeholder="X" /> | ||
<div hidden min-w-fit pt-1 sm:block> | ||
is what percent of | ||
</div> | ||
<n-input-number v-model:value="numberY" data-test-id="numberY" placeholder="Y" /> | ||
<input-copyable v-model:value="numberResult" data-test-id="numberResult" readonly placeholder="Result" style="max-width: 150px;" /> | ||
</div> | ||
</c-card> | ||
|
||
<c-card mb-3> | ||
<div mb-3> | ||
What is the percentage increase/decrease | ||
</div> | ||
<div flex gap-2> | ||
<n-input-number v-model:value="numberFrom" data-test-id="numberFrom" placeholder="From" /> | ||
<n-input-number v-model:value="numberTo" data-test-id="numberTo" placeholder="To" /> | ||
<input-copyable v-model:value="percentageIncreaseDecrease" data-test-id="percentageIncreaseDecrease" readonly placeholder="Result" style="max-width: 150px;" /> | ||
</div> | ||
</c-card> | ||
</div> | ||
</div> | ||
</template> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.