orm/src/filter/Focus.js

/**
 * @module flitter-orm/src/filter/Focus
 */

/**
 * A filter, focused on a particular field.
 * @class
 */
class Focus {
    constructor(parent_filter, field) {
        /**
         * The parent filter.
         * @private
         * @type {module:flitter-orm/src/filter/Filter~Filter}
         */
        this._parent = parent_filter

        /**
         * The field to focus on.
         * @type {string}
         * @private
         */
        this._field = field

        /**
         * The root filter.
         * @type {object}
         * @private
         */
        this._root = {}

        /**
         * Are we notting right now? Default false.
         * @type {boolean}
         * @private
         */
        this._notting = false
    }

    /**
     * Assert that the field must be equal to the value.
     * (Or if were in notting mode, the opposite.)
     * @param {*} value
     * @returns {module:flitter-orm/src/filter/Focus~Focus}
     */
    equal(value) {
        if ( this._notting ) this._root.$ne = value
        else this._root.$eq = value
        return this
    }

    /**
     * Assert that the field must be less than the value.
     * (Or if were in notting mode, the opposite.)
     * @param {*} value
     * @returns {module:flitter-orm/src/filter/Focus~Focus}
     */
    less_than(value) {
        if ( this._notting ) this._root.$gte = value
        else this._root.$lt = value
        return this
    }

    /**
     * Assert that the field must be greater than the value.
     * (Or if were in notting mode, the opposite.)
     * @param {*} value
     * @returns {module:flitter-orm/src/filter/Focus~Focus}
     */
    greater_than(value) {
        if ( this._notting ) this._root.$lte = value
        else this._root.$gt = value
        return this
    }

    /**
     * Assert that the field must be less than or equal to the value.
     * (Or if were in notting mode, the opposite.)
     * @param {*} value
     * @returns {module:flitter-orm/src/filter/Focus~Focus}
     */
    less_than_equal(value) {
        if ( this._notting ) this._root.$gt = value
        else this._root.$lte = value
        return this
    }

    /**
     * Assert that the field must be greater than or equal to the value.
     * (Or if were in notting mode, the opposite.)
     * @param {*} value
     * @returns {module:flitter-orm/src/filter/Focus~Focus}
     */
    greater_than_equal(value) {
        if ( this._notting ) this._root.$lt = value
        else this._root.$gte = value
        return this
    }

    /**
     * Assert that the field must be in an array of possible values.
     * (Or if were in notting mode, the opposite.)
     * @param {*} value
     * @returns {module:flitter-orm/src/filter/Focus~Focus}
     */
    in(value_array) {
        if ( typeof value_array === 'function' ) value_array = value_array()
        if ( this._notting ) this._root.$nin = value_array.flat()
        else this._root.$in = value_array.flat()
        return this
    }

    /**
     * Enable notting mode.
     * @returns {module:flitter-orm/src/filter/Focus~Focus}
     */
    not() {
        this._notting = true
        return this
    }

    /**
     * If notting, end notting mode. Otherwise, unfocus back to the parent filter.
     * @returns {module:flitter-orm/src/filter/Filter~Filter|module:flitter-orm/src/filter/Focus~Focus}
     */
    end() {
        if ( this._notting ) {
            this._notting = false
            return this
        } else {
            return this._parent._unfocus(this)
        }
    }
}

module.exports = exports = Focus