common.js 3.1 KB

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