diff options
Diffstat (limited to 'src/client/react')
-rw-r--r-- | src/client/react/lib/withinRange.js | 11 | ||||
-rw-r--r-- | src/client/react/lib/withinRange.test.js | 18 |
2 files changed, 29 insertions, 0 deletions
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); + }); + }); +}); |