<commit_before>import cloneDeep from 'lodash.clonedeep';

import Bridge   from './Bridge';
import joinName from '../helpers/joinName';

let SimpleSchema = (typeof global === 'object' ? global : window).SimpleSchema;
let Match        = (typeof global === 'object' ? global : window).Match;

try {
    if (Match === undefined) {
        Match = require('meteor/check').Match;
    }

    if (SimpleSchema === undefined) {
        SimpleSchema = require('meteor/aldeed:simple-schema').SimpleSchema;
    }

    SimpleSchema.extendOptions({
        uniforms: Match.Optional(
            Match.OneOf(
                String,
                Function,
                Match.ObjectIncluding({
                    component: Match.Optional(
                        Match.OneOf(
                            String,
                            Function
                        )
                    )
                })
            )
        )
    });
} catch (_) {
    // eslint-disable-line
}

export default class SimpleSchemaBridge extends Bridge {
    static check (schema) {
        return SimpleSchema && (
            schema &&
            schema.getDefinition &&
            schema.messageForError &&
            schema.objectKeys &&
            schema.validator
        );
    }

    getError (name, error) {
        return (
            error &&
            error.details &&
            error.details.find &&
            error.details.find(error => error.name === name)
        );
    }

    getErrorMessage (name, error) {
        let scopedError = this.getError(name, error);
        if (scopedError) {
            return this.schema.messageForError(
                scopedError.type,
                scopedError.name,
                null,
                scopedError.details &&
                scopedError.details.value
            );
        }

        return '';
    }

    getErrorMessages (error) {
        return (error && error.details || []).map(error =>
            this.schema.messageForError(
                error.type,
                error.name,
                null,
                error.details &&
                error.details.value
            )
        );
    }

    getField (name) {
        let definition = this.schema.getDefinition(name);
        if (definition) {
            return definition;
        }

        throw new Error(`Field not found in schema: '${name}'`);
    }

    getInitialValue (name, props = {}) {
        const field = this.getField(name);

        if (field.type === Array) {
            const item = this.getInitialValue(joinName(name, '0'));
            const items = Math.max(
                props.initialCount || 0,
                field.minCount     || 0
            );

            return [...Array(items)].map(() => item);
        }

        return field.defaultValue
            ? field.defaultValue
            : field.allowedValues
                ? field.allowedValues[0]
                : field.type === Date || field.type === Number
                    ? field.min !== undefined
                        ? field.min
                        : field.max !== undefined
                            ? field.max
                            : field.type === Number
                                ? 0
                                : new Date()
                    : field.type();
    }

    getProps (name, props) {
        // Type should be omitted.
        // eslint-disable-next-line no-unused-vars
        let {optional, type, uniforms, ...field} = this.getField(name);

        field = {...field, required: !optional};

        if (uniforms) {
            if (typeof uniforms === 'string' ||
                typeof uniforms === 'function') {
                field = {...field, component: uniforms};
            } else {
                field = {...field, ...uniforms};
            }
        }

        let options = props.options || field.options;
        if (options) {
            if (typeof options === 'function') {
                options = options();
            }
<commit_msg>Bubble allowedValues and transforms up to Array field.<commit_after>import cloneDeep from 'lodash.clonedeep';

import Bridge   from './Bridge';
import joinName from '../helpers/joinName';

let SimpleSchema = (typeof global === 'object' ? global : window).SimpleSchema;
let Match        = (typeof global === 'object' ? global : window).Match;

try {
    if (Match === undefined) {
        Match = require('meteor/check').Match;
    }

    if (SimpleSchema === undefined) {
        SimpleSchema = require('meteor/aldeed:simple-schema').SimpleSchema;
    }

    SimpleSchema.extendOptions({
        uniforms: Match.Optional(
            Match.OneOf(
                String,
                Function,
                Match.ObjectIncluding({
                    component: Match.Optional(
                        Match.OneOf(
                            String,
                            Function
                        )
                    )
                })
            )
        )
    });
} catch (_) {
    // eslint-disable-line
}

export default class SimpleSchemaBridge extends Bridge {
    static check (schema) {
        return SimpleSchema && (
            schema &&
            schema.getDefinition &&
            schema.messageForError &&
            schema.objectKeys &&
            schema.validator
        );
    }

    getError (name, error) {
        return (
            error &&
            error.details &&
            error.details.find &&
            error.details.find(error => error.name === name)
        );
    }

    getErrorMessage (name, error) {
        let scopedError = this.getError(name, error);
        if (scopedError) {
            return this.schema.messageForError(
                scopedError.type,
                scopedError.name,
                null,
                scopedError.details &&
                scopedError.details.value
            );
        }

        return '';
    }

    getErrorMessages (error) {
        return (error && error.details || []).map(error =>
            this.schema.messageForError(
                error.type,
                error.name,
                null,
                error.details &&
                error.details.value
            )
        );
    }

    getField (name) {
        let definition = this.schema.getDefinition(name);
        if (definition) {
            return definition;
        }

        throw new Error(`Field not found in schema: '${name}'`);
    }

    getInitialValue (name, props = {}) {
        const field = this.getField(name);

        if (field.type === Array) {
            const item = this.getInitialValue(joinName(name, '0'));
            const items = Math.max(
                props.initialCount || 0,
                field.minCount     || 0
            );

            return [...Array(items)].map(() => item);
        }

        return field.defaultValue
            ? field.defaultValue
            : field.allowedValues
                ? field.allowedValues[0]
                : field.type === Date || field.type === Number
                    ? field.min !== undefined
                        ? field.min
                        : field.max !== undefined
                            ? field.max
                            : field.type === Number
                                ? 0
                                : new Date()
                    : field.type();
    }

    getProps (name, props) {
        // Type should be omitted.
        // eslint-disable-next-line no-unused-vars
        let {optional, type, uniforms, ...field} = this.getField(name);

        field = {...field, required: !optional};

        if (uniforms) {
            if (typeof uniforms === 'string' ||
                typeof uniforms === 'function') {
                field = {...field, component: uniforms};
            } else {
                field = {...field, ...uniforms};
            }
        }

        if (type === Array) {
            try {
                let itemProps = this.getProps(`${name}.$`, props);
                if (itemProps.allowedValues && !props.allowedValues) {
                    field.allowedValues = itemProps.allowedValues;
                }

                if (itemProps.transform && !props.transform) {
                    field.transform = itemProps.transform;
                }
            } catch (e) { /* do nothing */ }
        }

        let options = props.options || field.options;
        if (options) {
            if (typeof options === 'function') {
                options = options();
            }

{'fraction_non_alphanumeric': 0.11382397395651668, 'fraction_numerical': 0.001162655505173817, 'mean_word_length': 1.2244634083268684, 'pattern_counts': {'":': 0, '<': 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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '130273', 'n_tokens_mistral': 2183, 'n_tokens_neox': 2055, 'n_words': 459}