Skip to content

Instantly share code, notes, and snippets.

@thgreasi
thgreasi / superFastSort.js
Last active December 27, 2015 08:09
superFastSort.js
function superFastSort ( t , start, len ) {
"use strict";
if (start === undefined) {
start = 0;
}
if (len === undefined) {
len = t.length - start;
}
if (len > 1) {
@thgreasi
thgreasi / sortWithParam.js
Created December 26, 2013 20:14
superFastSort with sorting function
function superFastSort(t, sortfunction, start, len) {
"use strict";
if (!sortfunction || typeof sortfunction !== 'function') {
sortfunction = function(a, b) {
return a - b;
};
}
if (start === undefined) {
@thgreasi
thgreasi / gen.genericDirectives.js
Last active August 29, 2015 14:01
A simple directive to dynamically load proper directive based on the type of the provided model
(function() {
"use strict";
angular.module('gen.genericDirectives', [])
.directive('genDynamicDirective', ['$compile',
function($compile) {
return {
restrict: "E",
require: '^ngModel',
scope: true,
;(function($, undefined) {
function findCenter(elem) {
var offset,
document = $(elem.ownerDocument);
elem = $(elem);
offset = elem.offset();
return {
x: offset.left + elem.outerWidth() / 2 - document.scrollLeft(),
y: offset.top + elem.outerHeight() / 2 - document.scrollTop()
@thgreasi
thgreasi / crossGet.js
Created September 27, 2014 15:39
crossGetProviders
var crossGetProviders = {
whateverorigin: function crossGet(url) {
var promise = $.getJSON('http://whateverorigin.org/get?url=' + encodeURIComponent(url) +
'&callback=?');
var piped = promise.then(function (response) {
return response.contents;
});
return piped;
},
corsproxy: function corsproxy(url) {
@thgreasi
thgreasi / syncVSasyncLoopOver.js
Last active August 29, 2015 14:25
sync vs async loop over
function getParam(x) { }
function myFunc(argument) { }
function myAsyncFunc(argument) { return new Promise(function(resolve, reject) { /* ... */ }); }
//========== sync loop over
for (var i = 0; i < l; i++) {
try {
var result = myFunc(getParam(i));
@thgreasi
thgreasi / es7
Last active August 29, 2015 14:26
LocalForage setDriver Looper
async function setDriver(driverNames) {
for (var driverName of driverNames) {
if (this.supports(driverName) {
try {
let driver = await this.getDriver(driverName).then(driver => driver._initDriver());
if (driver) {
return Promise.resolve(driver);
}
} catch (e) { }
}
@thgreasi
thgreasi / PromiseGeneratorConsumers.js
Created August 7, 2015 19:07
Promise Generator Consumers
// iterate till first success
new Promise((resolve) => {
runGeneratorOnce(myPromiseGenerator, initialValue);
function runGeneratorOnce(pg, result) { var status = pg.next(result);
if (status.done) { reject(); return; }
status.value.then(resolve).catch(value => {
return runGeneratorOnce(pg, value);
@thgreasi
thgreasi / firebaseCheckpointArray.js
Last active September 20, 2017 17:28
firebase.checkpointArray
/*
Implemented by katowulf at:
https://jsfiddle.net/katowulf/1dfyz2rq/
Discussed in:
https://github.com/firebase/angularfire/issues/687
https://github.com/angular-ui/ui-sortable/issues/421
*/
angular.module('firebase.checkpointArray', ['firebase'])
.factory('firebaseCheckpointArray', function($firebaseArray) {
@thgreasi
thgreasi / appNamespace.appManager.js
Last active April 25, 2016 09:20
AngularJS lifecycle app manager
(function () {
"use strict";
window.appNamespace = window.appNamespace || {};
appNamespace.appManager = appNamespace.appManager || new AppManager();
function AppManager() {
this.currentAppName = '';
this.currentApp = null;