Skip to content

Commit 3f133ce

Browse files
author
Poltergeist
committed
feat(utilities): add percentage helper
This provides the user wiht a curried helper function. The first parameter is the percentage of 100 which we want to calculate the second is the total value so for example if we would like to calulate 30 percent of 1024 we would write `percentage(30, 1024)` or `percentage(30)(1024)` Close DCOS-38640
1 parent 016bae6 commit 3f133ce

3 files changed

Lines changed: 33 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function calculate(a, b) {
2+
return (b / 100) * a;
3+
}
4+
5+
function percentage(a?: number, b?: number) {
6+
if (arguments.length === 0) {
7+
return percentage;
8+
}
9+
if (arguments.length === 1) {
10+
return b => {
11+
return calculate(a, b);
12+
};
13+
}
14+
return calculate(a, b);
15+
}
16+
17+
export default percentage;

packages/utilities/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export { default as min } from "./components/min";
22
export { default as max } from "./components/max";
3+
export { default as percentage } from "./components/percentage";
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { default as percentage } from "../components/percentage";
2+
3+
describe("percentage", () => {
4+
it("returns percentage of total", () => {
5+
expect(percentage(30, 100)).toEqual(30);
6+
});
7+
8+
it("returns function if only percentage is provided", () => {
9+
expect(typeof percentage(30)).toBe("function");
10+
});
11+
12+
it("returns calculates the percentage from curried function", () => {
13+
expect(percentage(30)(100)).toEqual(30);
14+
});
15+
});

0 commit comments

Comments
 (0)