diff --git a/array.extensions.js b/array.extensions.js index f346e4d..09a79d9 100644 --- a/array.extensions.js +++ b/array.extensions.js @@ -20,4 +20,19 @@ Array.prototype.sum = function() { **/ Array.prototype.clone = function() { return this.concat(); -} \ No newline at end of file +} + +/** + * Array#isUnique(value) -> Boolean + * returns true if a specified value is unique in an array + * (or if it's not in an array) + * + * [1,2,3].isUnique(1); // true + * [1,2,3,1].isUnique(1); // false + * [5,6,'a'].isUnique('foo'); // true + * + **/ +Array.prototype.isUnique = function(value){ + var idx = this.indexOf(value); + return this.indexOf(value, idx + 1) == -1; +}; \ No newline at end of file diff --git a/experimental/report.js b/experimental/report.js new file mode 100644 index 0000000..9fecc3a --- /dev/null +++ b/experimental/report.js @@ -0,0 +1,52 @@ +/* + + Reports: + - number of event handlers on a page + - non-unique ID's + - collapsing names/ID's (IE bug) + + Requires: + prototype.js v. 1.6.0.3 + +*/ + +function report() { + function isUnique(arr, value) { + var idx = arr.indexOf(value); + return arr.indexOf(value, idx + 1) == -1; + } + // event handlers + var results = 0, c = Event.cache; + for (var element in c) { + for (var handler in c[element]) { + for (var eventName in c[element]) { + if (eventName == 'element') continue; + results += c[element][eventName].length; + } + } + } + // find non-unique ids + var ids = $$('*').pluck('id').without(''); + var nonUnique = []; + ids.each(function(id) { + if (!isUnique(ids, id)) + (nonUnique.indexOf(id) == -1) && nonUnique.push(id); + }) + + // find collapsing names/ids (IE) + var withName = $$('*[name]'); + var withId = $$('*[id]'); + var collapsing = []; + withId.each(function(el) { + var collapsed = withName.find(function(_el){ return (_el.name == el.id) && _el != el }) + if (collapsed) { + collapsing.push(el, collapsed); + } + }); + + // return result + return ['Event handlers: ', results, + '\nNon-Unique ID\'s: ', (nonUnique.length ? nonUnique : 'none'), + '\nCollapsing names/ID\'s: ', (collapsing.length ? collapsing : 'none') + ].join(''); +} \ No newline at end of file