]> git.wincent.com - hextrapolate.git/commitdiff
Implement `convert` function
authorGreg Hurrell <greg@hurrell.net>
Sat, 1 Aug 2015 02:46:18 +0000 (19:46 -0700)
committerGreg Hurrell <greg@hurrell.net>
Sat, 1 Aug 2015 02:46:18 +0000 (19:46 -0700)
Along with some illustrative tests. More to come (will fill in the blank
`it` blocks).

Based on the technique described at:

http://www.danvk.org/hex2dec.html

src/__tests__/convert-test.js [new file with mode: 0644]
src/convert.js [new file with mode: 0644]

diff --git a/src/__tests__/convert-test.js b/src/__tests__/convert-test.js
new file mode 100644 (file)
index 0000000..5df1fca
--- /dev/null
@@ -0,0 +1,92 @@
+/**
+ * Copyright 2015-present Greg Hurrell. All rights reserved.
+ * Licensed under the terms of the MIT license.
+ */
+
+'use strict';
+
+import convert from '../convert';
+
+describe('convert()', () => {
+  describe('converting from binary', () => {
+    it('treats binary-to-binary as a no-op', () => {
+      expect(convert('1011001', 2, 2)).toBe('1011001');
+    });
+
+    it('converts binary to decimal', () => {
+    });
+
+    it('converts binary to octal', () => {
+    });
+
+    it('converts binary to hexadecimal', () => {
+    });
+
+    it('converts with arbitrary precision', () => {
+    });
+  });
+
+  describe('converting from decimal', () => {
+    it('treats decimal-to-decimal as a no-op', () => {
+      expect(convert('324234124', 10, 10)).toBe('324234124');
+    });
+
+    it('converts decimal to binary', () => {
+    });
+
+    it('converts decimal to octal', () => {
+    });
+
+    it('converts decimal to hexadecimal', () => {
+      expect(convert('255', 10, 16)).toBe('ff');
+      expect(convert('4277009102', 10, 16)).toBe('feedface');
+    });
+
+    it('converts with arbitrary precision', () => {
+    });
+  });
+
+  describe('converting from octal', () => {
+    it('treats octal-to-octal as a no-op', () => {
+      expect(convert('723147234234', 8, 8)).toBe('723147234234');
+    });
+
+    it('converts octal to binary', () => {
+    });
+
+    it('converts octal to decimal', () => {
+    });
+
+    it('converts octal to hexadecimal', () => {
+    });
+
+    it('converts with arbitrary precision', () => {
+    });
+  });
+
+  describe('converting form hexadecimal', () => {
+    it('treats hexadecimal-to-hexadecimal as a no-op', () => {
+      expect(convert('deadbeef', 16, 16)).toBe('deadbeef');
+    });
+
+    it('converts hexadecimal to binary', () => {
+    });
+
+    it('converts hexadecimal to decimal', () => {
+      expect(convert('ff', 16, 10)).toBe('255');
+      expect(convert('feedface', 16, 10)).toBe('4277009102');
+    });
+
+    it('converts hexadecimal to octal', () => {
+    });
+
+    it('converts with arbitrary precision', () => {
+      expect(convert('ab92b650b5f18fb53ee9f6229fd82638d069d9af', 16, 10))
+        .toBe('979509212510335489186816896981475731336057182639');
+      expect(convert('ab92b650b5f18fb53ee9f6229fd82638d069d9af', 16, 8))
+        .toBe('125622554502657430766517564766105177301143432032354657');
+      expect(convert('ab92b650b5f18fb53ee9f6229fd82638d069d9af', 16, 2))
+        .toBe('1010101110010010101101100101000010110101111100011000111110110101001111101110100111110110001000101001111111011000001001100011100');
+    });
+  });
+});
diff --git a/src/convert.js b/src/convert.js
new file mode 100644 (file)
index 0000000..75e8ee3
--- /dev/null
@@ -0,0 +1,37 @@
+/**
+ * Copyright 2015-present Greg Hurrell. All rights reserved.
+ * Licensed under the terms of the MIT license.
+ *
+ * @flow
+ */
+
+'use strict';
+
+import addDigits from './addDigits';
+import getDigits from './getDigits';
+import joinDigits from './joinDigits';
+import multiplyDigits from './multiplyDigits';
+
+/**
+ * Convert `number` in base `inBase`, to base `outBase`.
+ */
+export default function convert(
+  number: string,
+  inBase: number,
+  outBase: number
+): string {
+  const digits = getDigits(number, inBase);
+  let result = [0];
+  let power = [1];
+  for (let i = 0; i < digits.length; i++) {
+    if (digits[i]) {
+      result = addDigits(
+        result,
+        multiplyDigits(power, digits[i], outBase),
+        outBase
+      );
+    }
+    power = multiplyDigits(power, inBase, outBase);
+  }
+  return joinDigits(result, outBase);
+}