index.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. const typeChecker = (type) => {
  2. const typeString = "[object " + type + "]";
  3. return function (value) {
  4. return getClassName(value) === typeString;
  5. };
  6. };
  7. const getClassName = value => Object.prototype.toString.call(value);
  8. const comparable = (value) => {
  9. if (value instanceof Date) {
  10. return value.getTime();
  11. }
  12. else if (isArray(value)) {
  13. return value.map(comparable);
  14. }
  15. else if (value && typeof value.toJSON === "function") {
  16. return value.toJSON();
  17. }
  18. return value;
  19. };
  20. const isArray = typeChecker("Array");
  21. const isObject = typeChecker("Object");
  22. const isFunction = typeChecker("Function");
  23. const isVanillaObject = value => {
  24. return (value &&
  25. (value.constructor === Object ||
  26. value.constructor === Array ||
  27. value.constructor.toString() === "function Object() { [native code] }" ||
  28. value.constructor.toString() === "function Array() { [native code] }") &&
  29. !value.toJSON);
  30. };
  31. const equals = (a, b) => {
  32. if (a == null && a == b) {
  33. return true;
  34. }
  35. if (a === b) {
  36. return true;
  37. }
  38. if (Object.prototype.toString.call(a) !== Object.prototype.toString.call(b)) {
  39. return false;
  40. }
  41. if (isArray(a)) {
  42. if (a.length !== b.length) {
  43. return false;
  44. }
  45. for (let i = 0, { length } = a; i < length; i++) {
  46. if (!equals(a[i], b[i]))
  47. return false;
  48. }
  49. return true;
  50. }
  51. else if (isObject(a)) {
  52. if (Object.keys(a).length !== Object.keys(b).length) {
  53. return false;
  54. }
  55. for (const key in a) {
  56. if (!equals(a[key], b[key]))
  57. return false;
  58. }
  59. return true;
  60. }
  61. return false;
  62. };
  63. /**
  64. * Walks through each value given the context - used for nested operations. E.g:
  65. * { "person.address": { $eq: "blarg" }}
  66. */
  67. const walkKeyPathValues = (item, keyPath, next, depth, key, owner) => {
  68. const currentKey = keyPath[depth];
  69. // if array, then try matching. Might fall through for cases like:
  70. // { $eq: [1, 2, 3] }, [ 1, 2, 3 ].
  71. if (isArray(item) && isNaN(Number(currentKey))) {
  72. for (let i = 0, { length } = item; i < length; i++) {
  73. // if FALSE is returned, then terminate walker. For operations, this simply
  74. // means that the search critera was met.
  75. if (!walkKeyPathValues(item[i], keyPath, next, depth, i, item)) {
  76. return false;
  77. }
  78. }
  79. }
  80. if (depth === keyPath.length || item == null) {
  81. return next(item, key, owner);
  82. }
  83. return walkKeyPathValues(item[currentKey], keyPath, next, depth + 1, currentKey, item);
  84. };
  85. class BaseOperation {
  86. constructor(params, owneryQuery, options) {
  87. this.params = params;
  88. this.owneryQuery = owneryQuery;
  89. this.options = options;
  90. this.init();
  91. }
  92. init() { }
  93. reset() {
  94. this.done = false;
  95. this.keep = false;
  96. }
  97. }
  98. class NamedBaseOperation extends BaseOperation {
  99. constructor(params, owneryQuery, options, name) {
  100. super(params, owneryQuery, options);
  101. this.name = name;
  102. }
  103. }
  104. class GroupOperation extends BaseOperation {
  105. constructor(params, owneryQuery, options, children) {
  106. super(params, owneryQuery, options);
  107. this.children = children;
  108. }
  109. /**
  110. */
  111. reset() {
  112. this.keep = false;
  113. this.done = false;
  114. for (let i = 0, { length } = this.children; i < length; i++) {
  115. this.children[i].reset();
  116. }
  117. }
  118. /**
  119. */
  120. childrenNext(item, key, owner) {
  121. let done = true;
  122. let keep = true;
  123. for (let i = 0, { length } = this.children; i < length; i++) {
  124. const childOperation = this.children[i];
  125. childOperation.next(item, key, owner);
  126. if (!childOperation.keep) {
  127. keep = false;
  128. }
  129. if (childOperation.done) {
  130. if (!childOperation.keep) {
  131. break;
  132. }
  133. }
  134. else {
  135. done = false;
  136. }
  137. }
  138. this.done = done;
  139. this.keep = keep;
  140. }
  141. }
  142. class NamedGroupOperation extends GroupOperation {
  143. constructor(params, owneryQuery, options, children, name) {
  144. super(params, owneryQuery, options, children);
  145. this.name = name;
  146. }
  147. }
  148. class QueryOperation extends GroupOperation {
  149. /**
  150. */
  151. next(item, key, parent) {
  152. this.childrenNext(item, key, parent);
  153. }
  154. }
  155. class NestedOperation extends GroupOperation {
  156. constructor(keyPath, params, owneryQuery, options, children) {
  157. super(params, owneryQuery, options, children);
  158. this.keyPath = keyPath;
  159. /**
  160. */
  161. this._nextNestedValue = (value, key, owner) => {
  162. this.childrenNext(value, key, owner);
  163. return !this.done;
  164. };
  165. }
  166. /**
  167. */
  168. next(item, key, parent) {
  169. walkKeyPathValues(item, this.keyPath, this._nextNestedValue, 0, key, parent);
  170. }
  171. }
  172. const createTester = (a, compare) => {
  173. if (a instanceof Function) {
  174. return a;
  175. }
  176. if (a instanceof RegExp) {
  177. return b => {
  178. const result = typeof b === "string" && a.test(b);
  179. a.lastIndex = 0;
  180. return result;
  181. };
  182. }
  183. const comparableA = comparable(a);
  184. return b => compare(comparableA, comparable(b));
  185. };
  186. class EqualsOperation extends BaseOperation {
  187. init() {
  188. this._test = createTester(this.params, this.options.compare);
  189. }
  190. next(item, key, parent) {
  191. if (!Array.isArray(parent) || parent.hasOwnProperty(key)) {
  192. if (this._test(item, key, parent)) {
  193. this.done = true;
  194. this.keep = true;
  195. }
  196. }
  197. }
  198. }
  199. const createEqualsOperation = (params, owneryQuery, options) => new EqualsOperation(params, owneryQuery, options);
  200. class NopeOperation extends BaseOperation {
  201. next() {
  202. this.done = true;
  203. this.keep = false;
  204. }
  205. }
  206. const numericalOperationCreator = (createNumericalOperation) => (params, owneryQuery, options, name) => {
  207. if (params == null) {
  208. return new NopeOperation(params, owneryQuery, options);
  209. }
  210. return createNumericalOperation(params, owneryQuery, options, name);
  211. };
  212. const numericalOperation = (createTester) => numericalOperationCreator((params, owneryQuery, options) => {
  213. const typeofParams = typeof comparable(params);
  214. const test = createTester(params);
  215. return new EqualsOperation(b => {
  216. return typeof comparable(b) === typeofParams && test(b);
  217. }, owneryQuery, options);
  218. });
  219. const createNamedOperation = (name, params, parentQuery, options) => {
  220. const operationCreator = options.operations[name];
  221. if (!operationCreator) {
  222. throw new Error(`Unsupported operation: ${name}`);
  223. }
  224. return operationCreator(params, parentQuery, options, name);
  225. };
  226. const containsOperation = (query) => {
  227. for (const key in query) {
  228. if (key.charAt(0) === "$")
  229. return true;
  230. }
  231. return false;
  232. };
  233. const createNestedOperation = (keyPath, nestedQuery, owneryQuery, options) => {
  234. if (containsOperation(nestedQuery)) {
  235. const [selfOperations, nestedOperations] = createQueryOperations(nestedQuery, options);
  236. if (nestedOperations.length) {
  237. throw new Error(`Property queries must contain only operations, or exact objects.`);
  238. }
  239. return new NestedOperation(keyPath, nestedQuery, owneryQuery, options, selfOperations);
  240. }
  241. return new NestedOperation(keyPath, nestedQuery, owneryQuery, options, [
  242. new EqualsOperation(nestedQuery, owneryQuery, options)
  243. ]);
  244. };
  245. const createQueryOperation = (query, owneryQuery = null, { compare, operations } = {}) => {
  246. const options = {
  247. compare: compare || equals,
  248. operations: Object.assign({}, operations || {})
  249. };
  250. const [selfOperations, nestedOperations] = createQueryOperations(query, options);
  251. const ops = [];
  252. if (selfOperations.length) {
  253. ops.push(new NestedOperation([], query, owneryQuery, options, selfOperations));
  254. }
  255. ops.push(...nestedOperations);
  256. if (ops.length === 1) {
  257. return ops[0];
  258. }
  259. return new QueryOperation(query, owneryQuery, options, ops);
  260. };
  261. const createQueryOperations = (query, options) => {
  262. const selfOperations = [];
  263. const nestedOperations = [];
  264. if (!isVanillaObject(query)) {
  265. selfOperations.push(new EqualsOperation(query, query, options));
  266. return [selfOperations, nestedOperations];
  267. }
  268. for (const key in query) {
  269. if (key.charAt(0) === "$") {
  270. const op = createNamedOperation(key, query[key], query, options);
  271. // probably just a flag for another operation (like $options)
  272. if (op != null) {
  273. selfOperations.push(op);
  274. }
  275. }
  276. else {
  277. nestedOperations.push(createNestedOperation(key.split("."), query[key], query, options));
  278. }
  279. }
  280. return [selfOperations, nestedOperations];
  281. };
  282. const createOperationTester = (operation) => (item, key, owner) => {
  283. operation.reset();
  284. operation.next(item, key, owner);
  285. return operation.keep;
  286. };
  287. const createQueryTester = (query, options = {}) => {
  288. return createOperationTester(createQueryOperation(query, null, options));
  289. };
  290. class $Ne extends NamedBaseOperation {
  291. init() {
  292. this._test = createTester(this.params, this.options.compare);
  293. }
  294. reset() {
  295. super.reset();
  296. this.keep = true;
  297. }
  298. next(item) {
  299. if (this._test(item)) {
  300. this.done = true;
  301. this.keep = false;
  302. }
  303. }
  304. }
  305. // https://docs.mongodb.com/manual/reference/operator/query/elemMatch/
  306. class $ElemMatch extends NamedBaseOperation {
  307. init() {
  308. this._queryOperation = createQueryOperation(this.params, this.owneryQuery, this.options);
  309. }
  310. reset() {
  311. super.reset();
  312. this._queryOperation.reset();
  313. }
  314. next(item) {
  315. if (isArray(item)) {
  316. for (let i = 0, { length } = item; i < length; i++) {
  317. // reset query operation since item being tested needs to pass _all_ query
  318. // operations for it to be a success
  319. this._queryOperation.reset();
  320. // check item
  321. this._queryOperation.next(item[i], i, item);
  322. this.keep = this.keep || this._queryOperation.keep;
  323. }
  324. this.done = true;
  325. }
  326. else {
  327. this.done = false;
  328. this.keep = false;
  329. }
  330. }
  331. }
  332. class $Not extends NamedBaseOperation {
  333. init() {
  334. this._queryOperation = createQueryOperation(this.params, this.owneryQuery, this.options);
  335. }
  336. reset() {
  337. this._queryOperation.reset();
  338. }
  339. next(item, key, owner) {
  340. this._queryOperation.next(item, key, owner);
  341. this.done = this._queryOperation.done;
  342. this.keep = !this._queryOperation.keep;
  343. }
  344. }
  345. class $Size extends NamedBaseOperation {
  346. init() { }
  347. next(item) {
  348. if (isArray(item) && item.length === this.params) {
  349. this.done = true;
  350. this.keep = true;
  351. }
  352. // if (parent && parent.length === this.params) {
  353. // this.done = true;
  354. // this.keep = true;
  355. // }
  356. }
  357. }
  358. class $Or extends NamedBaseOperation {
  359. init() {
  360. this._ops = this.params.map(op => createQueryOperation(op, null, this.options));
  361. }
  362. reset() {
  363. this.done = false;
  364. this.keep = false;
  365. for (let i = 0, { length } = this._ops; i < length; i++) {
  366. this._ops[i].reset();
  367. }
  368. }
  369. next(item, key, owner) {
  370. let done = false;
  371. let success = false;
  372. for (let i = 0, { length } = this._ops; i < length; i++) {
  373. const op = this._ops[i];
  374. op.next(item, key, owner);
  375. if (op.keep) {
  376. done = true;
  377. success = op.keep;
  378. break;
  379. }
  380. }
  381. this.keep = success;
  382. this.done = done;
  383. }
  384. }
  385. class $Nor extends $Or {
  386. next(item, key, owner) {
  387. super.next(item, key, owner);
  388. this.keep = !this.keep;
  389. }
  390. }
  391. class $In extends NamedBaseOperation {
  392. init() {
  393. this._testers = this.params.map(value => {
  394. if (containsOperation(value)) {
  395. throw new Error(`cannot nest $ under ${this.constructor.name.toLowerCase()}`);
  396. }
  397. return createTester(value, this.options.compare);
  398. });
  399. }
  400. next(item, key, owner) {
  401. let done = false;
  402. let success = false;
  403. for (let i = 0, { length } = this._testers; i < length; i++) {
  404. const test = this._testers[i];
  405. if (test(item)) {
  406. done = true;
  407. success = true;
  408. break;
  409. }
  410. }
  411. this.keep = success;
  412. this.done = done;
  413. }
  414. }
  415. class $Nin extends $In {
  416. next(item, key, owner) {
  417. super.next(item, key, owner);
  418. this.keep = !this.keep;
  419. }
  420. }
  421. class $Exists extends NamedBaseOperation {
  422. next(item, key, owner) {
  423. if (owner.hasOwnProperty(key) === this.params) {
  424. this.done = true;
  425. this.keep = true;
  426. }
  427. }
  428. }
  429. class $And extends NamedGroupOperation {
  430. constructor(params, owneryQuery, options, name) {
  431. super(params, owneryQuery, options, params.map(query => createQueryOperation(query, owneryQuery, options)), name);
  432. }
  433. next(item, key, owner) {
  434. this.childrenNext(item, key, owner);
  435. }
  436. }
  437. const $eq = (params, owneryQuery, options) => new EqualsOperation(params, owneryQuery, options);
  438. const $ne = (params, owneryQuery, options, name) => new $Ne(params, owneryQuery, options, name);
  439. const $or = (params, owneryQuery, options, name) => new $Or(params, owneryQuery, options, name);
  440. const $nor = (params, owneryQuery, options, name) => new $Nor(params, owneryQuery, options, name);
  441. const $elemMatch = (params, owneryQuery, options, name) => new $ElemMatch(params, owneryQuery, options, name);
  442. const $nin = (params, owneryQuery, options, name) => new $Nin(params, owneryQuery, options, name);
  443. const $in = (params, owneryQuery, options, name) => new $In(params, owneryQuery, options, name);
  444. const $lt = numericalOperation(params => b => b < params);
  445. const $lte = numericalOperation(params => b => b <= params);
  446. const $gt = numericalOperation(params => b => b > params);
  447. const $gte = numericalOperation(params => b => b >= params);
  448. const $mod = ([mod, equalsValue], owneryQuery, options) => new EqualsOperation(b => comparable(b) % mod === equalsValue, owneryQuery, options);
  449. const $exists = (params, owneryQuery, options, name) => new $Exists(params, owneryQuery, options, name);
  450. const $regex = (pattern, owneryQuery, options) => new EqualsOperation(new RegExp(pattern, owneryQuery.$options), owneryQuery, options);
  451. const $not = (params, owneryQuery, options, name) => new $Not(params, owneryQuery, options, name);
  452. const typeAliases = {
  453. number: v => typeof v === "number",
  454. string: v => typeof v === "string",
  455. bool: v => typeof v === "boolean",
  456. array: v => Array.isArray(v),
  457. null: v => v === null,
  458. timestamp: v => v instanceof Date
  459. };
  460. const $type = (clazz, owneryQuery, options) => new EqualsOperation(b => {
  461. if (typeof clazz === "string") {
  462. if (!typeAliases[clazz]) {
  463. throw new Error(`Type alias does not exist`);
  464. }
  465. return typeAliases[clazz](b);
  466. }
  467. return b != null ? b instanceof clazz || b.constructor === clazz : false;
  468. }, owneryQuery, options);
  469. const $and = (params, ownerQuery, options, name) => new $And(params, ownerQuery, options, name);
  470. const $all = $and;
  471. const $size = (params, ownerQuery, options) => new $Size(params, ownerQuery, options, "$size");
  472. const $options = () => null;
  473. const $where = (params, ownerQuery, options) => {
  474. let test;
  475. if (isFunction(params)) {
  476. test = params;
  477. }
  478. else if (!process.env.CSP_ENABLED) {
  479. test = new Function("obj", "return " + params);
  480. }
  481. else {
  482. throw new Error(`In CSP mode, sift does not support strings in "$where" condition`);
  483. }
  484. return new EqualsOperation(b => test.bind(b)(b), ownerQuery, options);
  485. };
  486. var defaultOperations = /*#__PURE__*/Object.freeze({
  487. __proto__: null,
  488. $Size: $Size,
  489. $eq: $eq,
  490. $ne: $ne,
  491. $or: $or,
  492. $nor: $nor,
  493. $elemMatch: $elemMatch,
  494. $nin: $nin,
  495. $in: $in,
  496. $lt: $lt,
  497. $lte: $lte,
  498. $gt: $gt,
  499. $gte: $gte,
  500. $mod: $mod,
  501. $exists: $exists,
  502. $regex: $regex,
  503. $not: $not,
  504. $type: $type,
  505. $and: $and,
  506. $all: $all,
  507. $size: $size,
  508. $options: $options,
  509. $where: $where
  510. });
  511. const createDefaultQueryOperation = (query, ownerQuery, { compare, operations } = {}) => {
  512. return createQueryOperation(query, ownerQuery, {
  513. compare: compare,
  514. operations: Object.assign({}, defaultOperations, operations || {})
  515. });
  516. };
  517. const createDefaultQueryTester = (query, options = {}) => {
  518. const op = createDefaultQueryOperation(query, null, options);
  519. return createOperationTester(op);
  520. };
  521. export default createDefaultQueryTester;
  522. 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 };
  523. //# sourceMappingURL=index.js.map