query: function query()

in lib/util-browser.js [259:308]


        query: function query(expression, data) {
            if (!data) return [];

            var results = [];
            var expressions = expression.split(/\s+or\s+/);
            TableStore.util.arrayEach.call(this, expressions, function (expr) {
                var objects = [data];
                var tokens = expr.split('.');
                TableStore.util.arrayEach.call(this, tokens, function (token) {
                    var match = token.match('^(.+?)(?:\\[(-?\\d+|\\*)\\])?$');
                    var newObjects = [];
                    TableStore.util.arrayEach.call(this, objects, function (obj) {
                        if (match[1] === '*') {
                            TableStore.util.arrayEach.call(this, obj, function (value) {
                                newObjects.push(value);
                            });
                        } else if (obj.hasOwnProperty(match[1])) {
                            newObjects.push(obj[match[1]]);
                        }
                    });
                    objects = newObjects;

                    // handle indexing (token[0], token[-1])
                    if (match[2]) {
                        newObjects = [];
                        TableStore.util.arrayEach.call(this, objects, function (obj) {
                            if (Array.isArray(obj)) {
                                if (match[2] === '*') {
                                    newObjects = newObjects.concat(obj);
                                } else {
                                    var idx = parseInt(match[2], 10);
                                    if (idx < 0) idx = obj.length + idx; // negative indexing
                                    newObjects.push(obj[idx]);
                                }
                            }
                        });
                        objects = newObjects;
                    }

                    if (objects.length === 0) return TableStore.util.abort;
                });

                if (objects.length > 0) {
                    results = objects;
                    return TableStore.util.abort;
                }
            });

            return results;
        },