-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccess.Memory.js
63 lines (63 loc) · 1.41 KB
/
Access.Memory.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
var q = require('q');
var group = require('./group');
module.exports = (data, option = {}) => (({ map, transform, relate }) => ({
get: ({ id, type, relation }) =>
relation ?
q.resolve(map.call(id, id => relate(id, type, relation))) :
q.resolve(map.call(id, id => transform(data[type][id]))),
batch: function* (queue) {
var request = group.call(queue, [
request => request.relation || "", [
request => request.type, []
]
]);
for (var relation in request)
for (var type in request[relation])
yield [
{
id: request[relation][type].map(request => request.id),
type,
relation
},
request[relation][type]
];
}
}))({
map:
option.collection ?
option.collection == 'array' ?
Array.prototype.map :
option.collection == 'object' ?
mapToObject :
undefined :
Array.prototype.map,
transform:
option.clone ? clone : identity,
relate:
(id, type, relation) =>
relation in data ?
filterObject.call(
data[relation],
object => object[`${type}Id`] == id
) :
`/${type}/${id}/${relation}`
});
function mapToObject(f) {
var object = {};
for (var i in this)
object[this[i]] = f(this[i]);
return object;
}
function filterObject(f) {
var array = [];
for (var i in this)
if (f(this[i]))
array.push(i);
return array;
}
function clone(object) {
return JSON.parse(JSON.stringify(object));
}
function identity(object) {
return object;
}