/**
* jQuery Bracket
*
* Copyright (c) 2011-2018, Teijo Laine,
* http://aropupu.fi/bracket/
*
* Licenced under the MIT licence
*/
// tslint:disable-next-line: no-reference
///
($ => {
class Option {
public static of(value: A | null): Option {
return new Option(value);
}
public static empty(): Option {
return new Option(null);
}
protected constructor(private val: A | null) {
if (val instanceof Option) {
throw new Error("Trying to wrap Option into an Option");
}
if (this.val === undefined) {
throw new Error("Option cannot contain undefined");
}
}
public get(): A {
if (this.val === null) {
throw new Error("Trying to get() empty Option");
}
return this.val;
}
public orElse(defaultValue: A): A {
return this.val === null ? defaultValue : this.val;
}
public orElseGet(defaultProvider: () => A): A {
return this.val === null ? defaultProvider() : this.val;
}
public map(f: (A) => B): Option {
return this.val === null ? Option.empty() : new Option(f(this.val));
}
public forEach(f: (A) => void): Option {
if (this.val !== null) {
f(this.val);
}
return this;
}
public toNull() {
return this.val === null ? null : this.val;
}
public isEmpty(): boolean {
return this.val === null;
}
}
class Score extends Option {
public static of(val: N | null) {
const type = typeof val;
const expected = "number";
if (val !== null && type !== expected) {
throw new Error(
`Invalid score format, expected ${expected}, got ${type}`
);
}
return super.of(val);
}
public static empty(): Option {
return Option.empty();
}
}
interface Connector {
height: number;
shift: number;
}
type ConnectorProvider = (tc: JQuery, match: Match) => Connector;
class ResultObject {
constructor(
readonly first: Score,
readonly second: Score,
readonly userData: any
) {
if (!first || !second) {
throw new Error("Cannot create ResultObject with undefined scores");
}
return;
}
}
type MatchCallback = (
round: Round,
match: MatchResult,
seed: number,
results: Option,
renderCb: Option,
isFirstBracket: boolean,
opts: Options
) => Match;
type RenderCallback = (match: Match) => boolean;
enum BranchType {
TBD,
BYE,
END
}
class Order {
public static first(): Order {
return new Order(true);
}
public static second(): Order {
return new Order(false);
}
private constructor(private isFirst: boolean) {}
public map(first: A, second: A): A {
return this.isFirst ? first : second;
}
}
// Hack to get the branch leaf (round 0) "name" lazily if it's modified
type NameGetter = () => Option