UNPKG

@yext/analytics

Version:

An analytics library for Yext

63 lines 2.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.convertStringToValue = void 0; // Define the list of possibly numerical properties to check and convert const propertiesToCheck = [ 'sessionTrackingEnabled', 'versionNumber', 'siteUid', 'count', 'entity', 'amount', 'latitude', 'longitude', 'timestamp', 'bot', 'internalUser', 'isDirectAnswer', 'isGenerativeDirectAnswer' ]; function convertStringToValue(data) { // Recursive function to traverse and convert nested objects function recursiveConversion(obj) { for (const property in obj) { if (propertiesToCheck.includes(property)) { // Check if the property is in the list of properties to convert, // and if it's a string that represents a number, convert it to a number. // If it's a boolean string, convert it to a boolean. if (!isNaN(Number(obj[property]))) { obj[property] = Number(obj[property]); } else if (obj[property] === 'true' || obj[property] === 'false') { obj[property] = obj[property] === 'true'; } } // If the property is an object, recursively call the function on it else if (typeof obj[property] === 'object') { recursiveConversion(obj[property]); } } } // Copy the input data to avoid modifying the original const result = Object.assign({}, data); // Start the recursive conversion recursiveConversion(result); // Handle customValues separately as it's nested properties can have a user defined key name if (result.customValues && typeof result.customValues === 'object') { convertCustomValues(result.customValues); } // Return the modified result return Object.assign(Object.assign({}, result), { action: result.action }); } exports.convertStringToValue = convertStringToValue; // Function to convert custom values within customValues object function convertCustomValues(obj) { for (const key in obj) { if (obj.hasOwnProperty(key) && typeof obj[key] === 'string' && !isNaN(Number(obj[key]))) { obj[key] = Number(obj[key]); } } } //# sourceMappingURL=convertStringToValue.js.map