commit
message
Formulas.SIN written and tested.
author
Ben Vogt <[email protected]>
date
2017-01-31 02:01:29
stats
3 file(s) changed,
46 insertions(+),
2 deletions(-)
files
src/RawFormulas/RawFormulas.ts
src/RawFormulas/Utils.ts
tests/FormulasTest.ts
1diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
2index 0869f6b..1a88485 100644
3--- a/src/RawFormulas/RawFormulas.ts
4+++ b/src/RawFormulas/RawFormulas.ts
5@@ -32,7 +32,7 @@ import {
6 TRUE,
7 NOT
8 } from "./Logical"
9-import {checkArgumentsAtLeastLength, filterOutStringValues, valueToNumber} from "./Utils";
10+import {checkArgumentsAtLeastLength, filterOutStringValues, valueToNumber, checkArgumentsLength, firstValueAsNumber} from "./Utils";
11 import { CellError } from "../Errors"
12 import * as ERRORS from "../Errors"
13
14@@ -129,9 +129,19 @@ var POWER = Formula["POWER"];
15 var ROUND = Formula["ROUND"];
16 var ROUNDDOWN = Formula["ROUNDDOWN"];
17 var ROUNDUP = Formula["ROUNDUP"];
18-var SIN = function (rad) {
19+
20+/**
21+ * Returns the sine of an angle provided in radians.
22+ * @param values[0] The angle to find the sine of, in radians.
23+ * @returns {number} Sine of angle.
24+ * @constructor
25+ */
26+var SIN = function (...values) {
27+ checkArgumentsLength(values, 1);
28+ var rad = firstValueAsNumber(values[0]);
29 return rad === Math.PI ? 0 : Math.sin(rad);
30 };
31+
32 var SINH = Formula["SINH"];
33 var SPLIT = Formula["SPLIT"];
34 var SQRT = Formula["SQRT"];
35diff --git a/src/RawFormulas/Utils.ts b/src/RawFormulas/Utils.ts
36index 16351b4..e973a69 100644
37--- a/src/RawFormulas/Utils.ts
38+++ b/src/RawFormulas/Utils.ts
39@@ -55,6 +55,21 @@ function valueToString(value: any) : string {
40 }
41 }
42
43+/**
44+ * Takes any input type and will throw a REF_ERROR or coerce it into a number.
45+ * @param input to attempt to coerce into a number
46+ * @returns {number} number representation of the input
47+ */
48+function firstValueAsNumber(input: any) : number {
49+ if (input instanceof Array) {
50+ if (input.length === 0) {
51+ throw new CellError(ERRORS.REF_ERROR, "Reference does not exist.");
52+ }
53+ return firstValueAsNumber(input[0]);
54+ }
55+ return valueToNumber(input);
56+}
57+
58
59 /**
60 * Converts any value to a number or throws an error if it cannot coerce it to the number type
61@@ -65,6 +80,9 @@ function valueToNumber(value: any) : number {
62 if (typeof value === "number") {
63 return value;
64 } else if (typeof value === "string") {
65+ if (value === "") {
66+ return 0;
67+ }
68 if (value.indexOf(".") > -1) {
69 var fl = parseFloat(value);
70 if (isNaN(fl)) {
71@@ -119,6 +137,7 @@ export {
72 flatten,
73 valueToNumber,
74 valueToString,
75+ firstValueAsNumber,
76 filterOutStringValues,
77 checkArgumentsAtLeastLength,
78 checkArgumentsLength
79diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
80index b7b6a90..d504414 100644
81--- a/tests/FormulasTest.ts
82+++ b/tests/FormulasTest.ts
83@@ -11,6 +11,7 @@ import { ABS, ACCRINT, ACOS, ACOSH, ACOTH, AND, ARABIC, ASIN, ASINH, ATAN, ATAN2
84 SUMSQ, SUMX2MY2, SUMX2PY2, TAN, TANH, TRUNC, XOR, YEARFRAC } from "../src/RawFormulas/RawFormulas"
85 import * as ERRORS from "../src/Errors"
86 import {assertEquals, assertEqualsDates, assertArrayEquals} from "./utils/Asserts"
87+import {firstValueAsNumber} from "../src/RawFormulas/Utils";
88
89 function catchAndAssertEquals(toExecute, expected) {
90 try {
91@@ -648,10 +649,23 @@ assertEquals(ROUNDDOWN(99.46, 1), 99.4);
92
93 assertEquals(ROUNDUP(99.46, 1), 99.5);
94
95+
96+// Test SIN
97 assertEquals(SIN(0), 0);
98 assertEquals(SIN(1), 0.8414709848078965);
99 assertEquals(SIN(PI() / 2), 1);
100 assertEquals(SIN(PI()), 0);
101+assertEquals(SIN(true), 0.8414709848078965);
102+assertEquals(SIN(false), 0);
103+assertEquals(SIN("0"), 0);
104+assertEquals(SIN(""), 0);
105+catchAndAssertEquals(function() {
106+ SIN("str");
107+}, ERRORS.VALUE_ERROR);
108+assertEquals(SIN([1]), 0.8414709848078965);
109+assertEquals(SIN([[1]]), 0.8414709848078965);
110+assertEquals(SIN([1, "str"]), 0.8414709848078965);
111+
112
113 assertEquals(SINH(PI()), 11.548739357257752);
114