Skip to content

Instantly share code, notes, and snippets.

// resets first levels object props
function shallowResetObject(obj){
for(var key in obj){
if( !obj.hasOwnProperty(key) ){
continue;
}
obj[ key ] = undefined;
}
}
@pavvell
pavvell / youtubeID.js
Created April 10, 2016 16:15 — forked from takien/youtubeID.js
Get YouTube ID from various YouTube URL using JavaScript
/**
* Get YouTube ID from various YouTube URL
* @author: takien
* @url: http://takien.com
* For PHP YouTube parser, go here http://takien.com/864
*/
function YouTubeGetID(url){
var ID = '';
url = url.replace(/(>|<)/gi,'').split(/(vi\/|v=|\/v\/|youtu\.be\/|\/embed\/)/);
// Sum multidimensional array without using recursion
// Values in the given array could be of any type, numerical strings will be converted to numbers
function arraySum(arr){
var sum = 0;
var i;
var subArray = [];
while(arr.length !== 0){
// PubSub. Returns unsubscribe function on call subscribe()
function PubSub(){
this.events = {};
}
PubSub.prototype.subscribe = function(eventName, callback){
var self = this;
// 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){
function assert(condition, message) {
if (!condition) {
message = message || "Assertion failed";
if (typeof Error !== "undefined") {
throw new Error(message);
}
throw message; // Fallback
}
}
[1,2,3,4,5,6,7,8,9,10].sort(function(){
return 50 - Math.floor(Math.random() * 100)
})
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',
@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;
}
@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]) {