<commit_before>/**
 * @file Check define dictionary
 */
/*#ifndef(UMD)*/
/*exported _gpfDefineAllowedCommon$Keys*/ // Common list of allowed $ keys
/*exported _gpfDefineCheckDefinition*/ // Check the dictionary passed to gpf.define
/*exported _gpfDefineTypedCheckers*/ // Dictionary of typed definition checker
/*global _gpfErrorDeclare*/ // Declare new gpf.Error names
"use strict";
/*#endif*/

_gpfErrorDeclare("define/checkDefinition", {
    /**
     * ### Summary
     *
     * Entity type is invalid in the definition passed to {@link gpf.define}
     *
     * ### Description
     *
     * This error is thrown when the entity type is either missing or invalid
     */
    invalidEntityType: "Invalid entity type"

});

/**
 * Common list of allowed $ keys
 *
 * @type {String[]}
 */
var _gpfDefineAllowedCommon$Keys = "type,name,namespace".split(",").map(function (name) {
    return "$" + name;
});

//region Key transformations

/**
 * Dictionary of key transformations:
 * - $class => $type="class", $name=value (overwrite)
 * - $name => $name if none specified
 */
var _gpfDefineKeyTransformations = {

    "$class": function (value, definition, transformed) {
        transformed.$type = "class";
        transformed.$name = value; // Overwrite even if existing
    },

    "$name": function (value, definition, transformed) {
        if (!transformed.$name) { // Ignore if existing
            transformed.$name = value;
        }
    }

};

/**
 * Apply key transformations
 *
 * @param {Object} definition Entity definition given to gpf.define
 * @return {Object} Definition (not the same object) where key transformation were applied
 * @see _gpfDefineKeyTransformations
 */
function _gpfDefineApplyKeyTransformations (definition) {
    var transformed = {};
    Object.keys(definition).forEach(function (key) {
        var transform = _gpfDefineKeyTransformations[key],
            value = definition[key];
        if (transform) {
            transform(value, definition, transformed);
        } else {
            transformed[key] = value;
        }
    });
    return transformed;
}

function _gpfDefineConvertNamespacedName (transformed, name) {
    var relNamespace = name.split("."),
        namespace;
    transformed.$name = relNamespace.pop();
    namespace = transformed.$namespace;
    if (namespace) {
        relNamespace.unshift(namespace);
    }
    transformed.$namespace = relNamespace.join(".");
}

/**
 * If $name looks like a namespace (contains .), append to or define in $namespace
 * @param {Object} transformed Transformed definition where key transformation were applied
 */
function _gpfDefineProcessNameAndNamespace (transformed) {
    var name = transformed.$name || "";
    if (name.indexOf(".") > -1) {
        _gpfDefineConvertNamespacedName(transformed, name);
    }
}

/**<commit_msg>Adds namespace validation<commit_after>/**
 * @file Check define dictionary
 */
/*#ifndef(UMD)*/
/*exported _gpfDefineAllowedCommon$Keys*/ // Common list of allowed $ keys
/*exported _gpfDefineCheckDefinition*/ // Check the dictionary passed to gpf.define
/*exported _gpfDefineTypedCheckers*/ // Dictionary of typed definition checker
/*global _gpfErrorDeclare*/ // Declare new gpf.Error names
"use strict";
/*#endif*/

_gpfErrorDeclare("define/checkDefinition", {
    /**
     * ### Summary
     *
     * Entity type is invalid in the definition passed to {@link gpf.define}
     *
     * ### Description
     *
     * This error is thrown when the entity type is either missing or invalid
     */
    invalidEntityType: "Invalid entity type",

    /**
     * ### Summary
     *
     * Entity namespace is invalid in the definition passed to {@link gpf.define}
     *
     * ### Description
     *
     * This error is thrown when the namespace is invalid
     */
    invalidEntityNamespace: "Invalid entity namespace"
});

/**
 * Common list of allowed $ keys
 *
 * @type {String[]}
 */
var _gpfDefineAllowedCommon$Keys = "type,name,namespace".split(",").map(function (name) {
    return "$" + name;
});

//region Key transformations

/**
 * Dictionary of key transformations:
 * - $class => $type="class", $name=value (overwrite)
 * - $name => $name if none specified
 */
var _gpfDefineKeyTransformations = {

    "$class": function (value, definition, transformed) {
        transformed.$type = "class";
        transformed.$name = value; // Overwrite even if existing
    },

    "$name": function (value, definition, transformed) {
        if (!transformed.$name) { // Ignore if existing
            transformed.$name = value;
        }
    }

};

/**
 * Apply key transformations
 *
 * @param {Object} definition Entity definition given to gpf.define
 * @return {Object} Definition (not the same object) where key transformation were applied
 * @see _gpfDefineKeyTransformations
 */
function _gpfDefineApplyKeyTransformations (definition) {
    var transformed = {};
    Object.keys(definition).forEach(function (key) {
        var transform = _gpfDefineKeyTransformations[key],
            value = definition[key];
        if (transform) {
            transform(value, definition, transformed);
        } else {
            transformed[key] = value;
        }
    });
    return transformed;
}

function _gpfDefineConvertNamespacedName (transformed, name) {
    var relNamespace = name.split("."),
        namespace;
    transformed.$name = relNamespace.pop();
    namespace = transformed.$namespace;
    if (namespace) {
        relNamespace.unshift(namespace);
    }
    transformed.$namespace = relNamespace.join(".");
}

var _gpfNamespaceValidationRegExp = /^[a-z_$][a-zA-Z0-9]+(:?\.[a-z_$][a-zA-Z0-9]+)*$/;

function _gpfDefineCheckNamespace (transformed) {
    var namespace = transformed.$namespace;
    _gpfNamespaceValidationRegExp.lastIndex = 0;
    if (namespace && !_gpfNamespaceValidationRegExp.exec(namespace)) {
        gpf.Error.invalidEntityNamespace();
    }
}

/**
 * If $name looks like a namespace (contains .), append to or define in $namespace
 * @param {Object} transformed Transformed definition where key transformation were applied
 */
function _gpfDefineProcessNameAndNamespace (transformed) {
    var name = transformed.$name || "";
    if (name.indexOf(".") > -1) {
        _gpfDefineConvertNamespacedName(transformed, name);
    }
    _gpfDefineCheckNamespace(transformed);
}

/**
{'fraction_non_alphanumeric': 0.12655438375570596, 'fraction_numerical': 0.0011018416496143555, 'mean_word_length': 3.2191235059760954, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 9, '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': '3284026', 'n_tokens_mistral': 1774, 'n_tokens_neox': 1738, 'n_words': 488}