]> git.wincent.com - hextrapolate.git/commitdiff
Extract joinDigits() function
authorGreg Hurrell <greg@hurrell.net>
Sat, 1 Aug 2015 01:31:22 +0000 (18:31 -0700)
committerGreg Hurrell <greg@hurrell.net>
Sat, 1 Aug 2015 01:32:40 +0000 (18:32 -0700)
src/add.js
src/joinDigits.js [new file with mode: 0644]

index fbf49d6ed597f513b13428f808aca087152f7d21..294d176e75b47f97c6dc8c9ac8dbdebf9dd2f3ad 100644 (file)
@@ -9,6 +9,7 @@
 
 import addDigits from './addDigits';
 import getDigits from './getDigits';
+import joinDigits from './joinDigits';
 
 /**
  * Adds two numbers `a` and `b`, both in `base` and returns the answer as a
@@ -17,8 +18,6 @@ import getDigits from './getDigits';
 export default function add(a: string, b: string, base: number): string {
   const aDigits = getDigits(a, base);
   const bDigits = getDigits(b, base);
-  return addDigits(aDigits, bDigits, base)
-    .reverse()
-    .map(number => number.toString(base))
-    .join('');
+  const result = addDigits(aDigits, bDigits, base);
+  return joinDigits(result, base);
 }
diff --git a/src/joinDigits.js b/src/joinDigits.js
new file mode 100644 (file)
index 0000000..2230467
--- /dev/null
@@ -0,0 +1,21 @@
+/**
+ * Copyright 2015-present Greg Hurrell. All rights reserved.
+ * Licensed under the terms of the MIT license.
+ *
+ * @flow
+ */
+
+'use strict';
+
+/**
+ * Turns an unpacked arbitrary-precision representation of a number, `digits`,
+ * (as produced by `getDigits`) back into a string representation in `base`.
+ */
+export default function joinDigits(
+  digits: Array<number>,
+  base: number
+): string {
+  return digits.reverse()
+    .map(number => number.toString(base))
+    .join('');
+}