Skip to content

Instantly share code, notes, and snippets.

View krstffr's full-sized avatar

Kristoffer Klintberg krstffr

View GitHub Profile
@krstffr
krstffr / maybe.ts
Created September 11, 2019 13:00
typescript mabye monad, playing around
type Maybe<A> = {
type: string;
map: <B>(f: (x: A) => B) => Maybe<B | A>;
flatMap: <B>(f: (x: A) => Maybe<B>) => Maybe<B | A>;
valueOrDefault: <C>(x: C) => A | C;
};
function none<A>(x: A): Maybe<A> {
return {
type: "none",
Oh, you know, don't forget to add a security group which do not block outgoing mail traffic :)
For future reference (and my own memory): if you think that Amazon or Mailgun are not working on port 25, 465 or 587 and are running on some kind of cloud server, remember to check if ports are blocked on the server!
@krstffr
krstffr / debounced-redux-thunk-action.js
Created December 16, 2016 12:04
Debouncing redux thunk actions.
// A common redux pattern when dealing with async functions is to use thunk.
// This usually means your action returns a new function instead of an action object,
// and the thunk middleware will make it all work. Example:
const asyncAction = () => dispatch => setTimeout(() => dispatch(someOtherAction()), 10000);
// Now: maybe that async stuff going on is calling some API which you don't want to overload
// with request, and that's what debounce is for.
// This is an example of a debounced function which will only be calleable once every second.
import { debounce } from 'lodash';
const debouncedFn = debounce(() => callApi(), 1000, { leading: true, trailing: false });
@krstffr
krstffr / find-element-by-highest-key-js.js
Created December 7, 2016 12:59
Find element based on highest property value (Javascript)
// Let's say you have a collection like this one:
// [{ name: 'Kristoffer', IQ: 120 }, { name: 'Steve', IQ: 88 }, { name: 'Jim', IQ: 142 }]
// And you want to get the person with the highest IQ. It's very simple. You can just do this:
const findElementByHighestKey = (collection, key) =>
collection.reduce((memo, el) => el[key] > memo[key] ? el : memo, { [key]: 0 });
// A version with somem more safety (empty array? No problem!)
const findElementByHighestKey = (collection, key) =>
// Make sure we have a collection with items and a key…
!collection || !key || collection.length < 1
@krstffr
krstffr / get-all-heroku-env-vars.sh
Created November 27, 2016 13:29
Getting all your Heroku env vars from all your apps listed in one file!
# First store all of your apps in a file
heroku apps > heroku-apps.txt
# Manually remove everything which is not an app name from the file, for example the first line ("=== name@domain.com Apps")
# (Now you should have a file with only one appname per line)
# Pipe the text file into xargs -n1, which in turn passes every line into the "heroku-config -a $1" command, and then
# pass the result of that into the heroku-apps-with-config.txt
cat heroku-apps.txt | xargs -n1 heroku config -a $1 > heroku-apps-with-config.txt
# Get all of the env vars!
@krstffr
krstffr / sorting-array-from-another-array.js
Last active October 4, 2017 14:02
Sorting an array based on another array (JS)
// We have this array of people, we want to get a new array sorted using the names in the order array below.
const collection = [{ name: 'carol' }, { name: 'stewie' }, { name: 'steve' }, { name: 'carl' }];
const order = ['carl', 'steve', 'carol', 'horseboy', 'stewie'];
// Just map over the order, and find the corresponding item in the collection.
const sortedCollection = order.map(orderName => collection.find(({ name }) => name === orderName ));
// To also remove undefindes, if the order element isn't found in the collection,
// do this (filter out any falsy elements)
sortedCollection.filter(x => x);
@krstffr
krstffr / upload_file_size_rancher_docker.sh
Last active November 11, 2017 03:41
Updating the file max upload size for the Rancher Wordpress Docker image
# So, what we want to accomplish is this: add a uploads.ini file to the conf.d path (right now it's here: /usr/local/etc/php/conf.d)
# Go to the folder
cd /usr/local/etc/php/conf.d
# Create the file
touch upload.ini
# Add the required config stuff into the file
echo "file_uploads = On