index.js 30 KB

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