working on it ...

Filters

Explore Public Snippets

Sort by

Found 839 snippets matching: oop

    public by sTiLL-iLL @ SniPitz-KND modified Apr 12, 2015  549915  5  8  16

    Async Queue in C#

    using System;
    using System.Net;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    
    public class ParalyzKue
    {
    	readonly object _locker = new object();
    	Task _tail = Task.FromResult (true);
    	
    	public Task Enqueue (Action action)
    	{
    		lock (_locker)
    			return _tail = _tail.ContinueWith (ant => action());
    	}
    
    	public Task<TResult> Enqueue<TResult> (Func<TResult> func)
    	{
    		lock (_locker)
    			return (Task<TResult>) (_tail = _tail.ContinueWith (ant => func()));
    	}
    
    	public Task Enqueue (Func<Task> action)
    	{
    		lock (_locker)
    			return _tail = _tail.Catch().Then (action);
    	}	
    
    	public Task<TResult> Enqueue<TResult> (Func<Task<TResult>> func)
    	{
    		lock (_locker)
    			return (Task<TResult>) (_tail = _tail.Catch().Then (func));
    	}
    }
    
    
    public static class Extensions
    {
    	public static Task<T> Catch<T,TError> (this Task<T> task, Func<TError,T> onError) where TError : Exception
    	{
    		var tcs = new TaskCompletionSource<T> ();
    		task.ContinueWith (ant => {
    			if (task.IsFaulted && task.Exception.InnerException is TError)
    			{
    				tcs.SetResult (onError ((TError) task.Exception.InnerException));
    			}
    			else if (ant.IsCanceled)
    			{
    				tcs.SetCanceled ();
    			}
    			else if (task.IsFaulted)
    			{
    				tcs.SetException (ant.Exception.InnerException);
    			}
    			else
    			{
    				tcs.SetResult (ant.Result);
    			}
    		});
    		return tcs.Task;
    	}		
    	public static Task<T> Catch<T,TError> (this Task<T> task, T onError) where TError : Exception
    	{
    		return task.Catch<T,TError> (ex => onError);
    	}
    	
    	public static Task<T> Catch<T,TError> (this Task task, T onError) where TError : Exception
    	{
    		return task.ToTaskOfT<T>().Catch<T,TError> (onError);
    	}
    
    	public static Task Catch (this Task task)
    	{
    		return task.Catch<object,Exception> (null);
    	}
    	
    	public static Task<T> Catch<T> (this Task<T> task, T valueIfError = default(T))
    	{
    		return task.Catch<T,Exception> (valueIfError);
    	}
    
    	public async static Task<T> ToTaskOfT<T> (this Task t)
    	{
    		await t;
    		return default(T);
    	}
    	
    	public static async Task Then(this Task task, Func<Task> continuation)
    	{
    		await task;
    		await continuation();
    	}
    	
    	public static async Task<TNewResult> Then<TNewResult>(this Task task, Func<Task<TNewResult>> continuation)
    	{
    		await task;
    		return await continuation();
    	}
    	
    	public static async Task Then<TResult>(this Task<TResult> task, Func<TResult,Task> continuation)
    	{
    		await continuation(await task);
    	}
    	
    	public static async Task<TNewResult> Then<TResult, TNewResult>(this Task<TResult> task, Func<TResult, Task<TNewResult>> continuation)
    	{
    		return await continuation(await task);
    	}
    }
    
    /*
    public void doKue()
    {
    	var pcQ = new ParaKue();
    	
      pcQ.Enqueue (() => AsyncMethod1 ("http://www.rotten.com/")).Dump ("Task 1");
    	pcQ.Enqueue (() => AsyncMethod1 ("https://youtube.com/")).Dump ("Task 2");
    	pcQ.Enqueue (() => AsyncMethod1 ("https://yahoo.com/search?q=the cube")).Dump ("Task 3");
      pcQ.Enqueue (() => AsyncMethod1 ("http://www.rotten.com/")).Dump ("Task 4");
    	pcQ.Enqueue (() => AsyncMethod1 ("https://youtube.com/")).Dump ("Task 5");
    	pcQ.Enqueue (() => AsyncMethod1 ("https://www.google.com/search?q=web scrapers")).Dump ("Task 6");
      pcQ.Enqueue (() => AsyncMethod1 ("http://www.rotten.com/")).Dump ("Task 7");
    	pcQ.Enqueue (() => AsyncMethod1 ("https://youtube.com/")).Dump ("Task 8");
    	pcQ.Enqueue (() => AsyncMethod1 ("https://youtube.com/search?q=aliens gods")).Dump ("Task 9");
      pcQ.Enqueue (() => AsyncMethod ("http://www.rotten.com/")).Dump ("Task 10");
    	pcQ.Enqueue (() => AsyncMethod ("https://youtube.com/")).Dump ("Task 11");
    	pcQ.Enqueue (() => AsyncMethod ("https://www.google.com/search?q=javascript")).Dump ("Task 12");
      pcQ.Enqueue (() => AsyncMethod ("http://www.rotten.com/")).Dump ("Task 13");
    	pcQ.Enqueue (() => AsyncMethod ("https://youtube.com/search?q=aliens")).Dump ("Task 14");
      pcQ.Enqueue (() => AsyncMethod ("https://www.yahoo.com/")).Dump ("Task 15");
    	pcQ.Enqueue (() => AsyncMethod ("https://youtube.com/")).Dump ("Task 16");
    	pcQ.Enqueue (() => AsyncMethod ("https://www.google.com/search?q=aliens disclosure")).Dump ("Task 17");
    }
    
    string SyncMethod (string uri)
    {
        var x = new System.Net.WebClient();
        var y = x.DownloadString(uri);
        return y;
    }
    
    async Task<string> AsyncMethod (string uri)
    {
       return await Task.Run(() => new System.Net.WebClient().DownloadStringTaskAsync(uri).Dump());
    }
    
    
    async Task<string> AsyncMethod1 (string uri)
    {
       var x = await new System.Net.WebClient().DownloadStringTaskAsync(uri);
       return x;
    }
    
    */

    public by sTiLL-iLL @ SniPitz-KND modified Dec 1, 2015  532344  2  8  21

    Ajax again?? Wrap it!! XHR Agent 4Ya

    XHR wrapper! object pooling and cueing!!! EveryOneZ dOing IT!
    
    var myXHR = {
           ESpec: function (wrap, evt) {
               return { xhrWrap: wrap, event: evt };
           },
           Agent: function(maxConnect) {
               var bCued = [], slf = this,
                   max = maxConnect || 3;
                   this.curCon = 0;
               function inCue() {
                   if (self.bCued < max) {
                       self.bCued++;
                       var itm = is.Readied.shift();
                       itm.evt.addEventListener(['EVENT_COMPLETE'], 
                         function (e) {
                             self.bCued--;
                             if (this.Readied.length !== 0) {
                               inCue();
                             }
                         });
                       itm.wrap.call(itm, args, itm.evt);
                    }
               }
               this.Readied = function (ESpec) {
                   bCued.push(ESpec);
                   inCue();
                   return ESpec.evt;
               };
            }
       };
    
                

    public by sTiLL-iLL @ SniPitz-KND modified Dec 15, 2014  444390  3  8  24

    Signals.js... A fast and efficient event system in javascript.

    Signals.js... A fast and efficient event system in javascript.: Signals.js
    // Signalz.js
    
    var Signals = (function () {
    	'use strict';
    	var sigCache = {},
    	addReceiver = function(eventName, func) {
    		return wen(eventName, function(evnt){
    			return func(evnt);
    		});
    	},
    	signalReceived = function(eventName, func) {
    		return wen(eventName, function(evnt){
    			return func(evnt);
    		});
    	},
    	dropReceivers = function (eventName) {
    		if (!eventName) {
    			sigCache = {};
    			return this;
    		}
    		if (sigCache && sigCache[eventName]) {
    			sigCache[eventName] = null;
    		}
    		return this;
    	},    
    	signalOnce = function (eventName, func) {
    		var slf = this;
    		setSingleRV(true);
    		function onit () {
    			dropReceiver(eventName, wen);
    			func.apply(this, arguments);
    		};
    		wen.receiver = onit;	
    		return this;
    	},
    	dropReceiver = function (eventName, func) {
    		if (sigCache && sigCache[eventName]) {
    			var sigLst = sigCache[eventName];	
    			if (isAry(sigLst)) {
    				var idx = -1;
    				for (var i = 0, l = sigLst.length; i < l; i++) {
    					if (sigLst[i] === func || (sigLst[i].receiver && sigLst[i].receiver === func)) {
    						idx = i;
    						break;
    					}
    				}
    				if (idx < 0) {
    					return this;
    				}
    				sigLst.splice(idx, 1);
    				if (!sigLst.length) {
    					delete sigCache[eventName];
    				}
    			} 
    			else if (sigLst === func || (sigLst.receiver && sigLst.receiver === func)) {
    				delete sigCache[eventName];
    			}
    		}
    		return this;
    	},  
    	setSingleRV = function (val) {
    		singleRtnVal = val;
    		return this;
    	},  
    	getSingleRV = function() {
    		if (receivers.hasOwnProperty('singleRtnVal')) {
    			return singleRtnVal;
    		}
    		else {
    			return true;
    		}
    	},	 
    	castSignal = function(eventName, args) {
    		var recvrLst = receiverObjects(eventName),
    		k, recvr, i, rspns;
    		for (k in recvrLst ) {
    			if (recvrLst.hasOwnProperty(k)) {
    				i = recvrLst[k].length;		
    				while (i--) {
    					recvr = recvrLst[k][i];
    					if (recvr.signalOnce === true) {
    						dropReceiver(eventName, recvr);
    					}	
    					rspns = recvr.call(this, args || []);
    					if (rspns === getSingleRV()) {
    						dropReceiver(eventName, recvr);
    					}
    				}
    			}
    		}
    	 	return this;
      	};
    	function isAry(obj) {
    		return (obj.constructor === Array);
    	};
    	function receiverObjects(eventName) {
    		var recvrLst = receivers(eventName), rspns;	
    		if (isAry(recvrLst)) {
    			rspns = {};
    			rspns[eventName] = recvrLst;
    		}	
    		return rspns || recvrLst;
    	};
    	function receivers(eventName) {
    		if (!sigCache) {
    			sigCache = {};
    		}
    		if (!sigCache[eventName]) {
    			sigCache[eventName] = [];
    		}
    		if (!isAry(sigCache[eventName])) {
    			sigCache[eventName] = [sigCache[eventName]];
    		}
    		return sigCache[eventName];
    	};
    	function wen(eventName, func) {
    		if (!sigCache) {
    			sigCache = {};
    		}
    		if (!sigCache[eventName]) {
    			sigCache[eventName] = func;
    		} 
    		else if (isArry(sigCache[eventName])) {
    			sigCache[eventName].push(func);
    		} 
    		else {
    			sigCache[eventName] = [sigCache[eventName], func];
    		}	
    		return;
    	};
    	return {
    		signal: castSignal,
    		signaled: signalReceived,
    		receive: addReceiver,
    		receiveOnce: signalOnce,
    		signaledOnce: signalOnce,
    		dropReceiver: dropReceiver,
    		assignReceiver: addReceiver,
    		receivers: receivers,
    		dropReceivers: dropReceivers
    	};
    }());
    
    exports.Signals = Signals;
    
    
                

    public by sTiLL-iLL @ SniPitz-KND modified Nov 10, 2014  378408  6  9  29

    set.js... its my "Set", it runs this code-block

    Full on structure type implimentation of a Set
    
    // Set....   hut-1, hut-2, hike!
    
    function Set() {
       this.dStr = [];
       this.size = size;
       this.unionOf = unionOf;
       this.contains = contains;
       this.subSet = subSet;
       this.getDiff = getDiff;
       this.removeItem = removeItem;
       this.intersection = intersection;
       this.addItem = addItem;
       this.show = show;
    }
    
    function show() {
       return "[" + this.dStr + "]";
    }
    
    function size() {
       return this.dStr.length;
    }
    
    
    
    function addItem(dta) {
       if (this.dStr.indexOf(dta) < 0) {
          this.dStr.push(dta);
          return true;
       } 
       else {
          return false;
       }
    }
    
    function removeItem(dta) {
       var pstn = this.dStr.indexOf(dta);
       if (pstn > -1) {
          this.dStr.splice(pstn, 1);
          return true;
       }
       else {
          return false;
       }
    }
    
    function contains(dta) {
       if (this.dStr.indexOf(dta) > -1) {
          return true;
       }
       else {
          return false;
       }
    }
    
    function unionOf(set) {
       var tmp = new Set(),
           i = 0;
       for (;i < this.dStr.length; ++i) {
          tmp.add(this.dStr[i]);
       }
       i = 0;
       for (;i < set.dStr.length; ++i) {
          if (!tmp.contains(set.dStr[i])) {
             tmp.dStr.push(set.dStr[i]);
          }
       }
       return tmp;
    }
    
    function intersection(set) {
       var tSet = new Set();
       for (var i = 0; i < this.dStr.length; ++i) {
          if (set.contains(this.dStr[i])) {
             tSet.add(this.dStr[i]);
          }
       }
       return tmp;
    }
    
    function subSet(set) {
       if (this.size() > set.size()) {
          return false;
       }
       else {
          for each (var m in this.dStr) {
             if (!set.contains(m)) {
                return false;
             }
          }
       }
       return true;
    }
    i = 0;
    function difference(set) {
       var tmp = new Set();
       for (;i < this.dStor.length; ++i) {
          if (!set.contains(this.dStor[i])) {
             tmp.add(this.dStor[i]);
          }
       }
       return tmp;
    }  
    
    
    

    public by sTiLL-iLL modified Feb 17, 2015  203234  1  8  27

    kWetntz.js ... next in line please...

    sequential async execution... NEXT!!! *** idk if this even werks, havent tried yet! ***
    //  sequential execution....
    
    var kWentz = (function() {
        var roster = {}, lastTime = {}, slf = this,
            signal = function(name, arg) {
               var idx=0, kall = {};
               if(roster[name]) {
                   for(idx; kall=roster[name][idx]; idx++) {
                       kall(arg);
                   }
               }
            },
            receive = function(name, receiver) {
               if(!roster[name]) {
                  roster[name] = [];
               }
               roster[name].push(receiver);
               return slf;
            },
            drop = function(name, receiver) {
               if(roster[name]) {
                   var lst = [], kall = {}, idx = 0;
                   for(idx; kall=roster[name][idx]; idx++) {
                       if(kall !== receiver) {
                           lst.push(kall);
                       }
                   }
                   roster[name] = lst;
               }
               return slf;
            },
            exec = function() {
              if(arguments.length > 0) {
                var cbks = [], kall = {}, elm = {}, idx = 0;
                for(idx; kall=arguments[idx]; idx++) {
                   cbks.push(kall);
                }
                elm = cbks.shift();
                if(typeof elm === 'function') {
                   elm(lastTime, function(dta) {
                         lastTime = dta;
                         exec.apply(slf, cbks);
                   });
                } 
                else if(typeof elm === 'object' && elm.length > 0) {
                   var kall = elm.shift(),
                      callBK = function(dta) {
                         lastTime = dta;
                         exec.apply(slf, cbks);
                      };
                   kall.apply(kall, elm.concat([callBK]));
                }
             } 
             else {
                  signal("done", lastTime);
             }
             return slf;
          };
          return {
              execute: exec,
              receive: receive,
              drop: drop
          };
    })();            

    public by micurs modified Sep 23, 2014  12167  4  10  4

    Typescript + underscore: serializing objects

    This little example runs on node.js and assumes you have the typescript type-definition file in the types subdirectory. The example defines two classes one representing a undergrad course in computer science and another one representing possible students for the course. Few things are interesting here: 1 - the overloading of the toString()
    ///<reference path='../types/node.d.ts'/>
    ///<reference path='../types/underscore.d.ts'/>
    
    import _ = require("underscore");
    
    function write(msg) {
        process.stdout.write(msg+'\n');
    }
    
    class Student {
      firstName : string;
      lastName : string;
    
      constructor( n: string, v: string ) {
        this.firstName = n;
        this.lastName = v;
      }
    
      toString() : string {
        return '[ ' + this.firstName + ' ' + this.lastName + ' ]';
      }
    }
    
    class Class {
      title : string = "Computer Science 101";
      participants : Student[] = new Array();
    
      addStudent( s : Student ) : any {
        this.participants.push(s);
      }
    
      toString() : string {
        return this.title + '\n\t' 
              + _.reduce( this.participants, (acc,s) => acc += '\n\t'+ s );
      }
    
    }
    
    var firstSemesterClass : Class = new Class();
    
    firstSemesterClass.addStudent( new Student("John","Smith"));
    firstSemesterClass.addStudent( new Student("Frank","Gehry"));
    
    write( firstSemesterClass );
    
        

    public by sTiLL-iLL @ SniPitz-KND modified Feb 22, 2015  4365  9  8  36

    Make anything a SignlSEE broadcasting transmitter

    create observables for pubsub, or home-spun MVcwWCvvV666. The kids will eat their vegies and grandma finaly went to bed! Its a great conversation starter!
    
      // OBSERVE ME!!!!
    
    var SeeMe=(function(obj) {
    	var watchd={},mod1=obj||{};
    	function Model (mod1) {
    		this.base = mod1;
    		this.get = function(k) {
    			if (k) {
    				var u = this.base;
    				return u[k];
    			}
    			else {
    				var uu = this.base;
    				return uu;
    			}
    		};
    	}
    	watchd = Model;
    	watchd.prototype = Signals;
    	watchd.prototype.on = function(v, cbk) {
    		this.receive(v, function(d) {
    			cbk.call(this.base, d);
    		});
    	};
    	watchd.prototype.emit = function(v) {
    		this.signal("change", v);
    	};
    	watchd.prototype.off = function(v) {
    		this.drop(v);
    	};
    	watchd.prototype.set = function(k, v) {
    		if (v && v !== this.base[k]) {
    			if (v==Array||v!=Array&&k!=Array) {
    				this.base[k]=v;
    			} else if(v==Array) {
    				this.base[k].push(v);
    			}
    			this.signal("change", this.base);
    		}
    	};
      watchd = new watchd(mod1);
    	return watchd;
    }(Signals));
    
    
    var user = {
    		name1: "Richard",
    		name2: "Longwood",
    		occupation: "Software Engineer",
    		age: 40,
    		title: 'User!',
    		subTitle: "Edit Me!",
    		canEdit: true,
    		skills: ["ajax", "css", "javascript", "HTML5"]
    	};
    
    
    // Create an instance of our new type....
    
    SeeMe.base = user;
    var User = Object.create(SeeMe);
    
    
    // Now we'll see that the values are set and accessable
    
    User.get(); // Object {name1: "Richard", name2: "Longwood", occupation: "Software Engineer", age: 40, title: "User!"…}
    
    User.get("skills"); // ["ajax", "css", "javascript", "HTML5"]
    
    
    User.set("skills","blah");
    
    User.get("skills"); // "blah"
    
    
    User.set("skills",["ajax", "css", "javascript", "HTML5"]);
    
    User.get("skills");  //  ["ajax", "css", "javascript", "HTML5"]
    
    User.get("skills")[2];  // "javascript"
    
    
    User.set("skills",[1,2,3]); 
    
    User.get("skills");  // [1, 2, 3]
    
    
    /* 
        Now the kewl part... 
        lets wire up a signal channel to
        fire an alert whenever our User is changed...
    */
    
    User.on("change", function(d) {
        alert(JSON.stringify(d));
    });
    
    
    
    // Now we'll reassign them and watch our alerts go BOING!
    
    User.set("age", "OlD");
    User.set("name1", "HEaTH");
    User.set("name2", "DERN!");
    User.set("title", "who kares");
    User.set("subTitle", "whatever");
    User.set("occupation", "coding");
    User.set("skills", [1,2,3,4,5,6,7,8,9,0]);
    
    
    // Just to make sure the new values were pesisted...
    
    User.get();
    User.get("age");
    User.get("name1");
    User.get("name2");
    User.get("title");
    User.get("subTitle");
    User.get("occupation");
    User.get("skills");
    
    
    /* 
       The modules arent in the correct order of dependancy, 
       but I wanted the newest part to be featured. Below is my
       Signals eventing system. The module at the top, 
       
       SEEKr.... is a signaling, (observable), wrapper that can make
       almost anything a signal transmitter for use in pub-sub, mediator,
       
       whatever you like. theres also a small example just below the
       SEEKr module.
    */
    
    
    var Signals = (function() {
        'use strict';
        var sigCache = {}, singleRtnVal = false, 
        received = function(eventName, func) {
            return wen(eventName, function(evnt) {
                return func(evnt);
            });
        }, 
        dropReceivers = function(eventName, func) {
            if (eventName && func && typeof func === "function") {
                dropReceiver(eventName, func);
                return this;
            }
            if (!eventName) {
                sigCache = {};
                return this;
            }
            if (sigCache && sigCache[eventName]) {
                sigCache[eventName] = null;
            }
            return this;
        }, 
        setSingleRV = function(val) {
            singleRtnVal = val;
            return this;
        }, 
        getSingleRV = function() {
            if (receivers.hasOwnProperty('singleRtnVal')) {
                return singleRtnVal;
            } 
            else {
                return true;
            }
        }, 
        dropReceiver = function(eventName, func) {
            if (sigCache && sigCache[eventName]) {
                var sigLst = sigCache[eventName];
                if (isAry(sigLst)) {
                    var idx = -1;
                    for (var i = 0, l = sigLst.length; i < l; i++) {
                        if (sigLst[i] === func || (sigLst[i].receiver && sigLst[i].receiver === func)) {
                            idx = i;
                            break;
                        }
                    }
                    if (idx < 0) {
                        return this;
                    }
                    sigLst.splice(idx, 1);
                    if (!sigLst.length) {
                        delete sigCache[eventName];
                    }
                } 
                else if (sigLst === func || (sigLst.receiver && sigLst.receiver === func)) {
                    delete sigCache[eventName];
                }
            }
            return this;
        }, 
        signalOnce = function(eventName, func) {
            setSingleRV(true);
            var slf = this, 
            onit = function() {
                dropReceiver(eventName, onit);
                func.apply(slf, arguments);
            };
            onit.receiver = func;
            wen(eventName, onit);
            return this;
        }, 
        castSignal = function(eventName, args) {
            var recvrLst = receiverObjects(eventName), 
            k = {}, recvr = [], 
            i = 0, rspns = {};
            for (k in recvrLst) {
                if (recvrLst.hasOwnProperty(k)) {
                    i = recvrLst[k].length;
                    while (i--) {
                        recvr = recvrLst[k][i];
                        if (recvr.signalOnce === true) {
                            dropReceiver(eventName, recvr);
                        }
                        rspns = recvr.call(this, args || arguments || []);
                        if (rspns === getSingleRV()) {
                            dropReceiver(eventName, recvr);
                        }
                    }
                }
            }
            return this;
        };
        function isAry(obj) {
            return (obj.constructor === Array);
        }
        function receiverObjects(eventName) {
            var recvrLst = receivers(eventName), rspns;
            if (isAry(recvrLst)) {
                rspns = {};
                rspns[eventName] = recvrLst;
            }
            return rspns || recvrLst;
        }
        function receivers(eventName) {
            if (!sigCache) {
                sigCache = {};
            }
            if (!sigCache[eventName]) {
                sigCache[eventName] = [];
            }
            if (!isAry(sigCache[eventName])) {
                sigCache[eventName] = [sigCache[eventName]];
            }
            return sigCache[eventName];
        }
        function wen(eventName, func) {
            if (!sigCache) {
                sigCache = {};
            }
            if (!sigCache[eventName]) {
                sigCache[eventName] = func;
            } 
            else if (isAry(sigCache[eventName])) {
                sigCache[eventName].push(func);
            } 
            else {
                sigCache[eventName] = [sigCache[eventName], func];
            }
            return this;
        }
        return {
            signal: castSignal,
            receive: received,
            once: signalOnce,
            channels: receivers,
            drop: dropReceivers
        };
    }());
    
                                                                            

    public by sTiLL-iLL @ SniPitz-KND modified Dec 1, 2015  4102  2  7  19

    Try this HashMap! ++ ++ ++

    hash-table and key generator,.. uuids, cool extedo method, oh yeah, it also supports using objects as keys!!!
    ///////////////////////////
    // unique ids puhleez!!! //
    ///////////////////////////
    
    function nextUid() {
      var index = uid.length;
      var digit;
    
      while(index) {
        index--;
        digit = uid[index].charCodeAt(0);
        if (digit == 57 /*'9'*/) {
          uid[index] = 'A';
          return uid.join('');
        }
        if (digit == 90  /*'Z'*/) {
          uid[index] = '0';
        } else {
          uid[index] = String.fromCharCode(digit + 1);
          return uid.join('');
        }
      }
      uid.unshift('0');
      return uid.join('');
    }
    
    /////////////////////////////////////////////
    // Set or clear the hashkey for an object. //
    ////////////////////////////////////////////
    
    function setHashKey(obj, h) {
      if (h) {
        obj.$$hashKey = h;
      }
      else {
        delete obj.$$hashKey;
      }
    }
    
    function extend(dest) {
      var h = dst.$$hashKey;
      forEach(arguments, function(obj){
        if (obj !== dest) {
          forEach(obj, function(value, key){
            dest[key] = value;
          });
        }
      });
      setHashKey(dest,h);
      return dset;
    }
    
    function int(str) {
      return parseInt(str, 10);
    }
    
    
    function inherit(parent, extra) {
      return extend(new (extend(function() {},
        { prototype:parent })
      )(), extra);
    }
    
    ///////////////////////////////
       // Heath'sHash(map) //
       // keys & values //
     //////////////////////////////
    
    function hashKey(obj) {
      var objType = typeof obj,
          key;
    
      if (objType == 'object' && obj !== null) {
        if (typeof (key = obj.$$hashKey) == 'function') {
          // must invoke on object to keep the right this
          key = obj.$$hashKey();
        } else if (key === undefined) {
          key = obj.$$hashKey = nextUid();
        }
      } else {
        key = obj;
      }
    
      return objType + ':' + key;
    }
    
    ////////////////////////////////////////////
     
    function HashMap(array){
      forEach(array, this.put, this);
    }
    
    HashMap.prototype = {
      put: function(key, value) {
        this[hashKey(key)] = value;
      },
      get: function(key) {
        return this[hashKey(key)];
      },
      remove: function(key) {
        var value = this[key = hashKey(key)];
        delete this[key];
        return value;
      }
    };
                

    public by sTiLL-iLL @ SniPitz-KND modified Oct 13, 2014  3744  3  7  19

    Equality in javascript...

    An "equals" method for any type... try it, you might like it
    
    // a true Equalty comparor in JS
    
    Object.prototype.equals = function(x) {
        var p;
        for(p in this) {
          if (typeof(x[p]) == "undefined") {
            return false;
          }
        }
        for(p in this) {
          if (this[p]) {
            switch(typeof(this[p])) {
              case "object":
                if (!this[p].equals(x[p])) {
                  return false;
                }
                break;
              case "function":
                if (typeof(x[p]) == "undefined"||(p != "equals" &&
                  this[p].toString() != x[p].toString())) {
                    return false;
                }
                break;
              default:
                if (this[p] != x[p]) {
                  return false;
                }
            }
          }
          else {
            if (x[p]) {
              return false;
            }
        }
        for(p in x) {
          if(typeof(this[p])=="undefined") {
            return false;
          }
        }
        return true;
      }
    }
    

    public by micurs modified Mar 6, 2014  3365  0  7  2

    Typescript basic class

    Classes in typescript can contain properties and functions and can be instantiated by typescript code or directly in javascript.
    // TypeScript
    class Student {
    
      // Properties are public by default
      firstName: string;
      lastName: string;
      id: int;   
    
      // Constructor 
      constructor( name: string, lastName: string, id: int) {
        this.firstName = name;
        this.lastName = lastName;
        this.id = id;
      }
    }  
    
    // You can instantiate the Student from javascript or typescript in 
    // the same way.
    var aStudent = new Student('john','Smith', 1024 );
     
     
    • Public Snippets
    • Channels Snippets