Skip to content

Instantly share code, notes, and snippets.

@mgtitimoli
mgtitimoli / module.js
Last active January 29, 2016 05:59
common-js compatible module definition
(function(module, exports){
module.exports = {
a: 1,
b: 2
};
}).apply(
undefined,
(
(typeof module === "object" && module !== null)
&& (typeof module.exports === "object" && module.exports !== null)
@mgtitimoli
mgtitimoli / transform-object.js
Last active March 15, 2016 21:37
Transform object: entries > filter > map > reduce (entries > destination object)
function objectSetEntry(object, [ key, value ]) {
object[key] = value;
return object;
}
export default function transformObject(
source,
filterEntries = undefined,
@mgtitimoli
mgtitimoli / create-action-creator.js
Last active April 26, 2016 07:08
Temporary gist (I will publish a npm module soon) that contains all the necessary files to use in combination with redux-thunk to handle asynchronism in redux
import defineConstant from "./define-constant";
export default function createActionCreator(type) {
function create(payload, meta) {
const action = {
type,
payload
};
@mgtitimoli
mgtitimoli / README.md
Last active June 17, 2016 03:24
Vagrant NFS Debian
  • Run the following commands
$ sudo apt-get install nfs-kernel-server
$ modprobe nfs; modprobe nfsd
  • Make sure that /etc/sudoers is reading configuration files located in /etc/sudoers.d
#includedir /etc/sudoers.d
  • Add a file vagrant within /etc/sudoers.d with the following
@mgtitimoli
mgtitimoli / different-obj-descriptors-with-cache.js
Last active June 29, 2016 15:53
Add multiple descriptors with numeric keys
function createDescriptor(key) {
return {
enumerable: true,
get() {
// do some stuff
return this.data[key];
},
set(value) {
// do some stuff
this.data[key] = value;
@mgtitimoli
mgtitimoli / fibonacci-generator.js
Last active July 22, 2016 20:10
reduce-until & iteration-context & generator-to-array & fibonacci-generator
export default function* fibonacciGenerator() {
let prev = 1;
let cur = 1;
yield prev;
while(true) {
yield cur;
[prev, cur] = [cur, prev + cur];
@mgtitimoli
mgtitimoli / example-map.js
Last active August 31, 2016 12:54
Reduce While: Applies a reduce function as long as filter function returns true and returns the last value returned by reduceFn
const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
const numbersNames = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
const namesOfNumbersLessThan = maxNumber => reduceWhile(
numbers,
number => number < maxNumber,
(result, number, index) => result.concat(numbersNames[index])
);
console.log(namesOfNumbersLessThan(5)); // ['zero', 'one', 'two', 'three', 'four']
@mgtitimoli
mgtitimoli / duplicates.js
Last active September 21, 2016 23:25
Duplicates
const not = fn => (...args) => !fn(...args);
const notEqual = (v1, v2) => !Object.is(v1, v2);
const withDuplicates = (filterIndex, arr, isEqual = Object.is) => arr.filter(
(filteredValue, filteredIndex) => filterIndex(
filteredIndex,
arr.findIndex(curValue => isEqual(curValue, filteredValue))
)
);
@mgtitimoli
mgtitimoli / example.js
Last active November 19, 2016 02:09
Tiny immutable set implementation
const o1 = {k1: {k2: false}};
const o2 = setImmutable(o1, ["k1", "k2"], true);
console.log(o1 === o2); // false
console.log(o1.k1 === o2.k1); // false
console.log(o1.k1.k2 === o2.k1.k2); // false
@mgtitimoli
mgtitimoli / shape-of-proptype.js
Created November 21, 2016 14:24
shapeOf propType
import {PropTypes} from 'react';
const setValueOn = (keys, object, value) => keys.reduce(
(result, curKey) => Object.assign(result, {[curKey]: value}),
object
);
const shapeOf = (type, keys) => PropTypes.shape(
setValueOn(keys, {}, type)
);