-
Notifications
You must be signed in to change notification settings - Fork 1
/
2.3.js
executable file
·43 lines (37 loc) · 847 Bytes
/
2.3.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
function merge() {
return [].reduce.call(arguments, function (res, obj) {
return Object.keys(obj).reduce(function (res, prop) {
res[prop] = obj[prop];
return res;
}, res);
}, {});
}
function not(fn) {
return function (val) {
return !fn(val);
};
}
function contained(list) {
return function (item) {
return list.indexOf(item) >= 0;
};
}
function applyChangeset(user, changes) {
return merge({
flags: user.flags.filter(not(contained(changes.removeFlags))).
concat(changes.addFlags),
currentPage: changes.currentPage,
pages: user.pages.concat(changes.currentPage)
});
}
var user = {
name: 'Chris',
flags: ['n00b'],
pages: [0]
};
var changeset = {
removeFlags: ['n00b', 'advanced'],
addFlags: ['beginner'],
currentPage: 1
};
console.log(applyChangeset(user, changeset));