StringUtility.js

angular.module('homepage').
factory('StringUtility', function() {
    return {
        getPartsOfString: getPartsOfString,
        stringToBoolean: stringToBoolean
    }

    /**
     * SPLIT the following string into three parts
     * @param text
     * @returns [part1, part2, part3]
     */
    // TEST INPUTS
    // var text = 'eventType, attributeName, value';
    // var text = '[eventType1, eventType2, ..., eventTypeN], attributeName, value';
    // var text = 'eventType, [attributeName1, attributeName2, ..., attributeNameN], value';
    // var text = 'eventType, attributeName, [value1, value2, ..., valueN]';
    // var text = 'eventType, [attributeName1, attributeName2, ..., attributeNameN], [value1, value2, ..., valueN]';
    // var text = '[eventType1, eventType2, ..., eventTypeN], [attributeName1, attributeName2, ..., attributeNameN], [value1, value2, ..., valueN]';

    function getPartsOfString(text) {
        // if no square bracket found, split by comma
        // else parse each part
        //      find comma separated part or square bracket part
        var parts = [];
        if(text.indexOf('[') > -1) {
            return findPart(parts, text);
        }
        else {
            return parts = text.split(',');
        }
    }
    /**
     * Recursively find parts of strings
     * @param parts, text
     * @return [part1, part2, part3]
     */
    function findPart(parts, text) {
        text = text.trim();
        var indexOfComma = text.indexOf(',');
        var indexOfOpenBracket = text.indexOf('[');

        if(text.length != 0) {
            // if part doesn't contain bracket
            if(text.indexOf('[') == -1) {
                var indexOfPartEnd = indexOfComma > -1 ? indexOfComma : text.trim().length;
                parts.push(text.substring(0, indexOfPartEnd));
                findPart(parts, text.substring(indexOfPartEnd + 1));
            }
            // if part contains bracket
            else if(indexOfComma > indexOfOpenBracket) {
                var indexOfCloseBracket = text.indexOf(']');
                var partStr = text.substring(indexOfOpenBracket + 1, indexOfCloseBracket);
                var partParts = partStr.split(',');
                parts.push(partParts);
                findPart(parts, text.substring(indexOfCloseBracket + 2));
            }
            else if(indexOfComma < indexOfOpenBracket) {
                parts.push(text.substring(0, indexOfComma));
                findPart(parts, text.substring(indexOfComma + 1));
            }
        }

        return parts;
    }

    // convert value to boolean from string if string contains 'true' or 'false' value
    function stringToBoolean(string){
        switch(string.toLowerCase().trim()){
            case "true": case "yes": case "1": return true;
            case "false": case "no": case "0": case null: return false;
        }
        return string;
    }
});