spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[Date.ts] ExcelDate no longer used in TypeCaster.ts
author
Ben Vogt <[email protected]>
date
2017-05-06 00:51:34
stats
4 file(s) changed, 47 insertions(+), 67 deletions(-)
files
src/ExcelDate.ts
src/Formulas/Date.ts
src/Formulas/Financial.ts
src/Utilities/TypeCaster.ts
  1diff --git a/src/ExcelDate.ts b/src/ExcelDate.ts
  2index c60bf13..cd30f53 100644
  3--- a/src/ExcelDate.ts
  4+++ b/src/ExcelDate.ts
  5@@ -10,10 +10,6 @@ const SECONDS_IN_DAY = 86400;
  6 class ExcelDate {
  7   private seconds : number;
  8 
  9-  static fromDay(day : number) {
 10-    return new ExcelDate(moment.utc(ORIGIN_MOMENT).add(day, 'days'));
 11-  }
 12-
 13   /**
 14    * Constructs an ExcelDate when given a day or moment.
 15    * @param m Moment to use as the day.
 16@@ -38,18 +34,6 @@ class ExcelDate {
 17     return this.seconds / SECONDS_IN_DAY;
 18   }
 19 
 20-  toNumberFloored() {
 21-    return Math.floor(this.toNumber());
 22-  }
 23-
 24-  /**
 25-   * Converts to a moment
 26-   * @returns {Moment}
 27-   */
 28-  toMoment() : moment.Moment {
 29-    return moment.utc(ORIGIN_MOMENT).add(this.toNumber(), "days");
 30-  }
 31-
 32   /**
 33    * Tests equality.
 34    * @param ed other ExcelDate to compare to
 35diff --git a/src/Formulas/Date.ts b/src/Formulas/Date.ts
 36index ad9d098..b524986 100644
 37--- a/src/Formulas/Date.ts
 38+++ b/src/Formulas/Date.ts
 39@@ -11,9 +11,6 @@ import {
 40   ValueError,
 41   RefError
 42 } from "../Errors";
 43-import {
 44-  ORIGIN_MOMENT
 45-} from "../ExcelDate";
 46 import {
 47   ExcelTime
 48 } from "../ExcelTime";
 49@@ -32,7 +29,7 @@ var DATE = function (...values) : number {
 50   var year = Math.abs(Math.floor(TypeCaster.firstValueAsNumber(values[0]))); // No negative values for year
 51   var month = Math.floor(TypeCaster.firstValueAsNumber(values[1])) - 1; // Months are between 0 and 11.
 52   var day = Math.floor(TypeCaster.firstValueAsNumber(values[2])) - 1; // Days are also zero-indexed.
 53-  var m = moment.utc(ORIGIN_MOMENT)
 54+  var m = moment.utc(TypeCaster.ORIGIN_MOMENT)
 55     .add(2, "days")
 56     .add(year < FIRST_YEAR ? year : year - FIRST_YEAR, 'years') // If the value is less than 1900, assume 1900 as start index for year
 57     .add(month, 'months')
 58@@ -58,7 +55,7 @@ var DATEVALUE = function (...values) : number {
 59   var dateString = TypeCaster.firstValueAsString(values[0]);
 60   var dateAsNumber;
 61   try {
 62-    dateAsNumber = TypeCaster.stringToExcelDate(dateString);
 63+    dateAsNumber = TypeCaster.stringToDateNumber(dateString);
 64   } catch (e) {
 65     throw new ValueError("DATEVALUE parameter '" + dateString + "' cannot be parsed to date/time.");
 66   }
 67@@ -77,14 +74,14 @@ var DATEVALUE = function (...values) : number {
 68  */
 69 var EDATE = function (...values) : number {
 70   ArgsChecker.checkLength(values, 2);
 71-  var startDateNumber = TypeCaster.firstValueAsExcelDate(values[0], true); // tell firstValueAsExcelDate to coerce boolean
 72+  var startDateNumber = TypeCaster.firstValueAsDateNumber(values[0], true); // tell firstValueAsDateNumber to coerce boolean
 73   if (startDateNumber < 0) {
 74     throw new NumError("Function EDATE parameter 1 value is " + startDateNumber+ ". It should be greater than or equal to 0.");
 75   }
 76   var months = Math.floor(TypeCaster.firstValueAsNumber(values[1]));
 77   // While momentToDayNumber will return an inclusive count of days since 1900/1/1, moment.Moment.add assumes exclusive
 78   // count of days.
 79-  return TypeCaster.momentToDayNumber(moment.utc(ORIGIN_MOMENT).add(startDateNumber, "days").add(months, "months"));
 80+  return TypeCaster.momentToDayNumber(moment.utc(TypeCaster.ORIGIN_MOMENT).add(startDateNumber, "days").add(months, "months"));
 81 };
 82 
 83 
 84@@ -99,12 +96,12 @@ var EDATE = function (...values) : number {
 85  */
 86 var EOMONTH = function (...values) : number {
 87   ArgsChecker.checkLength(values, 2);
 88-  var startDateNumber = TypeCaster.firstValueAsExcelDate(values[0], true); // tell firstValueAsExcelDate to coerce boolean
 89+  var startDateNumber = TypeCaster.firstValueAsDateNumber(values[0], true); // tell firstValueAsDateNumber to coerce boolean
 90   if (startDateNumber < 0) {
 91     throw new NumError("Function EOMONTH parameter 1 value is " + startDateNumber + ". It should be greater than or equal to 0.");
 92   }
 93   var months = Math.floor(TypeCaster.firstValueAsNumber(values[1]));
 94-  return TypeCaster.momentToDayNumber(moment.utc(ORIGIN_MOMENT)
 95+  return TypeCaster.momentToDayNumber(moment.utc(TypeCaster.ORIGIN_MOMENT)
 96     .add(startDateNumber, "days")
 97     .add(months, "months")
 98     .endOf("month"));
 99@@ -120,7 +117,7 @@ var EOMONTH = function (...values) : number {
100  */
101 var DAY = function (...values) : number {
102   ArgsChecker.checkLength(values, 1);
103-  var dateNumber = TypeCaster.firstValueAsExcelDate(values[0], true); // tell firstValueAsExcelDate to coerce boolean
104+  var dateNumber = TypeCaster.firstValueAsDateNumber(values[0], true); // tell firstValueAsDateNumber to coerce boolean
105   if (dateNumber < 0) {
106     throw new NumError("Function DAY parameter 1 value is " + dateNumber + ". It should be greater than or equal to 0.");
107   }
108@@ -137,8 +134,8 @@ var DAY = function (...values) : number {
109  */
110 var DAYS = function (...values) : number {
111   ArgsChecker.checkLength(values, 2);
112-  var end = TypeCaster.firstValueAsExcelDate(values[0], true); // tell firstValueAsExcelDate to coerce boolean
113-  var start = TypeCaster.firstValueAsExcelDate(values[1], true); // tell firstValueAsExcelDate to coerce boolean
114+  var end = TypeCaster.firstValueAsDateNumber(values[0], true); // tell firstValueAsDateNumber to coerce boolean
115+  var start = TypeCaster.firstValueAsDateNumber(values[1], true); // tell firstValueAsDateNumber to coerce boolean
116   return end - start;
117 };
118 
119@@ -161,8 +158,8 @@ var DAYS = function (...values) : number {
120  */
121 var DAYS360 = function (...values) : number {
122   ArgsChecker.checkLengthWithin(values, 2, 3);
123-  var start = TypeCaster.numberToMoment(TypeCaster.firstValueAsExcelDate(values[0], true)); // tell firstValueAsExcelDate to coerce boolean
124-  var end = TypeCaster.numberToMoment(TypeCaster.firstValueAsExcelDate(values[1], true)); // tell firstValueAsExcelDate to coerce boolean
125+  var start = TypeCaster.numberToMoment(TypeCaster.firstValueAsDateNumber(values[0], true)); // tell firstValueAsDateNumber to coerce boolean
126+  var end = TypeCaster.numberToMoment(TypeCaster.firstValueAsDateNumber(values[1], true)); // tell firstValueAsDateNumber to coerce boolean
127   var methodToUse = false;
128   if (values.length === 3) {
129     methodToUse = TypeCaster.firstValueAsBoolean(values[2]);
130@@ -200,7 +197,7 @@ var DAYS360 = function (...values) : number {
131  */
132 var MONTH = function (...values) : number {
133   ArgsChecker.checkLength(values, 1);
134-  var date = TypeCaster.firstValueAsExcelDate(values[0], true); // tell firstValueAsExcelDate to coerce boolean
135+  var date = TypeCaster.firstValueAsDateNumber(values[0], true); // tell firstValueAsDateNumber to coerce boolean
136   if (date < 0) {
137     throw new NumError("Function MONTH parameter 1 value is " + date + ". It should be greater than or equal to 0.");
138   }
139@@ -217,7 +214,7 @@ var MONTH = function (...values) : number {
140  */
141 var YEAR = function (...values) : number {
142   ArgsChecker.checkLength(values, 1);
143-  var date = TypeCaster.firstValueAsExcelDate(values[0], true); // tell firstValueAsExcelDate to coerce boolean
144+  var date = TypeCaster.firstValueAsDateNumber(values[0], true); // tell firstValueAsDateNumber to coerce boolean
145   if (date < 0) {
146     throw new NumError("Function YEAR parameter 1 value is " + date + ". It should be greater than or equal to 0.");
147   }
148@@ -239,7 +236,7 @@ var YEAR = function (...values) : number {
149  */
150 var WEEKDAY = function (...values) : number {
151   ArgsChecker.checkLengthWithin(values, 1, 2);
152-  var date = TypeCaster.firstValueAsExcelDate(values[0], true); // tell firstValueAsExcelDate to coerce boolean
153+  var date = TypeCaster.firstValueAsDateNumber(values[0], true); // tell firstValueAsDateNumber to coerce boolean
154   var offsetType = values.length === 2 ? TypeCaster.firstValueAsNumber(values[1]) : 1;
155   if (date < 0) {
156     throw new NumError("Function WEEKDAY parameter 1 value is " + date + ". It should be greater than or equal to 0.");
157@@ -298,7 +295,7 @@ function calculateWeekNum(dm : moment.Moment, shifterArray : Array<number>) : nu
158  */
159 var WEEKNUM = function (...values) : number {
160   ArgsChecker.checkLengthWithin(values, 1, 2);
161-  var date = TypeCaster.firstValueAsExcelDate(values[0], true); // tell firstValueAsExcelDate to coerce boolean
162+  var date = TypeCaster.firstValueAsDateNumber(values[0], true); // tell firstValueAsDateNumber to coerce boolean
163   var shiftType = values.length === 2 ? TypeCaster.firstValueAsNumber(values[1]) : 1;
164   if (date < 0) {
165     throw new NumError("Function YEAR parameter 1 value is " + date + ". It should be greater than or equal to 0.");
166@@ -370,8 +367,8 @@ var WEEKNUM = function (...values) : number {
167  */
168 var DATEDIF = function (...values) : number {
169   ArgsChecker.checkLength(values, 3);
170-  var start = TypeCaster.firstValueAsExcelDate(values[0], true);
171-  var end = TypeCaster.firstValueAsExcelDate(values[1], true);
172+  var start = TypeCaster.firstValueAsDateNumber(values[0], true);
173+  var end = TypeCaster.firstValueAsDateNumber(values[1], true);
174   var unit = TypeCaster.firstValueAsString(values[2]);
175   var unitClean = unit.toUpperCase();
176   var startMoment = TypeCaster.numberToMoment(start);
177@@ -439,8 +436,8 @@ var DATEDIF = function (...values) : number {
178  */
179 var YEARFRAC = function (...values) : number {
180   ArgsChecker.checkLengthWithin(values, 2, 3);
181-  var start = TypeCaster.firstValueAsExcelDate(values[0], true);
182-  var end = TypeCaster.firstValueAsExcelDate(values[1], true);
183+  var start = TypeCaster.firstValueAsDateNumber(values[0], true);
184+  var end = TypeCaster.firstValueAsDateNumber(values[1], true);
185   var basis = values.length === 2 ? 0 : TypeCaster.firstValueAsNumber(values[2]);
186 
187   var s = TypeCaster.numberToMoment(start);
188@@ -610,8 +607,8 @@ var SECOND = function (...values) : number {
189  */
190 var NETWORKDAYS = function (...values) : number {
191   ArgsChecker.checkLengthWithin(values, 2, 3);
192-  var start = TypeCaster.firstValueAsExcelDate(values[0], true);
193-  var end = TypeCaster.firstValueAsExcelDate(values[1], true);
194+  var start = TypeCaster.firstValueAsDateNumber(values[0], true);
195+  var end = TypeCaster.firstValueAsDateNumber(values[1], true);
196   var hasHolidays = values.length === 3;
197   var holidays = [];
198   if (hasHolidays) {
199@@ -676,8 +673,8 @@ var NETWORKDAYS = function (...values) : number {
200  */
201 var NETWORKDAYS$INTL = function (...values) : number {
202   ArgsChecker.checkLengthWithin(values, 2, 4);
203-  var start = TypeCaster.firstValueAsExcelDate(values[0], true);
204-  var end = TypeCaster.firstValueAsExcelDate(values[1], true);
205+  var start = TypeCaster.firstValueAsDateNumber(values[0], true);
206+  var end = TypeCaster.firstValueAsDateNumber(values[1], true);
207   var weekendDays = [];
208   if (values.length >= 3) {
209     var weekend = TypeCaster.firstValue(values[2]);
210@@ -825,7 +822,7 @@ var TIME = function (...values) : number {
211  */
212 var WORKDAY = function (...values) : number {
213   ArgsChecker.checkLengthWithin(values, 2, 3);
214-  var start = TypeCaster.firstValueAsExcelDate(values[0], true);
215+  var start = TypeCaster.firstValueAsDateNumber(values[0], true);
216   var days = TypeCaster.firstValueAsNumber(values[1]);
217   var hasHolidays = values.length === 3;
218   var holidays = [];
219@@ -877,7 +874,7 @@ var WORKDAY = function (...values) : number {
220  */
221 var WORKDAY$INTL = function (...values) : number {
222   ArgsChecker.checkLengthWithin(values, 2, 3);
223-  var start = TypeCaster.firstValueAsExcelDate(values[0], true);
224+  var start = TypeCaster.firstValueAsDateNumber(values[0], true);
225   var days = TypeCaster.firstValueAsNumber(values[1]);
226   var weekendDays = [];
227   if (values.length >= 3) {
228diff --git a/src/Formulas/Financial.ts b/src/Formulas/Financial.ts
229index 8ef1b4d..ae252a0 100644
230--- a/src/Formulas/Financial.ts
231+++ b/src/Formulas/Financial.ts
232@@ -387,16 +387,16 @@ var CUMIPMT = function (...values) : number {
233  */
234 var ACCRINT = function (...values) {
235   ArgsChecker.checkLengthWithin(values, 6, 7);
236-  var issue = TypeCaster.firstValueAsExcelDate(values[0]);
237+  var issue = TypeCaster.firstValueAsDateNumber(values[0]);
238   // "firstPayment" param is only here to check for errors for GS implementation.
239   // In MSE, there is a 7th (zero-indexed-6th) param that indicates the calculation-method to use, which indicates
240   // weather the total accrued interest starting at the first_intrest date, instead of the issue date.
241-  var firstPayment = TypeCaster.firstValueAsExcelDate(values[1]);
242+  var firstPayment = TypeCaster.firstValueAsDateNumber(values[1]);
243   if (firstPayment < 0) {
244     throw new NumError("Function ACCRINT parameter 2 value is " + firstPayment
245         + ". It should be greater than 0.");
246   }
247-  var settlement = TypeCaster.firstValueAsExcelDate(values[2]);
248+  var settlement = TypeCaster.firstValueAsDateNumber(values[2]);
249   if (issue > settlement) {
250     throw new NumError("Function ACCRINT parameter 1 (" + issue.toString()
251       + ") should be on or before Function ACCRINT parameter 3 (" + settlement.toString() + ").")
252diff --git a/src/Utilities/TypeCaster.ts b/src/Utilities/TypeCaster.ts
253index 502b837..b9aafb4 100644
254--- a/src/Utilities/TypeCaster.ts
255+++ b/src/Utilities/TypeCaster.ts
256@@ -4,9 +4,6 @@ import {
257   RefError,
258   ValueError, DivZeroError
259 } from "../Errors";
260-import {
261-  ExcelDate
262-} from "../ExcelDate";
263 import {
264   DateRegExBuilder
265 } from "./DateRegExBuilder";
266@@ -57,7 +54,7 @@ const TIMESTAMP = DateRegExBuilder.DateRegExBuilder()
267   .TIMESTAMP_UNITS_CAPTURE_GROUP()
268   .end()
269   .build();
270-// The first year to use when calculating the number of days in an ExcelDate
271+// The first year to use when calculating the number of days in a date
272 const FIRST_YEAR = 1900;
273 // The year 2000.
274 const Y2K_YEAR = 2000;
275@@ -129,7 +126,7 @@ function matchTimestampAndMutateMoment(timestampString : string, momentToMutate:
276  */
277 class TypeCaster {
278 
279-  private static ORIGIN_MOMENT = moment.utc([1899, 11, 30]).startOf("day");
280+  public static ORIGIN_MOMENT = moment.utc([1899, 11, 30]).startOf("day");
281   private static SECONDS_IN_DAY = 86400;
282 
283 
284@@ -327,11 +324,11 @@ class TypeCaster {
285   }
286 
287   /**
288-   * Casts a string to an ExcelDate. Throws error if parsing not possible.
289+   * Parses a string as a date number. Throws error if parsing not possible.
290    * @param dateString to parse
291-   * @returns {ExcelDate} resulting date
292+   * @returns {number} resulting date
293    */
294-  public static stringToExcelDate(dateString : string) : number {
295+  public static stringToDateNumber(dateString : string) : number {
296     // m will be set and valid or invalid, or will remain undefined
297     var m = TypeCaster.parseStringToMoment(dateString);
298     if (m === undefined || !m.isValid()) {
299@@ -514,19 +511,19 @@ class TypeCaster {
300   }
301 
302   /**
303-   * Takes the input type and will throw a REF_ERROR or coerce it into a ExcelDate
304-   * @param input input to attempt to coerce to a ExcelDate
305+   * Takes the input type and will throw a REF_ERROR or coerce it into a date number
306+   * @param input input to attempt to coerce to a date number
307    * @param coerceBoolean should a boolean be converted
308-   * @returns {ExcelDate} representing a date
309+   * @returns {number} representing a date
310    */
311-  static firstValueAsExcelDate(input: any, coerceBoolean?: boolean) : number {
312+  static firstValueAsDateNumber(input: any, coerceBoolean?: boolean) : number {
313     if (input instanceof Array) {
314       if (input.length === 0) {
315         throw new RefError("Reference does not exist.");
316       }
317-      return TypeCaster.firstValueAsExcelDate(input[0], coerceBoolean);
318+      return TypeCaster.firstValueAsDateNumber(input[0], coerceBoolean);
319     }
320-    return TypeCaster.valueToExcelDate(input, coerceBoolean);
321+    return TypeCaster.valueToDateNumber(input, coerceBoolean);
322   }
323 
324   static firstValueAsTimestampNumber(input : any) : number {
325@@ -540,17 +537,17 @@ class TypeCaster {
326   }
327 
328   /**
329-   * Convert a value to ExcelDate if possible.
330+   * Convert a value to date number if possible.
331    * @param value to convert
332    * @param coerceBoolean should a boolean be converted
333-   * @returns {ExcelDate} ExcelDate
334+   * @returns {number} date
335    */
336-  static valueToExcelDate(value: any, coerceBoolean?: boolean) : number {
337+  static valueToDateNumber(value: any, coerceBoolean?: boolean) : number {
338     if (typeof value === "number") {
339       return value;
340     } else if (typeof value === "string") {
341       try {
342-        return TypeCaster.stringToExcelDate(value)
343+        return TypeCaster.stringToDateNumber(value)
344       } catch (e) {
345         if (TypeCaster.canCoerceToNumber(value)) {
346           return TypeCaster.valueToNumber(value);