This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const select = (obj, propArr) => | |
| propArr.reduce((acc, val) => { | |
| acc[val] = obj[val]; | |
| return acc; | |
| }, {}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const getCacheAll = async (name, q) => { | |
| const cache = await caches.open(name); | |
| return cache.matchAll(q); | |
| }; | |
| const logText = (matches) => | |
| Promise.all(matches.map((r) => r.text().then((txt) => [r.url, txt]))); | |
| async function clearCache(name) { | |
| const cache = await caches.open(name); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| async function connectToWindow(listener) { | |
| const port = await new Promise((resolve) => { | |
| const handleMessage = (e) => { | |
| if (e.data.type === "__messaging_connect__") { | |
| resolve(e.ports[0]); | |
| self.removeEventListener("message", handleMessage); | |
| } | |
| }; | |
| self.addEventListener("message", handleMessage); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function buildQueryStr(obj) { | |
| const s = new URLSearchParams(); | |
| Object.keys(obj).forEach((key) => { | |
| s.set(key, obj[key]); | |
| }); | |
| return s.toString(); | |
| } | |
| const randomBetween = (min, max) => { | |
| return Math.round(Math.random() * (max - min)) + min; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function el(...args) { | |
| const argMap = { String: "type", Object: "props", Array: "children" }; | |
| args.forEach((arg) => (args[argMap[arg.constructor.name]] = arg)); | |
| if (!args.props && !args.children) return document.createTextNode(args.type); | |
| const $el = document.createElement(args.type); | |
| for (let key in args.props || {}) { | |
| $el[key.toLowerCase()] === null | |
| ? $el.addEventListener(key.substr(2).toLowerCase(), args.props[key]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function date(date = new Date(), props = {}) { | |
| [date, props] = | |
| date.constructor.name === "Object" | |
| ? [new Date(), date] | |
| : [new Date(date), props]; | |
| for (let key in props) { | |
| if (typeof props[key] === "string" && props[key].match(/^[+-]/)) | |
| props[key] = date[key.replace(/^set/, "get")]() + parseInt(props[key]); | |
| date[key](props[key]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function sortBy(objArr, key, ascDesc = "asc") { | |
| const sortingFunc = | |
| ascDesc === "desc" ? (a, b) => b[key] - a[key] : (a, b) => a[key] - b[key]; | |
| return [...objArr].sort(sortingFunc); | |
| } | |
| /* | |
| const data = [ | |
| { order: 1 }, | |
| { order: 3 }, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const group = (arr, func) => | |
| arr.reduce((acc, val) => { | |
| (acc[func(val)] = acc[func(val)] || []).push(val); | |
| return acc; | |
| }, {}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const path = require("path"); | |
| const exec = (command, options = {}) => | |
| require("child_process").execSync(command, { encoding: "utf-8", ...options }); | |
| const crontabRead = () => exec("crontab -l").trim(); | |
| const crontabWrite = (str) => exec(`echo "${str}" | crontab`); | |
| function parseCrontab() { | |
| return crontabRead() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const path = require("path"); | |
| function* walkSync(dir) { | |
| const files = fs.readdirSync(dir, { withFileTypes: true }); | |
| for (const file of files) { | |
| file.isDirectory() | |
| ? yield* walkSync(path.join(dir, file.name)) | |
| : yield path.join(dir, file.name); | |
| } | |
| } |