Skip to content

Instantly share code, notes, and snippets.

View renaudtertrais's full-sized avatar

Renaud TERTRAIS renaudtertrais

View GitHub Profile
[
{
"country": "Afghanistan",
"continent": "Asia"
},
{
"country": "Albania",
"continent": "Europe"
},
{
.observablehq .octicon {
display: inline-block;
fill: currentColor;
vertical-align: text-bottom;
}
.observablehq .anchor {
float: left;
line-height: 1;
margin-left: -20px;
{
"date": "2018-09-05",
"channelName": "RTS",
"programmes": [
{
"id": "2367276",
"start": 1538715600000,
"name": "La Matinale en vidéo",
"fileSize": 28354000
},
@renaudtertrais
renaudtertrais / tfs-without-visualstudio.md
Last active August 21, 2017 09:47
Use tfs without visual studio
@renaudtertrais
renaudtertrais / omit.js
Last active September 11, 2017 15:52
A simple ES7 omit function
const omit = (keys, obj) =>
Object.entries(obj)
.filter(([ key ]) => !keys.includes(key))
.reduce((acc, [key, value]) => Object.assign({}, acc, {
[key]: value,
}), {});
omit(['bar'], { foo: 1, bar: 2, baz: 3 }); // { foo: 1, baz: 3 }
@renaudtertrais
renaudtertrais / mock-global-object-jest.js
Created May 17, 2017 12:00
A simple way to mock/unmock globals object methods or constant
/* global expect jest */
const delay = ms => fn => setTimeout(fn, ms);
const mockGlobalProperty = globalObject => key => value => {
// save original implementation in order to unmock later
const original = globalObject[key];
// mock key on the global object
Object.defineProperty(globalObject, key, { value, writable: true });
@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 / 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('[email protected]'); // { _global: "[email protected]", domain: "bar", user: "foo" }
@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 / 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