Skip to content

Instantly share code, notes, and snippets.

@arasatasaygin
arasatasaygin / pub-sub.js
Created August 10, 2016 10:25 — forked from reu/pub-sub.js
node.js redis pub-sub example
var redis = require("redis")
, subscriber = redis.createClient()
, publisher = redis.createClient();
subscriber.on("message", function(channel, message) {
console.log("Message '" + message + "' on channel '" + channel + "' arrived!")
});
subscriber.subscribe("test");
@arasatasaygin
arasatasaygin / merge.js
Created December 8, 2014 09:27
Merge, mergesort implementation
function merge(left, right) {
var result = [];
while(left.length > 0 && right.length > 0) {
if(left[0] < right[0]) {
result.push(left.shift());
} else {
result.push(right.shift());
}
}
return result.concat(left).concat(right);
@arasatasaygin
arasatasaygin / class.js
Last active August 29, 2015 14:08
How classes works on CoffeeScript
var Animal, Horse, Snake, sam, tom,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) {
for (var key in parent) {
if (__hasProp.call(parent, key))
child[key] = parent[key];
}
function ctor() {
this.constructor = child;
@arasatasaygin
arasatasaygin / console-time.js
Created October 17, 2014 12:22
Console time
console.time("answer time");
alert("Click to continue");
console.timeEnd("answer time");
require.config({
baseUrl: '/backbone-tests/',
paths: {
'jquery' : '/app/libs/jquery',
'underscore' : '/app/libs/underscore',
'backbone' : '/app/libs/backbone',
'mocha' : 'libs/mocha',
'chai' : 'libs/chai',
'chai-jquery' : 'libs/chai-jquery',
'models' : '/app/models'
// # Mocha Guide to Testing
// Objective is to explain describe(), it(), and before()/etc hooks
// 1. `describe()` is merely for grouping, which you can nest as deep
// 2. `it()` is a test case
// 3. `before()`, `beforeEach()`, `after()`, `afterEach()` are hooks to run
// before/after first/each it() or describe().
//
// Which means, `before()` is run before first it()/describe()
function reportError(error, message) {
message = message || '';
console.error(
'ERROR: ' + message + ' [' + error.toString() + ']\n' +
'\nName:\t\t' + (error.name || '-') +
'\nMessage:\t' + (error.message || '-') +
'\nFile:\t\t\t' + (error.fileName || '-') +
'\nSource:\t\t' + ((error.toSource && error.toSource()) || '-') +
'\nLine #:\t\t' + (error.lineNumber || '-') +
'\nColumn #:\t' + (error.columnNumber || '-') +
@arasatasaygin
arasatasaygin / renderProduct.js
Created August 7, 2014 13:35
Render products
var renderSearchedProduct = function(products) {
var Product = Backbone.Model.extend({});
var Products = Backbone.Collection.extend({
model: Product,
initialize: function() {
this.add(products);
}
});
PageData.productCollection = new Products();
var ProductListView = Backbone.View.extend({
@arasatasaygin
arasatasaygin / nodeHttpServer.js
Last active June 4, 2018 02:56
Sample Node HTTP Server
var http = require('http'); //HTTP server api
var fs = require('fs'); //For working with local files
var server = new http.Server(); //Create a new HTTP server
server.listen(8000); //Run it in port 8000
//When the server gets a new request, run this function to handle it
server.on("request", function(request, response) {
var url = require('url').parse(request.url); // Parse the requested url
if(url.pathname === "test/delay") {
@arasatasaygin
arasatasaygin / functionProperty.js
Created July 9, 2014 17:23
Functions can have properties
var test = function(x) { return x + test.prop };
test.prop = 1;
test(5); //logs: 6
//You don't know how much counter variable I've written till today to outer scope of function