|
| 1 | +/* eslint-disable no-param-reassign */ |
| 2 | +/* eslint-disable no-not-accumulator-reassign/no-not-accumulator-reassign */ |
| 3 | + |
| 4 | +import evalExp from '@doc-tools/transform/lib/liquid/evaluation'; |
| 5 | + |
| 6 | +import {ContentVariables} from './transformers'; |
| 7 | + |
| 8 | +type WhenValue = string | boolean | undefined; |
| 9 | +type OrArray<T> = T | T[]; |
| 10 | +type OrArrayRecord<T> = Record<string, OrArray<T>>; |
| 11 | +interface FilteredContentItem extends OrArrayRecord<FilteredContentItem> {} |
| 12 | +type FilteredContent = OrArray<FilteredContentItem>; |
| 13 | +type FilterableContentItem = FilteredContentItem & {when: WhenValue}; |
| 14 | +export type FilterableContent = OrArray<FilterableContentItem>; |
| 15 | + |
| 16 | +function filterItems( |
| 17 | + items: FilterableContentItem[], |
| 18 | + vars: ContentVariables, |
| 19 | + propertyName?: string, |
| 20 | +): FilterableContentItem[] | FilteredContentItem[] { |
| 21 | + if (!Array.isArray(items)) { |
| 22 | + throw new Error( |
| 23 | + `Error while filtering: items has invalid key '${propertyName}' equals ${JSON.stringify( |
| 24 | + items, |
| 25 | + )}`, |
| 26 | + ); |
| 27 | + } |
| 28 | + |
| 29 | + return items.reduce<FilteredContentItem[]>((result, item) => { |
| 30 | + const passedFiltration = checkWhenCondition(item.when, vars); |
| 31 | + |
| 32 | + if (passedFiltration) { |
| 33 | + const property = propertyName && item[propertyName]; |
| 34 | + |
| 35 | + if (property === undefined) { |
| 36 | + result.push(item); |
| 37 | + } else { |
| 38 | + const filteredProperty = filterItems( |
| 39 | + property as FilterableContentItem[], |
| 40 | + vars, |
| 41 | + propertyName, |
| 42 | + ); |
| 43 | + |
| 44 | + if (filteredProperty.length !== 0) { |
| 45 | + result.push({ |
| 46 | + ...item, |
| 47 | + [propertyName as string]: filteredProperty, |
| 48 | + }); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return result; |
| 54 | + }, []); |
| 55 | +} |
| 56 | + |
| 57 | +function checkWhenCondition(whenValue: WhenValue, vars: ContentVariables) { |
| 58 | + return ( |
| 59 | + whenValue === true || |
| 60 | + whenValue === undefined || |
| 61 | + (typeof whenValue === 'string' && (!whenValue || evalExp(whenValue, vars))) |
| 62 | + ); |
| 63 | +} |
| 64 | + |
| 65 | +function isItemArray( |
| 66 | + content: FilterableContent | FilteredContent, |
| 67 | +): content is FilterableContentItem[] | FilteredContentItem[] { |
| 68 | + return Array.isArray(content); |
| 69 | +} |
| 70 | + |
| 71 | +function isItem( |
| 72 | + content: FilterableContent | FilteredContent, |
| 73 | +): content is FilterableContentItem | FilteredContentItem { |
| 74 | + return content && typeof content === 'object' && !Array.isArray(content); |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * Filters content recoursively by result of it's 'when' property evaluation applied to vars argument. |
| 79 | + * e.g. property {..., when: 'someVar == "someValue"' } will be included in result only if vars.someVar === 'someValue' |
| 80 | + * @param {FilterableContent} content |
| 81 | + * @param {ContentVariables} vars |
| 82 | + * @return {FilteredContent} |
| 83 | + */ |
| 84 | +export function filterContent( |
| 85 | + content: FilterableContent | FilteredContent, |
| 86 | + vars: ContentVariables, |
| 87 | +): FilteredContent { |
| 88 | + if (isItemArray(content)) { |
| 89 | + return filterItems(content as FilterableContentItem[], vars).map((item) => |
| 90 | + filterContent(item, vars), |
| 91 | + ) as FilteredContent; |
| 92 | + } else if (isItem(content)) { |
| 93 | + return Object.entries(content).reduce<FilteredContentItem>((acc, [key, value]) => { |
| 94 | + acc[key] = filterContent(value, vars); |
| 95 | + return acc; |
| 96 | + }, {}); |
| 97 | + } else { |
| 98 | + return content; |
| 99 | + } |
| 100 | +} |
0 commit comments