Skip to content

Instantly share code, notes, and snippets.

View topliceanu's full-sized avatar
💭
🚀

Alexandru Topliceanu topliceanu

💭
🚀
View GitHub Profile
@topliceanu
topliceanu / setters_and_getters.coffee
Created March 17, 2014 08:44
example of how to implement setters and getters in a coffeescript class. not accessible to children classes.
###
Utility to add setters and getters to a class in coffeescript.
@see https://github.com/thingdom/node-neo4j/blob/master/lib/PropertyContainer._coffee
###
class A
get = (props) =>
@::__defineGetter__ name, getter for name, getter of props
@topliceanu
topliceanu / testUtil.coffee
Last active August 29, 2015 13:58
A implementation of the _.any api but for asynchronous computation and Q promises.
assert = (require 'chai').assert
Q = require 'q'
util = require './util'
describe '.any()', ->
it 'should fail when no promise resolves', (done) ->
fns = [
@topliceanu
topliceanu / host.html
Created May 28, 2011 16:56
xPubSub.js allows for cross-frame custom DOM events synchronization. Requires jquery and postmessage.js
<!doctype html>
<title> host </title>
<meta charset="utf-8">
<div id="container">host.html</div>
<script src="jquery-1.6.1.js"></script>
<script src="jquery.tinypubsub.js"></script>
<script src="postmessage.js"></script>
<script src="xPubSub.js"></script>
<script>
@topliceanu
topliceanu / barrier.js
Created May 28, 2011 16:57
posix barrier-like functionality for events
// js barrier, waits for all events to be fired before executing the callback
// dependencies:
// 1. jquery 1.4.2+ - jquery.com
// 2. ben alman's tiny jquery pub/sub - https://gist.github.com/661855
(function($){
$.barrier = function( evs , callback ){
var events = evs.split(' ') ;
var flags = {} ;
var collector = {} ;
$.each( events , function( i, ev ){
@topliceanu
topliceanu / deferred.js
Created July 9, 2011 12:53
simple deferred object implementation, library & platform agnostic
var newDefered = function(){
var state = 0 ; // 0 - unresolved, 1 - resolved, 2 - rejected
var resolveCallbacks = [] ;
var resolveArgs = [] ;
var rejectCallbacks = [] ;
var rejectArgs = [] ;
var execute = function( callbacks, args ){
while( callbacks.length !== 0 ){
callbacks.shift().call( {}, args ) ;
@topliceanu
topliceanu / learn.js
Created October 14, 2011 16:55
mongoose problems
//deps
var mongoose = require( 'mongoose' );
var Schema = mongoose.Schema;
var ObjectId = mongoose.Schema.ObjectId;
//config
var mongo = require( './../conf.js' ).mongo ;
mongo.database = 'learn';
//connect to mongo
@topliceanu
topliceanu / html.js
Created November 9, 2011 14:16
a simple html renderer for express.js, no need for ejs or jade
//register a *simple* html renderer
app.register( '.html', {
compile: function( str, options ){
return function( locals ){
return str;
}
}
});
@topliceanu
topliceanu / requestGzip.js
Created February 19, 2012 16:22
Stream downloader of web resources, based on mikeal's request, but processed first through zlib if necessary
var request = require('request');
var zlib = require('zlib');
// A two-way passthrough Stream, made by @felixge
// https://github.com/felixge/node-passthrough-stream/blob/master/lib/passthrough_stream.js
var PassthroughStream = function () {
this.writable = true;
this.readable = true;
}
util.inherits(PassthroughStream, streams.Stream);
@topliceanu
topliceanu / documentLocalStorage.js
Created March 3, 2012 11:05
store data in localstorage, api similar to document databases
// stores arrays binded to keys in localstorage
(function (win) {
var isStorage = function (obj) {
return (Object.prototype.toString.call(obj) === '[object Storage]');
};
var decode = function (str) {
try {
var arr = JSON.parse(str);
if (!Array.isArray(arr)) return false;
return arr;
@topliceanu
topliceanu / extend.es5.js
Created March 6, 2012 11:43
extends objects, similar in functionality to $.extend or _.extend
// jquery style `extend` work in ES5 compatible envs
var extend = function (obj) {
var args = Array.prototype.slice.call(arguments, 1);
args.forEach( function (source) {
for (var prop in source)
if (source.hasOwnProperty(prop)) obj[prop] = source[prop];
});
return obj;
};