all files / util/ get-class-names.js

100% Statements 31/31
100% Branches 22/22
100% Functions 2/2
100% Lines 19/19
1 statement, 3 branches Ignored     
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39                 26× 23× 23× 23×   17×     10×                    
import * as _ from 'lodash';
 
/**
 * Get a string of classNames from the object passed in. Uses the keys for class names and only adds them if the value is true. Value of keys can be boolean, function, or strings. Functions are evaluated on call. Strings are appended to end of key.
 *
 * @param {object} classObject Object containing keys of class names.
 * @returns {string}
 */
function getClassNames(classObject) {
    var classNames = [];
 
    for (var key in classObject) {
        if (classObject.hasOwnProperty(key)) {
            let check = classObject[key];
            let className = _.kebabCase(key);
            if (_.isFunction(check)) {
                if (check()) {
                    classNames.push(className);
                }
            } else if (_.isString(check)) {
                if (className === 'include' || _.includes(check, ' ')) {
                    classNames = _.concat(classNames, check.split(' '));
                } else {
                    classNames.push(className + '-' + _.kebabCase(check));
                }
            } else if (check) {
                classNames.push(className);
            }
        }
    }
 
    classNames = _.uniq(classNames);
 
    return classNames.join(' ');
}
 
export default getClassNames;