Skip to content

Commit 8758039

Browse files
author
Poltergeist
committed
feat(utilities): add min helper function
This will provide a curried function to calculate the min value of two parameters. This should be replaced by ramda as soon as their types have been fixed. Close DCOS-38496
1 parent cbb0b6a commit 8758039

3 files changed

Lines changed: 51 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 < a ? b : a;
3+
}
4+
5+
function min(a?: number | Date | string, b?: number | Date | string) {
6+
if (arguments.length === 0) {
7+
return min;
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 min;

packages/utilities/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default as min } from "./components/min";
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { default as min } from "../components/min";
2+
3+
describe("min", () => {
4+
it("returns the smaller of its two arguments", () => {
5+
expect(min(-7, 7)).toEqual(-7);
6+
expect(min(7, -7)).toEqual(-7);
7+
});
8+
9+
it("works for any orderable type", () => {
10+
var d1 = new Date("2001-01-01");
11+
var d2 = new Date("2002-02-02");
12+
13+
expect(min(d1, d2)).toEqual(d1);
14+
expect(min(d2, d1)).toEqual(d1);
15+
expect(min("a", "b")).toEqual("a");
16+
expect(min("b", "a")).toEqual("a");
17+
});
18+
it("returns is a function", () => {
19+
expect(typeof min).toBe("function");
20+
});
21+
22+
it("returns a function after adding the min value", () => {
23+
expect(typeof min(1)).toBe("function");
24+
});
25+
26+
it("is returning the min value", () => {
27+
expect(min(1)(10)).toBe(1);
28+
});
29+
30+
it("is returning the lower value value", () => {
31+
expect(min(12)(10)).toBe(10);
32+
});
33+
});

0 commit comments

Comments
 (0)