Created
December 16, 2018 21:13
-
-
Save dead-claudia/4880395520ffdda9a6f54c0f3e8b956e to your computer and use it in GitHub Desktop.
`classnames`, but emitting an object instead of a string.
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
var hasOwn = {}.hasOwnProperty | |
function writeClasses() { | |
for (var i = 0; i < arguments.length; i++) { | |
var arg = arguments[i] | |
if (!arg) continue | |
if (typeof arg === 'string' || typeof arg === 'number') { | |
this[arg] = true | |
} else if (typeof arg === 'string') { | |
if (/^\S+$/g.test(arg)) { | |
this[arg] = true | |
} else { | |
arg = arg.split(" ") | |
for (var j = 0; j < arg.length; j++) this[arg[i]] = true | |
} | |
} else if (Array.isArray(arg)) { | |
if (arg.length) writeClasses.apply(this, arg) | |
} else if (typeof arg === 'object') { | |
for (var key in arg) { | |
if (hasOwn.call(arg, key) && arg[key]) { | |
writeClasses.call(this, key) | |
} | |
} | |
} | |
} | |
} | |
function classNames() { | |
var classes = Object.create(null) | |
writeClasses.apply(this, arguments) | |
return classes | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment