commit
message
Removing A1CellKey because it's unnecessary abstraction
author
Ben Vogt <[email protected]>
date
2016-12-31 22:37:10
stats
5 file(s) changed,
49 insertions(+),
93 deletions(-)
files
README.md
src/A1CellKey.ts
src/Cell.ts
src/Sheet.ts
tests/A1CellKeyTest.ts
1diff --git a/README.md b/README.md
2index 0f6e682..5d9c4db 100644
3--- a/README.md
4+++ b/README.md
5@@ -4,10 +4,6 @@ TypeScript implementation of a spreadsheet.
6 ## TODO
7 Things I should do.
8
9-### Remove A1CellKey. It is unnecessary.
10-
11-### Write documentation for A1CellKey
12-
13 ### Write documentation for Cell
14
15 ### Write documentation for Sheet
16diff --git a/src/A1CellKey.ts b/src/A1CellKey.ts
17deleted file mode 100644
18index c905d32..0000000
19--- a/src/A1CellKey.ts
20+++ /dev/null
21@@ -1,42 +0,0 @@
22-/**
23- * A1-notation style cell id. Used to index the cells.
24- * */
25-class A1CellKey {
26- private x: number;
27- private y: number;
28- private key: string;
29-
30- constructor(key: string) {
31- this.key = key;
32- var row = parseInt(key.match(/\d+$/)[0], 10);
33- this.x = lettersToNumber(key.replace(row.toString(), ''));
34- this.y = row - 1;
35- }
36- static of(x: number, y: number): A1CellKey {
37- return new A1CellKey(numberToLetters(x+1) + (y+1).toString());
38- }
39- toString(): string {
40- return this.key;
41- }
42- getX(): number {
43- return this.x;
44- }
45- getY(): number {
46- return this.y;
47- }
48-}
49-
50-function lettersToNumber(letters: string): number {
51- return letters.toLowerCase().charCodeAt(0) - 97;
52-}
53-
54-function numberToLetters(num: number): string {
55- let mod = num % 26,
56- pow = num / 26 | 0,
57- out = mod ? String.fromCharCode(64 + mod) : (--pow, 'Z');
58- return pow ? numberToLetters(pow) + out : out;
59-}
60-
61-export {
62- A1CellKey
63-}
64\ No newline at end of file
65diff --git a/src/Cell.ts b/src/Cell.ts
66index 971c07d..0dd4fd0 100644
67--- a/src/Cell.ts
68+++ b/src/Cell.ts
69@@ -1,5 +1,3 @@
70-import {A1CellKey} from "./A1CellKey";
71-
72 class Cell {
73 public formula: string;
74 public value: string;
75@@ -9,15 +7,15 @@ class Cell {
76 public row: number;
77 public col: number;
78 constructor(id: string) {
79- var key = new A1CellKey(id);
80+ var key = parseKey(id);
81
82 this.formula = null;
83 this.value = "";
84 this.dependencies = [];
85 this.error = null;
86 this.id = id;
87- this.row = key.getY();
88- this.col = key.getX();
89+ this.row = key.y;
90+ this.col = key.x;
91 }
92 updateDependencies(dependencies: Array<string>) {
93 for (var index in dependencies) {
94@@ -46,6 +44,31 @@ class Cell {
95 }
96 }
97
98+function toNum(chr) {
99+ chr = chr.replace(/\$/g, '');
100+ var base = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', i, j, result = 0;
101+
102+ for (i = 0, j = chr.length - 1; i < chr.length; i += 1, j -= 1) {
103+ result += Math.pow(base.length, j) * (base.indexOf(chr[i]) + 1);
104+ }
105+
106+ if (result) {
107+ --result;
108+ }
109+
110+ return result;
111+}
112+
113+function parseKey(cell) {
114+ var num = cell.match(/\d+$/),
115+ alpha = cell.replace(num, '');
116+
117+ return {
118+ x: toNum(alpha),
119+ y: parseInt(num[0], 10) - 1
120+ };
121+}
122+
123 export {
124 Cell
125 }
126\ No newline at end of file
127diff --git a/src/Sheet.ts b/src/Sheet.ts
128index cf2a1f9..72118bc 100644
129--- a/src/Sheet.ts
130+++ b/src/Sheet.ts
131@@ -1,6 +1,5 @@
132 /// <reference path="parser.d.ts"/>
133 import { Parser } from "./Parser";
134-import { A1CellKey } from "./A1CellKey"
135 import { SUPPORTED_FORMULAS } from "./SupportedFormulas"
136 import { Cell } from "./Cell"
137 import { Errors } from "./Errors"
138@@ -48,22 +47,21 @@ var Sheet = (function () {
139 constructor() {
140 this.data = {};
141 }
142- getCell(key: A1CellKey) {
143- return this.data[key.toString()];
144+ getCell(key: string) {
145+ return this.data[key];
146 }
147 addCell(cell: Cell) {
148 var cellId = cell.id;
149- var key = new A1CellKey(cellId);
150
151 if (!(cellId in this.data)) {
152 this.data[cellId] = cell;
153 } else {
154- this.getCell(key).updateDependencies(cell.dependencies);
155- this.getCell(key).setValue(cell.value);
156- this.getCell(key).setError(cell.error);
157+ this.getCell(cellId).updateDependencies(cell.dependencies);
158+ this.getCell(cellId).setValue(cell.value);
159+ this.getCell(cellId).setError(cell.error);
160 }
161
162- return this.getCell(new A1CellKey(cellId));
163+ return this.getCell(cellId);
164 }
165 getDependencies(id: string) {
166 var getDependencies = function (id: string) {
167@@ -95,7 +93,7 @@ var Sheet = (function () {
168 if (allDependencies.indexOf(refId) === -1) {
169 allDependencies.push(refId);
170
171- var cell = this.getCell(new A1CellKey(refId));
172+ var cell = this.getCell(refId);
173 if (cell.dependencies.length) {
174 getTotalDependencies(refId);
175 }
176@@ -126,7 +124,7 @@ var Sheet = (function () {
177 var allDependencies = instance.matrix.getCellDependencies(cell);
178
179 allDependencies.forEach(function (refId) {
180- var currentCell = instance.matrix.getCell(new A1CellKey(refId));
181+ var currentCell = instance.matrix.getCell(refId);
182 if (currentCell && currentCell.formula) {
183 calculateCellFormula(currentCell);
184 }
185@@ -136,10 +134,9 @@ var Sheet = (function () {
186 var calculateCellFormula = function (cell: Cell) {
187 // to avoid double translate formulas, update cell data in parser
188 var parsed = parse(cell.formula, cell.id);
189- var key = new A1CellKey(cell.id);
190
191- instance.matrix.getCell(key).setValue(parsed.result);
192- instance.matrix.getCell(key).setError(parsed.error);
193+ instance.matrix.getCell(cell.id).setValue(parsed.result);
194+ instance.matrix.getCell(cell.id).setError(parsed.error);
195
196 return parsed;
197 };
198@@ -427,12 +424,12 @@ var Sheet = (function () {
199 cellValue: function (cellId) {
200 var value,
201 origin = this,
202- cell = instance.matrix.getCell(new A1CellKey(cellId));
203+ cell = instance.matrix.getCell(cellId);
204
205 // get value
206 value = cell ? cell.value : "0"; // TODO: fix this, it's sloppy.
207 //update dependencies
208- instance.matrix.getCell(new A1CellKey(origin)).updateDependencies([cellId]);
209+ instance.matrix.getCell(origin).updateDependencies([cellId]);
210 // check references error
211 if (cell && cell.dependencies) {
212 if (cell.dependencies.indexOf(cellId) !== -1) {
213@@ -465,7 +462,7 @@ var Sheet = (function () {
214 var cells = instance.utils.iterateCells.call(this, coordsStart, coordsEnd),
215 result = [];
216 //update dependencies
217- instance.matrix.getCell(new A1CellKey(origin)).updateDependencies(cells.index);
218+ instance.matrix.getCell(origin).updateDependencies(cells.index);
219
220 result.push(cells.value);
221 return result;
222@@ -496,8 +493,8 @@ var Sheet = (function () {
223 if (deps.indexOf(key) !== -1) {
224 result = null;
225 deps.forEach(function (id) {
226- instance.matrix.getCell(new A1CellKey(id)).setError(Errors.get('REF'));
227- instance.matrix.getCell(new A1CellKey(id)).setValue(null);
228+ instance.matrix.getCell(id).setError(Errors.get('REF'));
229+ instance.matrix.getCell(id).setValue(null);
230 });
231 throw Error('REF');
232 }
233@@ -521,7 +518,7 @@ var Sheet = (function () {
234 };
235
236 var getCell = function (cellKeyString: string) : Cell {
237- var cell = instance.matrix.getCell(new A1CellKey(cellKeyString));
238+ var cell = instance.matrix.getCell(cellKeyString);
239 if (cell === undefined) {
240 return null;
241 }
242diff --git a/tests/A1CellKeyTest.ts b/tests/A1CellKeyTest.ts
243deleted file mode 100644
244index 1effb1e..0000000
245--- a/tests/A1CellKeyTest.ts
246+++ /dev/null
247@@ -1,23 +0,0 @@
248-import { A1CellKey } from "../src/A1CellKey"
249-import { assertEquals } from "./utils/Asserts"
250-
251-//Test constructor
252-var one = new A1CellKey("A1");
253-assertEquals(0, one.getX());
254-assertEquals(0, one.getY());
255-assertEquals("A1", one.toString());
256-var two = new A1CellKey("Z4563534");
257-assertEquals(25, two.getX());
258-assertEquals(4563533, two.getY());
259-assertEquals("Z4563534", two.toString());
260-
261-
262-//Test static constructor
263-var one = A1CellKey.of(0, 0);
264-assertEquals(0, one.getX());
265-assertEquals(0, one.getY());
266-assertEquals("A1", one.toString());
267-var two = A1CellKey.of(25, 4563533);
268-assertEquals(25, two.getX());
269-assertEquals(4563533, two.getY());
270-assertEquals("Z4563534", two.toString());
271\ No newline at end of file