Skip to content

Instantly share code, notes, and snippets.

@DorkForce
DorkForce / scanDomForDuplicateIds
Last active July 25, 2016 17:06
Scan DOM for duplicate element IDs
(function () {
console.groupCollapsed('Duplicate element IDs within DOM');
var dupes = [],
elms = document.getElementsByTagName("*"), i, len, ids = {}, id;
for (i = 0, len = elms.length; i < len; i += 1) {
id = elms[i].id || null;
if (id) {
ids[id] = ids.hasOwnProperty(id) ? ids[id] +=1 : 0;
}
}
@DorkForce
DorkForce / mock_service_return_promise.js
Created February 5, 2016 17:42
Mock a service to return a promise
// from http://stackoverflow.com/questions/23705051/how-do-i-mock-a-service-that-returns-promise-in-angularjs-jasmine-unit-test
beforeEach(inject( function(_myService_, $q){
myService = _myService_;
myOtherServiceMock.makeRemoteCallReturningPromise = function() {
var deferred = $q.defer();
deferred.resolve('Remote call result');
return deferred.promise;
};
}
@DorkForce
DorkForce / multiDimensionalArray.js
Created January 3, 2016 13:41
Create Multi-dimensional Array Function
function createArray(length) {
var arr = new Array(length || 0),
i = length;
if (arguments.length > 1) {
var args = Array.prototype.slice.call(arguments, 1);
while(i--) arr[length-1 - i] = createArray.apply(this, args);
}
return arr;
@DorkForce
DorkForce / selfIdentifyingFunction
Last active July 25, 2016 17:05
Self-Identify a function by name
function getFunctionName() {
var re = /function (.*?)\(/
var s = getFunctionName.caller.toString();
var m = re.exec( s )
return m[1];
}
function me() {
console.log( getFunctionName() );
}
@DorkForce
DorkForce / countAngularWatchers
Last active July 25, 2016 17:04
Console Script to Count Angular Watchers - change 'body' to whichever element has ng-app
(function () {
var root = angular.element(document.getElementsByTagName('body'));
var watchers = [];
var f = function (element) {
angular.forEach(['$scope', '$isolateScope'], function (scopeProperty) {
if (element.data() && element.data().hasOwnProperty(scopeProperty)) {
angular.forEach(element.data()[scopeProperty].$$watchers, function (watcher) {
watchers.push(watcher);
@DorkForce
DorkForce / scanScope.txt
Last active August 16, 2024 10:00
Console script - Search breakpoint model for value. SYNTAX: scanScope($scope.model, 'Fred');
console.log("Usage Syntax: scanScope(objectToScan, 'scanFor', ['whatToIgnore']); %c(whatToIgnore is optional and can be a string, or an array of strings) (scanScope can be shortened to ss)", 'color: red');
var abortAtLevel = 20,
callStack = 0,
errArray = [],
funArray = [],
scanLoop = function (whatToScan, scanValue, whatToIgnore, parentTree) {
scanValue = scanValue.toLowerCase();
if (Array.isArray(whatToIgnore)) {
whatToIgnore.forEach(function (ignoreVal) {
ignoreVal = lowerCase(ignoreVal);