Repository: char0n/ramda-adjunct
Pull request #805 (2019-01-27 21:17:08)

feat: add round
Ref #106




Patch:
diff --git a/src/index.d.ts b/src/index.d.ts
index cb75e0fee4..aa72cad1c2 100644
--- a/src/index.d.ts
+++ b/src/index.d.ts
@@ -320,7 +320,7 @@ declare namespace RamdaAdjunct {
          * Checks if value is odd integer number.
          * An odd number is an integer which is not a multiple DIVISIBLE of two.
          */
-        isOdd(val: any): number;
+        isOdd(val: any): boolean;
 
         /**
          * Checks if value is even integer number.
@@ -329,7 +329,7 @@ declare namespace RamdaAdjunct {
          * which despite not being a natural number, is an integer.
          * Even numbers are either positive or negative.
          */
-        isEven(val: any): number;
+        isEven(val: any): boolean;
 
         /**
          * Checks if input value is a pair.
@@ -1055,6 +1055,11 @@ declare namespace RamdaAdjunct {
             (toIdx: number): (list: T[]) => T[];
         };
 
+        /**
+         * Returns the value of a number rounded to the nearest integer.
+         */
+         round(val: number): number;
+
         /**
          * Identity type.
          */
diff --git a/src/index.js b/src/index.js
index 357c12219a..cd3e2cd7b5 100644
--- a/src/index.js
+++ b/src/index.js
@@ -162,5 +162,7 @@ export { default as notAllPass } from './notAllPass';
 export { default as nonePass } from './nonePass';
 export { default as argsPass } from './argsPass';
 export { default as dropArgs } from './dropArgs';
+// Math
+export { default as round } from './round';
 // Types
 export { default as Identity } from './fantasy-land/Identity';
diff --git a/src/round.js b/src/round.js
new file mode 100644
index 0000000000..395caa1a17
--- /dev/null
+++ b/src/round.js
@@ -0,0 +1,27 @@
+import { bind } from 'ramda';
+import curry1 from 'ramda/src/internal/_curry1';
+
+/**
+ * Returns the value of a number rounded to the nearest integer.
+ *
+ * @func round
+ * @memberOf RA
+ * @since {@link https://char0n.github.io/ramda-adjunct/2.15.0|v2.15.0}
+ * @category Math
+ * @sig Number -> Number
+ * @param {*} number The number to round
+ * @return {number} The value of the given number rounded to the nearest integer
+ * @example
+ *
+ * RA.round(0.9); //=> 1
+ * RA.round(5.95); //=> 6
+ * RA.round(5.5); //=> 6
+ * RA.round(5.05); //=> 5
+ * RA.round(-5.05); //=> -5
+ * RA.round(-5.5); //=> -5
+ * RA.round(-5.95); //=> -6
+ */
+
+const round = curry1(bind(Math.round, Math));
+
+export default round;


Test patch:
diff --git a/test/round.js b/test/round.js
new file mode 100644
index 0000000000..83fd3dc6b1
--- /dev/null
+++ b/test/round.js
@@ -0,0 +1,81 @@
+import * as R from 'ramda';
+import { assert } from 'chai';
+
+import * as RA from '../src';
+import eq from './shared/eq';
+import Symbol from './shared/Symbol';
+
+describe('round', function() {
+  it('should round float to nearest integer', function() {
+    eq(RA.round(0.9), 1);
+    eq(RA.round(5.95), 6);
+    eq(RA.round(5.5), 6);
+    eq(RA.round(5.05), 5);
+    eq(RA.round(-5.05), -5);
+    eq(RA.round(-5.5), -5);
+    eq(RA.round(-5.95), -6);
+  });
+
+  context('given integer number', function() {
+    specify('should return original integer number', function() {
+      eq(RA.round(0), 0);
+      eq(RA.round(1), 1);
+      eq(RA.round(-1), -1);
+    });
+  });
+
+  context('given value that is not a number', function() {
+    specify('should return NaN', function() {
+      eq(RA.round(undefined), NaN);
+      eq(RA.round(NaN), NaN);
+      eq(RA.round({}), NaN);
+      eq(RA.round(/a/), NaN);
+      eq(RA.round('test'), NaN);
+      eq(RA.round(new Error()), NaN);
+    });
+  });
+
+  context('given null', function() {
+    specify('should return 0', function() {
+      eq(RA.round(null), 0);
+    });
+  });
+
+  context('given Symbol value', function() {
+    specify('should throw TypeError', function() {
+      let shouldThrow;
+      try {
+        Math.round(Symbol(''));
+        shouldThrow = false;
+      } catch (e) {
+        shouldThrow = true;
+      }
+
+      if (shouldThrow) {
+        assert.throw(() => RA.round(Symbol('')));
+      }
+    });
+  });
+
+  context('given valid date object', function() {
+    specify(
+      'should return the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC',
+      function() {
+        eq(RA.round(new Date(2)), 2);
+      }
+    );
+  });
+
+  context('given Infinity value', function() {
+    specify('should return untouched Infinity value', function() {
+      eq(RA.round(Infinity), Infinity);
+      eq(RA.round(-Infinity), -Infinity);
+    });
+  });
+
+  it('should support placeholder to specify "gaps"', function() {
+    const round = RA.round(R.__);
+
+    eq(round(0.9), 1);
+  });
+});