UNPKG

baldrick-broth

Version:
81 lines (80 loc) 2.92 kB
/** * Responsibilities: Manages runtime data namespacing and value access. * - Namespaces task data with memory ids and supports root fallbacks * - Provides helpers to get/set values and truthy/falsy checks */ import { getProperty } from 'dot-prop'; import { rootId } from './id-generator.js'; import { currentTaskLogger } from './logging.js'; export const createDataId = (memoryId, key) => `${memoryId}::${key}`; export const setDataValue = (memoryId, ctx, key, value) => { if (ctx.data === undefined) { throw new Error('ctx.data should have defined by now'); } if (value === undefined) { delete ctx.data[`${memoryId}::${key}`]; } else { ctx.data[`${memoryId}::${key}`] = value; } }; export const withMemoryPrefix = (memoryId) => (key) => key.startsWith(`${memoryId}::`); export const splitDataKey = (dataKey) => { const [memoryId, key] = dataKey.split('::'); if (memoryId === undefined) { throw new Error('MemoryId should not be undefined'); } if (key === undefined) { throw new Error('Key should not be undefined'); } return [memoryId, key]; }; const getSpecificDataProperty = (memoryId, valuePath, value) => { if (value === undefined) { return undefined; } const [data_prefix, keyName] = valuePath.split('.'); if (data_prefix === undefined || data_prefix !== 'data' || keyName === undefined) { currentTaskLogger.warn(`getDataProperty: Invalid path ${valuePath}: ${data_prefix} and ${keyName}`); return undefined; } return value[`${memoryId}::${keyName}`]; }; const getDataProperty = (memoryId, valuePath, value) => { const localValue = getSpecificDataProperty(memoryId, valuePath, value); const useLocalValue = localValue !== undefined || memoryId === rootId; if (useLocalValue) { return localValue; } return getSpecificDataProperty(rootId, valuePath, value); }; /** * Return the current value or otherwise fallback to root value */ export const getSupportedProperty = (memoryId, ctx, valuePath) => { const isInsideData = valuePath.startsWith('data.'); const value = isInsideData ? getDataProperty(memoryId, valuePath, ctx.data) : getProperty(ctx, valuePath); return typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean' || typeof value === 'object' ? value : undefined; }; /** * Check if the the value would generally considered false or empty */ export const isFalsy = (value) => value === false || value === undefined || value === null || value === 0 || (typeof value === 'string' && value.trim().length === 0) || (Array.isArray(value) && value.length === 0); /** * Check if the the value would generally considered like having a value or true */ export const isTruthy = (value) => !isFalsy(value);