core.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.createQueryTester = exports.createOperationTester = exports.createQueryOperation = exports.containsOperation = exports.numericalOperation = exports.numericalOperationCreator = exports.NopeOperation = exports.createEqualsOperation = exports.EqualsOperation = exports.createTester = exports.NestedOperation = exports.QueryOperation = exports.NamedGroupOperation = exports.NamedBaseOperation = void 0;
  4. const utils_1 = require("./utils");
  5. /**
  6. * Walks through each value given the context - used for nested operations. E.g:
  7. * { "person.address": { $eq: "blarg" }}
  8. */
  9. const walkKeyPathValues = (item, keyPath, next, depth, key, owner) => {
  10. const currentKey = keyPath[depth];
  11. // if array, then try matching. Might fall through for cases like:
  12. // { $eq: [1, 2, 3] }, [ 1, 2, 3 ].
  13. if (utils_1.isArray(item) && isNaN(Number(currentKey))) {
  14. for (let i = 0, { length } = item; i < length; i++) {
  15. // if FALSE is returned, then terminate walker. For operations, this simply
  16. // means that the search critera was met.
  17. if (!walkKeyPathValues(item[i], keyPath, next, depth, i, item)) {
  18. return false;
  19. }
  20. }
  21. }
  22. if (depth === keyPath.length || item == null) {
  23. return next(item, key, owner);
  24. }
  25. return walkKeyPathValues(item[currentKey], keyPath, next, depth + 1, currentKey, item);
  26. };
  27. class BaseOperation {
  28. constructor(params, owneryQuery, options) {
  29. this.params = params;
  30. this.owneryQuery = owneryQuery;
  31. this.options = options;
  32. this.init();
  33. }
  34. init() { }
  35. reset() {
  36. this.done = false;
  37. this.keep = false;
  38. }
  39. }
  40. class NamedBaseOperation extends BaseOperation {
  41. constructor(params, owneryQuery, options, name) {
  42. super(params, owneryQuery, options);
  43. this.name = name;
  44. }
  45. }
  46. exports.NamedBaseOperation = NamedBaseOperation;
  47. class GroupOperation extends BaseOperation {
  48. constructor(params, owneryQuery, options, children) {
  49. super(params, owneryQuery, options);
  50. this.children = children;
  51. }
  52. /**
  53. */
  54. reset() {
  55. this.keep = false;
  56. this.done = false;
  57. for (let i = 0, { length } = this.children; i < length; i++) {
  58. this.children[i].reset();
  59. }
  60. }
  61. /**
  62. */
  63. childrenNext(item, key, owner) {
  64. let done = true;
  65. let keep = true;
  66. for (let i = 0, { length } = this.children; i < length; i++) {
  67. const childOperation = this.children[i];
  68. childOperation.next(item, key, owner);
  69. if (!childOperation.keep) {
  70. keep = false;
  71. }
  72. if (childOperation.done) {
  73. if (!childOperation.keep) {
  74. break;
  75. }
  76. }
  77. else {
  78. done = false;
  79. }
  80. }
  81. this.done = done;
  82. this.keep = keep;
  83. }
  84. }
  85. class NamedGroupOperation extends GroupOperation {
  86. constructor(params, owneryQuery, options, children, name) {
  87. super(params, owneryQuery, options, children);
  88. this.name = name;
  89. }
  90. }
  91. exports.NamedGroupOperation = NamedGroupOperation;
  92. class QueryOperation extends GroupOperation {
  93. constructor() {
  94. super(...arguments);
  95. this.propop = true;
  96. }
  97. /**
  98. */
  99. next(item, key, parent) {
  100. this.childrenNext(item, key, parent);
  101. }
  102. }
  103. exports.QueryOperation = QueryOperation;
  104. class NestedOperation extends GroupOperation {
  105. constructor(keyPath, params, owneryQuery, options, children) {
  106. super(params, owneryQuery, options, children);
  107. this.keyPath = keyPath;
  108. this.propop = true;
  109. /**
  110. */
  111. this._nextNestedValue = (value, key, owner) => {
  112. this.childrenNext(value, key, owner);
  113. return !this.done;
  114. };
  115. }
  116. /**
  117. */
  118. next(item, key, parent) {
  119. walkKeyPathValues(item, this.keyPath, this._nextNestedValue, 0, key, parent);
  120. }
  121. }
  122. exports.NestedOperation = NestedOperation;
  123. const createTester = (a, compare) => {
  124. if (a instanceof Function) {
  125. return a;
  126. }
  127. if (a instanceof RegExp) {
  128. return b => {
  129. const result = typeof b === "string" && a.test(b);
  130. a.lastIndex = 0;
  131. return result;
  132. };
  133. }
  134. const comparableA = utils_1.comparable(a);
  135. return b => compare(comparableA, utils_1.comparable(b));
  136. };
  137. exports.createTester = createTester;
  138. class EqualsOperation extends BaseOperation {
  139. constructor() {
  140. super(...arguments);
  141. this.propop = true;
  142. }
  143. init() {
  144. this._test = exports.createTester(this.params, this.options.compare);
  145. }
  146. next(item, key, parent) {
  147. if (!Array.isArray(parent) || parent.hasOwnProperty(key)) {
  148. if (this._test(item, key, parent)) {
  149. this.done = true;
  150. this.keep = true;
  151. }
  152. }
  153. }
  154. }
  155. exports.EqualsOperation = EqualsOperation;
  156. const createEqualsOperation = (params, owneryQuery, options) => new EqualsOperation(params, owneryQuery, options);
  157. exports.createEqualsOperation = createEqualsOperation;
  158. class NopeOperation extends BaseOperation {
  159. constructor() {
  160. super(...arguments);
  161. this.propop = true;
  162. }
  163. next() {
  164. this.done = true;
  165. this.keep = false;
  166. }
  167. }
  168. exports.NopeOperation = NopeOperation;
  169. const numericalOperationCreator = (createNumericalOperation) => (params, owneryQuery, options, name) => {
  170. if (params == null) {
  171. return new NopeOperation(params, owneryQuery, options);
  172. }
  173. return createNumericalOperation(params, owneryQuery, options, name);
  174. };
  175. exports.numericalOperationCreator = numericalOperationCreator;
  176. const numericalOperation = (createTester) => exports.numericalOperationCreator((params, owneryQuery, options) => {
  177. const typeofParams = typeof utils_1.comparable(params);
  178. const test = createTester(params);
  179. return new EqualsOperation(b => {
  180. return typeof utils_1.comparable(b) === typeofParams && test(b);
  181. }, owneryQuery, options);
  182. });
  183. exports.numericalOperation = numericalOperation;
  184. const createNamedOperation = (name, params, parentQuery, options) => {
  185. const operationCreator = options.operations[name];
  186. if (!operationCreator) {
  187. throwUnsupportedOperation(name);
  188. }
  189. return operationCreator(params, parentQuery, options, name);
  190. };
  191. const throwUnsupportedOperation = (name) => {
  192. throw new Error(`Unsupported operation: ${name}`);
  193. };
  194. const containsOperation = (query, options) => {
  195. for (const key in query) {
  196. if (options.operations.hasOwnProperty(key) || key.charAt(0) === "$")
  197. return true;
  198. }
  199. return false;
  200. };
  201. exports.containsOperation = containsOperation;
  202. const createNestedOperation = (keyPath, nestedQuery, parentKey, owneryQuery, options) => {
  203. if (exports.containsOperation(nestedQuery, options)) {
  204. const [selfOperations, nestedOperations] = createQueryOperations(nestedQuery, parentKey, options);
  205. if (nestedOperations.length) {
  206. throw new Error(`Property queries must contain only operations, or exact objects.`);
  207. }
  208. return new NestedOperation(keyPath, nestedQuery, owneryQuery, options, selfOperations);
  209. }
  210. return new NestedOperation(keyPath, nestedQuery, owneryQuery, options, [
  211. new EqualsOperation(nestedQuery, owneryQuery, options)
  212. ]);
  213. };
  214. const createQueryOperation = (query, owneryQuery = null, { compare, operations } = {}) => {
  215. const options = {
  216. compare: compare || utils_1.equals,
  217. operations: Object.assign({}, operations || {})
  218. };
  219. const [selfOperations, nestedOperations] = createQueryOperations(query, null, options);
  220. const ops = [];
  221. if (selfOperations.length) {
  222. ops.push(new NestedOperation([], query, owneryQuery, options, selfOperations));
  223. }
  224. ops.push(...nestedOperations);
  225. if (ops.length === 1) {
  226. return ops[0];
  227. }
  228. return new QueryOperation(query, owneryQuery, options, ops);
  229. };
  230. exports.createQueryOperation = createQueryOperation;
  231. const createQueryOperations = (query, parentKey, options) => {
  232. const selfOperations = [];
  233. const nestedOperations = [];
  234. if (!utils_1.isVanillaObject(query)) {
  235. selfOperations.push(new EqualsOperation(query, query, options));
  236. return [selfOperations, nestedOperations];
  237. }
  238. for (const key in query) {
  239. if (options.operations.hasOwnProperty(key)) {
  240. const op = createNamedOperation(key, query[key], query, options);
  241. if (op) {
  242. if (!op.propop && parentKey && !options.operations[parentKey]) {
  243. throw new Error(`Malformed query. ${key} cannot be matched against property.`);
  244. }
  245. }
  246. // probably just a flag for another operation (like $options)
  247. if (op != null) {
  248. selfOperations.push(op);
  249. }
  250. }
  251. else if (key.charAt(0) === "$") {
  252. throwUnsupportedOperation(key);
  253. }
  254. else {
  255. nestedOperations.push(createNestedOperation(key.split("."), query[key], key, query, options));
  256. }
  257. }
  258. return [selfOperations, nestedOperations];
  259. };
  260. const createOperationTester = (operation) => (item, key, owner) => {
  261. operation.reset();
  262. operation.next(item, key, owner);
  263. return operation.keep;
  264. };
  265. exports.createOperationTester = createOperationTester;
  266. const createQueryTester = (query, options = {}) => {
  267. return exports.createOperationTester(exports.createQueryOperation(query, null, options));
  268. };
  269. exports.createQueryTester = createQueryTester;
  270. //# sourceMappingURL=core.js.map