-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.ts
94 lines (85 loc) · 2.22 KB
/
utils.ts
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
import dropRepeats from "xstream/extra/dropRepeats";
import { GoalID, Goal, Status, GoalStatus, Result } from "./types";
export function generateGoalID({
stamp = undefined,
id = undefined
} = {}): GoalID {
const now = new Date();
return {
stamp: typeof stamp === "undefined" ? now : stamp,
id:
typeof id === "undefined"
? `${Math.random()
.toString(36)
.substring(2)}-${now.getTime()}`
: id
};
}
export function generateGoalStatus(options?): GoalStatus {
if (!options) options = {};
return {
goal_id: generateGoalID(),
status:
typeof options.status !== "undefined" ? options.status : Status.SUCCEEDED
};
}
export function generateResult(options?): Result {
if (!options) options = {};
return {
status: generateGoalStatus(options.status),
result: typeof options.result !== "undefined" ? options.result : null
};
}
export function initGoal(
goal: any,
isGoal: (g: any) => boolean = g =>
typeof g === "object" && g !== null && !!g.goal_id
): Goal {
return isGoal(goal)
? goal
: {
goal_id: generateGoalID(),
goal
};
}
export function isEqualGoalID(first: GoalID, second: GoalID) {
if (!first || !second) {
return false;
}
return first.stamp === second.stamp && first.id === second.id;
}
export function isEqualGoal(first: Goal, second: Goal) {
if (first === null && second === null) {
return true;
}
if (!first || !second) {
return false;
}
return isEqualGoalID(first.goal_id, second.goal_id);
}
export function isEqualGoalStatus(first: GoalStatus, second: GoalStatus) {
return (
isEqualGoalID(first.goal_id, second.goal_id) &&
first.status === second.status
);
}
export function isEqualResult(first: Result, second: Result) {
if (!first || !second) {
return false;
}
// doesn't compare .result yet
return isEqualGoalStatus(first.status, second.status);
}
export function selectActionResult(actionName) {
return in$ =>
in$
.filter(
s =>
!!s &&
!!s[actionName] &&
!!s[actionName].outputs &&
!!s[actionName].outputs.result
)
.map(s => s[actionName].outputs.result)
.compose(dropRepeats(isEqualResult));
}