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!')})
process.stdin.resume();//so the program will not close instantly
function exitHandler(options, err) {
if (options.cleanup) console.log('clean');
if (err) console.log(err.stack);
if (options.exit) process.exit();
}
//do something when app is closing
process.on('exit', exitHandler.bind(null,{cleanup:true}));
@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 / interactivePrompt.js
Created August 31, 2016 07:18
Simple interactive node.js script to have users continuously enter input.
// Taken from:
// http://stackoverflow.com/questions/24464404/how-to-readline-infinitely-in-node-js
const readline = require('readline');
var log = console.log;
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
-- *********************************************
-- demonstrate how to get the program parameters
-- *********************************************
local internal_name = select(1,...);
local visible_name = select(2,...);
gma.echo("Hello, you loaded the plugin " .. internal_name); -- you will see this message in the system monitor
-- *********************************************
@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)
|