<commit_before>/**
 * @copyright Copyright (c) 2019 <NAME> <<EMAIL>>
 */
'use strict';

const Base = require('./Validator');

module.exports = class FilterValidator extends Base {

    static async filterSplit (value, attr, model, validator) {
        return StringHelper.split(value, validator.separator);
    }

    static async filterTrim (value) {
        return typeof value === 'string' ? value.trim() : '';
    }

    constructor (config) {
        super({
            filter: null,
            skipOnEmpty: false,
            skipOnArray: false,
            separator: ',',
            ...config
        });
        this.prepareFilter();
    }

    prepareFilter () {
        let filter = this.filter;
        if (filter === null) {
            throw new Error('Filter property must be set');
        }
        if (typeof filter === 'string') {
            filter = `filter${StringHelper.toFirstUpperCase(filter)}`;
            filter = this.constructor[filter];
            if (typeof filter !== 'function') {
                throw new Error(`Inline filter not found: ${this.filter}`);
            }
        } else if (typeof filter !== 'function') {
            throw new Error('Filter must be function');
        }
        this.filter = filter;
    }

    async validateAttr (attr, model) {
        const value = model.get(attr);
        if (Array.isArray(value) && this.skipOnArray) {
            return;
        }
        if (typeof this.filter === 'function') {
            return this.executeFilter(value, attr, model);
        }
        if (value === null || value === undefined) {
            return;
        }
        if (typeof value[this.filter] !== 'function') {
            throw new Error(`Inline filter not found: ${this.filter} in ${value.constructor.name}`);
        }
        model.set(attr, await value[this.filter]());
    }

    async executeFilter (value, attr, model) {
        const result = await this.filter(...arguments, this);
        result instanceof Message
            ? this.addError(model, attr, result)
            : model.set(attr, result);
    }
};

const StringHelper = require('../helper/StringHelper');
const Message = require('../i18n/Message');<commit_msg>Refactor filter validator<commit_after>/**
 * @copyright Copyright (c) 2019 <NAME> <<EMAIL>>
 */
'use strict';

const Base = require('./Validator');

module.exports = class FilterValidator extends Base {

    static async filterSplit (value, attr, model, validator) {
        return StringHelper.split(value, validator.separator);
    }

    static async filterTrim (value) {
        return typeof value === 'string' ? value.trim() : '';
    }

    constructor (config) {
        super({
            method: null,
            skipOnEmpty: false,
            skipOnArray: false,
            separator: ',',
            ...config
        });
        this.prepareFilter();
    }

    prepareFilter () {
        let method = this.method;
        if (method === null) {
            throw new Error('Method property must be set');
        }
        if (typeof method === 'string') {
            method = `filter${StringHelper.toFirstUpperCase(method)}`;
            method = this.constructor[method];
            if (typeof method !== 'function') {
                throw new Error(`Filter not found: ${this.method}`);
            }
        } else if (typeof method !== 'function') {
            throw new Error('Method must be string or function');
        }
        this.method = method;
    }

    async validateAttr (attr, model) {
        const value = model.get(attr);
        if (Array.isArray(value) && this.skipOnArray) {
            return;
        }
        const result = await this.method(value, attr, model, this);
        result instanceof Message
            ? this.addError(model, attr, result)
            : model.set(attr, result);
    }
};

const StringHelper = require('../helper/StringHelper');
const Message = require('../i18n/Message');
{'fraction_non_alphanumeric': 0.1351010101010101, 'fraction_numerical': 0.0030303030303030303, 'mean_word_length': 2.0236641221374048, 'pattern_counts': {'":': 0, '<': 9, '<?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': '2618807', 'n_tokens_mistral': 1054, 'n_tokens_neox': 1031, 'n_words': 287}