Skip to content

Instantly share code, notes, and snippets.

View renaudtertrais's full-sized avatar

Renaud TERTRAIS renaudtertrais

View GitHub Profile
@renaudtertrais
renaudtertrais / Collection.js
Created December 28, 2014 21:48
Automatize querySelectorAll manipulation.
var t = console.log;
var select = function( selector ){
return new Collection( document.querySelectorAll( selector ) );
};
var Collection = function( collection ){
collection = collection || [];
this.collection = Array.prototype.slice.call(collection);
DataSystem = () ->
this.data = {}
this
proto = DataSystem.prototype
proto.set = ( keys , value , obj ) ->
obj = obj || this.data
if typeof keys is 'string'
keys = keys.split('.')
@renaudtertrais
renaudtertrais / bind.js
Last active July 4, 2016 16:08
A simple ES6 bind function to implement Railway oriented programming. https://vimeo.com/113707214
const compose = (...fns) => (...args) => {
return fns.slice(0, -1).reduceRight((res, fn) => fn(res),
fns[fns.length -1].apply(null,args)
);
};
const bind = (f) => (...args) => {
if(args[0] instanceof Error) return args[0];
try{
return f.apply(null, args);
@renaudtertrais
renaudtertrais / StateFull_Functional_React_Component.jsx
Last active November 3, 2016 16:50
StateFul functional React component proposal base on @sebmarkbage Stateful Functions https://git.io/vXnhF
/* ---------------------------------------------- *\
STATEFUL FUNCTIONAL REACT COMPONENT PROPOSAL
\* ---------------------------------------------- */
// event handler can return a state change
const handleClick = (props, state) => ({ value: state.value + 1 });
// event handler can return function in order to do a async state change
const handleClickAsync = (props, state) => setState => {
// setState after 1s
@renaudtertrais
renaudtertrais / promiseAllObject.js
Created February 10, 2017 12:40
extend Promise.all() in order to accept plain object
const assign = (obj, key, value) => Object.assign({}, obj, { [key]: value });
const promiseAllObject = obj => {
const keys = Object.keys(obj);
return Promise.all(Object.values(obj))
.then(results => results.map((val, i) => [keys[i], val]))
.then(results => results.reduce((res, val) => assign(res, ...val), {}));
}
@renaudtertrais
renaudtertrais / partial.js
Last active February 10, 2017 12:45
Simple ES6 partial function
const partial = (fn, ...args) => partialApply(fn, args);
const partialApply = (fn, args) => fn.bind(null, ...args);
// example
const add = (a, b) => a + b;
const sum = (...args) => args.reduce(add, 0);
const add10 = partial(sum, 4, 6);
const add40 = partialApply(sum, [5, 15, 20]);
@renaudtertrais
renaudtertrais / parseRgx.js
Created February 18, 2017 22:18
Convert regex matches to an object
const parseRgx = rgx => s => {
const keys = ['_global', ...rgx.match(/\$(\w+):/g).map(k => k.replace(/\$|:/g,''))];
const matches = s.match(new RegExp(rgx.replace(/\$\w+:/g,''))) || [];
return matches.reduce((res, val, i) => Object.assign({}, res, { [keys[i]]: val }), {});
};
const parseEmail = parseRgx('($user:\\w+)@($domain:\\w+)\\.(?:\\w+)');
parseEmail('foo@bar.com'); // { _global: "foo@bar.com", domain: "bar", user: "foo" }
@renaudtertrais
renaudtertrais / cap.js
Last active February 21, 2017 10:49
Cap function n params. Useful to cap iteratee for example.
const capTo = n => fn => (...args) => fn(...args.slice(0, n));
const capRightTo = n => fn => (...args) => fn(...args.slice(-n));
const cap = capTo(0);
const add = (a, b) => a + b;
const sum = (...args) => args.reduce(add, 0);
// Simple Example
sum(1, 2, 3, 4); // --> 10
@renaudtertrais
renaudtertrais / tfs-without-visualstudio.md
Last active August 21, 2017 09:47
Use tfs without visual studio
@renaudtertrais
renaudtertrais / curry.js
Last active September 1, 2017 09:43
A simple ES6 curry function
const curry = (fn, ...args) => fn.length === 0
? fn
: fn.length > args.length
? (...args2) => curry(...[fn,...args,...args2])
: fn(...args);
// example
const add = curry((a,b,c,d) => a + b + c + d);
const add1 = add(1);
const add3 = add1(2);