<commit_before>/*
 * #%L
 * %%
 * Copyright (C) 2011 - 2015 BMW Car IT GmbH
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

define(
        "joynr/util/Typing",
        [
            "joynr",
            "joynr/types/TypeRegistrySingleton"
        ],
        function(joynr, TypeRegistrySingleton) {

            /**
             * @name Typing
             * @class
             */
            var Typing = {};

            Typing.checkProperty =
                    function(obj, type, description) {
                        if (obj === undefined) {
                            throw new Error(description + " is undefined");
                        }
                        if (typeof type === "string" && Typing.getObjectType(obj) !== type) {
                            throw new Error(description
                                + " is not of type "
                                + type
                                + ". Actual type is "
                                + Typing.getObjectType(obj));
                        }
                        if (Object.prototype.toString.call(type) === "[object Array]"
                            && !Typing.getObjectType(obj).match("^" + type.join("$|^") + "$")) {
                            throw new Error(description
                                + " is not of a type from "
                                + type
                                + ". Actual type is "
                                + Typing.getObjectType(obj));
                        }
                        if (typeof type === "function" && !(obj instanceof type)) {
                            throw new Error(description
                                + " is not of type "
                                + Typing.getObjectType(type)
                                + ". Actual type is "
                                + Typing.getObjectType(obj));
                        }
                    };

            /**
             * Checks if the variable is of specified type
             * @function Typing#checkPropertyIfDefined
             *
             * @param {?}
             *            obj the object
             * @param {String|Function}
             *            type a string representation of the the data type (e.g. "Boolean", "Number",
             *             "String", "Array", "Object", "Function"
             *            "MyCustomType", "Object|MyCustomType") or a constructor function to check against
             *             using instanceof (e.g. obj
             *            instanceof type)
             * @param {String}
             *            description a description for the thrown error
             * @throws an
             *             error if the object not of the given type
             */
            Typing.checkPropertyIfDefined = function(obj, type, description) {
                if (obj !== undefined && obj !== null) {
                    Typing.checkProperty(obj, type, description);
                }
            };

            /**
             * @function Typing#getObjectType
             * @param {Boolean|Number|String|Object}
             *            obj the object to determine the type of
             * @returns {String} the object type
             */
            Typing.getObjectType = function(obj) {
                if (obj === null || obj === undefined) {
                    throw new Error("cannot determine the type of an undefined object");
                }
                var results = /function ([$\w]+)\(/.exec(obj.constructor.toString());
                return (results && results.length > 1) ? results[1] : "";
            };

            /**
             * Recursively deep copies a given object and augments type information on the way if a
             * constructor is registered in the typeRegistry for the value of the object's
             * member "_typeName"
             *
             * @function Typing#augmentTypes
             * @param {Boolean|Number|String|Array|Object}
             *            untyped the untyped object
             * @param {TypeRegistry}
             *            typeRegistry the typeRegistry to retrieve type information from
             * @param {String}
             *            typeHint optional parameter which provides the type informed of the untyped object.
             *            This is used i.e. for enums, where the type information is not included in the untyped object itself 
             * @returns a deep copy of the untyped object with the types being augmented
             * @throws {Error}<commit_msg>[JS] do not use regexp to determine name of constructor<commit_after>/*
 * #%L
 * %%
 * Copyright (C) 2011 - 2015 BMW Car IT GmbH
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

define(
        "joynr/util/Typing",
        [
            "joynr",
            "joynr/types/TypeRegistrySingleton"
        ],
        function(joynr, TypeRegistrySingleton) {

            /**
             * @name Typing
             * @class
             */
            var Typing = {};

            Typing.checkProperty =
                    function(obj, type, description) {
                        if (obj === undefined) {
                            throw new Error(description + " is undefined");
                        }
                        if (typeof type === "string" && Typing.getObjectType(obj) !== type) {
                            throw new Error(description
                                + " is not of type "
                                + type
                                + ". Actual type is "
                                + Typing.getObjectType(obj));
                        }
                        if (Object.prototype.toString.call(type) === "[object Array]"
                            && !Typing.getObjectType(obj).match("^" + type.join("$|^") + "$")) {
                            throw new Error(description
                                + " is not of a type from "
                                + type
                                + ". Actual type is "
                                + Typing.getObjectType(obj));
                        }
                        if (typeof type === "function" && !(obj instanceof type)) {
                            throw new Error(description
                                + " is not of type "
                                + Typing.getObjectType(type)
                                + ". Actual type is "
                                + Typing.getObjectType(obj));
                        }
                    };

            /**
             * Checks if the variable is of specified type
             * @function Typing#checkPropertyIfDefined
             *
             * @param {?}
             *            obj the object
             * @param {String|Function}
             *            type a string representation of the the data type (e.g. "Boolean", "Number",
             *             "String", "Array", "Object", "Function"
             *            "MyCustomType", "Object|MyCustomType") or a constructor function to check against
             *             using instanceof (e.g. obj
             *            instanceof type)
             * @param {String}
             *            description a description for the thrown error
             * @throws an
             *             error if the object not of the given type
             */
            Typing.checkPropertyIfDefined = function(obj, type, description) {
                if (obj !== undefined && obj !== null) {
                    Typing.checkProperty(obj, type, description);
                }
            };

            /**
             * @function Typing#getObjectType
             * @param {Boolean|Number|String|Object}
             *            obj the object to determine the type of
             * @returns {String} the object type
             */
            Typing.getObjectType = function(obj) {
                if (obj === null || obj === undefined) {
                    throw new Error("cannot determine the type of an undefined object");
                }
                return obj.constructor.name || "";
            };

            /**
             * Recursively deep copies a given object and augments type information on the way if a
             * constructor is registered in the typeRegistry for the value of the object's
             * member "_typeName"
             *
             * @function Typing#augmentTypes
             * @param {Boolean|Number|String|Array|Object}
             *            untyped the untyped object
             * @param {TypeRegistry}
             *            typeRegistry the typeRegistry to retrieve type information from
             * @param {String}
             *            typeHint optional parameter which provides the type informed of the untyped object.
             *            This is used i.e. for enums, where the type information is not included in the untyped object itself 
             * @returns a deep copy of the untyped object with the types being augmented
             * @throws {Error}
{'fraction_non_alphanumeric': 0.08511056511056511, 'fraction_numerical': 0.0025552825552825555, 'mean_word_length': 1.2040285899935024, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3628771', 'n_tokens_mistral': 2323, 'n_tokens_neox': 2211, 'n_words': 771}