Skip to content

Instantly share code, notes, and snippets.

View joedski's full-sized avatar
💭
Ætheric

Joe joedski

💭
Ætheric
View GitHub Profile
@joedski
joedski / unboxing-parameters-and-return-types-and-key-enumerations.ts
Created June 28, 2018 03:16
Wherein I finally learn how to unbox and rebox things in Typescript for great genericization justice and the great annoyance of my coworkers.
interface BoxA<T> {
a: T;
}
interface BoxB<T> {
b: T;
}
type BoxerA<T> = (a: T) => BoxA<T>;
@joedski
joedski / enum.scss
Created March 19, 2018 18:57
Simple enum function for doing things like automatic z-indices.
@function enum($keys) {
$index-map: ();
$running-index: 1;
@each $k in $keys {
@if type-of($k) == list {
// NOTE: We do _not_ check if this value is greater than prior values.
// This is intentional.
$running-index: nth($k, 2);
$k: nth($k, 1);
}
@joedski
joedski / HOC-after-2.2.0.js
Created February 14, 2018 14:14 — forked from cades/HOC-after-2.2.0.js
higher-order component for vue.js 2
export default function(WrappedComponent) {
const mixinProps = (WrappedComponent.mixins || [])
.filter((mixin) => mixin.props)
.map((mixin) => mixin.props);
const allProps = mixinProps.concat(WrappedComponent.props);
const mergedProps = allProps.reduce((merged, props) => Object.assign(merged, props), {});
return {
props: mergedProps,
render(createElement) {
@joedski
joedski / fizzbuzz.js
Created February 14, 2018 00:08
Fizzbuzz!
function fizzbuzz(max) {
for (let i = 1; i <= max; ++i)
console.log(i, `${!(i % 3) ? 'fizz' : ''}${!(i % 5) ? 'buzz' : ''}` || '.');
}
function fizzbuzz(max) {
for (let i = 1; i <= max; ++i)
console.log(i, ((!(i % 3) ? 'fizz' : '') + (!(i % 5) ? 'buzz' : '')) || '.');
}
@joedski
joedski / repeatUntil.js
Last active February 9, 2018 01:29
Simple function to repeat a call until a certain condition is met.
function repeatUntil({ interval, repeat, until }) {
// let callTime;
let isCanceled = false;
function cancel() {
isCanceled = true;
}
function conditionallyCancel() {
if (until()) {
@joedski
joedski / comp-undo-steps-4-all.js
Last active September 30, 2017 23:04
comp-undo-steps 4: All.
const PLACEHOLDER_ERROR = Symbol('placeholderError');
function deferred() {
const d = {
settled: false, resolved: false, rejected: false,
reason: null, resolution: null,
};
d.promise = new Promise((res, rej) => {
d.resolve = (r) => {
d.settled = true;
@joedski
joedski / comp-undo-steps-3-no-ident-fn.js
Last active September 27, 2017 01:22
comp-undo-steps 3: Instead of using an identity function to cap things off, use an unboxing/result-extracting function.
function handleSomeThing(ctx) {
return compose(
stepA(optionsA),
stepB(optionsB),
stepC(optionsC),
)(pctx => pctx.resultFoo)({ ctx });
}
// Or somewhat more mysterious looking...
const handleSomeThing = compose(
@joedski
joedski / comp-undo-steps-2-compose.js
Last active September 27, 2017 01:15
comp-undo-steps 2: Using compose to flatten things out.
const processFoo = compose(
stepA(optionsA),
stepB(optionsB),
stepC(optionsC)
)(res => res);
async function handleSomeThing(ctx) {
// Create a pctx with the original ctx and whatever else we want.
// NOTE: in this case, we're trying to not pollute the original ctx,
// which might be something like a Koa Context.
@joedski
joedski / comp-undo-steps-1-proc-example.js
Last active September 27, 2017 00:43
comp-undo-steps 1: An example process.
const processFoo =
stepA(optionsA)(
stepB(optionsB)(
stepC(optionsC)(
// A SACRIFICE TO THE PYRAMID OF DOOM
// On a more serious note, since each step function requires
// a next function, we need to pass in a "return" function which
// doesn't expect a next function to complete the chain.
// If we didn't do this, stepC would return a function expecting a next
// function... passing that function an object doesn't cause an error,
@joedski
joedski / comp-undo-steps-0-step-example.js
Last active September 27, 2017 00:36
comp-undo-steps 0: Example step implementation
function stepA(options) {
return (next) => async (pctx) => {
// Do stuff, allowing any errors to reject this promise,
// passing them back to previous steps.
const result = await doSomething(pctx.foo);
const nextPctx = { ...pctx, bar: result };
try {
// NOTE: Technically, you could further process the result
// before returning it.