Skip to content

Instantly share code, notes, and snippets.

Avatar
😃

David Wells DavidWells

😃
View GitHub Profile
@DavidWells
DavidWells / zoom-webinar-lambda.js
Created May 4, 2021
Zoom Webinar Lambda. Register Person for webinar programatically
View zoom-webinar-lambda.js
const jwt = require('jsonwebtoken')
const serverless = require('serverless-http')
const express = require('express')
const axios = require('axios')
const cors = require('cors')
const app = express()
app.use(express.json())
app.use(cors())
@DavidWells
DavidWells / parse-csv-and-iterate-with-delay.js
Last active Mar 16, 2021
Parse CSV file and iterate over values with artificial delay
View parse-csv-and-iterate-with-delay.js
/*
"csv-parse": "^4.15.3",
"follow-redirects": "^1.13.3",
*/
const path = require('path')
const fs = require('fs').promises
const parse = require('csv-parse')
const { https } = require('follow-redirects')
const parseFullName = require('./src/utils/parse-name')
@DavidWells
DavidWells / random-true-false.js
Created Feb 27, 2021
Random true / false in JS
View random-true-false.js
function randomFromRange(start, end) {
return Math.round(Math.random() * (end - start) + start);
}
function randomTrueFalse() {
return randomFromRange(0, 1) ? true : false;
}
@DavidWells
DavidWells / compose-functions.js
Last active Jan 30, 2021
Compose functions from left to right and right to left
View compose-functions.js
// Compose function from right to left. a.k.a. normal 'compose' in functional programming
function composeFromRight(...fns) {
return (initialVal) => fns.reduceRight((val, fn) => fn(val), initialVal)
}
// Compose functions from left to right. a.k.a. normal 'pipe' in functional programming
function composeFromLeft(...fns) {
return (initialVal) => fns.reduce((val, fn) => fn(val), initialVal)
}
// Functions to compose
@DavidWells
DavidWells / cache-manager-fs-hash.js
Created Jan 29, 2021
Temporary cache of API requests
View cache-manager-fs-hash.js
const path = require('path')
const cacheManager = require('cache-manager')
// storage for the cachemanager
const fsStore = require('cache-manager-fs')
const fsStoreHash = require('cache-manager-fs-hash');
// initialize caching on disk
const mbOfStorage = 512
@DavidWells
DavidWells / string-to-boolean.js
Created Jan 20, 2021
Convert string, number to boolean value
View string-to-boolean.js
function cleanBooleanParam(value) {
switch(value.toString().toLowerCase()) {
case "true":
case "on":
case "yes":
case "1":
return true;
case "false":
case "off":
case "no":
@DavidWells
DavidWells / _force-sync.js
Last active Dec 24, 2020
Force Async function to be sync for react server component. From https://davidwells.io/snippets/forcing-async-functions-to-sync-in-node
View _force-sync.js
const forceSync = require('sync-rpc')
const syncFunction = forceSync(require.resolve('./async-thing'))
console.log('before')
const paramOne = 'foo'
const paramTwo = 'bar'
const syncReturnValue = syncFunction(paramOne, paramTwo)
console.log('syncReturn', syncReturnValue)
@DavidWells
DavidWells / _config.js
Last active Dec 11, 2020
Helper utility to persist values between test files
View _config.js
/* Base Ava test config */
module.exports = {
tap: false,
verbose: true,
cache: true,
failFast: true,
failWithoutAssertions: false,
}
View custom-oclif-help-class.js
const { Help } = require('@oclif/plugin-help')
const chalk = require('chalk')
// https://github.com/oclif/plugin-help/blob/master/src/index.ts
module.exports = class MyHelpClass extends Help {
constructor(config, opts) {
super(config, opts)
const commandSlice = process.argv
console.log('commandSlice', commandSlice)
// Show all commands at root help
@DavidWells
DavidWells / command-with-listr.js
Created Sep 28, 2020
Oclif command with listr
View command-with-listr.js
const execa = require('execa')
const Listr = require('listr')
const { Observable } = require('rxjs')
const { Command, flags } = require('@oclif/command')
const timeout = ms => new Promise(res => setTimeout(res, ms))
class AddCommand extends Command {
async run() {