commit
message
WIP on making everything a strict type
author
Ben Vogt <[email protected]>
date
2016-12-26 18:23:53
stats
2 file(s) changed,
117 insertions(+),
23 deletions(-)
files
ts/sheet.js
ts/sheet.ts
1diff --git a/ts/sheet.js b/ts/sheet.js
2index d6e9408..3fc9ee3 100644
3--- a/ts/sheet.js
4+++ b/ts/sheet.js
5@@ -1,3 +1,40 @@
6+/**
7+ * A1-notation style cell id. Used to index the cells.
8+ * */
9+var A1CellKey = (function () {
10+ function A1CellKey(key) {
11+ this.row = parseInt(key.match(/\d+$/)[0], 10);
12+ this.column = key.replace(this.row.toString(), '');
13+ this.x = lettersToNumber(this.column);
14+ this.y = this.row - 1;
15+ }
16+ A1CellKey.of = function (x, y) {
17+ return new A1CellKey(numberToLetters(x + 1) + (y + 1).toString());
18+ };
19+ A1CellKey.prototype.toString = function () {
20+ return this.column + "" + this.row;
21+ };
22+ A1CellKey.prototype.getColumn = function () {
23+ return this.column;
24+ };
25+ A1CellKey.prototype.getRow = function () {
26+ return this.row;
27+ };
28+ A1CellKey.prototype.getX = function () {
29+ return this.x;
30+ };
31+ A1CellKey.prototype.getY = function () {
32+ return this.y;
33+ };
34+ return A1CellKey;
35+}());
36+function lettersToNumber(letters) {
37+ return letters.toLowerCase().charCodeAt(0) - 97;
38+}
39+function numberToLetters(num) {
40+ var mod = num % 26, pow = num / 26 | 0, out = mod ? String.fromCharCode(64 + mod) : (--pow, 'Z');
41+ return pow ? numberToLetters(pow) + out : out;
42+}
43 var mine = (function () {
44 'use strict';
45 var instance = this;
46@@ -57,9 +94,9 @@ var mine = (function () {
47 // formulaEdit: false
48 // };
49 this.data = [];
50- this.getItem = function (id) {
51+ this.getItem = function (key) {
52 return instance.matrix.data.filter(function (item) {
53- return item.id === id;
54+ return item.id === key.toString();
55 })[0];
56 };
57 this.removeItem = function (id) {
58@@ -69,7 +106,7 @@ var mine = (function () {
59 };
60 this.updateItem = function (item, props) {
61 if (instance.utils.isString(item)) {
62- item = instance.matrix.getItem(item);
63+ item = instance.matrix.getItem(new A1CellKey(item));
64 }
65 if (item && props) {
66 for (var p in props) {
67@@ -106,10 +143,10 @@ var mine = (function () {
68 else {
69 instance.matrix.updateItem(cellExist, item);
70 }
71- return instance.matrix.getItem(cellId);
72+ return instance.matrix.getItem(new A1CellKey(cellId));
73 };
74 this.updateCellItem = function (id, props) {
75- var item = instance.matrix.getItem(id);
76+ var item = instance.matrix.getItem(new A1CellKey(id));
77 instance.matrix.updateItem(item, props);
78 };
79 this.getDependencies = function (id) {
80@@ -134,7 +171,7 @@ var mine = (function () {
81 deps.forEach(function (refId) {
82 if (allDependencies.indexOf(refId) === -1) {
83 allDependencies.push(refId);
84- var item = instance.matrix.getItem(refId);
85+ var item = instance.matrix.getItem(new A1CellKey(refId));
86 if (item.deps.length) {
87 getTotalDependencies(refId);
88 }
89@@ -151,7 +188,7 @@ var mine = (function () {
90 var recalculateCellDependencies = function (cell) {
91 var allDependencies = instance.matrix.getCellDependencies(cell);
92 allDependencies.forEach(function (refId) {
93- var currentCell = instance.matrix.getItem(refId);
94+ var currentCell = instance.matrix.getItem(new A1CellKey(refId));
95 if (currentCell && currentCell.formula) {
96 calculateCellFormula(currentCell.formula, currentCell.id);
97 }
98@@ -441,7 +478,7 @@ var mine = (function () {
99 throw Error('NAME');
100 },
101 cellValue: function (cell) {
102- var value, element = this, item = instance.matrix.getItem(cell);
103+ var value, element = this, item = instance.matrix.getItem(new A1CellKey(cell));
104 // get value
105 value = item ? item.value : "0"; // TODO: fix this, it's sloppy.
106 //update dependencies
107diff --git a/ts/sheet.ts b/ts/sheet.ts
108index d0611dc..f590a75 100644
109--- a/ts/sheet.ts
110+++ b/ts/sheet.ts
111@@ -8,6 +8,69 @@ interface P {
112 declare var Parser: P;
113 declare var Formula: any;
114
115+
116+
117+
118+
119+
120+
121+
122+
123+/**
124+ * A1-notation style cell id. Used to index the cells.
125+ * */
126+class A1CellKey {
127+ private column: string;
128+ private row: number;
129+ private x: number;
130+ private y: number;
131+
132+ constructor(key: string) {
133+ this.row = parseInt(key.match(/\d+$/)[0], 10);
134+ this.column = key.replace(this.row.toString(), '');
135+ this.x = lettersToNumber(this.column);
136+ this.y = this.row - 1;
137+ }
138+ static of(x: number, y: number): A1CellKey {
139+ return new A1CellKey(numberToLetters(x+1) + (y+1).toString());
140+ }
141+ toString(): string {
142+ return this.column + "" + this.row;
143+ }
144+ getColumn(): string {
145+ return this.column;
146+ }
147+ getRow(): number {
148+ return this.row;
149+ }
150+ getX(): number {
151+ return this.x;
152+ }
153+ getY(): number {
154+ return this.y;
155+ }
156+}
157+
158+function lettersToNumber(letters: string): number {
159+ return letters.toLowerCase().charCodeAt(0) - 97;
160+}
161+
162+function numberToLetters(num: number): string {
163+ let mod = num % 26,
164+ pow = num / 26 | 0,
165+ out = mod ? String.fromCharCode(64 + mod) : (--pow, 'Z');
166+ return pow ? numberToLetters(pow) + out : out;
167+}
168+
169+
170+
171+
172+
173+
174+
175+
176+
177+
178 var mine = (function () {
179 'use strict';
180 var instance = this;
181@@ -82,21 +145,15 @@ var mine = (function () {
182
183 this.data = [];
184
185- this.getItem = function (id) {
186+ this.getItem = function (key: A1CellKey) {
187 return instance.matrix.data.filter(function (item) {
188- return item.id === id;
189+ return item.id === key.toString();
190 })[0];
191 };
192
193- this.removeItem = function (id) {
194- instance.matrix.data = instance.matrix.data.filter(function (item) {
195- return item.id !== id;
196- });
197- };
198-
199 this.updateItem = function (item, props) {
200 if (instance.utils.isString(item)) {
201- item = instance.matrix.getItem(item);
202+ item = instance.matrix.getItem(new A1CellKey(item));
203 }
204
205 if (item && props) {
206@@ -138,12 +195,12 @@ var mine = (function () {
207 instance.matrix.updateItem(cellExist, item);
208 }
209
210- return instance.matrix.getItem(cellId);
211+ return instance.matrix.getItem(new A1CellKey(cellId));
212 };
213
214
215 this.updateCellItem = function (id, props) {
216- var item = instance.matrix.getItem(id);
217+ var item = instance.matrix.getItem(new A1CellKey(id));
218
219 instance.matrix.updateItem(item, props);
220 };
221@@ -176,7 +233,7 @@ var mine = (function () {
222 if (allDependencies.indexOf(refId) === -1) {
223 allDependencies.push(refId);
224
225- var item = instance.matrix.getItem(refId);
226+ var item = instance.matrix.getItem(new A1CellKey(refId));
227 if (item.deps.length) {
228 getTotalDependencies(refId);
229 }
230@@ -198,7 +255,7 @@ var mine = (function () {
231 var allDependencies = instance.matrix.getCellDependencies(cell);
232
233 allDependencies.forEach(function (refId) {
234- var currentCell = instance.matrix.getItem(refId);
235+ var currentCell = instance.matrix.getItem(new A1CellKey(refId));
236 if (currentCell && currentCell.formula) {
237 calculateCellFormula(currentCell.formula, currentCell.id);
238 }
239@@ -554,7 +611,7 @@ var mine = (function () {
240 cellValue: function (cell) {
241 var value,
242 element = this,
243- item = instance.matrix.getItem(cell);
244+ item = instance.matrix.getItem(new A1CellKey(cell));
245 // get value
246 value = item ? item.value : "0"; // TODO: fix this, it's sloppy.
247 //update dependencies