package | const hasOwn = {}.hasOwnProperty;
function classNames () {
let classes = '';
for (let i = 0; i < arguments.length; i++) {
const arg = arguments[i];
if (arg) {
classes = appendClass(classes, parseValue(arg));
}
}
return classes;
}
function parseValue (arg) {
if (typeof arg === 'string') {
return arg;
}
if (typeof arg !== 'object') {
return '';
}
if (Array.isArray(arg)) {
return classNames.apply(null, arg);
}
if (arg.toString !== Object.prototype.toString && !arg.toString.toString().includes('[native code]')) {
return arg.toString();
}
let classes = '';
for (const key in arg) {
if (hasOwn.call(arg, key) && arg[key]) {
classes = appendClass(classes, key);
}
}
return classes;
}
function appendClass (value, newClass) {
if (!newClass) {
return value;
}
return value ? (value + ' ' + newClass) : newClass;
}
classNames('one two three', 'four five six', 'seven eight nine');
| ready |
package/dedupe |
function StorageObject () {}
StorageObject.prototype = Object.create(null);
function classNames () {
const classSet = new StorageObject();
appendArray(classSet, arguments);
let classes = '';
for (const key in classSet) {
if (classSet[key]) {
classes += classes ? (' ' + key) : key;
}
}
return classes;
}
function appendValue (classSet, arg) {
if (!arg) return;
const argType = typeof arg;
if (argType === 'string') {
appendString(classSet, arg);
} else if (Array.isArray(arg)) {
appendArray(classSet, arg);
} else if (argType === 'object') {
appendObject(classSet, arg);
}
}
const SPACE = /\s+/;
function appendString (classSet, str) {
const array = str.split(SPACE);
const length = array.length;
for (let i = 0; i < length; i++) {
classSet[array[i]] = true;
}
}
function appendArray (classSet, array) {
const length = array.length;
for (let i = 0; i < length; i++) {
appendValue(classSet, array[i]);
}
}
const hasOwn = {}.hasOwnProperty;
function appendObject (classSet, object) {
if (
object.toString !== Object.prototype.toString &&
!object.toString.toString().includes('[native code]')
) {
classSet[object.toString()] = true;
return;
}
for (const k in object) {
if (hasOwn.call(object, k)) {
classSet[k] = !!object[k];
}
}
}
classNames('one two three', 'four five six', 'seven eight nine');
| ready |
last one | const cx = (...args) => {
const matches = {};
let classList = '';
args.forEach((arg, k) => {
if (typeof arg === 'string') {
if(!matches[arg]) {
arg.split(' ').forEach(cls => {
if(!matches[cls]) {
matches[cls] = true;
classList += `${cls} `;
}
});
}
}
});
return classList.trim();
};
cx('one two three', 'four five six', 'seven eight nine');
| ready |