commit
message
Formulas.ARABIC written and tested.
author
Ben Vogt <[email protected]>
date
2017-01-16 23:41:39
stats
2 file(s) changed,
42 insertions(+),
0 deletions(-)
files
src/RawFormulas.ts
tests/FormulasTest.ts
1diff --git a/src/RawFormulas.ts b/src/RawFormulas.ts
2index 7aee389..e9172dc 100644
3--- a/src/RawFormulas.ts
4+++ b/src/RawFormulas.ts
5@@ -198,7 +198,36 @@ var AND = function (...values) {
6 };
7
8
9-var ARABIC = Formula["ARABIC"];
10+/**
11+ * Computes the value of a Roman numeral.
12+ * @param text The Roman numeral to format, whose value must be between 1 and 3999, inclusive.
13+ * @returns {number} value in integer format
14+ * @constructor
15+ */
16+var ARABIC = function (text?) {
17+ checkArgumentsLength(arguments, 1);
18+ if (typeof text !== "string") {
19+ throw new CellError(ERRORS.VALUE_ERROR, 'Invalid roman numeral in ARABIC evaluation.');
20+ }
21+ var negative = false;
22+ if (text[0] === "-") {
23+ negative = true;
24+ text = text.substr(1);
25+ }
26+ // Credits: Rafa? Kukawski
27+ if (!/^M*(?:D?C{0,3}|C[MD])(?:L?X{0,3}|X[CL])(?:V?I{0,3}|I[XV])$/.test(text)) {
28+ throw new CellError(ERRORS.VALUE_ERROR, 'Invalid roman numeral in ARABIC evaluation.');
29+ }
30+ var r = 0;
31+ text.replace(/[MDLV]|C[MD]?|X[CL]?|I[XV]?/g, function (i) {
32+ r += {M: 1000, CM: 900, D: 500, CD: 400, C: 100, XC: 90, L: 50, XL: 40, X: 10, IX: 9, V: 5, IV: 4, I: 1}[i];
33+ });
34+ if (negative) {
35+ return r * -1;
36+ }
37+ return r;
38+};
39+
40 var ASIN = Formula["ASIN"];
41 var ASINH = Formula["ASINH"];
42 var ATAN = Formula["ATAN"];
43diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
44index f3dafd0..0b3f409 100644
45--- a/tests/FormulasTest.ts
46+++ b/tests/FormulasTest.ts
47@@ -106,7 +106,19 @@ catchAndAssertEquals(function() {
48 }, ERRORS.VALUE_ERROR);
49
50
51+// Test ARABIC
52 assertEquals(ARABIC("XIV"), 14);
53+assertEquals(ARABIC("M"), 1000);
54+assertEquals(ARABIC("-IV"), -4);
55+catchAndAssertEquals(function() {
56+ return ARABIC("b");
57+}, ERRORS.VALUE_ERROR);
58+catchAndAssertEquals(function() {
59+ return ARABIC(false);
60+}, ERRORS.VALUE_ERROR);
61+catchAndAssertEquals(function() {
62+ return ARABIC(10);
63+}, ERRORS.VALUE_ERROR);
64
65 assertEquals(ASIN(0.1), 0.1001674211615598);
66