aboutsummaryrefslogtreecommitdiff
path: root/src/client
diff options
context:
space:
mode:
authorNoah Loomans <noahloomans@gmail.com>2018-07-03 20:51:14 +0200
committerNoah Loomans <noahloomans@gmail.com>2018-07-03 20:51:14 +0200
commiteddef78b794c62a1a257a742237634fab2648511 (patch)
treeb2d1cd1eb072df1bbf3fbd4b0e98a20bdfb492d2 /src/client
parentf5c7fc1c6aa985151cf9f151d2f3423772d6fc09 (diff)
client: Add withinRange
Diffstat (limited to 'src/client')
-rw-r--r--src/client/react/lib/withinRange.js11
-rw-r--r--src/client/react/lib/withinRange.test.js18
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);
+ });
+ });
+});