commit
message
Adding TODOs to README.md, using better storage for Matrix.data
author
Ben Vogt <[email protected]>
date
2016-12-30 21:42:12
stats
2 file(s) changed,
24 insertions(+),
16 deletions(-)
files
README.md
src/Sheet.ts
1diff --git a/README.md b/README.md
2index 5ecc9a2..2c58574 100644
3--- a/README.md
4+++ b/README.md
5@@ -3,3 +3,9 @@ TypeScript implementation of a spreadsheet.
6
7 ## TODO
8 Things I should do.
9+
10+### Rename anything referencing an "item"
11+
12+### `setValue()`, `getValue()` on a sheet
13+
14+## `load()` array of array of values for a sheet
15diff --git a/src/Sheet.ts b/src/Sheet.ts
16index 596e093..8e5da10 100644
17--- a/src/Sheet.ts
18+++ b/src/Sheet.ts
19@@ -44,14 +44,12 @@ var Sheet = (function () {
20 };
21
22 class Matrix {
23- public data: Array<Cell>;
24+ public data: Object;
25 constructor() {
26- this.data = [];
27+ this.data = {};
28 }
29 getItem(key: A1CellKey) {
30- return this.data.filter(function (cell) {
31- return cell.id === key.toString();
32- })[0];
33+ return this.data[key.toString()];
34 }
35 addItem(cell: Cell) {
36 var cellId = cell.id;
37@@ -61,12 +59,8 @@ var Sheet = (function () {
38 cell.row = coords.row;
39 cell.col = coords.col;
40
41- var existingCell = this.data.filter(function (cell) {
42- return cell.id === cellId;
43- })[0];
44-
45- if (!existingCell) {
46- this.data.push(cell);
47+ if (!(cellId in this.data)) {
48+ this.data[cellId] = cell;
49 } else {
50 this.getItem(key).updateDependencies(cell.dependencies);
51 this.getItem(key).setValue(cell.value);
52@@ -77,11 +71,15 @@ var Sheet = (function () {
53 }
54 getDependencies(id: string) {
55 var getDependencies = function (id: string) {
56- var filtered = this.data.filter(function (cell) {
57- if (cell.deps) {
58- return cell.deps.indexOf(id) > -1;
59+ var filtered = [];
60+ for (var key in this.data) {
61+ var cell = this.data[key];
62+ if (cell.dependencies) {
63+ if (cell.dependencies.indexOf(id) > -1) {
64+ filtered.push(cell)
65+ }
66 }
67- });
68+ }
69
70 var deps = [];
71 filtered.forEach(function (cell) {
72@@ -132,9 +130,9 @@ var Sheet = (function () {
73 recalculateCellDependencies(cell);
74 }
75 }
76- this.data.forEach(function (item) {
77- console.log(item.id, item.formula, item.value);
78- });
79+ for (var key in this.data) {
80+ console.log(this.data[key].id, this.data[key].formula, this.data[key].value);
81+ }
82 }
83 }
84