all files / dist/ realm-query.js

98.51% Statements 132/134
89.47% Branches 34/38
97.37% Functions 37/38
98.51% Lines 132/134
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191  52× 52× 52×   47×       38×   47×   45× 45× 40× 37× 36×           45×   45×   13× 13× 13×   13×                                     14× 14×   14×                                           13×   39×      
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var _ = require("lodash");
var RealmQuery = (function () {
    function RealmQuery(objects) {
        this.criteria = [];
        this.inGroup = false;
        this.objects = objects;
    }
    RealmQuery.prototype.addCriteria = function (critera) {
        if (this.inGroup) {
            var lastIndex = this.criteria.length - 1;
            var currentGroup = this.criteria[lastIndex];
            if (!currentGroup || !_.isArray(currentGroup)) {
                this.criteria.push([]);
                lastIndex += 1;
            }
            this.criteria[lastIndex].push(critera);
        }
        else {
            this.criteria.push(critera);
        }
        return this;
    };
    RealmQuery.prototype.toString = function () {
        var criteraStr = [];
        for (var i in this.criteria) {
            if (_.isString(this.criteria[i])) {
                if (this.criteria[i] !== 'NOT') {
                    criteraStr.push(this.criteria[i]);
                }
            }
            else {
                criteraStr.push('(' + this.criteria[i].join(' ') + ')');
            }
        }
        if (this.criteria.indexOf('NOT') > -1) {
            criteraStr = ["NOT(" + criteraStr.join(' AND ') + ")"];
        }
        return criteraStr.join(' AND ');
    };
    RealmQuery.prototype.getFilteredObjects = function () {
        var query = this.toString();
        var results = this.objects;
        if (query) {
            results = results.filtered(query);
        }
        return results;
    };
    RealmQuery.prototype.average = function (fieldName) {
        var results = this.getFilteredObjects();
        return _.sumBy(_.toArray(results), fieldName) / results.length;
    };
    RealmQuery.prototype.beginGroup = function () {
        this.inGroup = true;
        return this;
    };
    RealmQuery.prototype.beginsWith = function (fieldName, value, casing) {
        var op = casing ? 'BEGINSWITH[c]' : 'BEGINSWITH';
        Eif (_.isString(value))
            value = "\"" + value + "\"";
        return this.addCriteria(fieldName + " " + op + " " + value);
    };
    RealmQuery.prototype.between = function (fieldName, from, to) {
        return this.addCriteria(fieldName + " >= " + from + " AND " + fieldName + " <= " + to);
    };
    RealmQuery.prototype.contains = function (fieldName, value, casing) {
        var op = casing ? 'CONTAINS[c]' : 'CONTAINS';
        Eif (_.isString(value))
            value = "\"" + value + "\"";
        return this.addCriteria(fieldName + " " + op + " " + value);
    };
    RealmQuery.prototype.count = function () {
        var results = this.getFilteredObjects();
        return results.length;
    };
    RealmQuery.prototype.distinct = function (fieldName) {
        var results = this.getFilteredObjects();
        return _.uniqBy(_.toArray(results), fieldName);
    };
    RealmQuery.prototype.endGroup = function () {
        this.inGroup = false;
        return this;
    };
    RealmQuery.prototype.endsWith = function (fieldName, value, casing) {
        var op = casing ? 'ENDSWITH[c]' : 'ENDSWITH';
        Eif (_.isString(value))
            value = "\"" + value + "\"";
        return this.addCriteria(fieldName + " " + op + " " + value);
    };
    RealmQuery.prototype.equalTo = function (fieldName, value) {
        if (_.isString(value))
            value = "\"" + value + "\"";
        return this.addCriteria(fieldName + " == " + value);
    };
    RealmQuery.prototype.findAll = function () {
        var results = this.getFilteredObjects();
        if (this.sortField) {
            results = results.sorted(this.sortField, this.sortReverse);
        }
        return results;
    };
    RealmQuery.prototype.findFirst = function () {
        var results = this.getFilteredObjects();
        return results.length ? results[0] : undefined;
    };
    RealmQuery.prototype.greaterThan = function (fieldName, value) {
        return this.addCriteria(fieldName + " > " + value);
    };
    RealmQuery.prototype.greaterThanOrEqualTo = function (fieldName, value) {
        return this.addCriteria(fieldName + " >= " + value);
    };
    RealmQuery.prototype.in = function (fieldName, values) {
        var criteria = [];
        for (var i in values) {
            var value = values[i];
            if (_.isString(value)) {
                value = "\"" + value + "\"";
            }
            criteria.push(fieldName + " == " + value);
        }
        return this.addCriteria('(' + criteria.join(' OR ') + ')');
    };
    RealmQuery.prototype.isEmpty = function (fieldName) {
        throw new Error('Not yet supported "isEmpty"');
    };
    RealmQuery.prototype.isNotEmpty = function (fieldName) {
        throw new Error('Not yet supported "isNotEmpty"');
    };
    RealmQuery.prototype.isNotNull = function (fieldName) {
        throw new Error('Not yet supported "isNotNull"');
    };
    RealmQuery.prototype.isNull = function (fieldName) {
        throw new Error('Not yet supported "isNull"');
    };
    RealmQuery.prototype.lessThan = function (fieldName, value) {
        return this.addCriteria(fieldName + " < " + value);
    };
    RealmQuery.prototype.lessThanOrEqualTo = function (fieldName, value) {
        return this.addCriteria(fieldName + " <= " + value);
    };
    RealmQuery.prototype.like = function (fieldName, value) {
        throw new Error('Not yet supported "like"');
    };
    RealmQuery.prototype.max = function (fieldName) {
        var results = this.getFilteredObjects();
        return _.maxBy(_.toArray(results), fieldName);
    };
    RealmQuery.prototype.min = function (fieldName) {
        var results = this.getFilteredObjects();
        return _.minBy(_.toArray(results), fieldName);
    };
    RealmQuery.prototype.not = function () {
        return this.addCriteria('NOT');
    };
    RealmQuery.prototype.notEqualTo = function (fieldName, value) {
        if (_.isString(value))
            value = "\"" + value + "\"";
        return this.addCriteria(fieldName + " <> " + value);
    };
    RealmQuery.prototype.and = function () {
        return this.addCriteria('AND');
    };
    RealmQuery.prototype.or = function () {
        return this.addCriteria('OR');
    };
    RealmQuery.prototype.sum = function (fieldName) {
        var results = this.getFilteredObjects();
        return _.sumBy(_.toArray(results), fieldName);
    };
    RealmQuery.prototype.sort = function (fieldName, order) {
        this.sortField = fieldName;
        this.sortReverse = order === 'DESC' ? true : false;
        return this;
    };
    /**
     * Combine to another query
     */
    RealmQuery.prototype.join = function (query) {
        return this.addCriteria(query.toString());
    };
    RealmQuery.where = function (objects) {
        return new RealmQuery(objects);
    };
    RealmQuery.create = function (objects) {
        return new RealmQuery(objects);
    };
    return RealmQuery;
}());
exports.default = RealmQuery;
//# sourceMappingURL=realm-query.js.map