common.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const type_1 = require("../utils/type");
  4. const datatype_1 = require("./datatype");
  5. function flatten(query, shouldPreserverObject, parents, visited) {
  6. const cloned = Object.assign({}, query);
  7. for (const key in query) {
  8. if (/^\$/.test(key))
  9. continue;
  10. const value = query[key];
  11. if (value === undefined) {
  12. delete cloned[key];
  13. continue;
  14. }
  15. if (!value)
  16. continue;
  17. if (type_1.isObject(value) && !shouldPreserverObject(value)) {
  18. if (visited.indexOf(value) > -1) {
  19. throw new Error('Cannot convert circular structure to JSON');
  20. }
  21. const newParents = [...parents, key];
  22. const newVisited = [...visited, value];
  23. const flattenedChild = flatten(value, shouldPreserverObject, newParents, newVisited);
  24. cloned[key] = flattenedChild;
  25. let hasKeyNotCombined = false;
  26. for (const childKey in flattenedChild) {
  27. if (!/^\$/.test(childKey)) {
  28. cloned[`${key}.${childKey}`] = flattenedChild[childKey];
  29. delete cloned[key][childKey];
  30. }
  31. else {
  32. hasKeyNotCombined = true;
  33. }
  34. }
  35. if (!hasKeyNotCombined) {
  36. delete cloned[key];
  37. }
  38. }
  39. }
  40. return cloned;
  41. }
  42. function flattenQueryObject(query) {
  43. return flatten(query, isConversionRequired, [], [query]);
  44. }
  45. exports.flattenQueryObject = flattenQueryObject;
  46. function flattenObject(object) {
  47. return flatten(object, (_) => false, [], [object]);
  48. }
  49. exports.flattenObject = flattenObject;
  50. function mergeConditionAfterEncode(query, condition, key) {
  51. if (!condition[key]) {
  52. delete query[key];
  53. }
  54. for (const conditionKey in condition) {
  55. if (query[conditionKey]) {
  56. if (type_1.isArray(query[conditionKey])) {
  57. query[conditionKey].push(condition[conditionKey]);
  58. }
  59. else if (type_1.isObject(query[conditionKey])) {
  60. if (type_1.isObject(condition[conditionKey])) {
  61. Object.assign(query[conditionKey], condition[conditionKey]);
  62. }
  63. else {
  64. console.warn(`unmergable condition, query is object but condition is ${type_1.getType(condition)}, can only overwrite`, condition, key);
  65. query[conditionKey] = condition[conditionKey];
  66. }
  67. }
  68. else {
  69. console.warn(`to-merge query is of type ${type_1.getType(query)}, can only overwrite`, query, condition, key);
  70. query[conditionKey] = condition[conditionKey];
  71. }
  72. }
  73. else {
  74. query[conditionKey] = condition[conditionKey];
  75. }
  76. }
  77. }
  78. exports.mergeConditionAfterEncode = mergeConditionAfterEncode;
  79. function isConversionRequired(val) {
  80. return type_1.isInternalObject(val) || type_1.isDate(val) || type_1.isRegExp(val);
  81. }
  82. exports.isConversionRequired = isConversionRequired;
  83. function encodeInternalDataType(val) {
  84. return datatype_1.serialize(val);
  85. }
  86. exports.encodeInternalDataType = encodeInternalDataType;
  87. function decodeInternalDataType(object) {
  88. return datatype_1.deserialize(object);
  89. }
  90. exports.decodeInternalDataType = decodeInternalDataType;