UNPKG

loaders.gl

Version:

Framework-independent loaders for 3D graphics formats

102 lines (70 loc) 3.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = unpackJsonArrays; function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function unpackJsonArrays(json, buffers) { var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; return unpackJsonArraysRecursive(json, json, buffers, options); } // Recursively unpacks objects, replacing "JSON pointers" with typed arrays function unpackJsonArraysRecursive(json, topJson, buffers) { var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; var object = json; var buffer = decodeJSONPointer(object, buffers); if (buffer) { return buffer; } // Copy array if (Array.isArray(object)) { return object.map(function (element) { return unpackJsonArraysRecursive(element, topJson, buffers, options); }); } // Copy object if (object !== null && _typeof(object) === 'object') { var newObject = {}; for (var key in object) { newObject[key] = unpackJsonArraysRecursive(object[key], topJson, buffers, options); } return newObject; } return object; } function decodeJSONPointer(object, buffers) { var pointer = parseJSONPointer(object); if (pointer) { var _pointer = _slicedToArray(pointer, 2), field = _pointer[0], index = _pointer[1]; var buffer = buffers[field] && buffers[field][index]; if (!buffer) { console.error("Invalid JSON pointer ".concat(object, ": #/").concat(field, "/").concat(index)); // eslint-disable-line return null; } return buffer; } return object; } function parseJSONPointer(value) { if (typeof value === 'string') { // Remove escape character if (value.indexOf('##/') === 0) { return value.slice(1); } var matches = value.match(/\#\/[a-z]+\/([0-9]+)/); if (matches) { var index = parseInt(matches[2], 10); return [matches[1], index]; } // Legacy: `$$$i` matches = value.match(/\$\$\$([0-9]+)/); if (matches) { var _index = parseInt(matches[1], 10); return ['accessors', _index]; } } return null; } //# sourceMappingURL=unpack-binary-json.js.map