UNPKG

@cloudbase/js-sdk

Version:
119 lines (118 loc) 4.53 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; import { getType, isObject, isArray, isDate, isRegExp, isInternalObject } from '../utils/type'; import { serialize as serializeInternalDataType, deserialize as deserializeInternalDataType } from './datatype'; function flatten(query, shouldPreserverObject, parents, visited) { var cloned = __assign({}, query); for (var key in query) { if (/^\$/.test(key)) continue; var value = query[key]; if (!value) continue; if (isObject(value) && !shouldPreserverObject(value)) { if (visited.indexOf(value) > -1) { throw new Error('Cannot convert circular structure to JSON'); } var newParents = __spreadArray(__spreadArray([], __read(parents), false), [ key, ], false); var newVisited = __spreadArray(__spreadArray([], __read(visited), false), [ value, ], false); var flattenedChild = flatten(value, shouldPreserverObject, newParents, newVisited); cloned[key] = flattenedChild; var hasKeyNotCombined = false; for (var childKey in flattenedChild) { if (!/^\$/.test(childKey)) { cloned["".concat(key, ".").concat(childKey)] = flattenedChild[childKey]; delete cloned[key][childKey]; } else { hasKeyNotCombined = true; } } if (!hasKeyNotCombined) { delete cloned[key]; } } } return cloned; } export function flattenQueryObject(query) { return flatten(query, isConversionRequired, [], [query]); } export function flattenObject(object) { return flatten(object, function (_) { return false; }, [], [object]); } export function mergeConditionAfterEncode(query, condition, key) { if (!condition[key]) { delete query[key]; } for (var conditionKey in condition) { if (query[conditionKey]) { if (isArray(query[conditionKey])) { query[conditionKey].push(condition[conditionKey]); } else if (isObject(query[conditionKey])) { if (isObject(condition[conditionKey])) { Object.assign(query[conditionKey], condition[conditionKey]); } else { console.warn("unmergable condition, query is object but condition is ".concat(getType(condition), ", can only overwrite"), condition, key); query[conditionKey] = condition[conditionKey]; } } else { console.warn("to-merge query is of type ".concat(getType(query), ", can only overwrite"), query, condition, key); query[conditionKey] = condition[conditionKey]; } } else { query[conditionKey] = condition[conditionKey]; } } } export function isConversionRequired(val) { return isInternalObject(val) || isDate(val) || isRegExp(val); } export function encodeInternalDataType(val) { return serializeInternalDataType(val); } export function decodeInternalDataType(object) { return deserializeInternalDataType(object); }