Skip to content

Instantly share code, notes, and snippets.

View aGuyNamedJonas's full-sized avatar

Jonas Peeck aGuyNamedJonas

View GitHub Profile
@aGuyNamedJonas
aGuyNamedJonas / ArrowFunctionsJS.md
Created May 30, 2016 14:50
Arrow function syntax, taken from the MDN page (#GistToSelf)

###Basic Syntax

(param1, param2, , paramN) => { statements }
(param1, param2, , paramN) => expression
         // equivalent to:  => { return expression; }

// Parentheses are optional when there's only one parameter:
(singleParam) => { statements }
singleParam => { statements }
@aGuyNamedJonas
aGuyNamedJonas / listingArguments.js
Created May 30, 2016 15:37
node.js: List commandline arguments (#GistToSelf)
// print process.argv
process.argv.forEach(function (val, index, array) {
console.log(index + ': ' + val);
});
@aGuyNamedJonas
aGuyNamedJonas / getVariableType.js
Created June 9, 2016 20:09
Reliably determine the type of a variable in node.js
function getType (thing) {
switch (Object.prototype.toString.call(thing)) {
case '[object Object]':
return 'object'
break
case '[object Array]':
return 'array'
break
@aGuyNamedJonas
aGuyNamedJonas / helloPromiseWorld.js
Created July 25, 2016 09:40
Hello World in terms of promises in node.js
var Promise = require('promise');
var fs = require('fs');
var write = Promise.denodeify(fs.writeFile)
// Example #1: Using promisified functions
// Important!! .then() and .catch() need to be passed in a function!
var promise = write('bla.txt', 'Blablabla', 'utf-8')
.then(function(){console.log('Success!')})
@aGuyNamedJonas
aGuyNamedJonas / iterateOverObject.js
Created August 8, 2016 22:20
Iterating over an object in node.js. Can be used in conjunction with the getVariableType.js gist
// Source: http://stackoverflow.com/questions/684672/how-do-i-loop-through-or-enumerate-a-javascript-object
for (var key in p) {
if (p.hasOwnProperty(key)) {
console.log(key + " -> " + p[key]);
}
}
@aGuyNamedJonas
aGuyNamedJonas / module.js
Created August 11, 2016 14:06
Good pattern for node.js modules
var stuff = require('stuff');
var exp = {
someFunction: function(param1, param2) {
// Do Stuff
},
someOtherFunction: function(param1, param2, param3) {
this.someFunction('this', 'that');
}
@aGuyNamedJonas
aGuyNamedJonas / Logical layers of adding a todo item
Last active April 5, 2017 14:34
Meet slim-redux blog post code snippets
'ADD_TODO' constant (constants/ActionTypes.js)
| ↓
| 'todos' reducer (reducers/todo.js)
| ↓
| 'rootReducer' (reducers/index.js)
|
'addTodo' action creator (actions/index.jx)
|
import { createSlimReduxStore } from 'slim-redux';
// Create a store with initial state = 0
var store = createSlimReduxStore(0);
// Make sure we see any store changes in the console
store.subscribe(() =>
console.log(store.getState())
)
// Create a store with initial state = 0
var store = createSlimReduxStore(0 /*, yourExistingRootReducer, yourExistingMiddleware*/ );
// Calling increment() will dispatch the following action:
// {type: 'INCREMENT', payload: {value: 10}}
increment({value: 10});
/*
This will be picked up by your reducer:
reducer: (state, payload, action) => {
return state + payload.value;
}