]> git.wincent.com - hextrapolate.git/commitdiff
Don't mutate input array in joinDigits()
authorGreg Hurrell <greg@hurrell.net>
Sat, 1 Aug 2015 01:54:37 +0000 (18:54 -0700)
committerGreg Hurrell <greg@hurrell.net>
Sat, 1 Aug 2015 01:54:37 +0000 (18:54 -0700)
`reverse()` is a nasty API. It returns the reversed array, but also
mutates it in place. Noticed while using `joinDigits()` to log stuff
while developing.

src/joinDigits.js

index 2230467559c778b22e5f59cf362ffce98a8bd841..3d6eae2f82fa7836697d50c364138c21dfe8e3f6 100644 (file)
@@ -15,7 +15,9 @@ export default function joinDigits(
   digits: Array<number>,
   base: number
 ): string {
-  return digits.reverse()
+  return digits
+    .slice()
+    .reverse()
     .map(number => number.toString(base))
     .join('');
 }