Skip to content

Instantly share code, notes, and snippets.

View gartenfeld's full-sized avatar

David Rosson gartenfeld

View GitHub Profile
@gartenfeld
gartenfeld / q_promise.js
Last active August 29, 2015 14:27
Promises using Q.
var Q = require('q');
var async = function (prefix) {
// create a promise wrapper
var hold = Q.defer();
var success = function () {
var data = prefix + "SOLVED!";
// by calling 'resolve', the next steps in the
// 'then' handler (e.g. 'console.log' below)
// will be executed with 'data' passed in
@gartenfeld
gartenfeld / bluebird_promise.js
Created August 20, 2015 23:53
Promises with Bluebird.
var Promise = require('bluebird');
var async = function (input) {
return new Promise(function (resolve, reject) {
var data = input + ' Whatever.';
// call the async process here
setTimeout(function(){
// send the result when it arrives
resolve(data);
}, 100);
@gartenfeld
gartenfeld / blockspring.js
Created August 28, 2015 01:55
Serverless Slack Bots
// https://www.blockspring.com/blog/serverless-slack-bots
blockspring = require('blockspring');
var webhook = function(team_domain, service_id, token, user_name, team_id, user_id, channel_id, timestamp, channel_name, text, trigger_word, raw_text) {
// Basic bot will just echo back the message
response = ["*", user_name, "* said _", text, "_"].join('')
return {
@gartenfeld
gartenfeld / trigger_handlers.js
Created August 28, 2015 18:24
Organizing handler functions using a hash.
var handlers = {
onConnect: function () {
console.log('Connected!');
},
onMessage: function () {
console.log('Message!');
}
};
var map = {
@gartenfeld
gartenfeld / module.js
Created August 31, 2015 21:31
Creating new closure scopes in Node.
module.exports = function () {
var c = [];
return {
push: function (item) {
c.push(item);
},
print: function () {
console.log(c);
}
};
@gartenfeld
gartenfeld / text_wrap.js
Created September 14, 2015 16:58
Wrapping a long string into a paragraph with line-breaks.
function wrap (text, limit) {
if (text.length > limit) {
// find the last space within limit
var edge = text.slice(0, limit).lastIndexOf(' ');
if (edge > 0) {
var line = text.slice(0, edge);
var remainder = text.slice(edge + 1);
return line + '\n' + wrap(remainder, limit);
}
}
@gartenfeld
gartenfeld / preset_array.js
Last active October 3, 2015 21:49
An alternative to .push()
function solution(A) {
var n = A.length - 1,
lSum = 0,
rSum = 0,
diffs = new Array(n),
diff,
min = Infinity;
for (var p = 0; p < n; p++) {
lSum += A[p];
rSum += A[n - p];
@gartenfeld
gartenfeld / base_convert.js
Created October 6, 2015 04:07
Converting from Base 10.
var num = "27019",
base = 16,
output = '';
while (num > 0) {
digit = num % base;
num = (num - digit) / base;
if (digit > 9) {
digit = String.fromCharCode(digit + 55);
}
@gartenfeld
gartenfeld / curb.js
Created October 10, 2015 19:49
Solving a puzzle with async depth-first traversal.
var request = require('request');
var Promise = require('bluebird');
var authorize = require('./session-keys');
var BASE_URL = 'http://challenge.shopcurbside.com/';
var fetch = function(resource, session) {
var options = {
url: BASE_URL + resource,
headers: { 'Session': session }
// initiating a worker
var karl = new Worker("task.js");
// receiving messages
karl.onmessage = function(event){
console.log(event.data);
};
// in the end
karl.terminate();