Skip to content

Instantly share code, notes, and snippets.

angular.module('Scope.safeApply', [])
.run(['$rootScope', function ($rootScope) {
$rootScope.$safeApply = function () {
var fn, phase = this.$root.$$phase;
if (arguments.length == 1) {
fn = arguments[0];
} else {
myApp.directive('capitalizeFirst', function($parse) {
return {
require: 'ngModel',
link: function(scope, element, attrs, modelCtrl) {
var capitalize = function(inputValue) {
if (inputValue === undefined) { inputValue = ''; }
var capitalized = inputValue.charAt(0).toUpperCase() +
inputValue.substring(1);
if(capitalized !== inputValue) {
modelCtrl.$setViewValue(capitalized);
@toboqus
toboqus / speedTestJS
Created September 23, 2014 14:44
Performance testing JS
(function(global){
var testFunc = function testFunc(func){
var start = new Date();
func();
return new Date() - start;
},
speedTest = function speedTest(name, func){
var numOfTimes = 1000,
@toboqus
toboqus / loadDependencies.js
Last active August 29, 2015 14:07
Synchronously execute functions/load dependencies
$scope.loadDependencies = function(arr){
var deferred = $q.defer();
var arrLen = arr.length,i = -1;
(function loadNext(){
i++;
(i < arrLen) ? arr[i](loadNext) : deferred.resolve();
})();
return deferred.promise;
};
@toboqus
toboqus / RussianPeasant.java
Created September 14, 2015 18:32
Russian peasant algorithm
public class RussianPeasant {
public Static int product(int a, int b){
int x = a;
int y = b;
int z = 0;
while(x > 0){
if(x%2 == 1) z += y;
x = x >> 1;
@toboqus
toboqus / LinkedList.c
Created November 4, 2015 20:49
Linked list in C
/*
* LinkedList.c
*
* Created on: 4 Nov 2015
* Author: Alex
*/
#include <stdlib.h>
#include <stdio.h>
@toboqus
toboqus / BinarySearchTree.c
Created November 5, 2015 20:14
Binary Search Tree in C
/*
* Tree.c
*
* Created on: 5 Nov 2015
* Author: Alex
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
int main(void)
{
int fd = open("/dev/sdd", O_WRONLY);
if (fd < 0) {
@toboqus
toboqus / pushingToTemplate.js
Created December 2, 2015 13:23
Pushing an object onto another object if the element names match
/**
* @name pushToTemplate
* @param template
* @param object
* @returns {object}
* @description will push the object onto the template
*/
var pushToTemplate = function pushToTemplate(template, object){
var result = angular.copy(template);
@toboqus
toboqus / OrderedInsert.js
Last active December 14, 2015 08:43
Ordered insert using binary searching for angularJS
/**
* @name orderedInsert
* @param {Array} collection
* @param {String} key
* @param {Object | array} items - array of objects to insert
* @returns {promise}
* @description will insert an object into a sorted collection using a modified
* form of binary searching. it will also insert an array of objects if that is provided
* instead.
* @example