UNPKG

@speckle/objectloader2

Version:

This is an updated objectloader for the Speckle viewer written in typescript

92 lines 3.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ObjectLoader2Flags = void 0; exports.isBase = isBase; exports.isReference = isReference; exports.isScalar = isScalar; exports.take = take; exports.getFeatureFlag = getFeatureFlag; exports.indexOf = indexOf; function isBase(maybeBase) { return (maybeBase !== null && typeof maybeBase === 'object' && 'id' in maybeBase && typeof maybeBase.id === 'string'); } function isReference(maybeRef) { return (maybeRef !== null && typeof maybeRef === 'object' && 'referencedId' in maybeRef && typeof maybeRef.referencedId === 'string'); } function isScalar(value) { const type = typeof value; return (value === null || type === 'string' || type === 'number' || type === 'boolean' || type === 'bigint' || type === 'symbol' || type === 'undefined'); } function take(it, count) { const result = []; for (let i = 0; i < count; i++) { const itr = it.next(); if (itr.done) break; result.push(itr.value); } return result; } var ObjectLoader2Flags; (function (ObjectLoader2Flags) { ObjectLoader2Flags["DEBUG"] = "debug"; ObjectLoader2Flags["USE_CACHE"] = "useCache"; })(ObjectLoader2Flags || (exports.ObjectLoader2Flags = ObjectLoader2Flags = {})); const defaultValues = { [ObjectLoader2Flags.DEBUG]: 'false', [ObjectLoader2Flags.USE_CACHE]: 'true' }; function getFeatureFlag(paramName, useDefault = true) { // Check if the code is running in a browser environment 🌐 const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined'; if (!isBrowser) { // If in Node.js or another server environment, return the default return useDefault ? defaultValues[paramName] : undefined; } // In a browser, parse the query string const params = new URLSearchParams(window.location.search); // .get() returns the value, or null if it's not found. // The nullish coalescing operator (??) provides the default value // if the left-hand side is null or undefined. return params.get(paramName) ?? (useDefault ? defaultValues[paramName] : undefined); } /** * Finds the first index of a "needle" Uint8Array within a "haystack" Uint8Array. * @param haystack The larger array to search within. * @param needle The smaller array to search for. * @param start The index to start searching from. Defaults to 0. * @returns The starting index of the needle, or -1 if not found. */ function indexOf(haystack, needle, start = 0) { if (needle.length === 0) { return 0; } // The last possible starting position for a match const limit = haystack.length - needle.length; for (let i = start; i <= limit; i++) { let foundMatch = true; for (let j = 0; j < needle.length; j++) { if (haystack[i + j] !== needle[j]) { foundMatch = false; break; // Mismatch, break inner loop } } if (foundMatch) { return i; // Found a full match at index i } } return -1; // No match found } //# sourceMappingURL=functions.js.map