frontend-challenges icon indicating copy to clipboard operation
frontend-challenges copied to clipboard

10 - classNames - javascript

Open hackcharms opened this issue 1 year ago • 0 comments

index.js

export function classNames (...args) {
  const result=[];
  if(args.length===0){
    return result.join(' ');
  }
  args.forEach(el=>{
    if(Array.isArray(el)){
      result.push(classNames(...el));
    }
    else if(['string','number'].includes(typeof el)){
      result.push(el);
    }
    else if(el && typeof el ==='object'){
      const validEntries=Object.entries(el).filter(([key,value])=>!!value);
      result.push(...validEntries.map(([key,value])=>key));
    }
  })
  return result.join(' ');
};

hackcharms avatar Oct 11 '24 05:10 hackcharms