stringifyFunctionOperators.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. 'use strict';
  2. module.exports = function stringifyFunctionOperators(pipeline) {
  3. if (!Array.isArray(pipeline)) {
  4. return;
  5. }
  6. for (const stage of pipeline) {
  7. if (stage == null) {
  8. continue;
  9. }
  10. const canHaveAccumulator = stage.$group || stage.$bucket || stage.$bucketAuto;
  11. if (canHaveAccumulator != null) {
  12. for (const key of Object.keys(canHaveAccumulator)) {
  13. handleAccumulator(canHaveAccumulator[key]);
  14. }
  15. }
  16. const stageType = Object.keys(stage)[0];
  17. if (stageType && typeof stage[stageType] === 'object') {
  18. const stageOptions = stage[stageType];
  19. for (const key of Object.keys(stageOptions)) {
  20. if (stageOptions[key] != null &&
  21. stageOptions[key].$function != null &&
  22. typeof stageOptions[key].$function.body === 'function') {
  23. stageOptions[key].$function.body = stageOptions[key].$function.body.toString();
  24. }
  25. }
  26. }
  27. if (stage.$facet != null) {
  28. for (const key of Object.keys(stage.$facet)) {
  29. stringifyFunctionOperators(stage.$facet[key]);
  30. }
  31. }
  32. }
  33. };
  34. function handleAccumulator(operator) {
  35. if (operator == null || operator.$accumulator == null) {
  36. return;
  37. }
  38. for (const key of ['init', 'accumulate', 'merge', 'finalize']) {
  39. if (typeof operator.$accumulator[key] === 'function') {
  40. operator.$accumulator[key] = String(operator.$accumulator[key]);
  41. }
  42. }
  43. }