spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Added Formulas.BIN2OCT
author
Ben Vogt <[email protected]>
date
2017-02-18 05:52:16
stats
2 file(s) changed, 73 insertions(+), 2 deletions(-)
files
src/RawFormulas/RawFormulas.ts
tests/FormulasTest.ts
  1diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
  2index 6da8d24..5f3f58b 100644
  3--- a/src/RawFormulas/RawFormulas.ts
  4+++ b/src/RawFormulas/RawFormulas.ts
  5@@ -83,7 +83,6 @@ import * as ERRORS from "../Errors"
  6 import {Cell} from "../Cell";
  7 
  8 var ACCRINT = Formula["ACCRINT"];
  9-var BIN2OCT = Formula["BIN2OCT"];
 10 var DECIMAL = Formula["DECIMAL"];
 11 var COMBIN = Formula["COMBIN"];
 12 var CONVERT = Formula["CONVERT"];
 13@@ -200,6 +199,53 @@ var BIN2HEX = function (...values) : string {
 14 };
 15 
 16 
 17+/**
 18+ * Converts a signed binary number to signed octal format.
 19+ * @param values[0] signed_binary_number - The signed 10-bit binary value to be converted to signed octal, provided as a
 20+ * string. The most significant bit of signed_binary_number is the sign bit; that is, negative numbers are represented
 21+ * in two's complement format.
 22+ * @param values[1] significant_digits - [ OPTIONAL ] - The number of significant digits to ensure in the result. If
 23+ * this is greater than the number of significant digits in the result, the result is left-padded with zeros until the
 24+ * total number of digits reaches significant_digits.
 25+ * @returns {string} number in octal format
 26+ * @constructor
 27+ */
 28+var BIN2OCT = function (...values) : string {
 29+  ArgsChecker.checkLengthWithin(values, 1, 2);
 30+  if (typeof TypeCaster.firstValue(values[0]) === "boolean") {
 31+    throw new CellError(ERRORS.VALUE_ERROR, "Function BIN2OCT parameter 1 expects text values. But '" + values[0] + "' is a boolean and cannot be coerced to a text.");
 32+  }
 33+  var n = TypeCaster.firstValueAsString(values[0]);
 34+  var p = 10;
 35+  if (values.length === 2) {
 36+    p = TypeCaster.firstValueAsNumber(values[1]);
 37+  }
 38+  if (!(/^[01]{1,10}$/).test(n)) {
 39+    throw new CellError(ERRORS.NUM_ERROR, "Input for BIN2OCT ('"+n+"') is not a valid binary representation.");
 40+  }
 41+
 42+  if (n.length === 10 && n.substring(0, 1) === '1') {
 43+    return (1073741312 + parseInt(n.substring(1), 2)).toString(8);
 44+  }
 45+
 46+  if (p < 1 || p > 10) {
 47+    throw new CellError(ERRORS.NUM_ERROR, "Function BIN2OCT parameter 2 value is " + p + ". Valid values are between 1 and 10 inclusive.");
 48+  }
 49+  p = Math.floor(p);
 50+  var result = parseInt(n.toString(), 2).toString(8);
 51+  if (p === 10) {
 52+    return result;
 53+  }
 54+  if (p >= result.length) {
 55+    var str = "";
 56+    for (var i = 0; i < p - result.length - 1; i++) {
 57+      str += "0";
 58+    }
 59+    return str + result;
 60+  }
 61+};
 62+
 63+
 64 
 65 /**
 66  * Converts an angle value in degrees to radians.
 67diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
 68index bcfaca4..7667234 100644
 69--- a/tests/FormulasTest.ts
 70+++ b/tests/FormulasTest.ts
 71@@ -329,7 +329,32 @@ catchAndAssertEquals(function() {
 72 }, ERRORS.NA_ERROR);
 73 
 74 
 75-assertEquals(BIN2OCT(1010101010), "7777777252");
 76+// Test BIN2OCT
 77+assertEquals(BIN2OCT("1010101010"), "7777777252");
 78+assertEquals(BIN2OCT("10"), "2");
 79+assertEquals(BIN2OCT("100"), "4");
 80+assertEquals(BIN2OCT("10101010"), "252");
 81+assertEquals(BIN2OCT("10101010", 4), "252");
 82+assertEquals(BIN2OCT(["10101010"], [4]), "252");
 83+catchAndAssertEquals(function() {
 84+  BIN2OCT("10101010", 22);
 85+}, ERRORS.NUM_ERROR);
 86+catchAndAssertEquals(function() {
 87+  BIN2OCT(false);
 88+}, ERRORS.VALUE_ERROR);
 89+catchAndAssertEquals(function() {
 90+  BIN2OCT("10101010", 0);
 91+}, ERRORS.NUM_ERROR);
 92+catchAndAssertEquals(function() {
 93+  BIN2OCT("str");
 94+}, ERRORS.NUM_ERROR);
 95+catchAndAssertEquals(function() {
 96+  BIN2OCT();
 97+}, ERRORS.NA_ERROR);
 98+catchAndAssertEquals(function() {
 99+  BIN2OCT("10", 4, 4);
100+}, ERRORS.NA_ERROR);
101+
102 
103 assertEquals(DECIMAL(199.99999), 199);
104