UNPKG

loaders.gl

Version:

Framework-independent loaders for 3D graphics formats

87 lines (60 loc) 2.59 kB
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; } export default function unpackJsonArrays(json, buffers, options = {}) { return unpackJsonArraysRecursive(json, json, buffers, options); } // Recursively unpacks objects, replacing "JSON pointers" with typed arrays function unpackJsonArraysRecursive(json, topJson, buffers, options = {}) { const object = json; const buffer = decodeJSONPointer(object, buffers); if (buffer) { return buffer; } // Copy array if (Array.isArray(object)) { return object.map(element => unpackJsonArraysRecursive(element, topJson, buffers, options)); } // Copy object if (object !== null && typeof object === 'object') { const newObject = {}; for (const key in object) { newObject[key] = unpackJsonArraysRecursive(object[key], topJson, buffers, options); } return newObject; } return object; } function decodeJSONPointer(object, buffers) { const pointer = parseJSONPointer(object); if (pointer) { const _pointer = _slicedToArray(pointer, 2), field = _pointer[0], index = _pointer[1]; const buffer = buffers[field] && buffers[field][index]; if (!buffer) { console.error(`Invalid JSON pointer ${object}: #/${field}/${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); } let matches = value.match(/\#\/[a-z]+\/([0-9]+)/); if (matches) { const index = parseInt(matches[2], 10); return [matches[1], index]; } // Legacy: `$$$i` matches = value.match(/\$\$\$([0-9]+)/); if (matches) { const index = parseInt(matches[1], 10); return ['accessors', index]; } } return null; } //# sourceMappingURL=unpack-binary-json.js.map