UNPKG

@pipedream/roamresearch

Version:

Pipedream roamresearch Components

58 lines (47 loc) 1.23 kB
import { ConfigurationError } from "@pipedream/platform"; const parseJson = (input) => { const parse = (value) => { if (typeof(value) === "string") { try { return parseJson(JSON.parse(value)); } catch (e) { return value; } } else if (typeof(value) === "object" && value !== null) { return Object.entries(value) .reduce((acc, [ key, val, ]) => Object.assign(acc, { [key]: parse(val), }), {}); } return value; }; return parse(input); }; function parseArray(value) { try { if (!value) { return; } if (Array.isArray(value)) { return value; } const parsedValue = JSON.parse(value); if (!Array.isArray(parsedValue)) { throw new Error("Not an array"); } return parsedValue; } catch (e) { throw new ConfigurationError("Make sure the custom expression contains a valid array object"); } } function getNestedProperty(obj, propertyString) { const properties = propertyString.split("."); return properties.reduce((prev, curr) => prev?.[curr], obj); } export default { parseArray: (value) => parseArray(value)?.map(parseJson), getNestedProperty, };