This file contains 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 fetch = async (...args) => { | |
console.log(...args) | |
return { | |
statusCode: 200, | |
data: {}, | |
} | |
} | |
function httpRequest(url, method, data, opts, cb) { | |
const init = { method } |
This file contains 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
// Via https://twitter.com/RReverser/status/1490873967577640961 | |
function wwwProxy() { | |
return new Proxy(new URL('https://www/'), { | |
get: function get(target, prop) { | |
let orig = Reflect.get(target, prop); | |
if (typeof orig === 'function') return orig.bind(target); | |
if (typeof prop !== 'string') return orig; | |
if (prop === 'then') return Promise.prototype.then.bind(fetch(target)); | |
target = new URL(target); | |
target.hostname += `.${prop}`; |
This file contains 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
// via https://blog.sessionstack.com/how-javascript-works-proxy-and-reflect-11748452c695 | |
const p = new Proxy(function() {}, { | |
apply: function(target, thisArg, argumentsList) { | |
console.log('called: ' + argumentsList.join(', ')); | |
return argumentsList[0] + argumentsList[1] + argumentsList[2]; | |
} | |
}); | |
console.log(p(1, 2, 3)); // This will print called: 1, 2, 3 and 6 |
This file contains 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
// https://betterprogramming.pub/everything-you-should-know-about-javascript-proxy-67576f2e069e | |
function setPrivateField(obj, prefix = "_"){ | |
return new Proxy(obj, { | |
has: (obj, prop) => { | |
if(typeof prop === "string" && prop.startsWith(prefix)){ | |
return false | |
} | |
return prop in obj | |
}, | |
ownKeys: obj => { |
This file contains 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
// via https://stackoverflow.com/questions/46005706/get-the-path-to-the-accessed-value-in-a-nested-object | |
function wrap(o, fn, scope = []) { | |
const handler = { | |
set(target, prop, value, receiver) { | |
fn('set value in scope: ', scope.concat(prop)) | |
target[prop] = value | |
return true | |
}, | |
get(target, prop, receiver) { | |
fn('get value in scope: ', scope.concat(prop)) |
This file contains 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
/* Using a JavaScript proxy for a super low code REST client */ | |
// via https://dev.to/dipsaus9/javascript-lets-create-aproxy-19hg | |
// also see https://towardsdatascience.com/why-to-use-javascript-proxy-5cdc69d943e3 | |
// also see https://github.com/fastify/manifetch | |
// also see https://github.com/flash-oss/allserver | |
// and https://gist.github.com/v1vendi/75d5e5dad7a2d1ef3fcb48234e4528cb | |
const createApi = (url) => { | |
return new Proxy({}, { | |
get(target, key) { |
This file contains 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 { URL } = require('url') | |
const assert = require('assert') | |
const DEFAULT_BASE = 'https://api.github.com' | |
/* Zero dependency backward compatible url parser */ | |
function parseUrl(url) { | |
const match = url.match(/^(https?)?(?:[\:\/]*)([a-z0-9\.-]*)(?:\:(\d+))?(\/[^?#]*)?(?:\?([^#]*))?(?:#(.*))?$/i) | |
return { | |
protocol: match[1] || '', |
This file contains 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 process = require('process') | |
const styles = require('ansi-styles') | |
// via https://github.com/sindresorhus/is-unicode-supported/blob/main/index.js | |
function isUnicodeSupported() { | |
if (process.platform !== 'win32') return process.env.TERM !== 'linux'; // Linux console (kernel) | |
return Boolean(process.env.CI) | |
|| Boolean(process.env.WT_SESSION) // Windows Terminal | |
|| process.env.ConEmuTask === '{cmd::Cmder}' // ConEmu and cmder | |
|| process.env.TERM_PROGRAM === 'vscode' || process.env.TERM === 'xterm-256color' || process.env.TERM === 'alacritty'; |
This file contains 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
// via https://github.com/nalgeon/sqlime/blob/3.37.2/js/gister.js | |
// https://twitter.com/simonw/status/1482845419637903366 | |
// Github Gist API client | |
const HEADERS = { | |
Accept: "application/json", | |
"Content-Type": "application/json", | |
}; | |
class Gister { |
This file contains 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 cronstrue = require('cronstrue') | |
// https://github.com/harrisiirak/cron-parser | |
const MONTHS = [null, 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] | |
const DAYS = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] | |
const dayNum = { | |
sun: '0', | |
mon: '1', | |
tue: '2', | |
wed: '3', |