Skip to content

Instantly share code, notes, and snippets.

View chvonrohr's full-sized avatar

Christian von Rohr chvonrohr

  • Lucerne, Switzerland
View GitHub Profile
// helper functions
const compose = (...fns) => x => fns.reduceRight((res, fn) => fn(res), x);
const tap = f => x => { f(x); return x; };
const trace = label => tap(console.log.bind(console, label + ':'));
// container helpers
const isFunction = fn => fn && Object.prototype.toString.call(fn) === '[object Function]';
const isAsync = fn => fn && Object.prototype.toString.call(fn) === '[object AsyncFunction]';
@chvonrohr
chvonrohr / shorthanders.js
Created June 19, 2019 07:06
Shorthanders
// #1 - for
let images = [ 'test.jpg', 'dummy.gif' ];
for (let i = 0; i < allImgs.length; i++) {} // long
for (let i of allImgs) { } // short
// #2 - decimal base
1000000 === 1e6
// #3 - default parameters
volume = (l, w = 3, h = 4 ) => (l * w * h);
@chvonrohr
chvonrohr / js-snippets.js
Created June 17, 2019 16:35
found cool JS snippets
// 123456789 -> 123,456,789
n.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
@chvonrohr
chvonrohr / tryCatch.js
Created June 14, 2019 14:06
try-catch - functional programming
export function tryCatch({
tryer,
catcher
}) {
return (props) => {
try {
return tryer(props);
} catch (e) {
return catcher(props, e.message);
}
@chvonrohr
chvonrohr / conditionally.js
Created June 14, 2019 14:05
if-else – functional programming
export const conditionally = <Props, Result>(options: {
if: (props: Props) => any;
then: (props: Props) => Result | Result;
else: (props: Props) => Result | Result;
}) => (props: Props) => {
return options.if(props) ? options.then(props) : options.else(props);
};
@chvonrohr
chvonrohr / controllers.application.js
Last active September 27, 2018 14:43
computed-property-qp
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
items: [
{key: 1, title: "ONE"},
{key: 2, title: "TWO"},
{key: 3, title: "THREE"}
],
import Ember from 'ember';
import {inject as controller} from '@ember/controller';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
myvar: '',
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
@chvonrohr
chvonrohr / controllers.application.js
Created July 12, 2018 15:33
ember-boostrap-tooltip-problem
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
@chvonrohr
chvonrohr / controllers.address.js
Last active May 10, 2018 00:33
QueryParamExample
import Ember from 'ember';
export default Ember.Controller.extend({
queryParams: ['search'],
search: '',
});