Skip to content

Instantly share code, notes, and snippets.

View kevinkace's full-sized avatar

Kevin Cameron kevinkace

View GitHub Profile
@kevinkace
kevinkace / object-reduce.js
Created October 24, 2019 04:32
Reduce an object
function objectReduce(obj, cb, acc) {
let idx = 0;
for (let key in obj) {
acc = cb(acc, { key, value : obj[key] }, idx++);
}
return acc;
}
@kevinkace
kevinkace / someValue.js
Last active November 1, 2019 21:28
like [].find(), but return a value of your choosing
function findValue(collection, predicate) {
let value;
collection.some(c => {
const p = predicate(c)
if (p) {
value = p;
return true;
@kevinkace
kevinkace / timer.js
Last active June 17, 2021 20:12
see how long things take, without the fuckery of perf_hooks, so it's probably not terribly accurate, but it's easy
// usage:
// const timer = new Timer("timerName");
// doThing();
// timer.measure("didThing");
// await doThing2();
// timer.measure("didThing2");
// timer.report();
// ┌─────────┬─────────────┬───────────────┬──────────┬───────────┐
// │ (index) │ name │ time │ duration │ sincePrev │
// ├─────────┼─────────────┼───────────────┼──────────┼───────────┤
[ ...document.querySelectorAll("tbody tr") ]
.filter(tr => tr.querySelector("td[data-text]")?.attributes["data-text"].value.includes("404"))
.map(tr => {
const td = tr.querySelector("[data-text]");
const ts = tr.querySelector("td:nth-child(2)");
const { value } = td.attributes["data-text"];
const url = value.match(/url: (.*?)[;}]/)[1];
const referer = value.match(/referer: (.*?)[;}]/);