Skip to content

Commit

Permalink
feat(utilities): add min helper function
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Poltergeist committed Jul 6, 2018
1 parent cbb0b6a commit 8758039
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/utilities/components/min.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function calculate(a, b) {
return b < a ? b : a;
}

function min(a?: number | Date | string, b?: number | Date | string) {
if (arguments.length === 0) {
return min;
}
if (arguments.length === 1) {
return b => {
return calculate(a, b);
};
}
return calculate(a, b);
}

export default min;
1 change: 1 addition & 0 deletions packages/utilities/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as min } from "./components/min";
33 changes: 33 additions & 0 deletions packages/utilities/tests/min.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { default as min } from "../components/min";

describe("min", () => {
it("returns the smaller of its two arguments", () => {
expect(min(-7, 7)).toEqual(-7);
expect(min(7, -7)).toEqual(-7);
});

it("works for any orderable type", () => {
var d1 = new Date("2001-01-01");
var d2 = new Date("2002-02-02");

expect(min(d1, d2)).toEqual(d1);
expect(min(d2, d1)).toEqual(d1);
expect(min("a", "b")).toEqual("a");
expect(min("b", "a")).toEqual("a");
});
it("returns is a function", () => {
expect(typeof min).toBe("function");
});

it("returns a function after adding the min value", () => {
expect(typeof min(1)).toBe("function");
});

it("is returning the min value", () => {
expect(min(1)(10)).toBe(1);
});

it("is returning the lower value value", () => {
expect(min(12)(10)).toBe(10);
});
});

0 comments on commit 8758039

Please sign in to comment.