<commit_before>/// <reference path="_references.ts"/>

module Orange.Modularity 
{
    export interface KeyValuePair { key: any; value: any; }

    export class Container {
        private typeMap: Array<KeyValuePair> = [];
        private instances: Array<KeyValuePair> = [];

        constructor () {
            this.registerInstance(Container, this);
        }

        registerInstance(type: any, instance: any): void {
            this.instances.push({ key: type, value: instance });
        }

        registerType(type: any, instance: any): void {
            this.typeMap.push({ key: type, value: instance });
        }

        resolve(type: any | string, register: boolean = false) {
            
            if (typeof type === "string") 
                type = Container.getConstructorFromString(type);

            var instance: any = this.lookup(this.instances, type);
            
            if (instance != null)
                return instance;

            var resolvedType = this.lookup(this.typeMap, type) || type;

            Container.validateConstructor(resolvedType);
            this.checkArity(resolvedType);

            instance = this.createInstance(resolvedType);

            if (register === true)
                this.registerInstance(type, instance);

            return instance;
        }

        resolveWithOverride(type: any, overrides: Array<KeyValuePair>) {
            var sub = new Container();
            sub.typeMap = this.typeMap;
            sub.instances = overrides.concat(this.instances);
            return sub.resolve(type, false);
        }

        private static getConstructorFromString(constructorName: string) : any {

            var path = constructorName.split(".");

            // Try to construct a valid constructorfunction based of window.
            var func: any = window;
            for (var fragment of path) {

                if (func[fragment] == null)
                    break;

                func = func[fragment];
            }

            if (Container.isValidConstructor(func))
                return func;

            // If the constructor was now found on window, try to require it.
            // NOTE: This is done to support browserify modules.
            func = (<any>window).require(constructorName);

            if (Container.isValidConstructor(func))
                return func;

            throw new ReferenceError(`No constructor identified by "${constructorName}" could be found`);<commit_msg>Another bugfix for container<commit_after>/// <reference path="_references.ts"/>

module Orange.Modularity 
{
    export interface KeyValuePair { key: any; value: any; }

    export class Container {
        private typeMap: Array<KeyValuePair> = [];
        private instances: Array<KeyValuePair> = [];

        constructor () {
            this.registerInstance(Container, this);
        }

        registerInstance(type: any, instance: any): void {
            this.instances.push({ key: type, value: instance });
        }

        registerType(type: any, instance: any): void {
            this.typeMap.push({ key: type, value: instance });
        }

        resolve(type: any | string, register: boolean = false) {
            
            if (typeof type === "string") 
                type = Container.getConstructorFromString(type);

            var instance: any = this.lookup(this.instances, type);
            
            if (instance != null)
                return instance;

            var resolvedType = this.lookup(this.typeMap, type) || type;

            Container.validateConstructor(resolvedType);
            this.checkArity(resolvedType);

            instance = this.createInstance(resolvedType);

            if (register === true)
                this.registerInstance(type, instance);

            return instance;
        }

        resolveWithOverride(type: any, overrides: Array<KeyValuePair>) {
            var sub = new Container();
            sub.typeMap = this.typeMap;
            sub.instances = overrides.concat(this.instances);
            return sub.resolve(type, false);
        }

        private static getConstructorFromString(constructorName: string) : any {

            var path = constructorName.split(".");

            // Try to construct a valid constructorfunction based of window.
            var func: any = window;
            for (var fragment of path) {

                if (func[fragment] == null)
                    break;

                func = func[fragment];
            }

            if (Container.isValidConstructor(func))
                return func;

            // If the constructor was now found on window, try to require it.
            // NOTE: This is done to support browserify modules.
            if ((<any>window).require != null)
                func = (<any>window).require(constructorName);

            if (Container.isValidConstructor(func))
                return func;

            throw new ReferenceError(`No constructor identified by "${constructorName}" could be found`);
{'fraction_non_alphanumeric': 0.10055314105096799, 'fraction_numerical': 0.0, 'mean_word_length': 1.8620689655172413, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, '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': '4849631', 'n_tokens_mistral': 1236, 'n_tokens_neox': 1173, 'n_words': 343}