From eddef78b794c62a1a257a742237634fab2648511 Mon Sep 17 00:00:00 2001 From: Noah Loomans Date: Tue, 3 Jul 2018 20:51:14 +0200 Subject: client: Add withinRange --- src/client/react/lib/withinRange.js | 11 +++++++++++ src/client/react/lib/withinRange.test.js | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 src/client/react/lib/withinRange.js create mode 100644 src/client/react/lib/withinRange.test.js (limited to 'src') diff --git a/src/client/react/lib/withinRange.js b/src/client/react/lib/withinRange.js new file mode 100644 index 0000000..d3d4f24 --- /dev/null +++ b/src/client/react/lib/withinRange.js @@ -0,0 +1,11 @@ +export default function withinRange(number, rangeMax) { + if (number < 0) { + return withinRange(number + (rangeMax + 1), rangeMax); + } + + if (number > rangeMax) { + return withinRange(number - (rangeMax + 1), rangeMax); + } + + return number; +} diff --git a/src/client/react/lib/withinRange.test.js b/src/client/react/lib/withinRange.test.js new file mode 100644 index 0000000..baec122 --- /dev/null +++ b/src/client/react/lib/withinRange.test.js @@ -0,0 +1,18 @@ +import withinRange from './withinRange'; + +describe('withinRange', () => { + describe('the number is within range', () => { + it('just returns the number', () => { + expect(withinRange(5, 7)).toEqual(5); + }); + }); + + describe('the number is outside the range', () => { + it('wraps the number until within range', () => { + expect(withinRange(-1, 6)).toEqual(6); + expect(withinRange(7, 6)).toEqual(0); + expect(withinRange(-20, 6)).toEqual(1); + expect(withinRange(20, 6)).toEqual(6); + }); + }); +}); -- cgit v1.1