aboutsummaryrefslogtreecommitdiff
path: root/static/lib.ts
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2021-10-04 23:18:24 +0200
committerHugo Hörnquist <hugo@lysator.liu.se>2021-10-04 23:18:24 +0200
commit8ec2f441d40ab89b40cc3158f65c914eff497cee (patch)
treec05638cd570a641234ad5973a1790762e7bc8cca /static/lib.ts
parent{.js => .ts} on relavant files. (diff)
downloadcalp-8ec2f441d40ab89b40cc3158f65c914eff497cee.tar.gz
calp-8ec2f441d40ab89b40cc3158f65c914eff497cee.tar.xz
Major typescript work.
Diffstat (limited to 'static/lib.ts')
-rw-r--r--static/lib.ts154
1 files changed, 95 insertions, 59 deletions
diff --git a/static/lib.ts b/static/lib.ts
index 100f4161..35b6f867 100644
--- a/static/lib.ts
+++ b/static/lib.ts
@@ -3,10 +3,39 @@
General procedures which in theory could be used anywhere.
*/
+interface Object {
+ format: (fmt: string) => string
+}
+
+interface HTMLElement {
+ _addEventListener: (name: string, proc: (e: Event) => void) => void
+ listeners: Record<string, ((e: Event) => void)[]>
+}
+
+interface Date {
+ dateonly: boolean
+ utc: boolean
+ type: 'date' | 'date-time'
+}
+
+interface DOMTokenList {
+ find: (regex: string) => [number, string] | undefined
+}
+
+interface HTMLCollection {
+ forEach: (proc: ((el: Element) => void)) => void
+}
+
+interface HTMLCollectionOf<T> {
+ forEach: (proc: ((el: T) => void)) => void
+}
+
+type uid = string
+
HTMLElement.prototype._addEventListener = HTMLElement.prototype.addEventListener;
-HTMLElement.prototype.addEventListener = function (name, proc) {
- if (! this.listeners) this.listeners = {};
- if (! this.listeners[name]) this.listeners[name] = [];
+HTMLElement.prototype.addEventListener = function(name: string, proc: (e: Event) => void) {
+ if (!this.listeners) this.listeners = {};
+ if (!this.listeners[name]) this.listeners[name] = [];
this.listeners[name].push(proc);
return this._addEventListener(name, proc);
};
@@ -14,7 +43,8 @@ HTMLElement.prototype.addEventListener = function (name, proc) {
/* list of lists -> list of tuples */
-function zip(...args) {
+/* TODO figure out how to type this correctly */
+function zip(...args: any[]) {
// console.log(args);
if (args === []) return [];
return [...Array(Math.min(...args.map(x => x.length))).keys()]
@@ -37,8 +67,14 @@ function zip(...args) {
century, due to how javascript works (...).
*/
-function parseDate(str) {
- let year, month, day, hour=false, minute, second=0, utc;
+function parseDate(str: string): Date {
+ let year: number;
+ let month: number;
+ let day: number;
+ let hour: number | false = false;
+ let minute: number = 0;
+ let second: number = 0;
+ let utc: boolean = false;
let end = str.length - 1;
if (str[end] == 'Z') {
@@ -47,18 +83,18 @@ function parseDate(str) {
};
switch (str.length) {
- case '2020-01-01T13:37:00'.length:
- second = str.substr(17,2);
- case '2020-01-01T13:37'.length:
- hour = str.substr(11,2);
- minute = str.substr(14,2);
- case '2020-01-01'.length:
- year = str.substr(0,4);
- month = str.substr(5,2) - 1;
- day = str.substr(8,2);
- break;
- default:
- throw 'Bad argument';
+ case '2020-01-01T13:37:00'.length:
+ second = +str.substr(17, 2);
+ case '2020-01-01T13:37'.length:
+ hour = +str.substr(11, 2);
+ minute = +str.substr(14, 2);
+ case '2020-01-01'.length:
+ year = +str.substr(0, 4);
+ month = +str.substr(5, 2) - 1;
+ day = +str.substr(8, 2);
+ break;
+ default:
+ throw 'Bad argument';
}
let date;
@@ -73,32 +109,32 @@ function parseDate(str) {
return date;
}
-function copyDate(date) {
+function copyDate(date: Date): Date {
let d = new Date(date);
d.utc = date.utc;
d.dateonly = date.dateonly;
return d;
}
-function to_local(date) {
- if (! date.utc) return date;
+function to_local(date: Date): Date {
+ if (!date.utc) return date;
return new Date(date.getTime() - date.getTimezoneOffset() * 60 * 1000);
}
/* -------------------------------------------------- */
-function makeElement (name, attr={}) {
- let element = document.createElement(name);
+function makeElement(name: string, attr = {}): HTMLElement {
+ let element: HTMLElement = document.createElement(name);
for (let [key, value] of Object.entries(attr)) {
- element[key] = value;
+ (element as any)[key] = value;
}
return element;
}
-function round_time (time, fraction) {
+function round_time(time: number, fraction: number): number {
let scale = 1 / fraction;
- return Math.round (time * scale) / scale;
+ return Math.round(time * scale) / scale;
}
/* only used by the bar.
@@ -107,20 +143,20 @@ function round_time (time, fraction) {
Just doing (new Date()/(86400*1000)) would be nice, but there's no good
way to get the time in the current day.
*/
-function date_to_percent (date) {
- return (date.getHours() + (date.getMinutes() / 60)) * 100/24;
+function date_to_percent(date: Date): number /* in 0, 100 */ {
+ return (date.getHours() + (date.getMinutes() / 60)) * 100 / 24;
}
/* if only there was such a thing as a let to wrap around my lambda... */
/* js infix to not collide with stuff generated backend */
-const gensym = (counter => (prefix="gensym") => prefix + "js" + ++counter)(0)
+const gensym = (counter => (prefix = "gensym") => prefix + "js" + ++counter)(0)
-function setVar(str, val) {
- document.documentElement.style.setProperty("--" + str, val);
+function setVar(str: string, val: any) {
+ document.documentElement.style.setProperty("--" + str, val);
}
-function asList(thing) {
+function asList<T>(thing: Array<T> | T): Array<T> {
if (thing instanceof Array) {
return thing;
} else {
@@ -129,44 +165,44 @@ function asList(thing) {
}
-function boolean (value) {
+function boolean(value: any): boolean {
switch (typeof value) {
- case 'string':
- switch (value) {
- case 'true': return true;
- case 'false': return false;
- case '': return false;
- default: return true;
- }
- case 'boolean':
- return value;
- default:
- return !! value;
+ case 'string':
+ switch (value) {
+ case 'true': return true;
+ case 'false': return false;
+ case '': return false;
+ default: return true;
+ }
+ case 'boolean':
+ return value;
+ default:
+ return !!value;
}
}
/* internal */
-function datepad(thing, width=2) {
+function datepad(thing: number | string, width = 2): string {
return (thing + "").padStart(width, "0");
}
-function format_date(date, str) {
+function format_date(date: Date, str: string): string {
let fmtmode = false;
let outstr = "";
for (var i = 0; i < str.length; i++) {
if (fmtmode) {
switch (str[i]) {
/* Moves the date into local time. */
- case 'L': date = to_local(date); break;
- case 'Y': outstr += datepad(date.getFullYear(), 4); break;
- case 'm': outstr += datepad(date.getMonth() + 1); break;
- case 'd': outstr += datepad(date.getDate()); break;
- case 'H': outstr += datepad(date.getHours()); break;
- case 'M': outstr += datepad(date.getMinutes()); break;
- case 'S': outstr += datepad(date.getSeconds()); break;
- case 'Z': if (date.utc) outstr += 'Z'; break;
+ case 'L': date = to_local(date); break;
+ case 'Y': outstr += datepad(date.getFullYear(), 4); break;
+ case 'm': outstr += datepad(date.getMonth() + 1); break;
+ case 'd': outstr += datepad(date.getDate()); break;
+ case 'H': outstr += datepad(date.getHours()); break;
+ case 'M': outstr += datepad(date.getMinutes()); break;
+ case 'S': outstr += datepad(date.getSeconds()); break;
+ case 'Z': if (date.utc) outstr += 'Z'; break;
}
fmtmode = false;
} else if (str[i] == '~') {
@@ -178,17 +214,17 @@ function format_date(date, str) {
return outstr;
}
-Object.prototype.format = function (/* any number of arguments */) { return "" + this; }
-Date.prototype.format = function (str) { return format_date (this, str); }
+Object.prototype.format = function(/* any number of arguments */) { return "" + this; }
+Date.prototype.format = function(str) { return format_date(this, str); }
/*
* Finds the first element of the DOMTokenList whichs value matches
* the supplied regexp. Returns a pair of the index and the value.
*/
-DOMTokenList.prototype.find = function (regexp) {
+DOMTokenList.prototype.find = function(regexp) {
let entries = this.entries();
let entry;
- while (! (entry = entries.next()).done) {
+ while (!(entry = entries.next()).done) {
if (entry.value[1].match(regexp)) {
return entry.value;
}
@@ -196,7 +232,7 @@ DOMTokenList.prototype.find = function (regexp) {
}
/* HTMLCollection is the result of a querySelectorAll */
-HTMLCollection.prototype.forEach = function (proc) {
+HTMLCollection.prototype.forEach = function(proc) {
for (let el of this) {
proc(el);
}