Skip to content

Instantly share code, notes, and snippets.

Avatar
😃

David Wells DavidWells

😃
View GitHub Profile
@DavidWells
DavidWells / github-markdown-preview.js
Created Apr 8, 2022
Preview Github Markdown Files with tiny lil server
View github-markdown-preview.js
#! /usr/bin/env node
// via https://github.com/sure-thing/mdpr/blob/main/index.js
import fs from 'fs'
import path from 'path'
import http from 'http'
import getPort from 'get-port'
import { micromark } from 'micromark'
import { gfm, gfmHtml } from 'micromark-extension-gfm'
import pocket from 'pocket.io'
@DavidWells
DavidWells / regex-match-json-fields.js
Created Mar 14, 2022
Regex match JSON Object keys
View regex-match-json-fields.js
// via https://stackoverflow.com/questions/8750127/regex-for-parsing-single-key-values-out-of-json-in-javascript
const obj1 = {
id: 1,
'name.1': '123',
address: {
'address.1': 'Chicken Dinner Road, 69',
'address.2': 'Psycho lane, 666',
},
'age.1': {
'thisIsSomeCrazyJson.3': 10,
@DavidWells
DavidWells / find-closest-object-prop-via-proxy.js
Created Mar 13, 2022
Find closest object value via levenstein algo & JS proxy
View find-closest-object-prop-via-proxy.js
// https://github.com/SiddharthShyniben/typosquatter
const peq = new Uint32Array(0x10000);
const myers_32 = (a, b) => {
const n = a.length;
const m = b.length;
const lst = 1 << (n - 1);
let pv = -1;
let mv = 0;
let sc = n;
let i = m;
@DavidWells
DavidWells / state-proxy.js
Last active Mar 16, 2022
State management via JS proxy
View state-proxy.js
// via https://twitter.com/judicael_andria/status/1501643071494180868
/* usage
createStore({
context: {
initialize state here
},
actions: {
add: (context, event) => {}
}
@DavidWells
DavidWells / github-proxy-client.js
Last active Jun 30, 2022
Full Github REST api in 34 lines of code
View github-proxy-client.js
/* Ultra lightweight Github REST Client */
// original inspiration via https://gist.github.com/v1vendi/75d5e5dad7a2d1ef3fcb48234e4528cb
const token = 'github-token-here'
const githubClient = generateAPI('https://api.github.com', {
headers: {
'User-Agent': 'xyz',
'Authorization': `bearer ${token}`
}
})
@DavidWells
DavidWells / thunkify.js
Created Mar 10, 2022
Defer execution of function and preserve arguments. Aka thunkify
View thunkify.js
// via https://www.oreilly.com/library/view/you-dont-know/9781491905197/ch04.html
function foo(x, y) {
return x + y
}
function thunkify(fn) {
var args = [].slice.call( arguments, 1 );
return function(cb) {
args.push( cb );
return fn.apply( null, args );
@DavidWells
DavidWells / attach-multiple-onerror-listeners.js
Created Mar 1, 2022
Attach multiple onError or onLoad listeners
View attach-multiple-onerror-listeners.js
// Fork of https://gist.github.com/alexreardon/8833460
function addWindowEvent(event, fn) {
const existing = window[event]
if (typeof existing !== 'function') return fn
return function () {
existing.apply(window, arguments)
fn.apply(window, arguments)
}
}
@DavidWells
DavidWells / regex-find-code-blocks.js
Last active Feb 27, 2022
Find all ``` code blocks in markdown
View regex-find-code-blocks.js
const fs = require('fs')
const path = require('path')
const content = fs.readFileSync(path.resolve(__dirname, 'zmd-with-code.md'), 'utf-8')
// https://regex101.com/r/nIlW1U/6
const PATTERN = /^([A-Za-z \t]*)```([A-Za-z]*)?\n([\s\S]*?)```([A-Za-z \t]*)*$/gm
function findCodeBlocks(block) {
let matches
let errors = []
@DavidWells
DavidWells / async-forEach-with-abort-and-concurrency.js
Created Feb 26, 2022
Async forEach with concurrency limit and Abort Controller
View async-forEach-with-abort-and-concurrency.js
// via https://github.com/vatesfr/xen-orchestra/tree/a1c0d82889dbf40aebb87d6e486dc12fee49adbc/%40vates/async-each
const noop = Function.prototype
class AggregateError extends Error {
constructor(errors, message) {
super(message)
this.errors = errors
}
}
@DavidWells
DavidWells / proxy-factory.js
Created Feb 25, 2022
JS factory functions via a proxy
View proxy-factory.js
// https://github.com/Schniz/factoree
/**
* Creates a strict factory
*
* @param defaults the default values that would appear on all instances
*/
function factory(defaults) {
return attributes => {
const data = Object.assign(Object.assign({}, defaults), attributes);
const proxy = new Proxy(data, {