Repository: char0n/ramda-adjunct
Pull request #1081 (2019-10-02 01:06:14)

feat: add divideNum
https://github.com/char0n/ramda-adjunct/issues/482

willcaul: @char0n - thanks for the PR comments... updated.

Regarding the name, I changed it from `divideBy` to `divideNum` as requested. But I was wondering... you think `divideByNum` would be clearer, or do you think it is clearer as-is?

Patch:
diff --git a/src/divideNum.js b/src/divideNum.js
new file mode 100644
index 0000000000..0621d18507
--- /dev/null
+++ b/src/divideNum.js
@@ -0,0 +1,20 @@
+import { divide, flip } from 'ramda';
+
+/**
+ * Divides two numbers, where the second number is divided by the first number.
+ *
+ * @func divideNum
+ * @memberOf RA
+ * @since {@link https://char0n.github.io/ramda-adjunct/2.22.0|v2.22.0}
+ * @category Math
+ * @sig Number -> Number -> Number
+ * @param {number} divisor the number to divide by
+ * @param {number} dividend the number to divide
+ * @return {number} A number representing the quotient of dividing the dividend by the divisor
+ * @example
+ *
+ * RA.divideNum(2, 1); //=> 0.5
+ */
+const divideNum = flip(divide);
+
+export default divideNum;
diff --git a/src/index.js b/src/index.js
index 7a78c1179c..8a7c101f6d 100644
--- a/src/index.js
+++ b/src/index.js
@@ -174,6 +174,7 @@ export { default as dropArgs } from './dropArgs';
 // Math
 export { default as round } from './round';
 export { default as ceil } from './ceil';
+export { default as divideNum } from './divideNum';
 export { default as floor } from './floor';
 export { default as trunc } from './trunc';
 export { default as sign } from './sign';
diff --git a/types/index.d.ts b/types/index.d.ts
index 4cec88cde7..b3af7b7428 100644
--- a/types/index.d.ts
+++ b/types/index.d.ts
@@ -1157,6 +1157,12 @@ declare namespace RamdaAdjunct {
         escapeRegExp(val: string): string;
         escapeRegExp<T>(val: T): T;
 
+        /**
+         * Divides two numbers, where the second number is divided by the first number.
+         */
+        divideNum(divisor: number, dividend: number): number;
+        divideNum(divisor: number): (dividend: number) => number;
+
         /**
          * Identity type.
          */


Test patch:
diff --git a/test/divideNum.js b/test/divideNum.js
new file mode 100644
index 0000000000..1277b8baa2
--- /dev/null
+++ b/test/divideNum.js
@@ -0,0 +1,17 @@
+import * as RA from '../src';
+import eq from './shared/eq';
+
+describe('divideNum', function() {
+  it('should divide the second number by the first number', function() {
+    eq(RA.divideNum(2, 1), 0.5);
+    eq(RA.divideNum(2.0, 1.0), 0.5);
+    eq(RA.divideNum(0, 2.0), Infinity);
+    eq(RA.divideNum(0.0, 2.0), Infinity);
+    eq(RA.divideNum(-0.0, 2.0), -Infinity);
+  });
+
+  it('should curry', function() {
+    eq(RA.divideNum(2)(1), 0.5);
+    eq(RA.divideNum(2, 1), 0.5);
+  });
+});
diff --git a/types/test/divideNum.ts b/types/test/divideNum.ts
new file mode 100644
index 0000000000..7404b32efe
--- /dev/null
+++ b/types/test/divideNum.ts
@@ -0,0 +1,13 @@
+import * as RA from 'ramda-adjunct';
+
+RA.divideNum(2, 1); // $ExpectType number
+RA.divideNum(2.0, 1.0); // $ExpectType number
+RA.divideNum(0, 2.0); // $ExpectType number
+RA.divideNum(0.0, 2.0); // $ExpectType number
+RA.divideNum(-0.0, 2.0); // $ExpectType number
+
+RA.divideNum(2)(1); // $ExpectType number
+RA.divideNum(2.0)(1.0); // $ExpectType number
+RA.divideNum(0)(2.0); // $ExpectType number
+RA.divideNum(0.0)(2.0); // $ExpectType number
+RA.divideNum(-0.0)(2.0); // $ExpectType number