timer, wip

This commit is contained in:
Flummi
2018-03-03 02:40:07 +01:00
parent 3bc2aa2b44
commit 14d5bdb183
4 changed files with 54 additions and 1 deletions

View File

@@ -0,0 +1,32 @@
import lt from "long-timeout";
export default new class timer {
constructor() {
this.regex = /\d+[smhdwy]|mon/;
this.calc = {
y: val => val * 365 * 24 * 60 * 60, // years
mon: val => val * 30 * 24 * 60 * 60, // months
w: val => val * 7 * 24 * 60 * 60, // weeks
d: val => val * 24 * 60 * 60, // days
h: val => val * 60 * 60, // hours
m: val => val * 60, // minutes
s: val => val // seconds
};
}
add(time, fn, begin = ~~(Date.now()/1000)) {
if(this.regex.test(time)) {
let seconds = 0;
time.match(/\d+(mon|[smhdwy])/g).forEach(t => {
const [,val,mod] = t.match(/(\d+)(mon|[smhdwy])/);
seconds += parseInt(this.calc[mod](val));
})
const rest = seconds - (begin - ~~(Date.now() / 1000));
lt.setTimeout(() => {
fn();
}, rest * 1000);
return `timer eingetragen (${rest}s)`;
}
else
return "nope";
}
}