Skip to content

Instantly share code, notes, and snippets.

View sscovil's full-sized avatar

Shaun Scovil sscovil

View GitHub Profile
@sscovil
sscovil / create_table_foobar.sql
Created January 25, 2022 16:41
Example of update trigger to set updated_at to current timestamp
CREATE TABLE IF NOT EXISTS foobar (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
created_at TIMESTAMPTZ NOT NULL DEFAULT timezone('utc', now()),
updated_at TIMESTAMPTZ
);
CREATE OR REPLACE FUNCTION foobar_table_but()
RETURNS TRIGGER
LANGUAGE plpgsql
AS '
@sscovil
sscovil / utils.js
Created January 3, 2022 06:14
ES6 functions for calculating factorials and finding all possible combinations of an array
export function factorial(num) {
if (num < 0) return -1;
else if (num === 0) return 1;
return num * factorial(num - 1);
}
export function allPossibleCombos(list, size) {
if (size > list.length) return [];
const results = [];
<?php
class API {
public static function fetchUser($userID) {
static $cache = [];
$url = "https://example.com/users";
$method = "GET";
if (isset($cache[$url][$method][$userID])) {
@sscovil
sscovil / public.asc
Last active May 7, 2018 20:43
PGP Public Key
-----BEGIN PGP PUBLIC KEY BLOCK-----
Comment: GPGTools - https://gpgtools.org
mQINBFrwuf0BEADF1LS3UCcPN4Jw0a/UWpKtqN31unqpoTGZ17hst/4Qqo7N/zeE
txpT/LBv13o2uQNEPen9s9HgwvnZI4JMh90Yvr53uSedI0qHKdHWF6JvG90JW702
HySmDN2sZdXofB+Knk3OszQdgYpNBMTkulw+q3qAnUhkvyHe3mp/iFj3LO+U6ILV
CYlZ1Ds7HnVfKrLm0kBdaiVvKXySQ0O+J/vQZejdXLNa55CXWANUF1KhngcZO814
cFtKgEJqzHCS+0NqetqLZ/NXLPiCuEDoK7cKAFqxj2kcXAsPF3JrXuMZqXNph7Bd
Y6mO70kv76XqUbZdPwTtSgRWS6LJPELNe4M9FCKZYbKwwRX8mH4EncLwgGmFEjaO
mZwwAcumHx8/tJV4sdopCEKqjI6VUL0teM92tKyZPXUafrJDpGd3uQxICeAp5xmG
@sscovil
sscovil / until.js
Created February 20, 2018 19:30
A simple utility for Node.js to wait until a predicate function returns truthy before moving on; for use with ES6 async/await syntax.
/**
* Utility that waits for @predicate function to return truthy, testing at @interval until @timeout is reached.
*
* Example: await until(() => spy.called);
*
* @param {Function} predicate
* @param {Number} interval
* @param {Number} timeout
*
* @return {Promise}
@sscovil
sscovil / base.js
Last active January 2, 2018 19:09
Objection.js base model with code coverage
'use strict';
const Knex = require('knex');
const { knexSnakeCaseMappers, Model } = require('objection');
const uuid = require('uuid');
const knex = Knex({
client: 'pg',
connection: {
@sscovil
sscovil / custom.css
Last active May 24, 2017 13:29
CSS Exercise: What will the output look like, based on the CSS? By only editing custom.css, change the color of "Item 1" to purple.
/* Add custom CSS rules here */
@sscovil
sscovil / en.json
Last active June 14, 2020 08:38
Node Express server with i18next. Language files go in a `locales/` directory.
{
"home": {
"title": "Hello World!"
}
}
@sscovil
sscovil / mixin.js
Created October 5, 2016 22:46
Shallow (non-recursive) object key-to-camelCase mapper Lodash mixin
const _ = require('lodash');
function camelCaseKeys(obj) {
return _.mapKeys(obj, (value, key) => {
return _.camelCase(key);
});
}
_.mixin({camelCaseKeys});
@sscovil
sscovil / index.js
Last active September 17, 2016 23:43
Node.js recipe for an index.js file that exports all other .js files in the same directory.
'use strict';
const fs = require('fs');
const JS_FILE = /^((?!(index)).)*\.js$/;
const JS_FILE_EXTENSION = /\.js$/;
fs.readdirSync(__dirname)
.filter(file => JS_FILE.test(file))
.forEach(file => exports[file.replace(JS_FILE_EXTENSION, '')] = require(`./${file}`));