This repository has been archived by the owner on Dec 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgroup.ts
62 lines (49 loc) · 1.81 KB
/
group.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
// Copyright (c) 2017 Victorien Elvinger, Matthieu Nicolas
//
// Licensed under the zlib license (https://opensource.org/licenses/zlib).
import { SafeAny } from "../"
import { Person } from "./person"
export class Group {
readonly participants: Person[]
readonly leader?: Person
hasLeader (): boolean {
return this.leader !== undefined
}
constructor (participants: Person[], leader?: Person) {
this.participants = participants
this.leader = leader
}
/**
* @param x
* @return Group from x. undefined if x is mal-formed
*/
static fromPlain (x: SafeAny<Group>): Group | undefined {
if (typeof x === "object" && x !== null &&
x.participants instanceof Array) {
// Well-formed if all partiicipants are well-formed.
const participants: Person[] | undefined = x.participants.reduce (
(acc, itm: SafeAny<Person>) => {
if (acc !== undefined) {
const p: Person | undefined = Person.fromPlain(itm)
if (p !== undefined) {
return [p, ...acc]
}
}
return undefined
}, [])
if (participants !== undefined) {
// Well-formed if no leader or if well-formed leader
// Accept null value for no leader
if (x.leader === undefined || x.leader === null) {
return new Group(participants)
} else {
const leader = Person.fromPlain(x.leader)
if (leader !== undefined) {
return new Group(participants, leader)
}
}
}
}
return undefined
}
}