Skip to content

Instantly share code, notes, and snippets.

Avatar
😃

David Wells DavidWells

😃
View GitHub Profile
@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() {
@DavidWells
DavidWells / extend-segment-plugin.js
Last active Sep 25, 2020
Adding segment group functionality to analytics npm package. See the docs https://getanalytics.io/plugins/segment/#adding-group-functionality
View extend-segment-plugin.js
import Analytics from 'analytics'
import segmentPlugin from '@analytics/segment'
const originalSegmentInstance = segmentPlugin({
writeKey: '123-xyz'
})
// Extend originalSegmentInstance with custom methods
const enchancedSegmentInstance = Object.assign({}, originalSegmentInstance, {
methods: {
@DavidWells
DavidWells / serverless-function-reference-formats.yml
Created Sep 19, 2020
Various ways to reference function name and ARN in serverless.yml
View serverless-function-reference-formats.yml
function:
foo:
handler: index.handler
# Ways to reference function in serverless.yml
FunctionName: !Sub arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${self:service.name}-${self:provider.stage}-foo
FunctionName: !GetAtt FooLambdaFunction.Arn
FunctionName: !Sub "${AWS::StackName}-foo"
FunctionName: ${self:service.name}-${self:provider.stage}-foo