UNPKG

knowmax-quest-utils

Version:

Utilities for creating a Knowmax Quest client.

90 lines 4.51 kB
/** Match given IMetadata value with given label specification. * @param metadata value to evaluate. * @param labelOrFamilyWithLabel Either string or array of strings which should be evaluated. Allowed formats: 'familylabel:label' and 'label'. In case of array, a match is considered if at least one value matches. * @param origin Optional origin to match. * @returns Boolean indicating match. */ export const metadataMatch = (metadata, labelOrFamilyWithLabel, origin) => { const list = typeof labelOrFamilyWithLabel === 'string' ? [labelOrFamilyWithLabel] : labelOrFamilyWithLabel; return list.some((l) => { if (origin && metadata.origin !== origin) return false; const { family, label } = parseMetadataLabel(l); return family ? metadata.familyLabel === family && metadata.label === label : metadata.label === label; }); }; /** Parse optional family label and label from given label. * @param label Allowed format: 'familylabel:label' and 'label' * @returns Object containing optional familyLabel and label. */ export const parseMetadataLabel = (label) => { const index = label.indexOf(':'); return { family: index > 1 ? label.substring(0, index) : undefined, label: index >= 0 && index < label.length ? label.substring(index + 1) : label }; }; /** Combine family label and label. */ export const formatMetadataLabel = (familyLabel, label) => `${familyLabel}:${label}`; /** Get first value from metadata matching family label and label. Returns undefined in case value not found. */ export const metadataValue = (familyLabel, label, list) => list?.find((m) => m.label === label && m.familyLabel === familyLabel)?.value; /** Get array with all values matching family label and label. Returns empty array in case no values found. */ export const metadataValues = (familyLabel, label, list) => list ? list.filter((m) => m.label === label && m.familyLabel === familyLabel).map((m) => m.value) : []; /** Format given prefix value in given value by convention "[prefix]value". */ export const formatMetadataValueWithPrefix = (value, prefix) => `[${prefix}]${value}`; /** Format given postfix value in given value by convention "value[postfix]" */ export const formatMetadataValueWithPostfix = (value, postfix) => `${value}[${postfix}]`; /** Parses prefix by convention using [prefix] from metadata value. * @param value Metadata value to parse. Prefix formatted like [text] at beginning of value. * @returns Tuple containing value and optional prefix. */ export const parseMetadataValueWithPrefix = (value) => { if (value.startsWith('[')) { const index = value.indexOf(']'); if (index > 0) { return [value.substring(index + 1), value.substring(1, index)]; } } return [value, undefined]; }; /** Parses postfix by convention using [postfix] from metadata value. * @param value Metadata value to parse. Postfix formatted like [text] at end of value. * @returns Tuple containing value and optional postfix. */ export const parseMetadataValueWithPostfix = (value) => { if (value.endsWith(']')) { const index = value.lastIndexOf('['); if (index > 0) { return [value.substring(0, index), value.substring(index + 1, value.length - 1)]; } } return [value, undefined]; }; /** Parses pair id and value from metadata values with pair id. * @param value Metadata value to parse. Pair id formatted like [number] at beginning of value. * @returns Tuple containing value and optional numeric pair id. */ export const parseMetadataValueWithPairId = (value) => { const [v, prefix] = parseMetadataValueWithPrefix(value); if (prefix) { const id = parseInt(prefix); if (!isNaN(id)) { return [v, id]; } } return [value, undefined]; }; /** Parses list of IMetadata with pair id specified into IMetadata type expaned with pairId. * @param data List of IMetadata to parse. * @returns List of IMetadata extended numeric with pair id. Values without or invalid pair id are filtered out. */ export const parseMetadataValueListWithPairId = (data) => { return data .map((item) => { const [value, id] = parseMetadataValueWithPairId(item.value); return { ...item, value: value, pairId: id }; }) .filter((item) => item.pairId !== undefined && !isNaN(item.pairId)) .map((item) => ({ ...item, pairId: item.pairId ?? -1 })); }; //# sourceMappingURL=metadata.js.map