feat: percentage

This commit is contained in:
2023-11-19 15:14:09 +01:00
parent 1200e96c43
commit 8a122c5a25
7 changed files with 50 additions and 4 deletions

30
tests/percentage.test.ts Normal file
View File

@@ -0,0 +1,30 @@
// did I overenginner these tests?
import { percentage } from "../src/index";
import { describe, it, expect } from 'vitest';
describe('percentage', () => {
it('should return 50% when 1 is half of 2', () => {
expect(percentage(1, 2)).toBe(50);
});
it('should return 0% when 0 is half of 2', () => {
expect(percentage(0, 2)).toBe(0);
});
it('should return 100% when 2 is half of 2', () => {
expect(percentage(2, 2)).toBe(100);
});
it('should return 100% when 2 is half of 1', () => {
expect(percentage(2, 1)).toBe(100);
});
it('should return 200% when 2 is half of 1 and allowNumNot100 is true', () => {
expect(percentage(2, 1, true)).toBe(200);
})
it('should throw an error when total is 0', () => {
expect(() => percentage(1, 0)).toThrow();
});
it('should throw an error when partial is negative', () => {
expect(() => percentage(-1, 2)).toThrow();
});
it('should throw an error when total is negative', () => {
expect(() => percentage(1, -2)).toThrow();
});
})

View File

@@ -1,4 +1,4 @@
import { randomString } from '../src/index.ts'
import { randomString } from '../src/index'
import { describe, it, expect } from 'vitest'
describe('randomString', () => {