<commit_before>import {isFunction, isArray, isObject, isString, pop, assign} from 'd3-let';
import {map} from 'd3-collection';
import {dispatch} from 'd3-dispatch';

import base from './transition';
import createDirective from './directive';
import warn from '../utils/warn';
import asSelect from '../utils/select';
import maybeJson from '../utils/maybeJson';
import sel from '../utils/sel';
import dataAttributes from '../utils/data';
import viewEvents from './events';


// prototype for both views and components
const protoComponent = {
    //
    // hooks
    render () {},
    childrenMounted () {},
    mounted () {},
    destroy () {},
    //
    // Mount the component into an element
    // If this component is already mounted, or it is mounting, it does nothing
    mount (el, data, onMounted) {
        if (mounted(this)) return;
        viewEvents.call('component-mount', undefined, this, el, data);
        var sel = this.select(el),
            directives = sel.directives(),
            dattrs = directives ? directives.attrs : attributes(el),
            parentModel = this.parent.model,
            datum = sel.datum();

        let props = this.props,
            model = this.model,
            modelData = assign(dataAttributes(dattrs), datum, data),
            key, value;

        data = assign({}, datum, data);

        // override model keys from data object and element attributes
        for (key in model) {
            value = pop(modelData, key);
            if (value !== undefined) {
                if (isString(value)) {
                    if (parentModel.$isReactive(value)) {
                        if (value !== key) model[key] = reactiveParentProperty(value);
                        else delete model[key];
                    } else model[key] = maybeJson(value);
                } else model[key] = value;
            }
        }

        // Create model
        this.model = model = parentModel.$child(model);
        model.$$view = this;
        model.$$name = this.name;
        if (isArray(props)) props = props.reduce((o, key) => {
            o[key] = undefined;
            return o;
        }, {});

        if (isObject(props)) {
            Object.keys(props).forEach(key => {
                value = maybeJson(modelData[key] === undefined ? (data[key] === undefined ? dattrs[key] : data[key]) : modelData[key]);
                if (value !== undefined) {
                    // data point to a model attribute
                    if (isString(value) && model[value]) value = model[value];
                    data[key] = value;
                } else if (props[key] !== undefined) {
                    data[key] = props[key];
                }
            });
        }
        //
        // create the new element from the render function
        var newEl = this.render(data, dattrs, el);
        if (!newEl.then) newEl = Promise.resolve(newEl);
        return newEl.then(element => compile(this, el, element, onMounted));
    }
};

// factory of View and Component constructors
export function createComponent (name, o, coreDirectives) {
    if (isFunction(o)) o = {render: o};

    var obj = assign({}, o),
        classComponents = extendComponents(map(), pop(obj, 'components')),
        classDirectives = extendDirectives(map(), pop(obj, 'directives')),
        model = pop(obj, 'model'),
        props = pop(obj, 'props');

    function Component (options) {
        var parent = pop(options, 'parent'),
            components = map(parent ? parent.components : null),
            directives = map(parent ? parent.directives : coreDirectives),
            events = dispatch('message', 'mounted'),
            cache = {};

        classComponents.each((comp, key) => {
            components.set(key, comp);
        });
        classDirectives.each((comp, key) => {
            directives.set(key, comp);
        });
        extendComponents(components, pop(options, 'components'));
        extendDirectives(directives, pop(options, 'directives'));

        Object.defineProperties(this, {
            name : {
                get () {
                    return name;
                }
            },
            components: {
                get: function () {
                    return components;
                }
            },
            directives: {
                get: function () {
                    return directives;
                }
            },
            parent: {
                get: function () {
                    return parent;
                }
            },
            root: {
                get: function () {
                    return parent ? parent.root : this;
                }
            },
            cache: {
                get: function () {
                    return parent ? parent.cache : cache;
                }
            },
            props: {
                get: function () {
                    return props;
                }
            },
            uid: {
                get: function () {
                    return this.model.uid;
                }
            },
            events: {
                get: function () {
                    return events;
                }
            }
        });
        this.model = assign({}, isFunction(model) ? model() : model, pop(options, 'model'));
        viewEvents.call('component-created', undefined, this);
    }

    Component.prototype = assign({}, base, protoComponent, obj);

    function component (options) {
        return new Component(options);
    }

    component.prototype = Component.prototype;

    return component;
}


// Used by both Component and view

export function extendComponents (container, components) {
    map(components).each((obj, key) => {
        container.set(key, createComponent(key, obj, protoComponent));
    });
    return container;
}

export function extendDirectives (container, directives) {
    map(directives).each((obj, key) => {
        container.set(key, createDirective(obj));
    });
    return container;
}

// Finalise the binding between the view and the model
// inject the model into the view element
// call the mounted hook and can return a Promise
export function asView(vm, element, onMounted) {
    Object.defineProperty(sel(vm), 'el', {
        get: function () {
            return element;
        }<commit_msg>inject 95614view consistently<commit_after>import {isFunction, isArray, isObject, isString, pop, assign} from 'd3-let';
import {map} from 'd3-collection';
import {dispatch} from 'd3-dispatch';

import base from './transition';
import createDirective from './directive';
import warn from '../utils/warn';
import asSelect from '../utils/select';
import maybeJson from '../utils/maybeJson';
import sel from '../utils/sel';
import dataAttributes from '../utils/data';
import viewEvents from './events';


// prototype for both views and components
const protoComponent = {
    //
    // hooks
    render () {},
    childrenMounted () {},
    mounted () {},
    destroy () {},
    //
    // Mount the component into an element
    // If this component is already mounted, or it is mounting, it does nothing
    mount (el, data, onMounted) {
        if (mounted(this)) return;
        viewEvents.call('component-mount', undefined, this, el, data);
        var sel = this.select(el),
            directives = sel.directives(),
            dattrs = directives ? directives.attrs : attributes(el),
            parentModel = this.parent.model,
            datum = sel.datum();

        let props = this.props,
            model = this.model,
            modelData = assign(dataAttributes(dattrs), datum, data),
            key, value;

        data = assign({}, datum, data);

        // override model keys from data object and element attributes
        for (key in model) {
            value = pop(modelData, key);
            if (value !== undefined) {
                if (isString(value)) {
                    if (parentModel.$isReactive(value)) {
                        if (value !== key) model[key] = reactiveParentProperty(value);
                        else delete model[key];
                    } else model[key] = maybeJson(value);
                } else model[key] = value;
            }
        }

        // Create model
        this.model = model = parentModel.$child(model);
        if (isArray(props)) props = props.reduce((o, key) => {
            o[key] = undefined;
            return o;
        }, {});

        if (isObject(props)) {
            Object.keys(props).forEach(key => {
                value = maybeJson(modelData[key] === undefined ? (data[key] === undefined ? dattrs[key] : data[key]) : modelData[key]);
                if (value !== undefined) {
                    // data point to a model attribute
                    if (isString(value) && model[value]) value = model[value];
                    data[key] = value;
                } else if (props[key] !== undefined) {
                    data[key] = props[key];
                }
            });
        }
        //
        // create the new element from the render function
        var newEl = this.render(data, dattrs, el);
        if (!newEl.then) newEl = Promise.resolve(newEl);
        return newEl.then(element => compile(this, el, element, onMounted));
    }
};

// factory of View and Component constructors
export function createComponent (name, o, coreDirectives) {
    if (isFunction(o)) o = {render: o};

    var obj = assign({}, o),
        classComponents = extendComponents(map(), pop(obj, 'components')),
        classDirectives = extendDirectives(map(), pop(obj, 'directives')),
        model = pop(obj, 'model'),
        props = pop(obj, 'props');

    function Component (options) {
        var parent = pop(options, 'parent'),
            components = map(parent ? parent.components : null),
            directives = map(parent ? parent.directives : coreDirectives),
            events = dispatch('message', 'mounted'),
            cache = {};

        classComponents.each((comp, key) => {
            components.set(key, comp);
        });
        classDirectives.each((comp, key) => {
            directives.set(key, comp);
        });
        extendComponents(components, pop(options, 'components'));
        extendDirectives(directives, pop(options, 'directives'));

        Object.defineProperties(this, {
            name : {
                get () {
                    return name;
                }
            },
            components: {
                get: function () {
                    return components;
                }
            },
            directives: {
                get: function () {
                    return directives;
                }
            },
            parent: {
                get: function () {
                    return parent;
                }
            },
            root: {
                get: function () {
                    return parent ? parent.root : this;
                }
            },
            cache: {
                get: function () {
                    return parent ? parent.cache : cache;
                }
            },
            props: {
                get: function () {
                    return props;
                }
            },
            uid: {
                get: function () {
                    return this.model.uid;
                }
            },
            events: {
                get: function () {
                    return events;
                }
            }
        });
        this.model = assign({}, isFunction(model) ? model() : model, pop(options, 'model'));
        viewEvents.call('component-created', undefined, this);
    }

    Component.prototype = assign({}, base, protoComponent, obj);

    function component (options) {
        return new Component(options);
    }

    component.prototype = Component.prototype;

    return component;
}


// Used by both Component and view

export function extendComponents (container, components) {
    map(components).each((obj, key) => {
        container.set(key, createComponent(key, obj, protoComponent));
    });
    return container;
}

export function extendDirectives (container, directives) {
    map(directives).each((obj, key) => {
        container.set(key, createDirective(obj));
    });
    return container;
}

//
//  Finalise the binding between the view and the model
//  inject the model into the view element
//  call the mounted hook and can return a Promise
export function asView(vm, element, onMounted) {
    var model = vm.model;
    model.$$view = vm;
    model.$$name = vm.name;

    Object.defineProperty(sel(vm), 'el', {
        get: function () {
            return element;
        }
{'fraction_non_alphanumeric': 0.12377518225288077, 'fraction_numerical': 0.000862271693971937, 'mean_word_length': 1.6606882168925965, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '6235629', 'n_tokens_mistral': 3298, 'n_tokens_neox': 3164, 'n_words': 885}