Skip to content

Instantly share code, notes, and snippets.

View robinpokorny's full-sized avatar

Robin Pokorny robinpokorny

View GitHub Profile
@robinpokorny
robinpokorny / titlecaseWords.js
Created June 23, 2020 07:36
For vs map/filter/reduce
const words = ["hello", " ", "world"];
for (let i = 0; i <= words.length; i++) {
const word = words[i];
if (word.length <= 0) continue;
words[i] = uppercaseFirstLetter(word.trim());
}
const titlecasedWords = words
.filter(isNotEmpty)
@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 = {};
@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()
// A: Simple multiplication
function doubleA(n) {
return n * 2
}
// B: With a variable
var two = 2
function doubleB(n) {
return n * two
}
@robinpokorny
robinpokorny / 1-three-letter-emojis-github.md
Last active March 27, 2017 08:15
List of three-letter (or less) emojis available on GitHub and BitBucket

👍 - +1

👎 - -1

🅰️ - a

🆎 - ab

🔤 - abc

@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 / 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 / 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!')
)