UNPKG

@revenium/perplexity

Version:
68 lines (67 loc) 2.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.safeExtract = void 0; /** * Safe extraction utility functions. */ var safeExtract; (function (safeExtract) { /** * Safely extract a value from an object, returning undefined if the path doesn't exist. */ function get(obj, path, defaultValue) { try { const keys = path.split("."); let result = obj; for (const key of keys) { if (result == null || typeof result !== "object") { return defaultValue; } result = result[key]; } return result !== null && result !== void 0 ? result : defaultValue; } catch { return defaultValue; } } safeExtract.get = get; /** * Safely extract a string value, returning empty string if not found. */ function string(obj, path) { return get(obj, path, "") || ""; } safeExtract.string = string; /** * Safely extract a number value, returning 0 if not found. */ function number(obj, path) { const value = get(obj, path, 0); return typeof value === "number" ? value : 0; } safeExtract.number = number; /** * Safely extract a boolean value, returning false if not found. */ function boolean(obj, path) { return get(obj, path, false) || false; } safeExtract.boolean = boolean; /** * Safely extract an object value, returning empty object if not found. */ function object(obj, path) { const value = get(obj, path, {}); return typeof value === "object" && value !== null ? value : {}; } safeExtract.object = object; /** * Safely extract an array value, returning empty array if not found. */ function array(obj, path) { const value = get(obj, path, []); return Array.isArray(value) ? value : []; } safeExtract.array = array; })(safeExtract || (exports.safeExtract = safeExtract = {}));