@nozbe/lokijs
Version:
Nozbe's fork of LokiJS, optimized for WatermelonDB
2 lines • 75.2 kB
JavaScript
!function(t,e){"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?module.exports=e():t.loki=e()}(this,function(){return function(){"use strict";function t(i){var r,n;if(Array.isArray(i)){for(n=0;n<i.length;n++)t(i[n]);e(i)}else if(null!==i&&"object"==typeof i){for(r in i)i.hasOwnProperty(r)&&t(i[r]);e(i)}}function e(t){Object.isFrozen(t)||Object.freeze(t)}function i(t){return Object.isFrozen(t)?u(t,"shallow"):t}function r(t,e){var i,r,n,s;if(t===e)return!0;if(!t||!e||!0===t||!0===e||t!==t||e!==e){switch(t){case void 0:case null:n=1;break;case!1:n=3;break;case!0:n=4;break;case"":n=5;break;default:n=t===t?9:0}switch(e){case void 0:case null:s=1;break;case!1:s=3;break;case!0:s=4;break;case"":s=5;break;default:s=e===e?9:0}if(9!==n||9!==s)return n===s}return i=Number(t),r=Number(e),i===i||r===r?i===r:(i=t.toString(),r=e.toString(),i==r)}function n(t,e,i){var r,n,s,o;if(!t||!e||!0===t||!0===e||t!==t||e!==e){switch(t){case void 0:case null:s=1;break;case!1:s=3;break;case!0:s=4;break;case"":s=5;break;default:s=t===t?9:0}switch(e){case void 0:case null:o=1;break;case!1:o=3;break;case!0:o=4;break;case"":o=5;break;default:o=e===e?9:0}if(9!==s||9!==o)return s===o?i:s<o}return r=Number(t),n=Number(e),r===r&&n===n?r<n||!(r>n)&&i:r===r&&n!==n||(n!==n||r===r)&&(t<e||!(t>e)&&(t==e?i:(r=t.toString(),n=e.toString(),r<n||r==n&&i)))}function s(t,e,i){var r,n,s,o;if(!t||!e||!0===t||!0===e||t!==t||e!==e){switch(t){case void 0:case null:s=1;break;case!1:s=3;break;case!0:s=4;break;case"":s=5;break;default:s=t===t?9:0}switch(e){case void 0:case null:o=1;break;case!1:o=3;break;case!0:o=4;break;case"":o=5;break;default:o=e===e?9:0}if(9!==s||9!==o)return s===o?i:s>o}return r=Number(t),n=Number(e),r===r&&n===n?r>n||!(r<n)&&i:(r!==r||n===n)&&(n===n&&r!==r||(t>e||!(t<e)&&(t==e?i:(r=t.toString(),n=e.toString(),r>n||r==n&&i))))}function o(t,e,i){return B.aeq(t,e)?0:B.lt(t,e,!1)?i?1:-1:B.gt(t,e,!1)?i?-1:1:0}function a(t,e,i){for(var r,n,s,a,l,h=0,c=0,u=t.length;c<u;c++)if(r=t[c],n=r[0],~n.indexOf(".")?(l=n.split("."),s=R.getIn(e,l,!0),a=R.getIn(i,l,!0)):(s=e[n],a=i[n]),0!==(h=o(s,a,r[1])))return h;return 0}function l(t,e,i,r,n,s){var o,a=s||0,h=e[a],c=!1;if("object"==typeof t&&h in t&&(o=t[h]),a+1>=e.length)c=i(o,r,n);else if(Array.isArray(o))for(var u=0,d=o.length;u<d&&!0!==(c=l(o[u],e,i,r,n,a+1));u+=1);else c=l(o,e,i,r,n,a+1);return c}function h(t){return"string"==typeof t||Array.isArray(t)?function(e){return-1!==t.indexOf(e)}:"object"==typeof t&&null!==t?function(e){return M.call(t,e)}:null}function c(t,e,i){for(var r in e)if(M.call(e,r))return T[r](t,e[r],i);return!1}function u(t,e){if(null===t||void 0===t)return null;var i,r=e||"parse-stringify";switch(r){case"parse-stringify":i=JSON.parse(JSON.stringify(t));break;case"jquery-extend-deep":i=jQuery.extend(!0,{},t);break;case"shallow":i=Object.create(t.constructor.prototype),Object.keys(t).map(function(e){i[e]=t[e]});break;case"shallow-assign":i=Object.create(t.constructor.prototype),Object.assign(i,t);break;case"shallow-recurse-objects":i=u(t,"shallow");Object.keys(t).forEach(function(e){"object"==typeof t[e]&&"Object"===t[e].constructor.name?i[e]=u(t[e],"shallow-recurse-objects"):Array.isArray(t[e])&&(i[e]=d(t[e],"shallow-recurse-objects"))})}return i}function d(t,e){if("parse-stringify"==e)return u(t,e);for(var i=[],r=0,n=t.length;r<n;r++)i[r]=u(t[r],e);return i}function p(){try{return window&&void 0!==window.localStorage&&null!==window.localStorage}catch(t){return!1}}function f(){}function y(t,e){this.filename=t||"loki.db",this.collections=[],this.databaseVersion=1.5,this.engineVersion=1.5,this.autosave=!1,this.autosaveInterval=5e3,this.autosaveHandle=null,this.throttledSaves=!0,this.options={},this.persistenceMethod=null,this.persistenceAdapter=null,this.throttledSavePending=!1,this.throttledCallbacks=[],this.verbose=!(!e||!e.hasOwnProperty("verbose"))&&e.verbose,this.events={init:[],loaded:[],flushChanges:[],close:[],changes:[],warning:[]};e&&e.hasOwnProperty("env")?this.ENV=e.env:this.ENV=function(){return"undefined"!=typeof global&&(global.android||global.NSObject)?"NATIVESCRIPT":"undefined"==typeof window?"NODEJS":"undefined"!=typeof global&&global.window&&"undefined"!=typeof process?"NODEJS":"undefined"!=typeof document?-1===document.URL.indexOf("http://")&&-1===document.URL.indexOf("https://")?"CORDOVA":"BROWSER":"CORDOVA"}(),"undefined"===this.ENV&&(this.ENV="NODEJS"),this.configureOptions(e,!0),this.on("init",this.clearChanges)}function v(t){this.hashStore={},this.options=t||{},this.options.hasOwnProperty("asyncResponses")||(this.options.asyncResponses=!1),this.options.hasOwnProperty("asyncTimeout")||(this.options.asyncTimeout=50)}function g(t,e){if(this.mode="reference",this.adapter=null,this.options=e||{},this.dbref=null,this.dbname="",this.pageIterator={},!t)throw new Error("LokiPartitioningAdapter requires a (non-reference mode) adapter on construction");if("reference"===t.mode)throw new Error("LokiPartitioningAdapter cannot be instantiated with a reference mode adapter");this.adapter=t,this.options.hasOwnProperty("paging")||(this.options.paging=!1),this.options.hasOwnProperty("pageSize")||(this.options.pageSize=26214400),this.options.hasOwnProperty("delimiter")||(this.options.delimiter="$<\n")}function m(){try{this.fs=require("fs")}catch(t){this.fs=null}}function b(){}function w(t,e){return e=e||{},this.collection=t,this.filteredrows=[],this.filterInitialized=!1,this}function I(t,e){if("$regex"===t)Array.isArray(e)?e=new RegExp(e[0],e[1]):e instanceof RegExp||(e=new RegExp(e));else if("object"==typeof e)for(var i in e)"$regex"!==i&&"object"!=typeof e[i]||(e[i]=I(i,e[i]));return e}function O(t,e,i){this.collection=t,this.name=e,this.rebuildPending=!1,this.options=i||{},this.options.hasOwnProperty("persistent")||(this.options.persistent=!1),this.options.hasOwnProperty("sortPriority")||(this.options.sortPriority="passive"),this.options.hasOwnProperty("minRebuildInterval")||(this.options.minRebuildInterval=1),this.resultset=new w(t),this.resultdata=[],this.resultsdirty=!1,this.cachedresultset=null,this.filterPipeline=[],this.collection.disableFreeze||Object.freeze(this.filterPipeline),this.sortFunction=null,this.sortCriteria=null,this.sortCriteriaSimple=null,this.sortDirty=!1,this.events={rebuild:[],filter:[],sort:[]}}function x(t,e){function i(t){var e="function"==typeof Set?new Set:[];e.add||(e.add=function(t){return-1===this.indexOf(t)&&this.push(t),this}),t.forEach(function(t){e.add(t.object)}),e.forEach(function(t){if(!M.call(t,"$loki"))return o.removeAutoUpdateObserver(t);try{o.update(t)}catch(t){}})}function r(t,e){return e?n(e,t):JSON.parse(JSON.stringify(t))}function n(t,e){var i=null!==e&&"object"==typeof e?Object.keys(e):null;if(i&&i.length&&["string","boolean","number"].indexOf(typeof e)<0){for(var r={},s=0;s<i.length;s++){var a=i[s];if(e.hasOwnProperty(a))if(!t.hasOwnProperty(a)||o.uniqueNames.indexOf(a)>=0||"$loki"==a||"meta"==a)r[a]=e[a];else{var l=n(t[a],e[a]);void 0!==l&&l!={}&&(r[a]=l)}}return 0===Object.keys(r).length?void 0:r}return t===e?void 0:e}function s(){o.changes=[]}this.name=t,this.data=[],this.idIndex=null,this.binaryIndices={},this.constraints={unique:{},exact:{}},this.uniqueNames=[],this.transforms={},this.objType=t,this.dirty=!0,this.cachedIndex=null,this.cachedBinaryIndex=null,this.cachedData=null;var o=this;e=e||{},e.hasOwnProperty("unique")&&(Array.isArray(e.unique)||(e.unique=[e.unique]),e.unique.forEach(function(t){o.uniqueNames.push(t)})),e.hasOwnProperty("exact")&&e.exact.forEach(function(t){o.constraints.exact[t]=new F(t)}),this.adaptiveBinaryIndices=!e.hasOwnProperty("adaptiveBinaryIndices")||e.adaptiveBinaryIndices,this.transactional=!!e.hasOwnProperty("transactional")&&e.transactional,this.cloneObjects=!!e.hasOwnProperty("clone")&&e.clone,this.cloneMethod=e.hasOwnProperty("cloneMethod")?e.cloneMethod:"parse-stringify",this.asyncListeners=!!e.hasOwnProperty("asyncListeners")&&e.asyncListeners,this.disableMeta=!!e.hasOwnProperty("disableMeta")&&e.disableMeta,this.disableChangesApi=!e.hasOwnProperty("disableChangesApi")||e.disableChangesApi,this.disableDeltaChangesApi=!e.hasOwnProperty("disableDeltaChangesApi")||e.disableDeltaChangesApi,this.disableChangesApi&&(this.disableDeltaChangesApi=!0),this.autoupdate=!!e.hasOwnProperty("autoupdate")&&e.autoupdate,this.serializableIndices=!e.hasOwnProperty("serializableIndices")||e.serializableIndices,this.disableFreeze=!e.hasOwnProperty("disableFreeze")||e.disableFreeze,this.ttl={age:null,ttlInterval:null,daemon:null},this.setTTL(e.ttl||-1,e.ttlInterval),this.maxId=0,this.DynamicViews=[],this.events={insert:[],update:[],"pre-insert":[],"pre-update":[],close:[],flushbuffer:[],error:[],delete:[],warning:[]},this.changes=[],this.dirtyIds=[];var a=[];if(e&&e.indices)if("[object Array]"===Object.prototype.toString.call(e.indices))a=e.indices;else{if("string"!=typeof e.indices)throw new TypeError("Indices needs to be a string or an array of strings");a=[e.indices]}for(var l=0;l<a.length;l++)this.ensureIndex(a[l]);this.observerCallback=i,this.getChangeDelta=r,this.getObjectDelta=n,this.getChanges=function(){return o.changes},this.flushChanges=s,this.setChangesApi=function(t){o.disableChangesApi=!t,t||(o.disableDeltaChangesApi=!1)},this.on("delete",function(t){o.disableChangesApi||o.createChange(o.name,"R",t)}),this.on("warning",function(t){o.lokiConsoleWrapper.warn(t)}),s()}function P(t){return-1!==t.indexOf(".")}function D(t){return parseFloat(t,10)}function S(t,e){return t+e}function k(t,e){return t-e}function C(t){return t.reduce(S,0)/t.length}function A(t){var e=C(t),i=t.map(function(t){var i=t-e;return i*i}),r=C(i);return Math.sqrt(r)}function $(t,e,i){if(!1===i)return t[e];for(var r=e.split("."),n=t;r.length>0;)n=n[r.shift()];return n}function j(t,e,i){for(var r,n,s=0,o=t.length;s<o;){if(n=s+o>>1,0===(r=i.apply(null,[e,t[n]])))return{found:!0,index:n};r<0?o=n:s=n+1}return{found:!1,index:o}}function z(t){return function(e,i){return j(e,i,t)}}function q(){}function E(t){this.field=t,this.keyMap=Object.create(null),this.lokiMap=Object.create(null)}function F(t){this.index=Object.create(null),this.field=t}function N(t){this.field=t}var M=Object.prototype.hasOwnProperty,R={copyProperties:function(t,e){var i;for(i in t)e[i]=t[i]},resolveTransformObject:function(t,e,i){var r,n;if("number"!=typeof i&&(i=0),++i>=10)return t;for(r in t)"string"==typeof t[r]&&0===t[r].indexOf("[%lktxp]")?(n=t[r].substring(8),e.hasOwnProperty(n)&&(t[r]=e[n])):"object"==typeof t[r]&&(t[r]=R.resolveTransformObject(t[r],e,i));return t},resolveTransformParams:function(t,e){var i,r,n=[];if(void 0===e)return t;for(i=0;i<t.length;i++)r=u(t[i],"shallow-recurse-objects"),n.push(R.resolveTransformObject(r,e));return n},getIn:function(t,e,i){if(null!=t){if(!i)return t[e];if("string"==typeof e&&(e=e.split(".")),!Array.isArray(e))throw new Error("path must be a string or array. Found "+typeof e);for(var r=0,n=e.length;null!=t&&r<n;)t=t[e[r++]];return r&&r==n?t:void 0}}},B={aeq:r,lt:n,gt:s},T={$eq:function(t,e){return t===e},$aeq:function(t,e){return t==e},$ne:function(t,e){return e!==e?t===t:t!==e},$dteq:function(t,e){return B.aeq(t,e)},$gt:function(t,e){return B.gt(t,e,!1)},$gte:function(t,e){return B.gt(t,e,!0)},$lt:function(t,e){return B.lt(t,e,!1)},$lte:function(t,e){return B.lt(t,e,!0)},$jgt:function(t,e){return t>e},$jgte:function(t,e){return t>=e},$jlt:function(t,e){return t<e},$jlte:function(t,e){return t<=e},$between:function(t,e){return void 0!==t&&null!==t&&(B.gt(t,e[0],!0)&&B.lt(t,e[1],!0))},$jbetween:function(t,e){return void 0!==t&&null!==t&&(t>=e[0]&&t<=e[1])},$in:function(t,e){return-1!==e.indexOf(t)},$inSet:function(t,e){return e.has(t)},$nin:function(t,e){return-1===e.indexOf(t)},$keyin:function(t,e){return t in e},$nkeyin:function(t,e){return!(t in e)},$definedin:function(t,e){return void 0!==e[t]},$undefinedin:function(t,e){return void 0===e[t]},$regex:function(t,e){return e.test(t)},$containsString:function(t,e){return"string"==typeof t&&-1!==t.indexOf(e)},$containsNone:function(t,e){return!T.$containsAny(t,e)},$containsAny:function(t,e){var i=h(t);return null!==i&&(Array.isArray(e)?e.some(i):i(e))},$contains:function(t,e){var i=h(t);return null!==i&&(Array.isArray(e)?e.every(i):i(e))},$elemMatch:function(t,e){return!!Array.isArray(t)&&t.some(function(t){return Object.keys(e).every(function(i){var r=e[i];return"object"==typeof r&&r||(r={$eq:r}),-1!==i.indexOf(".")?l(t,i.split("."),c,e[i],t):c(t[i],r,t)})})},$type:function(t,e,i){var r=typeof t;return"object"===r&&(Array.isArray(t)?r="array":t instanceof Date&&(r="date")),"object"!=typeof e?r===e:c(r,e,i)},$finite:function(t,e){return e===isFinite(t)},$size:function(t,e,i){return!!Array.isArray(t)&&("object"!=typeof e?t.length===e:c(t.length,e,i))},$len:function(t,e,i){return"string"==typeof t&&("object"!=typeof e?t.length===e:c(t.length,e,i))},$where:function(t,e){return!0===e(t)},$not:function(t,e,i){return!c(t,e,i)},$and:function(t,e,i){for(var r=0,n=e.length;r<n;r+=1)if(!c(t,e[r],i))return!1;return!0},$or:function(t,e,i){for(var r=0,n=e.length;r<n;r+=1)if(c(t,e[r],i))return!0;return!1},$exists:function(t,e){return e?void 0!==t:void 0===t}};["$eq","$aeq","$ne","$dteq","$gt","$gte","$lt","$lte","$jgt","$jgte","$jlt","$jlte","$type"].forEach(function(t){var e=T[t];T["$"+t]=function(t,i,r){if("string"==typeof i)return e(t,r[i]);if("function"==typeof i)return e(t,i(r));throw new Error("Invalid argument to $$ matcher")}});var J={$eq:T.$eq,$aeq:!0,$dteq:!0,$gt:!0,$gte:!0,$lt:!0,$lte:!0,$in:!0,$between:!0};return f.prototype.events={},f.prototype.asyncListeners=!1,f.prototype.on=function(t,e){var i,r=this;return Array.isArray(t)?(t.forEach(function(t){r.on(t,e)}),e):(i=this.events[t],i||(i=this.events[t]=[]),i.push(e),e)},f.prototype.emit=function(t){var e,i=this;if(!t||!this.events[t])throw new Error("No event "+t+" defined");this.events[t].length&&(e=Array.prototype.slice.call(arguments,1),this.events[t].forEach(function(t){i.asyncListeners?setTimeout(function(){t.apply(i,e)},1):t.apply(i,e)}))},f.prototype.addListener=f.prototype.on,f.prototype.removeListener=function(t,e){var i=this;if(Array.isArray(t))return void t.forEach(function(t){i.removeListener(t,e)});if(this.events[t]){var r=this.events[t];r.splice(r.indexOf(e),1)}},y.prototype=new f,y.prototype.constructor=y,y.prototype.getIndexedAdapter=function(){var t;return"function"==typeof require&&(t=require("./loki-indexed-adapter.js")),t},y.prototype.configureOptions=function(t,e){var i={NODEJS:"fs",BROWSER:"localStorage",CORDOVA:"localStorage",MEMORY:"memory"},r={fs:m,localStorage:b,memory:v};if(this.options={},this.persistenceMethod=null,this.persistenceAdapter=null,void 0!==t){if(this.options=t,this.options.hasOwnProperty("persistenceMethod")&&"function"==typeof r[t.persistenceMethod]&&(this.persistenceMethod=t.persistenceMethod,this.persistenceAdapter=new r[t.persistenceMethod]),this.options.hasOwnProperty("adapter")&&(this.persistenceMethod="adapter",this.persistenceAdapter=t.adapter,this.options.adapter=null,this.isIncremental="incremental"===this.persistenceAdapter.mode),t.autoload&&e){var n=this;setTimeout(function(){n.loadDatabase(t,t.autoloadCallback)},1)}this.options.hasOwnProperty("autosaveInterval")&&(this.autosaveDisable(),this.autosaveInterval=parseInt(this.options.autosaveInterval,10)),this.options.hasOwnProperty("autosave")&&this.options.autosave&&(this.autosaveDisable(),this.autosave=!0,this.options.hasOwnProperty("autosaveCallback")?this.autosaveEnable(t,t.autosaveCallback):this.autosaveEnable()),this.options.hasOwnProperty("throttledSaves")&&(this.throttledSaves=this.options.throttledSaves)}this.options.hasOwnProperty("serializationMethod")||(this.options.serializationMethod="normal"),this.options.hasOwnProperty("destructureDelimiter")||(this.options.destructureDelimiter="$<\n"),null===this.persistenceAdapter&&(this.persistenceMethod=i[this.ENV],this.persistenceMethod&&(this.persistenceAdapter=new r[this.persistenceMethod]))},y.prototype.copy=function(t){var e,i,r=new y(this.filename,{env:"NA"});if(t=t||{},r.loadJSONObject(this,{retainDirtyFlags:!0}),t.hasOwnProperty("removeNonSerializable")&&!0===t.removeNonSerializable)for(r.autosaveHandle=null,r.persistenceAdapter=null,e=r.collections.length,i=0;i<e;i++)r.collections[i].constraints=null,r.collections[i].ttl=null;return r},y.prototype.addCollection=function(t,e){var i,r=this.collections.length;if(e&&!0===e.disableMeta){if(!1===e.disableChangesApi)throw new Error("disableMeta option cannot be passed as true when disableChangesApi is passed as false");if(!1===e.disableDeltaChangesApi)throw new Error("disableMeta option cannot be passed as true when disableDeltaChangesApi is passed as false");if("number"==typeof e.ttl&&e.ttl>0)throw new Error("disableMeta option cannot be passed as true when ttl is enabled")}for(i=0;i<r;i+=1)if(this.collections[i].name===t)return this.collections[i];var n=new x(t,e);return n.isIncremental=this.isIncremental,this.collections.push(n),this.verbose&&(n.lokiConsoleWrapper=console),n},y.prototype.loadCollection=function(t){if(!t.name)throw new Error("Collection must have a name property to be loaded");this.collections.push(t)},y.prototype.getCollection=function(t){var e,i=this.collections.length;for(e=0;e<i;e+=1)if(this.collections[e].name===t)return this.collections[e];return this.emit("warning","collection "+t+" not found"),null},y.prototype.renameCollection=function(t,e){var i=this.getCollection(t);return i&&(i.name=e),i},y.prototype.listCollections=function(){for(var t=this.collections.length,e=[];t--;)e.push({name:this.collections[t].name,type:this.collections[t].objType,count:this.collections[t].data.length});return e},y.prototype.removeCollection=function(t){var e,i=this.collections.length;for(e=0;e<i;e+=1)if(this.collections[e].name===t){var r=new x(t,{}),n=this.collections[e];for(var s in n)n.hasOwnProperty(s)&&r.hasOwnProperty(s)&&(n[s]=r[s]);return void this.collections.splice(e,1)}},y.prototype.getName=function(){return this.name},y.prototype.serializeReplacer=function(t,e){switch(t){case"autosaveHandle":case"persistenceAdapter":case"constraints":case"ttl":return null;case"throttledSavePending":case"throttledCallbacks":return;case"lokiConsoleWrapper":return null;default:return e}},y.prototype.serialize=function(t){switch(t=t||{},t.hasOwnProperty("serializationMethod")||(t.serializationMethod=this.options.serializationMethod),t.serializationMethod){case"normal":return JSON.stringify(this,this.serializeReplacer);case"pretty":return JSON.stringify(this,this.serializeReplacer,2);case"destructured":return this.serializeDestructured();default:return JSON.stringify(this,this.serializeReplacer)}},y.prototype.toJson=y.prototype.serialize,y.prototype.serializeDestructured=function(t){var e,i,r,n,s,o=[];if(t=t||{},t.hasOwnProperty("partitioned")||(t.partitioned=!1),t.hasOwnProperty("delimited")||(t.delimited=!0),t.hasOwnProperty("delimiter")||(t.delimiter=this.options.destructureDelimiter),!0===t.partitioned&&t.hasOwnProperty("partition")&&t.partition>=0)return this.serializeCollection({delimited:t.delimited,delimiter:t.delimiter,collectionIndex:t.partition});for(s=new y(this.filename),s.loadJSONObject(this),e=0;e<s.collections.length;e++)s.collections[e].data=[];if(!0===t.partitioned&&-1===t.partition)return s.serialize({serializationMethod:"normal"});for(o.push(s.serialize({serializationMethod:"normal"})),s=null,e=0;e<this.collections.length;e++)if(r=this.serializeCollection({delimited:t.delimited,delimiter:t.delimiter,collectionIndex:e}),!1===t.partitioned&&!1===t.delimited){if(!Array.isArray(r))throw new Error("a nondelimited, non partitioned collection serialization did not return an expected array");for(n=r.length,i=0;i<n;i++)o.push(r[i]),r[i]=null;o.push("")}else o.push(r);return t.partitioned?(t.delimited,o):t.delimited?(o.push(""),o.join(t.delimiter)):(o.push(""),o)},y.prototype.serializeCollection=function(t){var e,i,r=[];if(t=t||{},t.hasOwnProperty("delimited")||(t.delimited=!0),!t.hasOwnProperty("collectionIndex"))throw new Error("serializeCollection called without 'collectionIndex' option");for(e=this.collections[t.collectionIndex].data.length,r=[],i=0;i<e;i++)r.push(JSON.stringify(this.collections[t.collectionIndex].data[i]));return t.delimited?(r.push(""),r.join(t.delimiter)):r},y.prototype.deserializeDestructured=function(t,e){var i,r,n,s=[],o=0,a=1,l=!1;if(e=e||{},e.hasOwnProperty("partitioned")||(e.partitioned=!1),e.hasOwnProperty("delimited")||(e.delimited=!0),e.hasOwnProperty("delimiter")||(e.delimiter=this.options.destructureDelimiter),e.partitioned){if(e.hasOwnProperty("partition"))return-1===e.partition?i=JSON.parse(t[0]):this.deserializeCollection(t[e.partition+1],e);for(i=JSON.parse(t[0]),r=i.collections.length,o=0;o<r;o++)i.collections[o].data=this.deserializeCollection(t[o+1],e);return i}if(e.delimited){if(s=t.split(e.delimiter),t=null,0===s.length)return null}else s=t;for(i=JSON.parse(s[0]),r=i.collections.length,s[0]=null;!l;)s[a],""===s[a]?++o>r&&(l=!0):(n=JSON.parse(s[a]),i.collections[o].data.push(n)),s[a++]=null;return i},y.prototype.deserializeCollection=function(t,e){var i,r,n=[];for(e=e||{},e.hasOwnProperty("partitioned")||(e.partitioned=!1),e.hasOwnProperty("delimited")||(e.delimited=!0),e.hasOwnProperty("delimiter")||(e.delimiter=this.options.destructureDelimiter),e.delimited?(n=t.split(e.delimiter),n.pop()):n=t,r=n.length,i=0;i<r;i++)n[i]=JSON.parse(n[i]);return n},y.prototype.loadJSON=function(t,e){var i;if(0===t.length)i={};else switch(this.options.serializationMethod){case"normal":case"pretty":i=JSON.parse(t);break;case"destructured":i=this.deserializeDestructured(t);break;default:i=JSON.parse(t)}this.loadJSONObject(i,e)},y.prototype.loadJSONObject=function(e,i){var r,n,s,o,a,l,h=0,c=e.collections?e.collections.length:0;for(this.name=e.name,e.hasOwnProperty("throttledSaves")&&i&&!i.hasOwnProperty("throttledSaves")&&(this.throttledSaves=e.throttledSaves),this.collections=[],h;h<c;h+=1){if(r=e.collections[h],n=this.addCollection(r.name,{disableChangesApi:r.disableChangesApi,disableDeltaChangesApi:r.disableDeltaChangesApi,disableMeta:r.disableMeta,disableFreeze:!r.hasOwnProperty("disableFreeze")||r.disableFreeze}),n.adaptiveBinaryIndices=!!r.hasOwnProperty("adaptiveBinaryIndices")&&!0===r.adaptiveBinaryIndices,n.transactional=r.transactional,n.asyncListeners=r.asyncListeners,n.cloneObjects=r.cloneObjects,n.cloneMethod=r.cloneMethod||"parse-stringify",n.autoupdate=r.autoupdate,n.changes=r.changes,n.dirtyIds=r.dirtyIds||[],i&&!0===i.retainDirtyFlags?n.dirty=r.dirty:n.dirty=!1,r.getData){if(i&&i.hasOwnProperty(r.name)||!n.disableFreeze||n.autoupdate)throw new Error("this collection cannot be loaded lazily: "+r.name);n.getData=r.getData,Object.defineProperty(n,"data",{get:function(){var t=this.getData();return this.getData=null,Object.defineProperty(this,"data",{value:t,writable:!0}),t}})}else if(s=r.data.length,o=0,i&&i.hasOwnProperty(r.name))for(a=function(t){var e,r=i[t.name];return r.proto?(e=r.inflate||R.copyProperties,function(t){var i=new r.proto;return e(t,i),i}):r.inflate}(r),o;o<s;o++)l=a(r.data[o]),n.data[o]=l,n.addAutoUpdateObserver(l),n.disableFreeze||t(n.data[o]);else for(o;o<s;o++)n.data[o]=r.data[o],n.addAutoUpdateObserver(n.data[o]),n.disableFreeze||t(n.data[o]);if(n.maxId=void 0===r.maxId?0:r.maxId,void 0!==r.binaryIndices&&(n.binaryIndices=r.binaryIndices),void 0!==r.transforms&&(n.transforms=r.transforms),n.uniqueNames=[],r.hasOwnProperty("uniqueNames")&&(n.uniqueNames=r.uniqueNames),void 0!==r.DynamicViews){for(var u=0;u<r.DynamicViews.length;u++){var d=r.DynamicViews[u],p=n.addDynamicView(d.name,d.options);p.resultdata=d.resultdata,p.resultsdirty=d.resultsdirty,p.filterPipeline=d.filterPipeline,p.sortCriteriaSimple=d.sortCriteriaSimple,p.sortCriteria=d.sortCriteria,p.sortFunction=null,p.sortDirty=d.sortDirty,n.disableFreeze||(t(p.filterPipeline),p.sortCriteriaSimple?t(p.sortCriteriaSimple):p.sortCriteria&&t(p.sortCriteria)),p.resultset.filteredrows=d.resultset.filteredrows,p.resultset.filterInitialized=d.resultset.filterInitialized,p.rematerialize({removeWhereFilters:!0})}e.databaseVersion<1.5&&(n.ensureAllIndexes(!0),n.dirty=!0)}}},y.prototype.close=function(t){this.autosave&&(this.autosaveDisable(),this.autosaveDirty()&&(this.saveDatabase(t),t=void 0)),t&&this.on("close",t),this.emit("close")},y.prototype.generateChangesNotification=function(t){function e(t){return t.name}var i=[],r=t||this.collections.map(e);return this.collections.forEach(function(t){-1!==r.indexOf(e(t))&&(i=i.concat(t.getChanges()))}),i},y.prototype.serializeChanges=function(t){return JSON.stringify(this.generateChangesNotification(t))},y.prototype.clearChanges=function(){this.collections.forEach(function(t){t.flushChanges&&t.flushChanges()})},v.prototype.loadDatabase=function(t,e){var i=this;this.options.asyncResponses?setTimeout(function(){e(i.hashStore.hasOwnProperty(t)?i.hashStore[t].value:null)},this.options.asyncTimeout):e(this.hashStore.hasOwnProperty(t)?this.hashStore[t].value:null)},v.prototype.saveDatabase=function(t,e,i){var r,n=this;this.options.asyncResponses?setTimeout(function(){r=n.hashStore.hasOwnProperty(t)?n.hashStore[t].savecount:0,n.hashStore[t]={savecount:r+1,lastsave:new Date,value:e},i()},this.options.asyncTimeout):(r=this.hashStore.hasOwnProperty(t)?this.hashStore[t].savecount:0,this.hashStore[t]={savecount:r+1,lastsave:new Date,value:e},i())},v.prototype.deleteDatabase=function(t,e){this.hashStore.hasOwnProperty(t)&&delete this.hashStore[t],"function"==typeof e&&e()},g.prototype.loadDatabase=function(t,e){var i=this;this.dbname=t,this.dbref=new y(t),this.adapter.loadDatabase(t,function(t){if(!t)return void e(t);"string"!=typeof t&&e(new Error("LokiPartitioningAdapter received an unexpected response from inner adapter loadDatabase()"));var r=JSON.parse(t);i.dbref.loadJSONObject(r),r=null;i.dbref.collections.length;if(0===i.dbref.collections.length)return void e(i.dbref);i.pageIterator={collection:0,pageIndex:0},i.loadNextPartition(0,function(){e(i.dbref)})})},g.prototype.loadNextPartition=function(t,e){var i=this.dbname+"."+t,r=this;if(!0===this.options.paging)return this.pageIterator.pageIndex=0,void this.loadNextPage(e);this.adapter.loadDatabase(i,function(i){var n=r.dbref.deserializeCollection(i,{delimited:!0,collectionIndex:t});r.dbref.collections[t].data=n,++t<r.dbref.collections.length?r.loadNextPartition(t,e):e()})},g.prototype.loadNextPage=function(t){var e=this.dbname+"."+this.pageIterator.collection+"."+this.pageIterator.pageIndex,i=this;this.adapter.loadDatabase(e,function(e){var r=e.split(i.options.delimiter);e="";var n,s=r.length,o=""===r[s-1];for(o&&(r.pop(),s=r.length,""===r[s-1]&&1===s&&(r.pop(),s=r.length)),n=0;n<s;n++)i.dbref.collections[i.pageIterator.collection].data.push(JSON.parse(r[n])),r[n]=null;r=[],o?++i.pageIterator.collection<i.dbref.collections.length?i.loadNextPartition(i.pageIterator.collection,t):t():(i.pageIterator.pageIndex++,i.loadNextPage(t))})},g.prototype.exportDatabase=function(t,e,i){var r,n=e.collections.length;for(this.dbref=e,this.dbname=t,this.dirtyPartitions=[-1],r=0;r<n;r++)e.collections[r].dirty&&this.dirtyPartitions.push(r);this.saveNextPartition(function(t){i(t)})},g.prototype.saveNextPartition=function(t){var e=this,i=this.dirtyPartitions.shift(),r=this.dbname+(-1===i?"":"."+i);if(this.options.paging&&-1!==i)return this.pageIterator={collection:i,docIndex:0,pageIndex:0},void this.saveNextPage(function(i){0===e.dirtyPartitions.length?t(i):e.saveNextPartition(t)});var n=this.dbref.serializeDestructured({partitioned:!0,delimited:!0,partition:i});this.adapter.saveDatabase(r,n,function(i){if(i)return void t(i);0===e.dirtyPartitions.length?t(null):e.saveNextPartition(t)})},g.prototype.saveNextPage=function(t){var e=this,i=this.dbref.collections[this.pageIterator.collection],r=this.dbname+"."+this.pageIterator.collection+"."+this.pageIterator.pageIndex,n=0,s=i.data.length,o=this.options.delimiter.length,a="",l="",h=!1,c=!1,u=function(i){l="",i&&t(i),h?t(null):(e.pageIterator.pageIndex++,e.saveNextPage(t))};for(0===i.data.length&&(h=!0);;)if(h||(a=JSON.stringify(i.data[this.pageIterator.docIndex]),l+=a,n+=a.length,++this.pageIterator.docIndex>=s&&(h=!0)),n>=this.options.pageSize&&(c=!0),c&&!h||(l+=this.options.delimiter,n+=o),h||c)return void this.adapter.saveDatabase(r,l,u)},m.prototype.loadDatabase=function(t,e){var i=this;this.fs.stat(t,function(r,n){!r&&n.isFile()?i.fs.readFile(t,{encoding:"utf8"},function(t,i){e(t?new Error(t):i)}):e(null)})},m.prototype.saveDatabase=function(t,e,i){var r=this,n=t+"~";this.fs.writeFile(n,e,function(e){e?i(new Error(e)):r.fs.rename(n,t,i)})},m.prototype.deleteDatabase=function(t,e){this.fs.unlink(t,function(t){t?e(new Error(t)):e()})},b.prototype.loadDatabase=function(t,e){e(p()?localStorage.getItem(t):new Error("localStorage is not available"))},b.prototype.saveDatabase=function(t,e,i){p()?(localStorage.setItem(t,e),i(null)):i(new Error("localStorage is not available"))},b.prototype.deleteDatabase=function(t,e){p()?(localStorage.removeItem(t),e(null)):e(new Error("localStorage is not available"))},y.prototype.throttledSaveDrain=function(t,e){var i=this,r=(new Date).getTime();if(this.throttledSaves||t(!0),e=e||{},e.hasOwnProperty("recursiveWait")||(e.recursiveWait=!0),e.hasOwnProperty("recursiveWaitLimit")||(e.recursiveWaitLimit=!1),e.hasOwnProperty("recursiveWaitLimitDuration")||(e.recursiveWaitLimitDuration=2e3),e.hasOwnProperty("started")||(e.started=(new Date).getTime()),this.throttledSaves&&this.throttledSavePending){if(!e.recursiveWait)return void this.throttledCallbacks.push(t);this.throttledCallbacks.push(function(){return i.throttledSavePending?e.recursiveWaitLimit&&r-e.started>e.recursiveWaitLimitDuration?void t(!1):void i.throttledSaveDrain(t,e):void t(!0)})}else t(!0)},y.prototype.loadDatabaseInternal=function(t,e){var i=e||function(t,e){if(t)throw t},r=this;null!==this.persistenceAdapter?this.persistenceAdapter.loadDatabase(this.filename,function(e){if("string"==typeof e){var n=!1;try{r.loadJSON(e,t||{}),n=!0}catch(t){i(t)}n&&(i(null),r.emit("loaded","database "+r.filename+" loaded"))}else{if(!e)return i(null),void r.emit("loaded","empty database "+r.filename+" loaded");if(e instanceof Error)return void i(e);if("object"==typeof e)return r.loadJSONObject(e,t||{}),i(null),void r.emit("loaded","database "+r.filename+" loaded");i("unexpected adapter response : "+e)}}):i(new Error("persistenceAdapter not configured"))},y.prototype.loadDatabase=function(t,e){var i=this;if(!this.throttledSaves)return void this.loadDatabaseInternal(t,e);this.throttledSaveDrain(function(r){if(r)return i.throttledSavePending=!0,void i.loadDatabaseInternal(t,function(t){0===i.throttledCallbacks.length?i.throttledSavePending=!1:i.saveDatabase(),"function"==typeof e&&e(t)});"function"==typeof e&&e(new Error("Unable to pause save throttling long enough to read database"))},t)},y.prototype.saveDatabaseInternal=function(t){var e=t||function(t){if(t)throw t},i=this;if(!this.persistenceAdapter)return void e(new Error("persistenceAdapter not configured"));if("incremental"===this.persistenceAdapter.mode){var r;this.ignoreAutosave=!0,this.persistenceAdapter.saveDatabase(this.filename,function(){if(i.ignoreAutosave=!1,r)return void e(new Error("adapter error - getLokiCopy called more than once"));var t=i.copy({removeNonSerializable:!0});return r=i.collections.map(function(t){return[t.dirty,t.dirtyIds]}),i.collections.forEach(function(t){t.dirty=!1,t.dirtyIds=[]}),t},function(t){i.ignoreAutosave=!1,t&&r&&i.collections.forEach(function(t,e){var i=r[e];t.dirty=t.dirty||i[0],t.dirtyIds=t.dirtyIds.concat(i[1])}),e(t)})}else"reference"===this.persistenceAdapter.mode&&"function"==typeof this.persistenceAdapter.exportDatabase?this.persistenceAdapter.exportDatabase(this.filename,this.copy({removeNonSerializable:!0}),function(t){i.autosaveClearFlags(),e(t)}):(this.autosaveClearFlags(),this.persistenceAdapter.saveDatabase(this.filename,this.serialize(),function(t){e(t)
}))},y.prototype.saveDatabase=function(t){if(!this.throttledSaves)return void this.saveDatabaseInternal(t);if(this.throttledSavePending)return void this.throttledCallbacks.push(t);var e=this.throttledCallbacks;this.throttledCallbacks=[],e.unshift(t),this.throttledSavePending=!0;var i=this;this.saveDatabaseInternal(function(t){i.throttledSavePending=!1,e.forEach(function(e){"function"==typeof e&&setTimeout(function(){e(t)},1)}),i.throttledCallbacks.length>0&&i.saveDatabase()})},y.prototype.save=y.prototype.saveDatabase,y.prototype.deleteDatabase=function(t,e){var i=e||function(t,e){if(t)throw t};"function"!=typeof t||e||(i=t),null!==this.persistenceAdapter?this.persistenceAdapter.deleteDatabase(this.filename,function(t){i(t)}):i(new Error("persistenceAdapter not configured"))},y.prototype.autosaveDirty=function(){for(var t=0;t<this.collections.length;t++)if(this.collections[t].dirty)return!0;return!1},y.prototype.autosaveClearFlags=function(){for(var t=0;t<this.collections.length;t++)this.collections[t].dirty=!1},y.prototype.autosaveEnable=function(t,e){this.autosave=!0;var i=5e3,r=this;void 0!==this.autosaveInterval&&null!==this.autosaveInterval&&(i=this.autosaveInterval),this.autosaveHandle=setInterval(function(){r.autosaveDirty()&&!r.ignoreAutosave&&r.saveDatabase(e)},i)},y.prototype.autosaveDisable=function(){void 0!==this.autosaveHandle&&null!==this.autosaveHandle&&(clearInterval(this.autosaveHandle),this.autosaveHandle=null)},w.prototype.reset=function(){return this.filteredrows.length>0&&(this.filteredrows=[]),this.filterInitialized=!1,this},w.prototype.toJSON=function(){var t=this.copy();return t.collection=null,t},w.prototype.limit=function(t){this.filterInitialized||0!==this.filteredrows.length||(this.filteredrows=this.collection.prepareFullDocIndex());var e=new w(this.collection);return e.filteredrows=this.filteredrows.slice(0,t),e.filterInitialized=!0,e},w.prototype.offset=function(t){this.filterInitialized||0!==this.filteredrows.length||(this.filteredrows=this.collection.prepareFullDocIndex());var e=new w(this.collection);return e.filteredrows=this.filteredrows.slice(t),e.filterInitialized=!0,e},w.prototype.copy=function(){var t=new w(this.collection);return this.filteredrows.length>0&&(t.filteredrows=this.filteredrows.slice()),t.filterInitialized=this.filterInitialized,t},w.prototype.branch=w.prototype.copy,w.prototype.transform=function(t,e){var i,r,n=this;if("string"==typeof t&&this.collection.transforms.hasOwnProperty(t)&&(t=this.collection.transforms[t]),"object"!=typeof t||!Array.isArray(t))throw new Error("Invalid transform");for(void 0!==e&&(t=R.resolveTransformParams(t,e)),i=0;i<t.length;i++)switch(r=t[i],r.type){case"find":n.find(r.value);break;case"where":n.where(r.value);break;case"simplesort":n.simplesort(r.property,r.desc||r.options);break;case"compoundsort":n.compoundsort(r.value);break;case"sort":n.sort(r.value);break;case"limit":n=n.limit(r.value);break;case"offset":n=n.offset(r.value);break;case"map":n=n.map(r.value,r.dataOptions);break;case"eqJoin":n=n.eqJoin(r.joinData,r.leftJoinKey,r.rightJoinKey,r.mapFun,r.dataOptions);break;case"mapReduce":n=n.mapReduce(r.mapFunction,r.reduceFunction);break;case"update":n.update(r.value);break;case"remove":n.remove()}return n},w.prototype.sort=function(t){this.filterInitialized||0!==this.filteredrows.length||(this.filteredrows=this.collection.prepareFullDocIndex());var e=function(t,e){return function(i,r){return t(e[i],e[r])}}(t,this.collection.data);return this.filteredrows.sort(e),this},w.prototype.simplesort=function(t,e){var i,r=10,n=this.collection.data.length,s=this.filteredrows.length,a=this.collection.binaryIndices.hasOwnProperty(t);if(void 0!==e&&!1!==e||(e={desc:!1}),!0===e&&(e={desc:!0}),0===s){if(this.filterInitialized)return this;if(this.collection.binaryIndices.hasOwnProperty(t))return this.collection.ensureIndex(t),this.filteredrows=this.collection.binaryIndices[t].values.slice(0),e.desc&&this.filteredrows.reverse(),this;this.filteredrows=this.collection.prepareFullDocIndex()}else if(!e.disableIndexIntersect&&a&&(i=n/s,e.useJavascriptSorting&&(r=6),i<=r||e.forceIndexIntersect)){var l,h=this.filteredrows,c={};for(l=0;l<s;l++)c[h[l]]=!0;var u=this.collection.binaryIndices[t].values;return this.filteredrows=u.filter(function(t){return c[t]}),e.desc&&this.filteredrows.reverse(),this}if(e.useJavascriptSorting)return this.sort(function(e,i){return e[t]===i[t]?0:e[t]>i[t]?1:e[t]<i[t]?-1:void 0});var d=function(t,e,i){var r,n,s;return function(a,l){return~t.indexOf(".")?(s=t.split("."),r=R.getIn(i[a],s,!0),n=R.getIn(i[l],s,!0)):(r=i[a][t],n=i[l][t]),o(r,n,e)}}(t,e.desc,this.collection.data);return this.filteredrows.sort(d),this},w.prototype.compoundsort=function(t){if(0===t.length)throw new Error("Invalid call to compoundsort, need at least one property");var e;if(1===t.length)return e=t[0],Array.isArray(e)?this.simplesort(e[0],e[1]):this.simplesort(e,!1);for(var i=0,r=t.length;i<r;i+=1)e=t[i],Array.isArray(e)||(t[i]=[e,!1]);this.filterInitialized||0!==this.filteredrows.length||(this.filteredrows=this.collection.prepareFullDocIndex());var n=function(t,e){return function(i,r){return a(t,e[i],e[r])}}(t,this.collection.data);return this.filteredrows.sort(n),this},w.prototype.findOr=function(t){for(var e=null,i=0,r=0,n=[],s=[],o=0,a=(this.count(),0),l=t.length;a<l;a++)for(e=this.branch().find(t[a]).filteredrows,r=e.length,i=0;i<r;i++)o=e[i],void 0===s[o]&&(s[o]=!0,n.push(o));return this.filteredrows=n,this.filterInitialized=!0,this},w.prototype.$or=w.prototype.findOr,w.prototype.findAnd=function(t){for(var e=0,i=t.length;e<i;e++){if(0===this.count())return this;this.find(t[e])}return this},w.prototype.$and=w.prototype.findAnd,w.prototype.find=function(t,e){if(0===this.collection.data.length)return this.filteredrows=[],this.filterInitialized=!0,this;var i,r,n,s,o,a,h,c=t||"getAll",u=!1,d=[],p=[],f=null;if(e=e||!1,"object"==typeof c){for(i in c)s={},s[i]=c[i],p.push(s),M.call(c,i)&&(r=i,n=c[i]);if(p.length>1)return this.find({$and:p},e)}if(!r||"getAll"===c)return e&&(this.filterInitialized?this.filteredrows=this.filteredrows.slice(0,1):(this.filteredrows=this.collection.data.length>0?[0]:[],this.filterInitialized=!0)),this;if("$and"===r||"$or"===r)return this[r](n),e&&this.filteredrows.length>1&&(this.filteredrows=this.filteredrows.slice(0,1)),this;if(null===n||"object"!=typeof n||n instanceof Date)o="$eq",a=n;else{if("object"!=typeof n)throw new Error("Do not know what you want to do.");for(h in n)if(M.call(n,h)){o=h,a=n[h];break}}"$regex"!==o&&"object"!=typeof a||(a=I(o,a));var y=-1!==r.indexOf(".");!this.filterInitialized&&this.collection.binaryIndices[r]&&J[o]&&(!0!==this.collection.adaptiveBinaryIndices&&this.collection.ensureIndex(r),u=!0,f=this.collection.binaryIndices[r]),!u&&"$in"===o&&Array.isArray(a)&&"undefined"!=typeof Set&&(a=new Set(a),o="$inSet");var v,g,m=T[o],b=this.collection.data,w=0,O=0,x=0;if(this.filterInitialized){if(v=this.filteredrows,O=v.length,y){for(r=r.split("."),w=0;w<O;w++)if(x=v[w],g=b[x],l(g,r,m,a,g)&&(d.push(x),e))return this.filteredrows=d,this}else for(w=0;w<O;w++)if(x=v[w],g=b[x],m(g[r],a,g)&&(d.push(x),e))return this.filteredrows=d,this}else if(u){var P=this.collection.calculateRange(o,r,a);if("$in"!==o){for(w=P[0];w<=P[1];w++)if(!0!==J[o]){if(J[o](R.getIn(b[f.values[w]],r,y),a)&&(d.push(f.values[w]),e))return this.filteredrows=d,this.filterInitialized=!0,this}else if(d.push(f.values[w]),e)return this.filteredrows=d,this.filterInitialized=!0,this}else for(w=0,O=P.length;w<O;w++)if(d.push(f.values[P[w]]),e)return this.filteredrows=d,this.filterInitialized=!0,this}else if(O=b.length,y){for(r=r.split("."),w=0;w<O;w++)if(g=b[w],l(g,r,m,a,g)&&(d.push(w),e))return this.filteredrows=d,this.filterInitialized=!0,this}else for(w=0;w<O;w++)if(g=b[w],m(g[r],a,g)&&(d.push(w),e))return this.filteredrows=d,this.filterInitialized=!0,this;return this.filteredrows=d,this.filterInitialized=!0,this},w.prototype.where=function(t){var e,i=[];if("function"!=typeof t)throw new TypeError("Argument is not a stored view or a function");e=t;try{if(this.filterInitialized){for(var r=this.filteredrows.length;r--;)!0===e(this.collection.data[this.filteredrows[r]])&&i.push(this.filteredrows[r]);return this.filteredrows=i,this}for(var n=this.collection.data.length;n--;)!0===e(this.collection.data[n])&&i.push(n);return this.filteredrows=i,this.filterInitialized=!0,this}catch(t){throw t}},w.prototype.count=function(){return this.filterInitialized?this.filteredrows.length:this.collection.count()},w.prototype.data=function(t){var e,i,r,n,s=[],o=this.collection.data;if(t=t||{},t.removeMeta&&!t.forceClones&&(t.forceClones=!0,t.forceCloneMethod=t.forceCloneMethod||"shallow"),!this.collection.disableDeltaChangesApi&&this.collection.disableFreeze&&(t.forceClones=!0,t.forceCloneMethod="parse-stringify"),!this.filterInitialized){if(0===this.filteredrows.length){if(this.collection.cloneObjects||t.forceClones){for(i=o.length,n=t.forceCloneMethod||this.collection.cloneMethod,r=0;r<i;r++)e=u(o[r],n),t.removeMeta&&(delete e.$loki,delete e.meta),s.push(e);return s}return o.slice()}this.filterInitialized=!0}var a=this.filteredrows;if(i=a.length,this.collection.cloneObjects||t.forceClones)for(n=t.forceCloneMethod||this.collection.cloneMethod,r=0;r<i;r++)e=u(o[a[r]],n),t.removeMeta&&(delete e.$loki,delete e.meta),s.push(e);else for(r=0;r<i;r++)s.push(o[a[r]]);return s},w.prototype.update=function(t){if("function"!=typeof t)throw new TypeError("Argument is not a function");this.filterInitialized||0!==this.filteredrows.length||(this.filteredrows=this.collection.prepareFullDocIndex());for(var e,i=this.filteredrows.length,r=this.collection.data,n=0;n<i;n++)this.disableFreeze&&!this.collection.cloneObjects&&this.collection.disableDeltaChangesApi?(t(r[this.filteredrows[n]]),this.collection.update(r[this.filteredrows[n]])):(e=u(r[this.filteredrows[n]],this.collection.cloneMethod),t(e),this.collection.update(e));return this},w.prototype.remove=function(){return this.filterInitialized||0!==this.filteredrows.length||(this.filteredrows=this.collection.prepareFullDocIndex()),this.collection.removeBatchByPositions(this.filteredrows),this.filteredrows=[],this},w.prototype.mapReduce=function(t,e){try{return e(this.data().map(t))}catch(t){throw t}},w.prototype.eqJoin=function(t,e,i,r,n){var s,o,a,l=[],h=[],c=[],u="function"==typeof e,d="function"==typeof i,p={};if(l=this.data(n),s=l.length,t instanceof x)h=t.chain().data(n);else if(t instanceof w)h=t.data(n);else{if(!Array.isArray(t))throw new TypeError("joinData needs to be an array or result set");h=t}o=h.length;for(var f=0;f<o;f++)a=d?i(h[f]):h[f][i],p[a]=h[f];r||(r=function(t,e){return{left:t,right:e}});for(var y=0;y<s;y++)a=u?e(l[y]):l[y][e],c.push(r(l[y],p[a]||{}));return this.collection=new x("joinData"),this.collection.insert(c),this.filteredrows=[],this.filterInitialized=!1,this},w.prototype.map=function(t,e){var i=this.data(e).map(t);return this.collection=new x("mappedData"),this.collection.insert(i),this.filteredrows=[],this.filterInitialized=!1,this},O.prototype=new f,O.prototype.constructor=O,O.prototype.getSort=function(){return this.sortFunction||this.sortCriteria||this.sortCriteriaSimple},O.prototype.rematerialize=function(t){var e,i,r;t=t||{},this.resultdata=[],this.resultsdirty=!0,this.resultset=new w(this.collection),(this.sortFunction||this.sortCriteria||this.sortCriteriaSimple)&&(this.sortDirty=!0);var n=Object.isFrozen(this.filterPipeline);if(t.hasOwnProperty("removeWhereFilters"))for(n&&(this.filterPipeline=this.filterPipeline.slice()),e=this.filterPipeline.length,i=e;i--;)"where"===this.filterPipeline[i].type&&(i!==this.filterPipeline.length-1&&(this.filterPipeline[i]=this.filterPipeline[this.filterPipeline.length-1]),this.filterPipeline.length--);var s=this.filterPipeline;for(this.filterPipeline=[],e=s.length,r=0;r<e;r++)this.applyFind(s[r].val,s[r].uid);return n&&Object.freeze(this.filterPipeline),this.data(),this.emit("rebuild",this),this},O.prototype.branchResultset=function(t,e){var i=this.resultset.branch();return void 0===t?i:i.transform(t,e)},O.prototype.toJSON=function(){var t=new O(this.collection,this.name,this.options);return t.resultset=this.resultset,t.resultdata=[],t.resultsdirty=!0,t.filterPipeline=this.filterPipeline,t.sortFunction=this.sortFunction,t.sortCriteria=this.sortCriteria,t.sortCriteriaSimple=this.sortCriteriaSimple||null,t.sortDirty=this.sortDirty,t.collection=null,t},O.prototype.removeFilters=function(t){t=t||{},this.rebuildPending=!1,this.resultset.reset(),this.resultdata=[],this.resultsdirty=!0,this.cachedresultset=null;var e=Object.isFrozen(this.filterPipeline),i=this.filterPipeline.length>0;this.filterPipeline=[],e&&Object.freeze(this.filterPipeline),this.sortFunction=null,this.sortCriteria=null,this.sortCriteriaSimple=null,this.sortDirty=!1,!0===t.queueSortPhase&&this.queueSortPhase(),i&&this.emit("filter")},O.prototype.applySort=function(t){return this.sortFunction=t,this.sortCriteria=null,this.sortCriteriaSimple=null,this.queueSortPhase(),this.emit("sort"),this},O.prototype.applySimpleSort=function(e,i){return this.sortCriteriaSimple={propname:e,options:i||!1},this.collection.disableFreeze||t(this.sortCriteriaSimple),this.sortCriteria=null,this.sortFunction=null,this.queueSortPhase(),this.emit("sort"),this},O.prototype.applySortCriteria=function(e){return this.sortCriteria=e,this.collection.disableFreeze||t(this.sortCriteria),this.sortCriteriaSimple=null,this.sortFunction=null,this.queueSortPhase(),this.emit("sort"),this},O.prototype.startTransaction=function(){return this.cachedresultset=this.resultset.copy(),this},O.prototype.commit=function(){return this.cachedresultset=null,this},O.prototype.rollback=function(){return this.resultset=this.cachedresultset,this.options.persistent&&(this.resultdata=this.resultset.data(),this.emit("rebuild",this)),this},O.prototype._indexOfFilterWithId=function(t){if("string"==typeof t||"number"==typeof t)for(var e=0,i=this.filterPipeline.length;e<i;e+=1)if(t===this.filterPipeline[e].uid)return e;return-1},O.prototype._addFilter=function(e){var i=Object.isFrozen(this.filterPipeline);i&&(this.filterPipeline=this.filterPipeline.slice()),this.collection.disableFreeze||t(e),this.filterPipeline.push(e),i&&Object.freeze(this.filterPipeline),this.resultset[e.type](e.val)},O.prototype.reapplyFilters=function(){this.resultset.reset(),this.cachedresultset=null,this.options.persistent&&(this.resultdata=[],this.resultsdirty=!0);var t=this.filterPipeline,e=Object.isFrozen(t);this.filterPipeline=[];for(var i=0,r=t.length;i<r;i+=1)this._addFilter(t[i]);return e&&Object.freeze(this.filterPipeline),this.sortFunction||this.sortCriteria||this.sortCriteriaSimple?this.queueSortPhase():this.queueRebuildEvent(),this.emit("filter"),this},O.prototype.applyFilter=function(t){var i=this._indexOfFilterWithId(t.uid);if(i>=0){var r=Object.isFrozen(this.filterPipeline);return r&&(this.filterPipeline=this.filterPipeline.slice()),this.filterPipeline[i]=t,r&&(e(t),Object.freeze(this.filterPipeline)),this.reapplyFilters()}return this.cachedresultset=null,this.options.persistent&&(this.resultdata=[],this.resultsdirty=!0),this._addFilter(t),this.sortFunction||this.sortCriteria||this.sortCriteriaSimple?this.queueSortPhase():this.queueRebuildEvent(),this.emit("filter"),this},O.prototype.applyFind=function(t,e){return this.applyFilter({type:"find",val:t,uid:e}),this},O.prototype.applyWhere=function(t,e){return this.applyFilter({type:"where",val:t,uid:e}),this},O.prototype.removeFilter=function(t){var e=this._indexOfFilterWithId(t);if(e<0)throw new Error("Dynamic view does not contain a filter with ID: "+t);var i=Object.isFrozen(this.filterPipeline);return i&&(this.filterPipeline=this.filterPipeline.slice()),this.filterPipeline.splice(e,1),i&&Object.freeze(this.filterPipeline),this.reapplyFilters(),this},O.prototype.count=function(){return this.resultsdirty&&(this.resultdata=this.resultset.data()),this.resultset.count()},O.prototype.data=function(t){return(this.sortDirty||this.resultsdirty)&&this.performSortPhase({suppressRebuildEvent:!0}),this.options.persistent?this.resultdata:this.resultset.data(t)},O.prototype.queueRebuildEvent=function(){if(!this.rebuildPending){this.rebuildPending=!0;var t=this;setTimeout(function(){t.rebuildPending&&(t.rebuildPending=!1,t.emit("rebuild",t))},this.options.minRebuildInterval)}},O.prototype.queueSortPhase=function(){if(!this.sortDirty){this.sortDirty=!0;var t=this;"active"===this.options.sortPriority?setTimeout(function(){t.performSortPhase()},this.options.minRebuildInterval):this.queueRebuildEvent()}},O.prototype.performSortPhase=function(t){(this.sortDirty||this.resultsdirty)&&(t=t||{},this.sortDirty&&(this.sortFunction?this.resultset.sort(this.sortFunction):this.sortCriteria?this.resultset.compoundsort(this.sortCriteria):this.sortCriteriaSimple&&this.resultset.simplesort(this.sortCriteriaSimple.propname,this.sortCriteriaSimple.options),this.sortDirty=!1),this.options.persistent&&(this.resultdata=this.resultset.data(),this.resultsdirty=!1),t.suppressRebuildEvent||this.emit("rebuild",this))},O.prototype.evaluateDocument=function(t,e){if(!this.resultset.filterInitialized)return this.options.persistent&&(this.resultdata=this.resultset.data()),void(this.sortFunction||this.sortCriteria||this.sortCriteriaSimple?this.queueSortPhase():this.queueRebuildEvent());var i=this.resultset.filteredrows,r=e?-1:i.indexOf(+t),n=i.length,s=new w(this.collection);s.filteredrows=[t],s.filterInitialized=!0;for(var o,a=0,l=this.filterPipeline.length;a<l;a++)o=this.filterPipeline[a],s[o.type](o.val);var h=0===s.filteredrows.length?-1:0;return-1!==r||-1!==h?-1===r&&-1!==h?(i.push(t),this.options.persistent&&this.resultdata.push(this.collection.data[t]),void(this.sortFunction||this.sortCriteria||this.sortCriteriaSimple?this.queueSortPhase():this.queueRebuildEvent())):-1!==r&&-1===h?(r<n-1?(i.splice(r,1),this.options.persistent&&this.resultdata.splice(r,1)):(i.length=n-1,this.options.persistent&&(this