Packages

An easy to use frontend framework written in Gleam.

Current section

Files

Jump to
novdom src prototypes.mjs
Raw

src/prototypes.mjs

import { List } from "../prelude.mjs"
export default function apply() {
String.prototype.smartSplit = function (separator) {
return this.split(separator).filter((s) => s.length > 0)
}
Array.prototype.toList = function () {
return List.fromArray(this)
}
List.prototype.map = function (fn) {
let arr = []
for (const item of this) {
arr.push(fn(item))
}
return arr
}
List.prototype.forEach = function (fn) {
for (const item of this) {
fn(item)
}
}
const old_addEventListener = EventTarget.prototype.addEventListener
EventTarget.prototype.addEventListener = function (...args) {
old_addEventListener.apply(this, args)
const name = args[0] + "Listeners"
if (this[name] === undefined) {
this[name] = [args[1]]
return
}
this[name] = [args[1], ...this[name]]
}
const old_removeEventListener = EventTarget.prototype.removeEventListener
EventTarget.prototype.removeEventListener = function (...args) {
const name = args[0] + "Listeners"
if (args.length === 1) {
if (this[name] === undefined) {
return
}
this[name].forEach((l) => this.removeEventListener(args[0], l))
return
}
old_removeEventListener.apply(this, args)
if (this[name] === undefined) {
return
}
this[name] = this[name].filter((l) => l !== args[1])
}
}