commit
message
[Convert] TO_DATE, TO_PERCENT, TO_DOLLAR, TO_TEXT placeholders and tests added
author
Ben Vogt <[email protected]>
date
2017-08-12 20:39:11
stats
8 file(s) changed,
264 insertions(+),
6 deletions(-)
files
DOCS.md
TODO.md
dist/Formulas/AllFormulas.js
dist/Formulas/Convert.js
src/Formulas/AllFormulas.ts
src/Formulas/Convert.ts
tests/Formulas/ConvertTest.ts
tests/SheetFormulaTest.ts
1diff --git a/DOCS.md b/DOCS.md
2index 50bf791..56aa751 100644
3--- a/DOCS.md
4+++ b/DOCS.md
5@@ -1,5 +1,40 @@
6 # Documentation
7
8+## Convert
9+
10+
11+### DATE
12+
13+```
14+ Converts a number to a Date.
15+@param value - Value to convert. If the input is a number, will convert to a date. If value is non-numeric, will return value unchanged.
16+@returns {any}
17+@constructor
18+```
19+
20+### DOLLARS
21+
22+```
23+ Converts a number to a Dollar value.
24+@param value - Value to convert. If the input is a number, will return as a dollar value. If value is non-numeric, will return value unchanged.
25+@returns {any}
26+@constructor
27+```
28+
29+### PERCENT
30+
31+```
32+ Converts a number to a percent value where 1 = 100
33+```
34+
35+### TEXT
36+
37+```
38+ Converts a number to a text value
39+@param value - Value to convert. If the input is a text, will return as a text value.
40+@returns {any}
41+@constructor
42+```
43 ## Date
44
45
46diff --git a/TODO.md b/TODO.md
47index d205a46..705a965 100644
48--- a/TODO.md
49+++ b/TODO.md
50@@ -2,7 +2,7 @@
51
52
53 ### Cells should have `formatAs` fields.
54-Instead of having non-primitives, (i.e. Date, DateTime, Time, Dollar), cells should have formats based on the highest-order type that was used during the compilation and execution of a cell's dependency. For example, `DATE` might return a number, but the cell that called `DATE` would be aware of it calling a formula that returns an non-primitive type, and would display the returned number as a Date. If you're using `DATE` in conjunction with `DOLLAR` it would still display the returned value as a Date. The hierarchy would look like: [Date, DateTime, Time, Dollar, number, boolean, string]. Advantages to this would include not having to cast down when using primitive operators, and flexibility in display. It would also simplify the types themselves, by having types be constants and just having helpers to convert, display, and do normal operations with them.
55+Instead of having non-primitives, (i.e. Date, DateTime, Time, Dollar), cells should have formats based on the highest-order type that was used during the compilation and execution of a cell's dependency. For example, `DATE` might return a number, but the cell that called `DATE` would be aware of it calling a formula that returns an non-primitive type, and would display the returned number as a Date. If you're using `DATE` in conjunction with `DOLLAR` it would still display the returned value as a Date. The hierarchy would look like: [Date, DateTime, Time, Dollar, number, boolean, string]. Advantages to this would include not having to cast down when using primitive operators, and flexibility in display. It would also simplify the types themselves, by having types be constants and just having helpers to convert, display, and do normal operations with them. Requires changes to `TO_DATE`, `TO_PERCENT`, `TO_DOLLAR`, and `TO_TEXT`.
56
57
58 ### Sheet should automatically parse some values, unless told otherwise.
59@@ -56,10 +56,6 @@ Many of these formulas can be written by allowing the Sheet and Parser to return
60 ### Formulas to write
61 * SERIESSUM
62 * SUBTOTAL
63-* TO_DATE - Contingent upon cells having display formats derived from type-hierarchy
64-* TO_DOLLARS - Contingent upon cells having display formats derived from type-hierarchy
65-* TO_PERCENT - Contingent upon cells having display formats derived from type-hierarchy
66-* TO_TEXT - Contingent upon cells having display formats derived from type-hierarchy
67 * CRITBINOM
68 * F.DIST.RT
69 * HYPGEOMDIST
70diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
71index 443a5b7..bb6c529 100644
72--- a/dist/Formulas/AllFormulas.js
73+++ b/dist/Formulas/AllFormulas.js
74@@ -100,6 +100,11 @@ exports.COLUMN = Info_1.COLUMN;
75 exports.ROW = Info_1.ROW;
76 var Lookup_1 = require("./Lookup");
77 exports.CHOOSE = Lookup_1.CHOOSE;
78+var Convert_1 = require("./Convert");
79+exports.TO_DATE = Convert_1.TO_DATE;
80+exports.TO_DOLLARS = Convert_1.TO_DOLLARS;
81+exports.TO_PERCENT = Convert_1.TO_PERCENT;
82+exports.TO_TEXT = Convert_1.TO_TEXT;
83 var Logical_1 = require("./Logical");
84 exports.AND = Logical_1.AND;
85 exports.EXACT = Logical_1.EXACT;
86diff --git a/dist/Formulas/Convert.js b/dist/Formulas/Convert.js
87new file mode 100644
88index 0000000..424289a
89--- /dev/null
90+++ b/dist/Formulas/Convert.js
91@@ -0,0 +1,56 @@
92+"use strict";
93+exports.__esModule = true;
94+var ArgsChecker_1 = require("../Utilities/ArgsChecker");
95+var TypeConverter_1 = require("../Utilities/TypeConverter");
96+/**
97+ * Converts a number to a Date.
98+ * @param value - Value to convert. If the input is a number, will convert to a date. If value is non-numeric, will
99+ * return value unchanged.
100+ * @returns {any}
101+ * @constructor
102+ */
103+var TO_DATE = function (value) {
104+ ArgsChecker_1.ArgsChecker.checkLength(arguments, 1, "TO_DATE");
105+ var v = TypeConverter_1.TypeConverter.firstValue(value);
106+ if (typeof v === "number") {
107+ return TypeConverter_1.TypeConverter.valueToDateNumber(v);
108+ }
109+ return v;
110+};
111+exports.TO_DATE = TO_DATE;
112+/**
113+ * Converts a number to a Dollar value.
114+ * @param value - Value to convert. If the input is a number, will return as a dollar value. If value is non-numeric,
115+ * will return value unchanged.
116+ * @returns {any}
117+ * @constructor
118+ */
119+var TO_DOLLARS = function (value) {
120+ ArgsChecker_1.ArgsChecker.checkLength(arguments, 1, "TO_DOLLAR");
121+ return TypeConverter_1.TypeConverter.firstValue(value);
122+};
123+exports.TO_DOLLARS = TO_DOLLARS;
124+/**
125+ * Converts a number to a percent value where 1 = 100%
126+ * @param value - Value to convert. If the input is a number, will return as a percent value. If value is non-numeric,
127+ * will return value unchanged.
128+ * @returns {any}
129+ * @constructor
130+ */
131+var TO_PERCENT = function (value) {
132+ ArgsChecker_1.ArgsChecker.checkLength(arguments, 1, "TO_PERCENT");
133+ return TypeConverter_1.TypeConverter.firstValue(value);
134+};
135+exports.TO_PERCENT = TO_PERCENT;
136+/**
137+ * Converts a number to a text value
138+ * @param value - Value to convert. If the input is a text, will return as a text value.
139+ * @returns {any}
140+ * @constructor
141+ */
142+var TO_TEXT = function (value) {
143+ ArgsChecker_1.ArgsChecker.checkLength(arguments, 1, "TO_TEXT");
144+ var v = TypeConverter_1.TypeConverter.firstValue(value);
145+ return TypeConverter_1.TypeConverter.valueToString(v);
146+};
147+exports.TO_TEXT = TO_TEXT;
148diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
149index 4533e77..2b4100e 100644
150--- a/src/Formulas/AllFormulas.ts
151+++ b/src/Formulas/AllFormulas.ts
152@@ -102,6 +102,12 @@ import {
153 import {
154 CHOOSE
155 } from "./Lookup";
156+import {
157+ TO_DATE,
158+ TO_DOLLARS,
159+ TO_PERCENT,
160+ TO_TEXT
161+} from "./Convert";
162 import {
163 AND,
164 EXACT,
165@@ -483,5 +489,9 @@ export {
166 RANK$AVG,
167 RANK$EQ,
168 LOGNORMDIST,
169- TDIST
170+ TDIST,
171+ TO_DATE,
172+ TO_DOLLARS,
173+ TO_PERCENT,
174+ TO_TEXT
175 }
176\ No newline at end of file
177diff --git a/src/Formulas/Convert.ts b/src/Formulas/Convert.ts
178new file mode 100644
179index 0000000..a362fa0
180--- /dev/null
181+++ b/src/Formulas/Convert.ts
182@@ -0,0 +1,66 @@
183+import {
184+ ArgsChecker
185+} from "../Utilities/ArgsChecker";
186+import {
187+ TypeConverter
188+} from "../Utilities/TypeConverter";
189+
190+/**
191+ * Converts a number to a Date.
192+ * @param value - Value to convert. If the input is a number, will convert to a date. If value is non-numeric, will
193+ * return value unchanged.
194+ * @returns {any}
195+ * @constructor
196+ */
197+var TO_DATE = function (value) {
198+ ArgsChecker.checkLength(arguments, 1, "TO_DATE");
199+ var v = TypeConverter.firstValue(value);
200+ if (typeof v === "number") {
201+ return TypeConverter.valueToDateNumber(v);
202+ }
203+ return v;
204+};
205+
206+/**
207+ * Converts a number to a Dollar value.
208+ * @param value - Value to convert. If the input is a number, will return as a dollar value. If value is non-numeric,
209+ * will return value unchanged.
210+ * @returns {any}
211+ * @constructor
212+ */
213+var TO_DOLLARS = function (value) {
214+ ArgsChecker.checkLength(arguments, 1, "TO_DOLLAR");
215+ return TypeConverter.firstValue(value);
216+};
217+
218+
219+/**
220+ * Converts a number to a percent value where 1 = 100%
221+ * @param value - Value to convert. If the input is a number, will return as a percent value. If value is non-numeric,
222+ * will return value unchanged.
223+ * @returns {any}
224+ * @constructor
225+ */
226+var TO_PERCENT = function (value) {
227+ ArgsChecker.checkLength(arguments, 1, "TO_PERCENT");
228+ return TypeConverter.firstValue(value);
229+};
230+
231+/**
232+ * Converts a number to a text value
233+ * @param value - Value to convert. If the input is a text, will return as a text value.
234+ * @returns {any}
235+ * @constructor
236+ */
237+var TO_TEXT = function (value) {
238+ ArgsChecker.checkLength(arguments, 1, "TO_TEXT");
239+ var v = TypeConverter.firstValue(value);
240+ return TypeConverter.valueToString(v);
241+};
242+
243+export {
244+ TO_DATE,
245+ TO_DOLLARS,
246+ TO_PERCENT,
247+ TO_TEXT
248+}
249\ No newline at end of file
250diff --git a/tests/Formulas/ConvertTest.ts b/tests/Formulas/ConvertTest.ts
251new file mode 100644
252index 0000000..c6ce01d
253--- /dev/null
254+++ b/tests/Formulas/ConvertTest.ts
255@@ -0,0 +1,74 @@
256+import {
257+ TO_DATE,
258+ TO_DOLLARS,
259+ TO_PERCENT,
260+ TO_TEXT
261+} from "../../src/Formulas/Convert";
262+import * as ERRORS from "../../src/Errors";
263+import {
264+ assertEquals,
265+ catchAndAssertEquals,
266+ test
267+} from "../Utils/Asserts";
268+
269+
270+test("TO_DATE", function(){
271+ assertEquals(TO_DATE(10), 10);
272+ assertEquals(TO_DATE(false), false);
273+ assertEquals(TO_DATE(true), true);
274+ assertEquals(TO_DATE(-100), -100);
275+ assertEquals(TO_DATE("June 10, 2010"), "June 10, 2010");
276+ assertEquals(TO_DATE("str"), "str");
277+ catchAndAssertEquals(function() {
278+ TO_DATE.apply(this, [10, 10]);
279+ }, ERRORS.NA_ERROR);
280+ catchAndAssertEquals(function() {
281+ TO_DATE.apply(this, [])
282+ }, ERRORS.NA_ERROR);
283+});
284+
285+test("TO_DOLLARS", function(){
286+ assertEquals(TO_DOLLARS(10), 10);
287+ assertEquals(TO_DOLLARS(false), false);
288+ assertEquals(TO_DOLLARS(true), true);
289+ assertEquals(TO_DOLLARS(-100), -100);
290+ assertEquals(TO_DOLLARS("June 10, 2010"), "June 10, 2010");
291+ assertEquals(TO_DOLLARS("str"), "str");
292+ catchAndAssertEquals(function() {
293+ TO_DOLLARS.apply(this, [10, 10]);
294+ }, ERRORS.NA_ERROR);
295+ catchAndAssertEquals(function() {
296+ TO_DOLLARS.apply(this, [])
297+ }, ERRORS.NA_ERROR);
298+});
299+
300+test("TO_PERCENT", function(){
301+ assertEquals(TO_PERCENT(10), 10);
302+ assertEquals(TO_PERCENT(false), false);
303+ assertEquals(TO_PERCENT(true), true);
304+ assertEquals(TO_PERCENT(-100), -100);
305+ assertEquals(TO_PERCENT("June 10, 2010"), "June 10, 2010");
306+ assertEquals(TO_PERCENT("str"), "str");
307+ catchAndAssertEquals(function() {
308+ TO_PERCENT.apply(this, [10, 10]);
309+ }, ERRORS.NA_ERROR);
310+ catchAndAssertEquals(function() {
311+ TO_PERCENT.apply(this, [])
312+ }, ERRORS.NA_ERROR);
313+});
314+
315+test("TO_TEXT", function(){
316+ assertEquals(TO_TEXT(10), "10");
317+ assertEquals(TO_TEXT(10.23984728937), "10.23984728937");
318+ assertEquals(TO_TEXT(false), "FALSE");
319+ assertEquals(TO_TEXT(true), "TRUE");
320+ assertEquals(TO_TEXT(-100), "-100");
321+ assertEquals(TO_TEXT("June 10, 2010"), "June 10, 2010");
322+ assertEquals(TO_TEXT("str"), "str");
323+ catchAndAssertEquals(function() {
324+ TO_TEXT.apply(this, [10, 10]);
325+ }, ERRORS.NA_ERROR);
326+ catchAndAssertEquals(function() {
327+ TO_TEXT.apply(this, [])
328+ }, ERRORS.NA_ERROR);
329+});
330diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
331index 5ce48f0..3621d58 100644
332--- a/tests/SheetFormulaTest.ts
333+++ b/tests/SheetFormulaTest.ts
334@@ -949,6 +949,22 @@ test("Sheet LOGNORMDIST", function(){
335 assertFormulaEquals('=TDIST(0.55, 1, 2)', 0.6798800684756632);
336 });
337
338+test("Sheet TO_DATE", function(){
339+ assertFormulaEquals('=TO_DATE(2)', 2);
340+});
341+
342+test("Sheet TO_DOLLARS", function(){
343+ assertFormulaEquals('=TO_DOLLARS(2)', 2);
344+});
345+
346+test("Sheet TO_PERCENT", function(){
347+ assertFormulaEquals('=TO_PERCENT(20)', 20);
348+});
349+
350+test("Sheet TO_TEXT", function(){
351+ assertFormulaEquals('=TO_TEXT(false)', "FALSE");
352+});
353+
354 test("Sheet *", function(){
355 assertFormulaEquals('= 10 * 10', 100);
356 assertFormulaEquals('= 10 * 0', 0);