Created
July 25, 2018 08:46
-
-
Save yevhenorlov/be25e935b7383db9211a87e6b5bb4a46 to your computer and use it in GitHub Desktop.
Print component's classes (as CSS selectors) to a String and return it. Log to console optionally.
This file contains 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 printSelectorList(selector, logToConsole) { | |
const _logToConsole = !!logToConsole || false | |
if (!selector) throw new TypeError('Selector argument is required') | |
if (typeof selector !== 'string') { | |
throw new TypeError('Attribute must be a string') | |
} | |
const component = document.querySelector(selector) | |
const descendants = Array.prototype.slice.call( | |
component.querySelectorAll('*'), | |
0 | |
) | |
const classSelectorList = descendants | |
.map(el => { | |
if (!el.classList.length) return | |
return `\n// .${el.classList[0]} {}\n` | |
}) | |
.filter(el => typeof el !== 'undefined') | |
.join('') | |
if (_logToConsole) console.log(classSelectorList) | |
return classSelectorList | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment