Skip to content

Instantly share code, notes, and snippets.

View robinpokorny's full-sized avatar

Robin Pokorny robinpokorny

View GitHub Profile
@robinpokorny
robinpokorny / fizzbuzz.js
Last active November 1, 2016 09:42
Minimal readable Fizz Buzz test solution in JavaScript
// This function isolates the logic
// Its param is number `i`
// It returns 'Fizz', 'Bazz', 'FizzBuzz' or the number
// (This implementation takes advatage of some JavaScript specific features)
const toFizzBuzz = (i) =>
// We construct a string
// First part is 'Fizz' if the number is divisible by 3
// and an empty string otherwise
// `i%3` is 0 iff `i` is divisible by 3
export * as moduleA from './moduleA'
// should be a shorthand for:
import * as _moduleA from './moduleA'
export const moduleA = _moduleA
@robinpokorny
robinpokorny / cases.css
Created November 6, 2016 11:27
Evil BEM naming test cases for css-should-plugin-bem
ul.menu--primary
body .menu--primary
#page.item .item--primary .item--secondary
.menu__nav--primary i.item__icon.item__icon--spinning
.menu>.item--bold
a[lang|=cs].item--bold
*:hover .item--disabled+a.item--bold:active
@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 / 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> (
@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!')
)
@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 / 1-info.md
Last active January 17, 2017 13:43
📢 Dead simple tweetable JavaScript Emitter pattern module using Map (ES2015)
@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)