Skip to content

Instantly share code, notes, and snippets.

<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
@pavvell
pavvell / README.md
Last active November 24, 2015 13:52 — forked from balupton/README.md
Node.js Best Practice Exception Handling
@pavvell
pavvell / example.js
Last active December 10, 2015 19:06 — forked from addyosmani/example.js
Mediator pattern
// Example 1
mediator.name = 'Doug';
mediator.subscribe('nameChange', function(arg){
console.log(this.name);
this.name = arg;
console.log(this.name);
});
mediator.publish('nameChange', 'Jorn');
@pavvell
pavvell / pubsub.js
Created December 10, 2015 19:43 — forked from learncodeacademy/pubsub.js
Basic Javascript PubSub Pattern
//events - a super-basic Javascript (publish subscribe) pattern
var events = {
events: {},
on: function (eventName, fn) {
this.events[eventName] = this.events[eventName] || [];
this.events[eventName].push(fn);
},
off: function(eventName, fn) {
if (this.events[eventName]) {
@pavvell
pavvell / random.js
Created January 8, 2016 11:11 — forked from kerimdzhanov/random.js
Javascript — random number in a specific range
/**
* Get a random floating point number between `min` and `max`.
*
* @param {number} min - min number
* @param {number} max - max number
* @return {float} a random floating point number
*/
function getRandom(min, max) {
return Math.random() * (max - min) + min;
}
var myApp = angular.module('myApp', ['ngResource']);
myApp.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/', {
title: 'Home',
templateUrl: '/assets/views/home.html',
controller: 'HomeController'
});
$routeProvider.when('/Product/:id', {
title: 'Product',
[1,2,3,4,5,6,7,8,9,10].sort(function(){
return 50 - Math.floor(Math.random() * 100)
})
function assert(condition, message) {
if (!condition) {
message = message || "Assertion failed";
if (typeof Error !== "undefined") {
throw new Error(message);
}
throw message; // Fallback
}
}
// find keys in object, only those keys that names satisfies regexp
// returns array of found keys or empty array
function findKeysByRegexp(obj, re){
var allAttributes = Object.getOwnPropertyNames(obj);
var keys = [];
allAttributes.forEach(retrieveKeys);
function retrieveKeys(attrName){
// PubSub. Returns unsubscribe function on call subscribe()
function PubSub(){
this.events = {};
}
PubSub.prototype.subscribe = function(eventName, callback){
var self = this;