1 function timeUnitToSeconds(value
, unit
) {
5 seconds
*= 86400; //24*60*60
17 // Used only if increment, hence live game: no "day" unit
18 function isLargerUnit(unit1
, unit2
) {
20 (unit1
== "h" && ["s", "m"].includes(unit2
)) ||
21 (unit1
== "m" && unit2
== "s")
25 export function extractTime(cadence
) {
26 let tcParts
= cadence
.replace(/ /g
, "").split("+");
27 // Concatenate usual time control suffix, in case of none is provided
29 const mainTimeArray
= tcParts
[0].match(/^([0-9]+)([smhd]+)$/);
30 if (!mainTimeArray
) return null;
31 const mainTimeValue
= parseInt(mainTimeArray
[1], 10);
32 const mainTimeUnit
= mainTimeArray
[2][0];
33 const mainTime
= timeUnitToSeconds(mainTimeValue
, mainTimeUnit
);
35 if (tcParts
.length
>= 2) {
36 // Correspondance games don't use an increment:
37 if (mainTimeUnit
== 'd') return null;
39 const incrementArray
= tcParts
[1].match(/^([0-9]+)([smhd]+)$/);
40 if (!incrementArray
) return null;
41 const incrementValue
= parseInt(incrementArray
[1], 10);
42 const incrementUnit
= incrementArray
[2][0];
43 // Increment unit cannot be larger than main unit:
44 if (isLargerUnit(incrementUnit
, mainTimeUnit
)) return null;
45 increment
= timeUnitToSeconds(incrementValue
, incrementUnit
);
47 return { mainTime: mainTime
, increment: increment
};