-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Poltergeist
committed
Jul 6, 2018
1 parent
cbb0b6a
commit 8758039
Showing
3 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as min } from "./components/min"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |