export default function classNames(...args) {
const result = new Set();
function add(name) {
if (typeof name !== 'string' || name.length === 0) {
return;
}
result.add(name);
}
function remove(name) {
if (typeof name !== 'string' || name.length === 0) {
return;
}
result.delete(name);
}
function process(value) {
if (!value) {
return;
}
if (typeof value === 'string') {
add(value);
return;
}
if (typeof value === 'number') {
if (value) {
add(`${value}`);
return;
}
}
if (Array.isArray(value)) {
for (const item of value) {
process(item);
}
return;
}
if (typeof value === 'function') {
process(value());
return;
}
if (typeof value === 'object') {
for (const key in value) {
if (value[key]) {
add(key);
} else {
remove(key);
}
}
}
}
for (const arg of args) {
process(arg);
}
return Array.from(result).join(' ');
}
```
classNames('foo', 'foo'); // 'foo'
classNames({ foo: true }, { foo: true }); // 'foo'
classNames({ foo: true, bar: true }, { foo: false }); // 'bar'
classNames('foo', () => 'bar'); // 'foo bar'
classNames('foo', () => 'foo'); // 'foo'
```