index.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. /*! *****************************************************************************
  2. Copyright (c) Microsoft Corporation.
  3. Permission to use, copy, modify, and/or distribute this software for any
  4. purpose with or without fee is hereby granted.
  5. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
  6. REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  7. AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
  8. INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  9. LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  10. OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  11. PERFORMANCE OF THIS SOFTWARE.
  12. ***************************************************************************** */
  13. /* global Reflect, Promise */
  14. var extendStatics = function(d, b) {
  15. extendStatics = Object.setPrototypeOf ||
  16. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  17. function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
  18. return extendStatics(d, b);
  19. };
  20. function __extends(d, b) {
  21. if (typeof b !== "function" && b !== null)
  22. throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  23. extendStatics(d, b);
  24. function __() { this.constructor = d; }
  25. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  26. }
  27. var typeChecker = function (type) {
  28. var typeString = "[object " + type + "]";
  29. return function (value) {
  30. return getClassName(value) === typeString;
  31. };
  32. };
  33. var getClassName = function (value) { return Object.prototype.toString.call(value); };
  34. var comparable = function (value) {
  35. if (value instanceof Date) {
  36. return value.getTime();
  37. }
  38. else if (isArray(value)) {
  39. return value.map(comparable);
  40. }
  41. else if (value && typeof value.toJSON === "function") {
  42. return value.toJSON();
  43. }
  44. return value;
  45. };
  46. var isArray = typeChecker("Array");
  47. var isObject = typeChecker("Object");
  48. var isFunction = typeChecker("Function");
  49. var isVanillaObject = function (value) {
  50. return (value &&
  51. (value.constructor === Object ||
  52. value.constructor === Array ||
  53. value.constructor.toString() === "function Object() { [native code] }" ||
  54. value.constructor.toString() === "function Array() { [native code] }") &&
  55. !value.toJSON);
  56. };
  57. var equals = function (a, b) {
  58. if (a == null && a == b) {
  59. return true;
  60. }
  61. if (a === b) {
  62. return true;
  63. }
  64. if (Object.prototype.toString.call(a) !== Object.prototype.toString.call(b)) {
  65. return false;
  66. }
  67. if (isArray(a)) {
  68. if (a.length !== b.length) {
  69. return false;
  70. }
  71. for (var i = 0, length_1 = a.length; i < length_1; i++) {
  72. if (!equals(a[i], b[i]))
  73. return false;
  74. }
  75. return true;
  76. }
  77. else if (isObject(a)) {
  78. if (Object.keys(a).length !== Object.keys(b).length) {
  79. return false;
  80. }
  81. for (var key in a) {
  82. if (!equals(a[key], b[key]))
  83. return false;
  84. }
  85. return true;
  86. }
  87. return false;
  88. };
  89. /**
  90. * Walks through each value given the context - used for nested operations. E.g:
  91. * { "person.address": { $eq: "blarg" }}
  92. */
  93. var walkKeyPathValues = function (item, keyPath, next, depth, key, owner) {
  94. var currentKey = keyPath[depth];
  95. // if array, then try matching. Might fall through for cases like:
  96. // { $eq: [1, 2, 3] }, [ 1, 2, 3 ].
  97. if (isArray(item) && isNaN(Number(currentKey))) {
  98. for (var i = 0, length_1 = item.length; i < length_1; i++) {
  99. // if FALSE is returned, then terminate walker. For operations, this simply
  100. // means that the search critera was met.
  101. if (!walkKeyPathValues(item[i], keyPath, next, depth, i, item)) {
  102. return false;
  103. }
  104. }
  105. }
  106. if (depth === keyPath.length || item == null) {
  107. return next(item, key, owner, depth === 0);
  108. }
  109. return walkKeyPathValues(item[currentKey], keyPath, next, depth + 1, currentKey, item);
  110. };
  111. var BaseOperation = /** @class */ (function () {
  112. function BaseOperation(params, owneryQuery, options, name) {
  113. this.params = params;
  114. this.owneryQuery = owneryQuery;
  115. this.options = options;
  116. this.name = name;
  117. this.init();
  118. }
  119. BaseOperation.prototype.init = function () { };
  120. BaseOperation.prototype.reset = function () {
  121. this.done = false;
  122. this.keep = false;
  123. };
  124. return BaseOperation;
  125. }());
  126. var GroupOperation = /** @class */ (function (_super) {
  127. __extends(GroupOperation, _super);
  128. function GroupOperation(params, owneryQuery, options, children) {
  129. var _this = _super.call(this, params, owneryQuery, options) || this;
  130. _this.children = children;
  131. return _this;
  132. }
  133. /**
  134. */
  135. GroupOperation.prototype.reset = function () {
  136. this.keep = false;
  137. this.done = false;
  138. for (var i = 0, length_2 = this.children.length; i < length_2; i++) {
  139. this.children[i].reset();
  140. }
  141. };
  142. /**
  143. */
  144. GroupOperation.prototype.childrenNext = function (item, key, owner, root) {
  145. var done = true;
  146. var keep = true;
  147. for (var i = 0, length_3 = this.children.length; i < length_3; i++) {
  148. var childOperation = this.children[i];
  149. if (!childOperation.done) {
  150. childOperation.next(item, key, owner, root);
  151. }
  152. if (!childOperation.keep) {
  153. keep = false;
  154. }
  155. if (childOperation.done) {
  156. if (!childOperation.keep) {
  157. break;
  158. }
  159. }
  160. else {
  161. done = false;
  162. }
  163. }
  164. this.done = done;
  165. this.keep = keep;
  166. };
  167. return GroupOperation;
  168. }(BaseOperation));
  169. var NamedGroupOperation = /** @class */ (function (_super) {
  170. __extends(NamedGroupOperation, _super);
  171. function NamedGroupOperation(params, owneryQuery, options, children, name) {
  172. var _this = _super.call(this, params, owneryQuery, options, children) || this;
  173. _this.name = name;
  174. return _this;
  175. }
  176. return NamedGroupOperation;
  177. }(GroupOperation));
  178. var QueryOperation = /** @class */ (function (_super) {
  179. __extends(QueryOperation, _super);
  180. function QueryOperation() {
  181. var _this = _super !== null && _super.apply(this, arguments) || this;
  182. _this.propop = true;
  183. return _this;
  184. }
  185. /**
  186. */
  187. QueryOperation.prototype.next = function (item, key, parent, root) {
  188. this.childrenNext(item, key, parent, root);
  189. };
  190. return QueryOperation;
  191. }(GroupOperation));
  192. var NestedOperation = /** @class */ (function (_super) {
  193. __extends(NestedOperation, _super);
  194. function NestedOperation(keyPath, params, owneryQuery, options, children) {
  195. var _this = _super.call(this, params, owneryQuery, options, children) || this;
  196. _this.keyPath = keyPath;
  197. _this.propop = true;
  198. /**
  199. */
  200. _this._nextNestedValue = function (value, key, owner, root) {
  201. _this.childrenNext(value, key, owner, root);
  202. return !_this.done;
  203. };
  204. return _this;
  205. }
  206. /**
  207. */
  208. NestedOperation.prototype.next = function (item, key, parent) {
  209. walkKeyPathValues(item, this.keyPath, this._nextNestedValue, 0, key, parent);
  210. };
  211. return NestedOperation;
  212. }(GroupOperation));
  213. var createTester = function (a, compare) {
  214. if (a instanceof Function) {
  215. return a;
  216. }
  217. if (a instanceof RegExp) {
  218. return function (b) {
  219. var result = typeof b === "string" && a.test(b);
  220. a.lastIndex = 0;
  221. return result;
  222. };
  223. }
  224. var comparableA = comparable(a);
  225. return function (b) { return compare(comparableA, comparable(b)); };
  226. };
  227. var EqualsOperation = /** @class */ (function (_super) {
  228. __extends(EqualsOperation, _super);
  229. function EqualsOperation() {
  230. var _this = _super !== null && _super.apply(this, arguments) || this;
  231. _this.propop = true;
  232. return _this;
  233. }
  234. EqualsOperation.prototype.init = function () {
  235. this._test = createTester(this.params, this.options.compare);
  236. };
  237. EqualsOperation.prototype.next = function (item, key, parent) {
  238. if (!Array.isArray(parent) || parent.hasOwnProperty(key)) {
  239. if (this._test(item, key, parent)) {
  240. this.done = true;
  241. this.keep = true;
  242. }
  243. }
  244. };
  245. return EqualsOperation;
  246. }(BaseOperation));
  247. var createEqualsOperation = function (params, owneryQuery, options) { return new EqualsOperation(params, owneryQuery, options); };
  248. var NopeOperation = /** @class */ (function (_super) {
  249. __extends(NopeOperation, _super);
  250. function NopeOperation() {
  251. var _this = _super !== null && _super.apply(this, arguments) || this;
  252. _this.propop = true;
  253. return _this;
  254. }
  255. NopeOperation.prototype.next = function () {
  256. this.done = true;
  257. this.keep = false;
  258. };
  259. return NopeOperation;
  260. }(BaseOperation));
  261. var numericalOperationCreator = function (createNumericalOperation) { return function (params, owneryQuery, options, name) {
  262. if (params == null) {
  263. return new NopeOperation(params, owneryQuery, options, name);
  264. }
  265. return createNumericalOperation(params, owneryQuery, options, name);
  266. }; };
  267. var numericalOperation = function (createTester) {
  268. return numericalOperationCreator(function (params, owneryQuery, options, name) {
  269. var typeofParams = typeof comparable(params);
  270. var test = createTester(params);
  271. return new EqualsOperation(function (b) {
  272. return typeof comparable(b) === typeofParams && test(b);
  273. }, owneryQuery, options, name);
  274. });
  275. };
  276. var createNamedOperation = function (name, params, parentQuery, options) {
  277. var operationCreator = options.operations[name];
  278. if (!operationCreator) {
  279. throwUnsupportedOperation(name);
  280. }
  281. return operationCreator(params, parentQuery, options, name);
  282. };
  283. var throwUnsupportedOperation = function (name) {
  284. throw new Error("Unsupported operation: " + name);
  285. };
  286. var containsOperation = function (query, options) {
  287. for (var key in query) {
  288. if (options.operations.hasOwnProperty(key) || key.charAt(0) === "$")
  289. return true;
  290. }
  291. return false;
  292. };
  293. var createNestedOperation = function (keyPath, nestedQuery, parentKey, owneryQuery, options) {
  294. if (containsOperation(nestedQuery, options)) {
  295. var _a = createQueryOperations(nestedQuery, parentKey, options), selfOperations = _a[0], nestedOperations = _a[1];
  296. if (nestedOperations.length) {
  297. throw new Error("Property queries must contain only operations, or exact objects.");
  298. }
  299. return new NestedOperation(keyPath, nestedQuery, owneryQuery, options, selfOperations);
  300. }
  301. return new NestedOperation(keyPath, nestedQuery, owneryQuery, options, [
  302. new EqualsOperation(nestedQuery, owneryQuery, options)
  303. ]);
  304. };
  305. var createQueryOperation = function (query, owneryQuery, _a) {
  306. if (owneryQuery === void 0) { owneryQuery = null; }
  307. var _b = _a === void 0 ? {} : _a, compare = _b.compare, operations = _b.operations;
  308. var options = {
  309. compare: compare || equals,
  310. operations: Object.assign({}, operations || {})
  311. };
  312. var _c = createQueryOperations(query, null, options), selfOperations = _c[0], nestedOperations = _c[1];
  313. var ops = [];
  314. if (selfOperations.length) {
  315. ops.push(new NestedOperation([], query, owneryQuery, options, selfOperations));
  316. }
  317. ops.push.apply(ops, nestedOperations);
  318. if (ops.length === 1) {
  319. return ops[0];
  320. }
  321. return new QueryOperation(query, owneryQuery, options, ops);
  322. };
  323. var createQueryOperations = function (query, parentKey, options) {
  324. var selfOperations = [];
  325. var nestedOperations = [];
  326. if (!isVanillaObject(query)) {
  327. selfOperations.push(new EqualsOperation(query, query, options));
  328. return [selfOperations, nestedOperations];
  329. }
  330. for (var key in query) {
  331. if (options.operations.hasOwnProperty(key)) {
  332. var op = createNamedOperation(key, query[key], query, options);
  333. if (op) {
  334. if (!op.propop && parentKey && !options.operations[parentKey]) {
  335. throw new Error("Malformed query. " + key + " cannot be matched against property.");
  336. }
  337. }
  338. // probably just a flag for another operation (like $options)
  339. if (op != null) {
  340. selfOperations.push(op);
  341. }
  342. }
  343. else if (key.charAt(0) === "$") {
  344. throwUnsupportedOperation(key);
  345. }
  346. else {
  347. nestedOperations.push(createNestedOperation(key.split("."), query[key], key, query, options));
  348. }
  349. }
  350. return [selfOperations, nestedOperations];
  351. };
  352. var createOperationTester = function (operation) { return function (item, key, owner) {
  353. operation.reset();
  354. operation.next(item, key, owner);
  355. return operation.keep;
  356. }; };
  357. var createQueryTester = function (query, options) {
  358. if (options === void 0) { options = {}; }
  359. return createOperationTester(createQueryOperation(query, null, options));
  360. };
  361. var $Ne = /** @class */ (function (_super) {
  362. __extends($Ne, _super);
  363. function $Ne() {
  364. var _this = _super !== null && _super.apply(this, arguments) || this;
  365. _this.propop = true;
  366. return _this;
  367. }
  368. $Ne.prototype.init = function () {
  369. this._test = createTester(this.params, this.options.compare);
  370. };
  371. $Ne.prototype.reset = function () {
  372. _super.prototype.reset.call(this);
  373. this.keep = true;
  374. };
  375. $Ne.prototype.next = function (item) {
  376. if (this._test(item)) {
  377. this.done = true;
  378. this.keep = false;
  379. }
  380. };
  381. return $Ne;
  382. }(BaseOperation));
  383. // https://docs.mongodb.com/manual/reference/operator/query/elemMatch/
  384. var $ElemMatch = /** @class */ (function (_super) {
  385. __extends($ElemMatch, _super);
  386. function $ElemMatch() {
  387. var _this = _super !== null && _super.apply(this, arguments) || this;
  388. _this.propop = true;
  389. return _this;
  390. }
  391. $ElemMatch.prototype.init = function () {
  392. if (!this.params || typeof this.params !== "object") {
  393. throw new Error("Malformed query. $elemMatch must by an object.");
  394. }
  395. this._queryOperation = createQueryOperation(this.params, this.owneryQuery, this.options);
  396. };
  397. $ElemMatch.prototype.reset = function () {
  398. _super.prototype.reset.call(this);
  399. this._queryOperation.reset();
  400. };
  401. $ElemMatch.prototype.next = function (item) {
  402. if (isArray(item)) {
  403. for (var i = 0, length_1 = item.length; i < length_1; i++) {
  404. // reset query operation since item being tested needs to pass _all_ query
  405. // operations for it to be a success
  406. this._queryOperation.reset();
  407. var child = item[i];
  408. this._queryOperation.next(child, i, item, false);
  409. this.keep = this.keep || this._queryOperation.keep;
  410. }
  411. this.done = true;
  412. }
  413. else {
  414. this.done = false;
  415. this.keep = false;
  416. }
  417. };
  418. return $ElemMatch;
  419. }(BaseOperation));
  420. var $Not = /** @class */ (function (_super) {
  421. __extends($Not, _super);
  422. function $Not() {
  423. var _this = _super !== null && _super.apply(this, arguments) || this;
  424. _this.propop = true;
  425. return _this;
  426. }
  427. $Not.prototype.init = function () {
  428. this._queryOperation = createQueryOperation(this.params, this.owneryQuery, this.options);
  429. };
  430. $Not.prototype.reset = function () {
  431. _super.prototype.reset.call(this);
  432. this._queryOperation.reset();
  433. };
  434. $Not.prototype.next = function (item, key, owner, root) {
  435. this._queryOperation.next(item, key, owner, root);
  436. this.done = this._queryOperation.done;
  437. this.keep = !this._queryOperation.keep;
  438. };
  439. return $Not;
  440. }(BaseOperation));
  441. var $Size = /** @class */ (function (_super) {
  442. __extends($Size, _super);
  443. function $Size() {
  444. var _this = _super !== null && _super.apply(this, arguments) || this;
  445. _this.propop = true;
  446. return _this;
  447. }
  448. $Size.prototype.init = function () { };
  449. $Size.prototype.next = function (item) {
  450. if (isArray(item) && item.length === this.params) {
  451. this.done = true;
  452. this.keep = true;
  453. }
  454. // if (parent && parent.length === this.params) {
  455. // this.done = true;
  456. // this.keep = true;
  457. // }
  458. };
  459. return $Size;
  460. }(BaseOperation));
  461. var assertGroupNotEmpty = function (values) {
  462. if (values.length === 0) {
  463. throw new Error("$and/$or/$nor must be a nonempty array");
  464. }
  465. };
  466. var $Or = /** @class */ (function (_super) {
  467. __extends($Or, _super);
  468. function $Or() {
  469. var _this = _super !== null && _super.apply(this, arguments) || this;
  470. _this.propop = false;
  471. return _this;
  472. }
  473. $Or.prototype.init = function () {
  474. var _this = this;
  475. assertGroupNotEmpty(this.params);
  476. this._ops = this.params.map(function (op) {
  477. return createQueryOperation(op, null, _this.options);
  478. });
  479. };
  480. $Or.prototype.reset = function () {
  481. this.done = false;
  482. this.keep = false;
  483. for (var i = 0, length_2 = this._ops.length; i < length_2; i++) {
  484. this._ops[i].reset();
  485. }
  486. };
  487. $Or.prototype.next = function (item, key, owner) {
  488. var done = false;
  489. var success = false;
  490. for (var i = 0, length_3 = this._ops.length; i < length_3; i++) {
  491. var op = this._ops[i];
  492. op.next(item, key, owner);
  493. if (op.keep) {
  494. done = true;
  495. success = op.keep;
  496. break;
  497. }
  498. }
  499. this.keep = success;
  500. this.done = done;
  501. };
  502. return $Or;
  503. }(BaseOperation));
  504. var $Nor = /** @class */ (function (_super) {
  505. __extends($Nor, _super);
  506. function $Nor() {
  507. var _this = _super !== null && _super.apply(this, arguments) || this;
  508. _this.propop = false;
  509. return _this;
  510. }
  511. $Nor.prototype.next = function (item, key, owner) {
  512. _super.prototype.next.call(this, item, key, owner);
  513. this.keep = !this.keep;
  514. };
  515. return $Nor;
  516. }($Or));
  517. var $In = /** @class */ (function (_super) {
  518. __extends($In, _super);
  519. function $In() {
  520. var _this = _super !== null && _super.apply(this, arguments) || this;
  521. _this.propop = true;
  522. return _this;
  523. }
  524. $In.prototype.init = function () {
  525. var _this = this;
  526. this._testers = this.params.map(function (value) {
  527. if (containsOperation(value, _this.options)) {
  528. throw new Error("cannot nest $ under " + _this.name.toLowerCase());
  529. }
  530. return createTester(value, _this.options.compare);
  531. });
  532. };
  533. $In.prototype.next = function (item, key, owner) {
  534. var done = false;
  535. var success = false;
  536. for (var i = 0, length_4 = this._testers.length; i < length_4; i++) {
  537. var test = this._testers[i];
  538. if (test(item)) {
  539. done = true;
  540. success = true;
  541. break;
  542. }
  543. }
  544. this.keep = success;
  545. this.done = done;
  546. };
  547. return $In;
  548. }(BaseOperation));
  549. var $Nin = /** @class */ (function (_super) {
  550. __extends($Nin, _super);
  551. function $Nin(params, ownerQuery, options, name) {
  552. var _this = _super.call(this, params, ownerQuery, options, name) || this;
  553. _this.propop = true;
  554. _this._in = new $In(params, ownerQuery, options, name);
  555. return _this;
  556. }
  557. $Nin.prototype.next = function (item, key, owner, root) {
  558. this._in.next(item, key, owner);
  559. if (isArray(owner) && !root) {
  560. if (this._in.keep) {
  561. this.keep = false;
  562. this.done = true;
  563. }
  564. else if (key == owner.length - 1) {
  565. this.keep = true;
  566. this.done = true;
  567. }
  568. }
  569. else {
  570. this.keep = !this._in.keep;
  571. this.done = true;
  572. }
  573. };
  574. $Nin.prototype.reset = function () {
  575. _super.prototype.reset.call(this);
  576. this._in.reset();
  577. };
  578. return $Nin;
  579. }(BaseOperation));
  580. var $Exists = /** @class */ (function (_super) {
  581. __extends($Exists, _super);
  582. function $Exists() {
  583. var _this = _super !== null && _super.apply(this, arguments) || this;
  584. _this.propop = true;
  585. return _this;
  586. }
  587. $Exists.prototype.next = function (item, key, owner) {
  588. if (owner.hasOwnProperty(key) === this.params) {
  589. this.done = true;
  590. this.keep = true;
  591. }
  592. };
  593. return $Exists;
  594. }(BaseOperation));
  595. var $And = /** @class */ (function (_super) {
  596. __extends($And, _super);
  597. function $And(params, owneryQuery, options, name) {
  598. var _this = _super.call(this, params, owneryQuery, options, params.map(function (query) { return createQueryOperation(query, owneryQuery, options); }), name) || this;
  599. _this.propop = false;
  600. assertGroupNotEmpty(params);
  601. return _this;
  602. }
  603. $And.prototype.next = function (item, key, owner, root) {
  604. this.childrenNext(item, key, owner, root);
  605. };
  606. return $And;
  607. }(NamedGroupOperation));
  608. var $All = /** @class */ (function (_super) {
  609. __extends($All, _super);
  610. function $All(params, owneryQuery, options, name) {
  611. var _this = _super.call(this, params, owneryQuery, options, params.map(function (query) { return createQueryOperation(query, owneryQuery, options); }), name) || this;
  612. _this.propop = true;
  613. return _this;
  614. }
  615. $All.prototype.next = function (item, key, owner, root) {
  616. this.childrenNext(item, key, owner, root);
  617. };
  618. return $All;
  619. }(NamedGroupOperation));
  620. var $eq = function (params, owneryQuery, options) {
  621. return new EqualsOperation(params, owneryQuery, options);
  622. };
  623. var $ne = function (params, owneryQuery, options, name) { return new $Ne(params, owneryQuery, options, name); };
  624. var $or = function (params, owneryQuery, options, name) { return new $Or(params, owneryQuery, options, name); };
  625. var $nor = function (params, owneryQuery, options, name) { return new $Nor(params, owneryQuery, options, name); };
  626. var $elemMatch = function (params, owneryQuery, options, name) { return new $ElemMatch(params, owneryQuery, options, name); };
  627. var $nin = function (params, owneryQuery, options, name) { return new $Nin(params, owneryQuery, options, name); };
  628. var $in = function (params, owneryQuery, options, name) {
  629. return new $In(params, owneryQuery, options, name);
  630. };
  631. var $lt = numericalOperation(function (params) { return function (b) { return b < params; }; });
  632. var $lte = numericalOperation(function (params) { return function (b) { return b <= params; }; });
  633. var $gt = numericalOperation(function (params) { return function (b) { return b > params; }; });
  634. var $gte = numericalOperation(function (params) { return function (b) { return b >= params; }; });
  635. var $mod = function (_a, owneryQuery, options) {
  636. var mod = _a[0], equalsValue = _a[1];
  637. return new EqualsOperation(function (b) { return comparable(b) % mod === equalsValue; }, owneryQuery, options);
  638. };
  639. var $exists = function (params, owneryQuery, options, name) { return new $Exists(params, owneryQuery, options, name); };
  640. var $regex = function (pattern, owneryQuery, options) {
  641. return new EqualsOperation(new RegExp(pattern, owneryQuery.$options), owneryQuery, options);
  642. };
  643. var $not = function (params, owneryQuery, options, name) { return new $Not(params, owneryQuery, options, name); };
  644. var typeAliases = {
  645. number: function (v) { return typeof v === "number"; },
  646. string: function (v) { return typeof v === "string"; },
  647. bool: function (v) { return typeof v === "boolean"; },
  648. array: function (v) { return Array.isArray(v); },
  649. null: function (v) { return v === null; },
  650. timestamp: function (v) { return v instanceof Date; }
  651. };
  652. var $type = function (clazz, owneryQuery, options) {
  653. return new EqualsOperation(function (b) {
  654. if (typeof clazz === "string") {
  655. if (!typeAliases[clazz]) {
  656. throw new Error("Type alias does not exist");
  657. }
  658. return typeAliases[clazz](b);
  659. }
  660. return b != null ? b instanceof clazz || b.constructor === clazz : false;
  661. }, owneryQuery, options);
  662. };
  663. var $and = function (params, ownerQuery, options, name) { return new $And(params, ownerQuery, options, name); };
  664. var $all = function (params, ownerQuery, options, name) { return new $All(params, ownerQuery, options, name); };
  665. var $size = function (params, ownerQuery, options) { return new $Size(params, ownerQuery, options, "$size"); };
  666. var $options = function () { return null; };
  667. var $where = function (params, ownerQuery, options) {
  668. var test;
  669. if (isFunction(params)) {
  670. test = params;
  671. }
  672. else if (!process.env.CSP_ENABLED) {
  673. test = new Function("obj", "return " + params);
  674. }
  675. else {
  676. throw new Error("In CSP mode, sift does not support strings in \"$where\" condition");
  677. }
  678. return new EqualsOperation(function (b) { return test.bind(b)(b); }, ownerQuery, options);
  679. };
  680. var defaultOperations = /*#__PURE__*/Object.freeze({
  681. __proto__: null,
  682. $Size: $Size,
  683. $eq: $eq,
  684. $ne: $ne,
  685. $or: $or,
  686. $nor: $nor,
  687. $elemMatch: $elemMatch,
  688. $nin: $nin,
  689. $in: $in,
  690. $lt: $lt,
  691. $lte: $lte,
  692. $gt: $gt,
  693. $gte: $gte,
  694. $mod: $mod,
  695. $exists: $exists,
  696. $regex: $regex,
  697. $not: $not,
  698. $type: $type,
  699. $and: $and,
  700. $all: $all,
  701. $size: $size,
  702. $options: $options,
  703. $where: $where
  704. });
  705. var createDefaultQueryOperation = function (query, ownerQuery, _a) {
  706. var _b = _a === void 0 ? {} : _a, compare = _b.compare, operations = _b.operations;
  707. return createQueryOperation(query, ownerQuery, {
  708. compare: compare,
  709. operations: Object.assign({}, defaultOperations, operations || {})
  710. });
  711. };
  712. var createDefaultQueryTester = function (query, options) {
  713. if (options === void 0) { options = {}; }
  714. var op = createDefaultQueryOperation(query, null, options);
  715. return createOperationTester(op);
  716. };
  717. export default createDefaultQueryTester;
  718. export { $Size, $all, $and, $elemMatch, $eq, $exists, $gt, $gte, $in, $lt, $lte, $mod, $ne, $nin, $nor, $not, $options, $or, $regex, $size, $type, $where, EqualsOperation, createDefaultQueryOperation, createEqualsOperation, createOperationTester, createQueryOperation, createQueryTester };
  719. //# sourceMappingURL=index.js.map