-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.js
89 lines (78 loc) · 2.18 KB
/
table.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { create } from '../index';
import { query } from '../src/query';
import { link, ownerOf, pathOf, valueOf, atomOf } from '../src/meta';
export default function Table(T) {
class Table {
initialize(value) {
return Array.isArray(value) ? value : [];
}
get rows() {
return query(function*() {
let i = -1;
for (let _ of valueOf(this)) { //eslint-disable-line
i++;
yield new Row(this, i);
}
}.bind(this));
}
get columns() {
return query(function*() {
let value = valueOf(this);
if (value.length === 0 || !value[0].length) {
return;
} else {
// we just use the number of columns in the first row
// as the number of columns for everything. Maybe a better way?
let width = value[0].length;
for (let i = 0; i < width; i++) {
yield new Column(this, i);
}
}
}.bind(this));
}
// iterator over all values.
*[Symbol.iterator]() {
let i = -1;
for (let row of valueOf(this)) {
i++;
let j = -1;
for (let _ of row) { //eslint-disable-line
j++;
let owner = ownerOf(this);
yield link(create(T), T, pathOf(this).concat([i, j]), atomOf(this), owner.Type, owner.path);
}
}
}
}
class Row {
constructor(table, index) {
this.table = table;
this.index = index;
}
*[Symbol.iterator]() {
let i = -1;
let { table, index } = this;
for (let _ of valueOf(table)[index]) { //eslint-disable-line
i++;
let owner = ownerOf(table);
yield link(create(T), T, pathOf(table).concat([index, i]), atomOf(table), owner.Type, owner.path);
}
}
}
class Column {
constructor(table, index) {
this.table = table;
this.index = index;
}
*[Symbol.iterator]() {
let i = -1;
let { table, index } = this;
for (let _ of valueOf(table)) { //eslint-disable-line
i++;
let owner = ownerOf(table);
yield link(create(T), T, pathOf(table).concat([i, index]), atomOf(table), owner.Type, owner.path);
}
}
}
return Table;
}