UNPKG

@graffy/common

Version:

Common libraries that used by various Graffy modules.

52 lines (40 loc) 2.4 kB
function _createForOfIteratorHelperLoose(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } it = o[Symbol.iterator](); return it.next.bind(it); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } import { isRange, isBranch } from '../../node'; import { decodeArgs } from '../index.js'; export default function decodeQuery(query) { var result = decodeChildren(query); return result; } function decodeChildren(query) { var isPage = query.some(function (node) { return isRange(node); }); if (isPage) { return decodeGraphPage(query); } else { return decodeGraphBranch(query); } } function decodeGraphPage(query) { var result = []; for (var _iterator = _createForOfIteratorHelperLoose(query), _step; !(_step = _iterator()).done;) { var node = _step.value; var args = decodeArgs(node); var child = isBranch(node) ? decodeChildren(node.children) : {}; child.$key = args; // Object.defineProperty(child, '$key', { value: args }); result.push(child); } return result.length === 1 ? result[0] : result; } function decodeGraphBranch(query) { var result = {}; for (var _iterator2 = _createForOfIteratorHelperLoose(query), _step2; !(_step2 = _iterator2()).done;) { var node = _step2.value; var child = isBranch(node) ? decodeChildren(node.children) : true; var key = node.key; result[key] = child; } return result; }