Skip to content

Instantly share code, notes, and snippets.

View dengjonathan's full-sized avatar

Jon Deng dengjonathan

View GitHub Profile
@dengjonathan
dengjonathan / asyncIO.js
Last active August 28, 2016 20:21
Node asyn I/O with callback after I/O returns
var http = require('http');
var bl = require('bl');
// file path is from enviroment variable passed in through CLI
var url = process.argv[2];
var consolidated = '';
var callback = function(data) {
consolidated += data;
@dengjonathan
dengjonathan / pipe.js
Last active August 29, 2016 00:00
An example of piping in output from an HTTP request into another function
var http = require('http');
var bl = require('bl');
// file path is from enviroment variable passed in through CLI
var urls = process.argv.slice(2);
var results = [];
var returned = 0;
function httpGet(index) {
@dengjonathan
dengjonathan / simpleHTTPserver.js
Last active September 1, 2016 03:53
Create a basic HTTP server with node that returns the same file from a read stream on every request
var http = require('http');
var fs = require('fs');
var url = require('url');
var streamMap = require('through2-map');
var port = process.argv[2];
var path = process.argv[3];
var server = http.createServer(function(request, response) {
var get = url.parse(request.url, true);
@dengjonathan
dengjonathan / routerHttpServer.js
Created September 1, 2016 03:54
Simple HTTP server with router for API endpoints
var http = require('http');
var fs = require('fs');
var url = require('url');
var streamMap = require('through2-map');
var port = process.argv[2];
var path = process.argv[3];
var server = http.createServer(function(request, response) {
var get = url.parse(request.url, true);
@dengjonathan
dengjonathan / forLoop.js
Last active September 10, 2016 15:48
for loop
var names = ['jon', 'jane', 'jack'];
// something we want to do with each item in array
var log = function(a) {
console.log(a);
}
// using for loop
for (var i = 0; i < names.length; i++) {
log(names[i]);
@dengjonathan
dengjonathan / plusOne.js
Created September 10, 2016 17:14
shows difference between a pure function and not pure
// this is better
function plusOne(number) {
return number++;
}
plusOne(5); // 6
// this is worse
var number = 5;
function plusOne() {
@dengjonathan
dengjonathan / forLoop.js
Created September 10, 2016 17:40
using for loop to iterate through array
var i;
var names = ['jon', 'jane', 'jack'];
// imperative programming
for (i = 0; i < names.length; i++) {
names[i] = names[i][0].toUpperCase() + names[i].slice(1);
}
console.log(names) // ['Jon', 'Jane', 'Jack']
console.log(i) // 3
@dengjonathan
dengjonathan / forLoop.js
Created September 10, 2016 17:43
for loop
var i;
var names = ['jon', 'jane', 'jack'];
// imperative programming
for (i = 0; i < names.length; i++) {
names[i] = names[i][0].toUpperCase() + names[i].slice(1);
}
console.log(names) // ['Jon', 'Jane', 'Jack']
console.log(i) // 3
@dengjonathan
dengjonathan / callToMap.js
Created September 10, 2016 18:32
callToMap
names2 = ['kendra', 'kim', 'kampbell'];
var capitalize = function(word) {
return word[0].toUpperCase() + word.slice(1);
};
map(names, capitalize); // ['Kendra', 'Kim', 'Kampbell']
@dengjonathan
dengjonathan / map.js
Last active September 10, 2016 18:58
Naive implementations of each and map
names2 = ['kendra', 'kim', 'kampbell'];
// use this as your callback function in map
var capitalize = function(word) {
return word[0].toUpperCase() + word.slice(1);
};
// we need each implemented for map to work
var each = function(array, callback) {
for (var i = 0; i < array.length; i++) {