spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[Date, TypeCaster] Removing Excelime in favor of casters and primatives
author
Ben Vogt <[email protected]>
date
2017-05-06 01:04:43
stats
4 file(s) changed, 17 insertions(+), 58 deletions(-)
files
README.md
src/ExcelTime.ts
src/Formulas/Date.ts
src/Utilities/TypeCaster.ts
  1diff --git a/README.md b/README.md
  2index 3096f52..a9d237f 100644
  3--- a/README.md
  4+++ b/README.md
  5@@ -30,7 +30,7 @@ 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. ExcelTime, Dollar), cells should have formats based on the
 10+Instead of having non-primitives, (i.e. Date, DateTime, Time, 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@@ -67,5 +67,5 @@ Right now we're just using the number of days since 1900, but we should check th
 15 For example 64 tbs to a qt.
 16 
 17 
 18-### Sheet.ts and parser.js should be able to concatenate strings
 19+### Sheet.ts and parser.js should be able to concatenate criteria and values
 20 E.g. `=COUNTIFS(A7:A24, ">6", B7:B24, "<"&DATE(1969,7,20))`
 21\ No newline at end of file
 22diff --git a/src/ExcelTime.ts b/src/ExcelTime.ts
 23deleted file mode 100644
 24index 6fa22aa..0000000
 25--- a/src/ExcelTime.ts
 26+++ /dev/null
 27@@ -1,50 +0,0 @@
 28-/// <reference path="../node_modules/moment/moment.d.ts"/>
 29-import * as moment from "moment";
 30-
 31-class ExcelTime {
 32-  // Value representing the time of day. Between 0 and 1, exclusive on end.
 33-  private fractionOfDay: number;
 34-
 35-
 36-  /**
 37-   * Create ExcelTime from hours, seconds, and minutes. All capable of being overloaded, but are modded.
 38-   * @param hours in this timestamp.
 39-   * @param minutes in this timestamp.
 40-   * @param seconds in this timestamp.
 41-   */
 42-  constructor(hours: number, minutes: number, seconds: number) {
 43-    var v = (((hours % 24) * 60 * 60) + ((minutes) * 60) + (seconds)) / 86400;
 44-    this.fractionOfDay = v % 1;
 45-  }
 46-
 47-
 48-  /**
 49-   * Returns the number of seconds in this timestamp.
 50-   * @returns {number} of seconds.
 51-   */
 52-  toNumber() : number {
 53-    return this.fractionOfDay;
 54-  }
 55-
 56-  /**
 57-   * Returns the string in the format "12:04:09 AM".
 58-   * @returns {string} representing this timestamp.
 59-   */
 60-  toString() : string {
 61-    return moment.utc([1900]).startOf("year").add(this.fractionOfDay * 86400, "seconds").format("h:mm:ss A");
 62-  }
 63-
 64-  /**
 65-   * Equality checking.
 66-   * @param other ExcelTime to compare to.
 67-   * @returns {boolean} true if equals
 68-   */
 69-  equals(other: ExcelTime) : boolean {
 70-    return other.toNumber() === this.toNumber();
 71-  }
 72-
 73-}
 74-
 75-export {
 76-  ExcelTime
 77-}
 78\ No newline at end of file
 79diff --git a/src/Formulas/Date.ts b/src/Formulas/Date.ts
 80index c54ac6b..3871b4d 100644
 81--- a/src/Formulas/Date.ts
 82+++ b/src/Formulas/Date.ts
 83@@ -11,9 +11,6 @@ import {
 84   ValueError,
 85   RefError
 86 } from "../Errors";
 87-import {
 88-  ExcelTime
 89-} from "../ExcelTime";
 90 
 91 /**
 92  * Converts a provided year, month, and day into a date.
 93@@ -792,7 +789,7 @@ var TODAY = function (...values) : number {
 94  * @param values[0] hour - The hour component of the time.
 95  * @param values[1] minute - The minute component of the time.
 96  * @param values[2] second - The second component of the time.
 97- * @returns {ExcelTime} time
 98+ * @returns {number} time of day
 99  * @constructor
100  */
101 var TIME = function (...values) : number {
102@@ -800,9 +797,9 @@ var TIME = function (...values) : number {
103   var hours = Math.floor(TypeCaster.firstValueAsNumber(values[0]));
104   var minutes = Math.floor(TypeCaster.firstValueAsNumber(values[1]));
105   var seconds = Math.floor(TypeCaster.firstValueAsNumber(values[2]));
106-  var e = new ExcelTime(hours, minutes, seconds).toNumber();
107+  var e = TypeCaster.unitsToTimeNumber(hours, minutes, seconds);
108   if (e < 0) {
109-    throw new NumError("TIME evaluates to an out of range value -1.201273148. It should be greater than or equal to 0.");
110+    throw new NumError("TIME evaluates to an out of range value " + e + ". It should be greater than or equal to 0.");
111   }
112   return e;
113 };
114diff --git a/src/Utilities/TypeCaster.ts b/src/Utilities/TypeCaster.ts
115index 0a53eaa..bc9e81c 100644
116--- a/src/Utilities/TypeCaster.ts
117+++ b/src/Utilities/TypeCaster.ts
118@@ -588,6 +588,18 @@ class TypeCaster {
119   static numberToMoment(n : number) : moment.Moment {
120     return moment.utc(TypeCaster.ORIGIN_MOMENT).add(n, "days");
121   }
122+
123+  /**
124+   * Using timestamp units, create a time number between 0 and 1, exclusive on end.
125+   * @param hours
126+   * @param minutes
127+   * @param seconds
128+   * @returns {number} representing time of day between 0 and 1, exclusive on end.
129+   */
130+  static unitsToTimeNumber(hours: number, minutes: number, seconds: number): number {
131+    var v = (((hours % 24) * 60 * 60) + ((minutes) * 60) + (seconds)) / 86400;
132+    return v % 1;
133+  }
134 }
135 
136 /**