import { ActionAttribute } from '../actions/types' import { VarDataType } from '../types' const ATTRIBUTE_ID_SEPARATOR = '__' const ATTRIBUTE_TYPE_SEPARATOR = ':' /** * Parser for attribute global id * @param id * @returns */ export const parseAttributeGlobalId = (id: string) => { const [relId, parameter] = String(id).split(ATTRIBUTE_ID_SEPARATOR) if (relId && !parameter) { const [name, datatype] = String(relId).split(ATTRIBUTE_TYPE_SEPARATOR) return { relId: '', name, datatype: datatype || VarDataType.any } } const [name, datatype] = String(parameter).split(ATTRIBUTE_TYPE_SEPARATOR) return { relId, name, datatype: datatype || VarDataType.any } } /** * Create attribute global id * @param actionId * @param name * @param type * @returns */ export const createAttributeGlobalId = (params: { relId: string name: string datatype: VarDataType }) => { const name = String(params.name) .replace(/[^a-zA-Z0-9-\\$\\.]+/g, '') .toLocaleLowerCase() return `${params.relId}${ATTRIBUTE_ID_SEPARATOR}${name}${ATTRIBUTE_TYPE_SEPARATOR}${params.datatype}` } /** * Conversion from attributes to object entries * @param attributes * @param relId * @returns * @example * attributesToObjectEntries(attrs, relId) * // -> [['rel-1__name:url', '__name:url']] * */ export const attributesToObjectEntries = ( attributes: ActionAttribute[], relId: string = '' ) => attributes.map(({ name, datatype }) => [ createAttributeGlobalId({ relId, name, datatype }), createAttributeGlobalId({ relId: '', name, datatype }), ]) /** * Conversion from params attributes to object entries * @param attributes * @returns */ export const paramsAttributesToObjectEntries = ( attributes: ActionAttribute[] ) => attributes.map(({ name, datatype, value }) => [ createAttributeGlobalId({ relId: '', name, datatype }), value, ])