Skip to content

Instantly share code, notes, and snippets.

View DavidWells's full-sized avatar
😃

David Wells DavidWells

😃
View GitHub Profile
@DavidWells
DavidWells / stringify-object.js
Created September 13, 2023 23:23
stringify object into one line
const props = {
text: 'hello',
boolean: true,
array: ['hi', 'there', true],
object: {
cool: true,
nice: 'awesome'
},
func: () => {},
@DavidWells
DavidWells / highlightMatch.js
Created April 12, 2023 06:37
Highlight text in a string of content
// Used to match HTML entities and HTML characters.
const unescapedHtml = /[&<>"']/g
const hasUnescapedHtml = RegExp(unescapedHtml.source)
const htmlEscapes = {
"&": "&amp",
"<": "&lt",
">": "&gt",
'"': "&quot",
"'": "&#39"
}
@DavidWells
DavidWells / debug-node-script.js
Last active January 3, 2023 23:21
How to easily debug node.js script
const { inspect } = require('util')
/* Log out everything in the deep array/object */
function deepLog(obj) {
console.log(inspect(obj, {showHidden: false, depth: null, colors: true}))
}
function myScript(input) {
/* Lots of crap */
/* And lots more crap */
const fs = require('fs')
const path = require('path')
const cacheManager = require('cache-manager')
const fsStoreHash = require('cache-manager-fs-hash')
const CACHE_KEY = 'foo'
const STORAGE_PATH = (process.env.IS_OFFLINE) ? path.join(__dirname, '../tmp') : '/tmp'
const SECONDS = 60
const MINUTES = 60
const ONE_HOUR = SECONDS * MINUTES
const mbOfStorage = 512
@DavidWells
DavidWells / github-markdown-preview.js
Created April 8, 2022 19:35
Preview Github Markdown Files with tiny lil server
#! /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 March 14, 2022 01:46
Regex match JSON Object keys
// 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 March 13, 2022 06:58
Find closest object value via levenstein algo & JS proxy
// 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 March 16, 2022 09:51
State management via JS proxy
// 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 March 15, 2024 08:28
Full Github REST api in 34 lines of code
/* 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 March 10, 2022 09:23
Defer execution of function and preserve arguments. Aka thunkify
// 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 );