Skip to content

Instantly share code, notes, and snippets.

View robinpokorny's full-sized avatar

Robin Pokorny robinpokorny

View GitHub Profile
@robinpokorny
robinpokorny / export-all.md
Created March 2, 2018 14:34
Babel transform export all file-level variables
const length = 4

const duplicate = (n) => 2 * n

class Foo {}

export default (n) => duplicate(n) + length
@robinpokorny
robinpokorny / hyperapp-logger.js
Last active November 13, 2017 18:27
A simple Hyperapp v0.16.0 logger for debugging
/*
* Include after hyperapp.js and before your app
* Use this version provided by rawgit.com:
* https://cdn.rawgit.com/robinpokorny/c1f049f815ec274fdedbb454c56d74ee/raw/hyperapp-logger.js
*/
const mapValue = (object, iteratee) => {
// https://github.com/lodash/lodash/blob/master/mapValue.js
object = Object(object);
const result = {};
// A: Simple multiplication
function doubleA(n) {
return n * 2
}
// B: With a variable
var two = 2
function doubleB(n) {
return n * two
}
@robinpokorny
robinpokorny / sleep.js
Created May 3, 2017 16:16
Sleep that passes data
const sleep = (dur) => (data) =>
new Promise((res) =>
setTimeout(() => res(data), dur)
)
const yay = () => Promise.resolve('sleepy')
.then(sleep(1000))
.then((status) => console.log(`I am so ${status}!`))
yay()
@robinpokorny
robinpokorny / listToMap.js
Created March 23, 2017 10:42
Transform a list of entries with names (ids) to name-indexed Map
const iconList = [
{ name: 'star', code: 7807, size: 20 /* ... */ },
{ name: 'heart', code: 7897, size: 10 /* ... */ },
{ name: 'arrow', code: 7822, size: 25 /* ... */ },
{ name: 'home', code: 7901, size: 20 /* ... */ }
]
const entries = iconList.map(({ name, ...rest }) => [ name, rest ])
const icons = new Map(entries)
@robinpokorny
robinpokorny / 1-info.md
Last active January 17, 2017 13:43
📢 Dead simple tweetable JavaScript Emitter pattern module using Map (ES2015)
@robinpokorny
robinpokorny / promise-fallback.js
Created January 4, 2017 10:42
Promise sequence fallback (first non-empty)
export default (tasks, {
args = [],
initial,
isEmpty = (x) => !x
} = {}) =>
tasks.reduce(
(prev, next) => prev.then((value) =>
isEmpty(value) ? next(...args) : value
),
Promise.resolve(initial)
@robinpokorny
robinpokorny / first-result.js
Last active January 5, 2017 07:53
Boilerplate for promise fallback kata (for Medium article)
const firstResult = (services) => { /* … */ }
firstResult([
() => Promise.resolve([]),
() => Promise.resolve(['Berlin']),
() => console.error('Do not call me!')
])
.then((result) =>
console.assert(result[0] === 'Berlin', 'Test1 failed!')
)
@robinpokorny
robinpokorny / index.js
Created January 2, 2017 13:47
Flowtype and Promise.resolve
// @flow
// BAD
// Will throw this type error:
// ‘undefined This type is incompatible with some incompatible instantiation of `T`’
const resolve = <T> (init?: T): Promise<T> => Promise.resolve(init)
// GOOD
// The return promise type is `?T`, i.e. nullable T
const resolve = <T> (init?: T): Promise<?T> => Promise.resolve(init)
@robinpokorny
robinpokorny / fallback-promise.js
Last active January 4, 2017 11:04
Promise sequence fallback (first non-empty) – with Flow
// @flow
type Task<T> = (...args: any[]) => Promise<?T>
type Options<T> = {
args?: any[],
initial?: T,
isEmpty?: (value: ?T) => boolean
}
export default <T> (