<commit_before>'use strict';

var Path = require('path');

var BuildHelpers = require('./../build-helpers');
var EsprimaHelpers = require('./../esprima-helpers');
var PathingHelpers = require('./../storage-helpers/pathing');

var PIPE = '|';

var BEHAVIOR_STR = 'behavior';
var EVENT_STR = 'event';
var SETTER_STR = 'setter';
var IDENTITY_STR = 'identity';

function interpolateAssetStrings(moduleName, moduleVersionRef, moduleDefinitionAST) {
    EsprimaHelpers.eachStringLiteral(moduleDefinitionAST, function(stringValue, node) {
        var fullPath;
        var moduleCDNMatch = BuildHelpers.stringToModuleCDNMatch.call(this, stringValue);
        if (moduleCDNMatch) {
            var assetModuleName = moduleCDNMatch.value.split(PIPE)[1] || moduleName;
            fullPath = PathingHelpers.buildAssetURL.call(this, assetModuleName, moduleVersionRef, '');
            node.value = stringValue.split(moduleCDNMatch.match).join(fullPath);
        }
        else {
            var matches = 0;
            BuildHelpers.eachAssetStringMatchInString.call(this, stringValue, function(match, replaced) {
                fullPath = PathingHelpers.buildAssetURL.call(this, moduleName, moduleVersionRef, replaced);
                stringValue = stringValue.split(match).join(fullPath);
                matches++;
            }.bind(this));
            if (matches > 0) {
                node.value = stringValue;
            }
        }
    }.bind(this));
}

function expandBehaviorsObject(behaviorsAST) {
    EsprimaHelpers.eachObjectProperty(behaviorsAST, function(_0, _1, _2, valueObj) {
        EsprimaHelpers.eachObjectProperty(valueObj, function(keyName, _1, subValueVal, subValueObj, eventProp) {
            if (EsprimaHelpers.isStringLiteral(subValueObj) && subValueVal.match(this.options.behaviorSetterRegex)) {
                eventProp.value = buildFunctionAST(keyName, subValueVal, behaviorFnStringTemplate, errorFnTemplate, BEHAVIOR_STR);
            }
        }.bind(this));
    }.bind(this));
}

function behaviorFnStringTemplate(stateName) {
    return '(function(' + stateName + '){ return ' + stateName + '; })';
}

function eventFnStringTemplate(stateName) {
    return '(function($state,$payload){$state.set(\'' + stateName + '\',$payload);})';
}

function errorFnTemplate(type, wrongShorthand, rightShorthand) {
    return '(function(){console.warn(\'Cannot use ' + wrongShorthand + ' shorthand in ' + type + 's. Use ' + rightShorthand + ' instead.\');})';
}

var FUNCTION_FILTERS = {};
// Camel-case the given hyphen-separated string
FUNCTION_FILTERS.camel = function(str) {
    return str.replace(/-([a-z])/g, function(g) {
        return g[1].toUpperCase();
    });
};
// Alias
FUNCTION_FILTERS['camel-case'] = FUNCTION_FILTERS.camel;

function allEventFunctionFilters(key, filters) {
    for (var i = 0; i < filters.length; i++) {
        var filter = filters[i];
        if (FUNCTION_FILTERS[filter]) {
            key = FUNCTION_FILTERS[filter](key);
        }
    }
    return key;
}

function buildFunctionAST(key, value, fnStringTemplate, errorFnTemplate, type) {
    if (value[0] !== '[' && value[1] !== '[') {
        // Warn developer and correct syntax for backward compatibility
        console.warn('Please use the correct shorthand syntax for ' + key + ' denoted by double brackets. [[' + value + ']] rather than ' + value);
        value = '[[' + value + ']]';
    }

    var subValueVal = value.substr(2, value.length - 4); // Remove brackets
    var functionParts = subValueVal.split(PIPE);
    var functionKey = functionParts[0];
    var filters = functionParts.slice(1, functionParts.length);

    var stateName;
    var fnString;
    var body;

    switch (functionKey) {
        case SETTER_STR:
            stateName = allEventFunctionFilters(key, filters);

            if (type === BEHAVIOR_STR) {
                fnString = errorFnTemplate(type, SETTER_STR, IDENTITY_STR);
            }
            else {
                fnString = fnStringTemplate(stateName);
            }

            body = EsprimaHelpers.parse(fnString).body[0];
            return body.expression;
        case IDENTITY_STR:
            // 'identity'
            if (filters.length === 0) {
                stateName = key;
            }
            //'identity|myContent'
            else {
                stateName = filters.splice(-1);
                stateName = allEventFunctionFilters(stateName, filters);
            }<commit_msg>feat: Add ability to pipe states into setter shorthand<commit_after>'use strict';

var Path = require('path');

var BuildHelpers = require('./../build-helpers');
var EsprimaHelpers = require('./../esprima-helpers');
var PathingHelpers = require('./../storage-helpers/pathing');

var PIPE = '|';

var BEHAVIOR_STR = 'behavior';
var EVENT_STR = 'event';
var SETTER_STR = 'setter';
var IDENTITY_STR = 'identity';

function interpolateAssetStrings(moduleName, moduleVersionRef, moduleDefinitionAST) {
    EsprimaHelpers.eachStringLiteral(moduleDefinitionAST, function(stringValue, node) {
        var fullPath;
        var moduleCDNMatch = BuildHelpers.stringToModuleCDNMatch.call(this, stringValue);
        if (moduleCDNMatch) {
            var assetModuleName = moduleCDNMatch.value.split(PIPE)[1] || moduleName;
            fullPath = PathingHelpers.buildAssetURL.call(this, assetModuleName, moduleVersionRef, '');
            node.value = stringValue.split(moduleCDNMatch.match).join(fullPath);
        }
        else {
            var matches = 0;
            BuildHelpers.eachAssetStringMatchInString.call(this, stringValue, function(match, replaced) {
                fullPath = PathingHelpers.buildAssetURL.call(this, moduleName, moduleVersionRef, replaced);
                stringValue = stringValue.split(match).join(fullPath);
                matches++;
            }.bind(this));
            if (matches > 0) {
                node.value = stringValue;
            }
        }
    }.bind(this));
}

function expandBehaviorsObject(behaviorsAST) {
    EsprimaHelpers.eachObjectProperty(behaviorsAST, function(_0, _1, _2, valueObj) {
        EsprimaHelpers.eachObjectProperty(valueObj, function(keyName, _1, subValueVal, subValueObj, eventProp) {
            if (EsprimaHelpers.isStringLiteral(subValueObj) && subValueVal.match(this.options.behaviorSetterRegex)) {
                eventProp.value = buildFunctionAST(keyName, subValueVal, behaviorFnStringTemplate, errorFnTemplate, BEHAVIOR_STR);
            }
        }.bind(this));
    }.bind(this));
}

function behaviorFnStringTemplate(stateName) {
    return '(function(' + stateName + '){ return ' + stateName + '; })';
}

function eventFnStringTemplate(stateName) {
    return '(function($state,$payload){$state.set(\'' + stateName + '\',$payload);})';
}

function errorFnTemplate(type, wrongShorthand, rightShorthand) {
    return '(function(){console.warn(\'Cannot use ' + wrongShorthand + ' shorthand in ' + type + 's. Use ' + rightShorthand + ' instead.\');})';
}

var FUNCTION_FILTERS = {};
// Camel-case the given hyphen-separated string
FUNCTION_FILTERS.camel = function(str) {
    return str.replace(/-([a-z])/g, function(g) {
        return g[1].toUpperCase();
    });
};
// Alias
FUNCTION_FILTERS['camel-case'] = FUNCTION_FILTERS.camel;

function allEventFunctionFilters(key, filters) {
    for (var i = 0; i < filters.length; i++) {
        var filter = filters[i];
        if (FUNCTION_FILTERS[filter]) {
            key = FUNCTION_FILTERS[filter](key);
        }
    }
    return key;
}

function buildFunctionAST(key, value, fnStringTemplate, errorFnTemplate, type) {
    if (value[0] !== '[' && value[1] !== '[') {
        // Warn developer and correct syntax for backward compatibility
        console.warn('Please use the correct shorthand syntax for ' + key + ' denoted by double brackets. [[' + value + ']] rather than ' + value);
        value = '[[' + value + ']]';
    }

    var subValueVal = value.substr(2, value.length - 4); // Remove brackets
    var functionParts = subValueVal.split(PIPE);
    var functionKey = functionParts[0];
    var filters = functionParts.slice(1, functionParts.length);

    var stateName;
    var fnString;
    var body;

    switch (functionKey) {
        case SETTER_STR:
            // 'setter'
            if (filters.length === 0) {
                stateName = key;
            }
            // 'setter|something'
            else {
                // 'setter|camel'
                if (filters[0] === 'camel') {
                    stateName = allEventFunctionFilters(key, filters);
                }
                // 'setter|state'
                else {
                    stateName = filters.splice(-1);
                    stateName = allEventFunctionFilters(stateName, filters);
                }
            }

            if (type === BEHAVIOR_STR) {
                fnString = errorFnTemplate(type, SETTER_STR, IDENTITY_STR);
            }
            else {
                fnString = fnStringTemplate(stateName);
            }

            body = EsprimaHelpers.parse(fnString).body[0];
            return body.expression;
        case IDENTITY_STR:
            // 'identity'
            if (filters.length === 0) {
                stateName = key;
            }
            //'identity|something'
            else {
                // 'identity|camel'
                if (filters[0] === 'camel') {
                    stateName = allEventFunctionFilters(key, filters);
                }
                // 'identity|state'
                else {
                    stateName = filters.splice(-1);
                    stateName = allEventFunctionFilters(stateName, filters);
                }
            }
{'fraction_non_alphanumeric': 0.12640478399835034, 'fraction_numerical': 0.004124136508918445, 'mean_word_length': 2.4854473589651453, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4221911', 'n_tokens_mistral': 2833, 'n_tokens_neox': 2782, 'n_words': 552}