|
| 1 | +/** |
| 2 | + * Utilities for detecting and handling label-value (__lv) format |
| 3 | + * used by Pipedream components to preserve display labels for option values |
| 4 | + */ |
| 5 | + |
| 6 | +/** |
| 7 | + * Checks if a value is wrapped in the __lv format |
| 8 | + * @param value - The value to check |
| 9 | + * @returns true if value is an object with __lv property containing valid data |
| 10 | + * |
| 11 | + * @example |
| 12 | + * isLabelValueWrapped({ __lv: { label: "Option 1", value: 123 } }) // true |
| 13 | + * isLabelValueWrapped({ __lv: null }) // false |
| 14 | + * isLabelValueWrapped({ value: 123 }) // false |
| 15 | + */ |
| 16 | +export function isLabelValueWrapped(value: unknown): boolean { |
| 17 | + if (!value || typeof value !== "object") return false; |
| 18 | + if (!("__lv" in value)) return false; |
| 19 | + |
| 20 | + const lvContent = (value as Record<string, unknown>).__lv; |
| 21 | + return lvContent != null; |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Checks if a value is an array of __lv wrapped objects |
| 26 | + * @param value - The value to check |
| 27 | + * @returns true if value is an array of valid __lv wrapped objects |
| 28 | + * |
| 29 | + * @example |
| 30 | + * isArrayOfLabelValueWrapped([{ __lv: { label: "A", value: 1 } }]) // true |
| 31 | + * isArrayOfLabelValueWrapped([]) // false |
| 32 | + * isArrayOfLabelValueWrapped([{ value: 1 }]) // false |
| 33 | + */ |
| 34 | +export function isArrayOfLabelValueWrapped(value: unknown): boolean { |
| 35 | + if (!Array.isArray(value)) return false; |
| 36 | + if (value.length === 0) return false; |
| 37 | + |
| 38 | + return value.every((item) => |
| 39 | + item && |
| 40 | + typeof item === "object" && |
| 41 | + "__lv" in item && |
| 42 | + (item as Record<string, unknown>).__lv != null); |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Checks if a value has the label-value format (either single or array) |
| 47 | + * @param value - The value to check |
| 48 | + * @returns true if value is in __lv format (single or array) |
| 49 | + * |
| 50 | + * @example |
| 51 | + * hasLabelValueFormat({ __lv: { label: "A", value: 1 } }) // true |
| 52 | + * hasLabelValueFormat([{ __lv: { label: "A", value: 1 } }]) // true |
| 53 | + * hasLabelValueFormat({ value: 1 }) // false |
| 54 | + */ |
| 55 | +export function hasLabelValueFormat(value: unknown): boolean { |
| 56 | + return isLabelValueWrapped(value) || isArrayOfLabelValueWrapped(value); |
| 57 | +} |
0 commit comments