Last active
March 1, 2017 21:36
-
-
Save mattslack/83b9f47672c84a738e8a to your computer and use it in GitHub Desktop.
Find duplicate IDs in a document.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
"use strict"; | |
var find_dupes = function () { | |
var identified = document.querySelectorAll('[id]'), | |
unique = [], | |
dupes = [], | |
id, | |
idx = 0; | |
while (idx < identified.length) { | |
id = (identified[idx]).getAttribute('id'); | |
if (/^\s+$/.test(id)) { | |
id = "WHITESPACE"; | |
} | |
if (/^$/.test(id)) { | |
id = "EMPTYSTRING"; | |
} | |
if (unique.indexOf(id) === -1) { | |
unique.push(id); | |
} else { | |
if (dupes.indexOf(id) === -1) { | |
dupes.push(id) | |
} | |
} | |
idx++; | |
} | |
if (dupes.length) { | |
return dupes.length + " Duplicate IDs found:\n" + dupes.join("\n"); | |
} | |
return "No duplicate IDs"; | |
}; | |
console.log(find_dupes()); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment