-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
34 lines (27 loc) · 893 Bytes
/
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
import {map as mapArray, filter as filterArray} from 'immutable-array-methods';
import {set} from 'immutable-object-methods';
const filter = (array, fn) => {
return mapArray(filterArray(array, fn), obj => {
return obj.children ? set(obj, 'children', filter(obj.children, fn)) : obj;
});
};
export const filterText = (array, fn) => filter(array, (obj) => {
return obj.type === 'text' ? fn(obj) : true;
});
const map = (array, fn) => {
return mapArray(array, (obj) => {
if (obj.children) {
obj = set(obj, 'children', map(obj.children, fn));
}
if (obj.caption) {
obj = set(obj, 'caption', map(obj.caption, fn));
}
if (obj.attribution) {
obj = set(obj, 'attribution', map(obj.attribution, fn));
}
return fn(obj);
});
};
export const mapText = (array, fn) => map(array, (obj) => {
return obj.type === 'text' ? fn(obj) : obj;
});