spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Refactoring custom formulas and usage
author
Ben Vogt <[email protected]>
date
2017-01-14 21:29:34
stats
2 file(s) changed, 29 insertions(+), 18 deletions(-)
files
src/Sheet.ts
src/SupportedFormulas.ts
src/Formulas.ts
  1diff --git a/src/SupportedFormulas.ts b/src/Formulas.ts
  2similarity index 91%
  3rename from src/SupportedFormulas.ts
  4rename to src/Formulas.ts
  5index 770e1f4..5b9063a 100644
  6--- a/src/SupportedFormulas.ts
  7+++ b/src/Formulas.ts
  8@@ -21,7 +21,7 @@ const SUPPORTED_FORMULAS = [
  9   'XOR'
 10 ];
 11 
 12-const OverrideFormulas = {
 13+const CustomFormulas = {
 14   "F.DIST": Formula["FDIST"],
 15   "F.INV": Formula["FINV"],
 16   ATAN2: function (x, y) {
 17@@ -78,23 +78,23 @@ const OverrideFormulas = {
 18     switch (basis) {
 19       case 0:
 20         // US (NASD) 30/360
 21-        factor = OverrideFormulas.YEARFRAC(issue, settlement, basis);
 22+        factor = CustomFormulas.YEARFRAC(issue, settlement, basis);
 23         break;
 24       case 1:
 25         // Actual/actual
 26-        factor = OverrideFormulas.YEARFRAC(issue, settlement, basis);
 27+        factor = CustomFormulas.YEARFRAC(issue, settlement, basis);
 28         break;
 29       case 2:
 30         // Actual/360
 31-        factor = OverrideFormulas.YEARFRAC(issue, settlement, basis);
 32+        factor = CustomFormulas.YEARFRAC(issue, settlement, basis);
 33         break;
 34       case 3:
 35         // Actual/365
 36-        factor = OverrideFormulas.YEARFRAC(issue, settlement, basis);
 37+        factor = CustomFormulas.YEARFRAC(issue, settlement, basis);
 38         break;
 39       case 4:
 40         // European 30/360
 41-        factor = OverrideFormulas.YEARFRAC(issue, settlement, basis);
 42+        factor = CustomFormulas.YEARFRAC(issue, settlement, basis);
 43         break;
 44     }
 45     return par * rate * factor;
 46@@ -208,7 +208,19 @@ const OverrideFormulas = {
 47   }
 48 };
 49 
 50+var Formulas = {
 51+  exists: function(fn: string) {
 52+    return ((fn in CustomFormulas) || SUPPORTED_FORMULAS.indexOf(fn) > -1);
 53+  },
 54+  get: function(fn: string) {
 55+    if (fn in CustomFormulas) {
 56+      return CustomFormulas[fn];
 57+    }
 58+    return Formula[fn];
 59+  }
 60+};
 61+
 62 export {
 63-  SUPPORTED_FORMULAS,
 64-  OverrideFormulas
 65+  Formulas,
 66+  CustomFormulas
 67 }
 68\ No newline at end of file
 69diff --git a/src/Sheet.ts b/src/Sheet.ts
 70index 2a5b8a7..8c4f627 100644
 71--- a/src/Sheet.ts
 72+++ b/src/Sheet.ts
 73@@ -1,10 +1,10 @@
 74 /// <reference path="parser.d.ts"/>
 75 import { Parser } from "./Parser";
 76-import {SUPPORTED_FORMULAS, OverrideFormulas} from "./SupportedFormulas"
 77 import { Cell } from "./Cell"
 78 import { Errors } from "./Errors"
 79-import * as Formula from "formulajs"
 80 import { Helpers } from "./Helpers";
 81+import { Formulas } from "./Formulas";
 82+
 83 
 84 /**
 85  * Model representing a spreadsheet. When values/cells are added, dependencies recalculated, and true-values of those
 86@@ -438,13 +438,8 @@ var Sheet = (function () {
 87     callFunction: function (fn, args) {
 88       fn = fn.toUpperCase();
 89       args = args || [];
 90-      if (fn in OverrideFormulas) {
 91-        return OverrideFormulas[fn].apply(this, args);
 92-      }
 93-      if (SUPPORTED_FORMULAS.indexOf(fn) > -1) {
 94-        if (Formula[fn]) {
 95-          return Formula[fn].apply(this, args);
 96-        }
 97+      if (Formulas.exists(fn)) {
 98+        return Formulas.get(fn).apply(this, args);
 99       }
100 
101       throw Error('NAME');
102@@ -456,8 +451,8 @@ var Sheet = (function () {
103 
104       if (str) {
105         str = str.toUpperCase();
106-        if (Formula[str]) {
107-          return ((typeof Formula[str] === 'function') ? Formula[str].apply(this, args) : Formula[str]);
108+        if (Formulas.exists(str)) {
109+          return ((typeof Formulas.get(str) === 'function') ? Formulas.get(str).apply(this, args) : Formulas.get(str));
110         }
111       }
112