mirror of
https://github.com/SrIzan10/util-utils.git
synced 2026-06-05 16:56:59 +00:00
feat: percentage
This commit is contained in:
3
examples/percentage.ts
Normal file
3
examples/percentage.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import { percentage } from "../src/index";
|
||||
|
||||
console.log(percentage(10, 100)); // 10
|
||||
@@ -1,4 +1,4 @@
|
||||
import { randomString } from "../src/index.ts";
|
||||
import { randomString } from "../src/index";
|
||||
|
||||
console.log(randomString(10));
|
||||
// => (random 10-character string)
|
||||
@@ -1 +1,2 @@
|
||||
export { randomString } from './utils/randomstring.ts';
|
||||
export { randomString } from './utils/randomstring';
|
||||
export { percentage } from './utils/percentage';
|
||||
12
src/utils/percentage.ts
Normal file
12
src/utils/percentage.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
export function percentage(partial: number, total: number, allowNumNot100 = false) {
|
||||
if (total === 0) {
|
||||
throw new Error('Total cannot be 0');
|
||||
}
|
||||
if (partial < 0 || total < 0) {
|
||||
throw new Error('Partial and total must be non-negative');
|
||||
}
|
||||
if (!allowNumNot100 && partial > total) {
|
||||
return 100;
|
||||
}
|
||||
return (partial / total) * 100;
|
||||
}
|
||||
30
tests/percentage.test.ts
Normal file
30
tests/percentage.test.ts
Normal 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();
|
||||
});
|
||||
})
|
||||
@@ -1,4 +1,4 @@
|
||||
import { randomString } from '../src/index.ts'
|
||||
import { randomString } from '../src/index'
|
||||
import { describe, it, expect } from 'vitest'
|
||||
|
||||
describe('randomString', () => {
|
||||
|
||||
@@ -35,7 +35,7 @@
|
||||
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
|
||||
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
||||
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
|
||||
"allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */
|
||||
"allowImportingTsExtensions": false, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */
|
||||
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
|
||||
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
|
||||
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
|
||||
|
||||
Reference in New Issue
Block a user