commit
message
[All] Removing last references to ExcelDate. ExcelDate is dead! Long live numbers!
author
Ben Vogt <[email protected]>
date
2017-05-06 00:56:03
stats
5 file(s) changed,
28 insertions(+),
70 deletions(-)
files
README.md
src/ExcelDate.ts
src/Formulas/Date.ts
src/Utilities/TypeCaster.ts
tests/utils/Asserts.ts
1diff --git a/README.md b/README.md
2index 1806b7b..3096f52 100644
3--- a/README.md
4+++ b/README.md
5@@ -30,11 +30,11 @@ Pass name of calling formula into all functions that throw user-facing errors, o
6
7
8 ### Cells should have `formatAs` fields.
9-Instead of having non-primitives, (i.e. ExcelDate, ExcelTime, Dollar), cells should have formats based on the
10+Instead of having non-primitives, (i.e. ExcelTime, Dollar), cells should have formats based on the
11 highest-order type that was used during the compilation and execution of a cell's dependency. For example, `DATE` might
12 return a number, but the cell that called `DATE` would be aware of it calling a formula that returns an non-primative
13 type, and would display the returned number as a Date. If you're using `DATE` in conjunction with `DOLLAR` it would
14-still display the returned value as a Date. The heirarhchy would look like: [ExcelDate, ExcelTime, Dollar, number,
15+still display the returned value as a Date. The heirarhchy would look like: [Date, DateTime, Time, Dollar, number,
16 boolean, string]. Advantages to this would include not having to cast down when using primitive operators,
17 and flexibility in display. It would also simplify the types themselves, by having types be constants and just having
18 helpers to convert, display, and do normal operations with them.
19@@ -44,7 +44,7 @@ helpers to convert, display, and do normal operations with them.
20 Contingent upon cells having formats or types for primitives.
21
22
23-### Test all ExcelDate functions
24+### Test all Date-related functions
25 Right now we're just using the number of days since 1900, but we should check the other functions.
26
27
28diff --git a/src/ExcelDate.ts b/src/ExcelDate.ts
29deleted file mode 100644
30index cd30f53..0000000
31--- a/src/ExcelDate.ts
32+++ /dev/null
33@@ -1,50 +0,0 @@
34-/// <reference path="../node_modules/moment/moment.d.ts"/>
35-import * as moment from "moment";
36-
37-const ORIGIN_MOMENT = moment.utc([1899, 11, 30]).startOf("day");
38-const SECONDS_IN_DAY = 86400;
39-
40-/**
41- * Date that mimics the functionality of an Excel Date. Represented by the number of days since 1900/1/1.
42- */
43-class ExcelDate {
44- private seconds : number;
45-
46- /**
47- * Constructs an ExcelDate when given a day or moment.
48- * @param m Moment to use as the day.
49- */
50- constructor(m : moment.Moment) {
51- this.seconds = m.diff(ORIGIN_MOMENT, "seconds");
52- }
53-
54- /**
55- * String representation of the day in the format M/D/YYYY. Eg: 6/24/1992
56- * @returns {string} day in the format M/D/YYYY.
57- */
58- toString() : string {
59- return moment.utc(ORIGIN_MOMENT).add(this.toNumber(), 'days').format("M/D/Y").toString();
60- }
61-
62- /**
63- * Returns the day as a number of days since 1900/1/1, inclusively on both ends.
64- * @returns {number} days since 1900/1/1
65- */
66- toNumber() {
67- return this.seconds / SECONDS_IN_DAY;
68- }
69-
70- /**
71- * Tests equality.
72- * @param ed other ExcelDate to compare to
73- * @returns {boolean} true if equals
74- */
75- equals(ed : ExcelDate) : boolean {
76- return this.toNumber() === ed.toNumber();
77- }
78-}
79-
80-export {
81- ExcelDate,
82- ORIGIN_MOMENT
83-}
84\ No newline at end of file
85diff --git a/src/Formulas/Date.ts b/src/Formulas/Date.ts
86index b524986..c54ac6b 100644
87--- a/src/Formulas/Date.ts
88+++ b/src/Formulas/Date.ts
89@@ -69,7 +69,7 @@ var DATEVALUE = function (...values) : number {
90 * Returns a date a specified number of months before or after another date.
91 * @param values[0] start_date - The date from which to calculate the result.
92 * @param values[1] months - The number of months before (negative) or after (positive) start_date to calculate.
93- * @returns {ExcelDate} date a specified number of months before or after another date
94+ * @returns {number} date a specified number of months before or after another date
95 * @constructor
96 */
97 var EDATE = function (...values) : number {
98@@ -91,7 +91,7 @@ var EDATE = function (...values) : number {
99 * @param values[0] start_date - The date from which to calculate the the result.
100 * @param values[1] months - The number of months before (negative) or after (positive) start_date to consider. The last
101 * calendar day of the calculated month is returned.
102- * @returns {ExcelDate} the last day of a month
103+ * @returns {number} the last day of a month
104 * @constructor
105 */
106 var EOMONTH = function (...values) : number {
107@@ -767,7 +767,7 @@ var NETWORKDAYS$INTL = function (...values) : number {
108
109 /**
110 * Returns the current date and time as a date value.
111- * @returns {ExcelDate} representing the current date and time.
112+ * @returns {number} representing the current date and time.
113 * @constructor
114 */
115 var NOW = function (...values) : number {
116@@ -777,7 +777,7 @@ var NOW = function (...values) : number {
117
118 /**
119 * Returns the current date as a date value.
120- * @returns {ExcelDate} today
121+ * @returns {number} today
122 * @constructor
123 */
124 var TODAY = function (...values) : number {
125@@ -817,7 +817,7 @@ var TIME = function (...values) : number {
126 * values provided within an array for holidays must be date serial number values, as returned by N or date values, as
127 * returned by DATE, DATEVALUE or TO_DATE. Values specified by a range should be standard date values or date serial
128 * numbers.
129- * @returns {ExcelDate} end date after a specified number of working days.
130+ * @returns {number} end date after a specified number of working days.
131 * @constructor
132 */
133 var WORKDAY = function (...values) : number {
134@@ -869,7 +869,7 @@ var WORKDAY = function (...values) : number {
135 * pattern repeats until 7 = Friday/Saturday. 11 = Sunday is the only weekend, 12 = Monday is the only weekend, and this
136 * pattern repeats until 17 = Saturday is the only weekend.
137 * @param values[3] holidays - [ OPTIONAL ] - A range or array constant containing the dates to consider holidays.
138- * @returns {ExcelDate}
139+ * @returns {number}
140 * @constructor
141 */
142 var WORKDAY$INTL = function (...values) : number {
143diff --git a/src/Utilities/TypeCaster.ts b/src/Utilities/TypeCaster.ts
144index b9aafb4..0a53eaa 100644
145--- a/src/Utilities/TypeCaster.ts
146+++ b/src/Utilities/TypeCaster.ts
147@@ -562,14 +562,29 @@ class TypeCaster {
148 }
149 }
150
151+ /**
152+ * Casts a moment to a date number.
153+ * @param m to convert
154+ * @returns {number} date
155+ */
156 static momentToNumber(m : moment.Moment) : number {
157 return m.diff(this.ORIGIN_MOMENT, "seconds") / this.SECONDS_IN_DAY;
158 }
159
160+ /**
161+ * Casts a moment to a date number, floored to the whole day date.
162+ * @param m to convert
163+ * @returns {number} date
164+ */
165 static momentToDayNumber(m : moment.Moment) : number {
166 return Math.floor(TypeCaster.momentToNumber(m));
167 }
168
169+ /**
170+ * Casts a number to moment.
171+ * @param n to convert
172+ * @returns {Moment} date
173+ */
174 static numberToMoment(n : number) : moment.Moment {
175 return moment.utc(TypeCaster.ORIGIN_MOMENT).add(n, "days");
176 }
177diff --git a/tests/utils/Asserts.ts b/tests/utils/Asserts.ts
178index 65e96be..2740d3e 100644
179--- a/tests/utils/Asserts.ts
180+++ b/tests/utils/Asserts.ts
181@@ -1,22 +1,12 @@
182-import {
183- ExcelDate
184-} from "../../src/ExcelDate";
185 /**
186 * Assert two params are equal using strict equality testing.
187 * @param actual value
188 * @param expected value
189 */
190 function assertEquals(actual, expected) {
191- if (actual instanceof ExcelDate && expected instanceof ExcelDate) {
192- if (!actual.equals(expected)) {
193- console.log("expected:", expected.toString(), " actual:", actual.toString());
194- console.trace();
195- }
196- } else {
197- if (expected !== actual) {
198- console.log("expected:", expected, " actual:", actual);
199- console.trace();
200- }
201+ if (expected !== actual) {
202+ console.log("expected:", expected, " actual:", actual);
203+ console.trace();
204 }
205 }
206