<commit_before>'use strict';

var EventHandler = require('./../utilities/event-handler');

var converter = require('framework-utilities/converter');
var toSalty = converter.sweetToSalty;
var piecewise = require('./../helpers/helpers').piecewise;

function Timelines(timelineGroups, states) {
    EventHandler.apply(this);

    this._timelineGroups = timelineGroups || {};
    this._states = states;

    this._currName = null;
    this._currTimeline = null;

    this._pausedTimelines = {};

    this._behaviorList = this._createBehaviors();
}

Timelines.prototype = Object.create(EventHandler.prototype);
Timelines.prototype.constructor = Timelines;

Timelines.prototype._createBehaviors = function _createBehaviors() {
    var behaviors = {};

    for (var timelineName in this._timelineGroups) {
        var behaviorGroup = this._timelineGroups[timelineName];

        var time = '__' + timelineName + 'Time';
        var saltyTimeline = toSalty(behaviorGroup);

        for (var selectorBehavior in saltyTimeline) {
            var timeline = saltyTimeline[selectorBehavior];

            var selector = selectorBehavior.split('|')[0];
            var behavior = selectorBehavior.split('|')[1];

            if (!behaviors[selector]) behaviors[selector] = {};

            var valueFunction = 'BEST.helpers.piecewise(' + JSON.stringify(timeline) + ')';

            behaviors[selector][behavior] = new Function(time,
                'return ' + valueFunction + '(' + time + ')'
            );
        }
    }

    return behaviors;
}

Timelines.prototype.getBehaviors = function getBehaviors() {
    return this._behaviorList || {};
}
<commit_msg>refactor: remove use of new Function, emit behavior-update instead<commit_after>'use strict';

var EventHandler = require('./../utilities/event-handler');

var converter = require('framework-utilities/converter');
var toSalty = converter.sweetToSalty;
var piecewise = require('./../helpers/helpers').piecewise;

function Timelines(timelineGroups, states) {
    EventHandler.apply(this);

    this._timelineGroups = timelineGroups || {};
    this._states = states;

    this._currName = null;
    this._currTimeline = null;

    this._pausedTimelines = {};

    this._behaviorList = this._createBehaviors();
}

Timelines.prototype = Object.create(EventHandler.prototype);
Timelines.prototype.constructor = Timelines;

Timelines.prototype._createBehaviors = function _createBehaviors() {
    var behaviors = {};

    for (var timelineName in this._timelineGroups) {
        var behaviorGroup = this._timelineGroups[timelineName];

        var time = '__' + timelineName + 'Time';
        var saltyTimeline = toSalty(behaviorGroup);

        for (var selectorBehavior in saltyTimeline) {
            var timeline = saltyTimeline[selectorBehavior];

            var selector = selectorBehavior.split('|')[0];
            var behaviorName = selectorBehavior.split('|')[1];

            if (!behaviors[selector]) behaviors[selector] = {};

            var piecewiseFunction = BEST.helpers.piecewise(timeline);

            var definition = {
                timeName: time,
                selector: selector,
                name: behaviorName,
                params: []
            };

            this._states.subscribeTo(time, function(definition, timeline) {
                var time = this._states.get(definition.timeName);
                var value = BEST.helpers.piecewise(timeline)(time);
                definition.action = value;
                this._states.emit('behavior-update', definition);
            }.bind(this, definition, timeline));
        }
    }

    return behaviors;
}

Timelines.prototype.getBehaviors = function getBehaviors() {
    return this._behaviorList || {};
}

{'fraction_non_alphanumeric': 0.12099358974358974, 'fraction_numerical': 0.0010683760683760685, 'mean_word_length': 2.9338235294117645, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4792805', 'n_tokens_mistral': 1046, 'n_tokens_neox': 995, 'n_words': 200}