Skip to content

Instantly share code, notes, and snippets.

Robin Pokorny robinpokorny

Block or report user

Report or block robinpokorny

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@robinpokorny
robinpokorny / export-all.md
Created Mar 2, 2018
Babel transform export all file-level variables
View export-all.md
const length = 4

const duplicate = (n) => 2 * n

class Foo {…}

export default (n) => duplicate(n) + length
@robinpokorny
robinpokorny / hyperapp-logger.js
Last active Nov 13, 2017
A simple Hyperapp v0.16.0 logger for debugging
View hyperapp-logger.js
/*
* 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 = {};
View pure-functions.js
// 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
Sleep that passes data
View sleep.js
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 Mar 23, 2017
Transform a list of entries with names (ids) to name-indexed Map
View listToMap.js
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 Jan 17, 2017
📢 Dead simple tweetable JavaScript Emitter pattern module using Map (ES2015)
View 1-info.md
@robinpokorny
robinpokorny / promise-fallback.js
Created Jan 4, 2017
Promise sequence fallback (first non-empty)
View promise-fallback.js
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 Jan 5, 2017
Boilerplate for promise fallback kata (for Medium article)
View first-result.js
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 Jan 2, 2017
Flowtype and Promise.resolve
View index.js
// @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 Jan 4, 2017
Promise sequence fallback (first non-empty) – with Flow
View fallback-promise.js
// @flow
type Task<T> = (...args: any[]) => Promise<?T>
type Options<T> = {
args?: any[],
initial?: T,
isEmpty?: (value: ?T) => boolean
}
export default <T> (
You can’t perform that action at this time.