UNPKG

@amaui/style

Version:
22 lines (19 loc) 1.03 kB
import unique from '@amaui/utils/unique'; import { is } from './utils'; export function classNames(value) { let prefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; let array = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; let classNameValues = []; const method = (item, prop) => { if (is('string', item) && item.length) classNameValues.push(item);else if (is('object', item)) Object.keys(item).forEach(prop_ => method(item[prop_], prop_));else if (is('array', item)) item.forEach(item_ => method(item_));else if (prop && !!item) classNameValues.push(prop); }; // Move through the value method(value); classNameValues = classNameValues.filter(Boolean).map(item_ => { let item = item_.trim(); if (['.', '#'].indexOf(item[0]) > -1) item = item.slice(1); return "".concat(prefix || '').concat(item); }); classNameValues = unique(classNameValues); return array ? classNameValues : classNameValues.join(' '); } export default classNames;