-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
97 lines (73 loc) · 1.68 KB
/
index.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
90
91
92
93
94
95
96
97
import { nativeIsArray } from 'underscore/modules/_setup.js';
import { append } from 'helpers/append';
import { copyOwn } from 'helpers/copy';
import { push } from 'helpers/push';
import { remove } from 'helpers/remove';
import { fetchRefId, opt } from './data';
export class Index {
constructor(keys) {
this.data = {};
this.keys = keys.slice();
if (opt.keySorter) {
this.keys.sort(opt.keySorter);
}
if (this.initialize) {
this.initialize(keys);
}
}
select(params, buffer = []) {
for (var k in params) {
if (nativeIsArray(a = params[k])) {
var plain = copyOwn(params);
for (var j = 0, a; j < a.length; j++) {
plain[k] = a[j];
this.select(plain, buffer);
}
return buffer;
}
}
if ((a = this.records(params))) {
append(buffer, a);
}
return buffer;
}
records(o) {
var data = this.data;
var keys = this.keys;
var len = keys.length;
for (var i = 0; data && i < len; i++) {
var v = kv(o, keys[i]);
data = data[v];
}
return data;
}
contains(v) {
var data = this.data[v];
return data && data.length > 0;
}
unregister(o) {
var records = this.records(o);
if (records) {
remove(records, o);
}
}
register(o) {
var data = this.data;
var keys = this.keys;
var len = keys.length - 1;
for (var i = 0; i < len; i++) {
var v = kv(o, keys[i]);
data = data[v] || (data[v] = {});
}
v = kv(o, keys[i]);
push(data, v, o);
}
}
function kv(o, k) {
if (k) {
return o.query && !(o.fieldsMap[k] >= 0) ?
o.query(k, fetchRefId) :
o[k];
}
return k;
}