import type { Attribute, SelectAttribute } from '$conditions-core'; type…
- User
import type { Attribute, SelectAttribute } from '$conditions-core';
type SelectAtrAvailabilityProps = { attribute: Attribute; childAttributes: Attribute[]; availabilityMapper: (attribute: Attribute) => Attribute | null; };
export const selectAtrAvailability = ({ attribute,
childAttributes, availabilityMapper,}: SelectAtrAvailabilityProps) => { const { selectedOption } = attribute as SelectAttribute;
return { ...attribute, available: true, childAttributes: (childAttributes || []) .map(availabilityMapper) .filter((it): it is Attribute => Boolean(it)), ...(selectedOption && { selectedOption: { ...selectedOption, childAttributes: (selectedOption?.childAttributes || []) .map(availabilityMapper) .filter(Boolean), }, }), };}; write tests
- Assistant
- User
import type { Attribute } from '$conditions-core';
import { checkIsPartyList, checkIsRepeatingContainer, checkIsSelect, } from '../../../../attributeTypeGuards'; import { partyListAtrAvailability } from './partyListAtrAvailability'; import { repeatingContainerAtrAvailability } from './repeatingContainerAtrAvailability'; import { selectAtrAvailability } from './selectAtrAvailability';
type CreateAtrAvailabilityByTypeProps = { attribute: Attribute; availabilityMapper: (attribute: Attribute) => Attribute | null; childAttributes: Attribute[]; resolvedXmlTree: XMLDocument; xmlTree: XMLDocument; propagationIndex: number; };
export const createAtrAvailabilityByType = ({ attribute, availabilityMapper, childAttributes, resolvedXmlTree, xmlTree, propagationIndex, }: CreateAtrAvailabilityByTypeProps) => { if (checkIsSelect(attribute)) { return selectAtrAvailability({ attribute, availabilityMapper, childAttributes, }); }
if (checkIsPartyList(attribute)) { return partyListAtrAvailability({ attribute, availabilityMapper }); } if (checkIsRepeatingContainer(attribute)) { return repeatingContainerAtrAvailability({ attribute, availabilityMapper, childAttributes, propagationIndex, resolvedXmlTree, xmlTree, }); }};
- Assistant
- User
- Assistant
- User
import isEqual from 'lodash.isequal'; import memoizeOne from 'memoize-one'; import wgxpath from 'wicked-good-xpath';
export class XmlTreeCache { xmlTreeString: string | null;
doc: XMLDocument | null; serializer: XMLSerializer; xmlTemplate: XMLDocument | null; constructor() { wgxpath.install(window); this.xmlTreeString = null; // Дерево в виде строки this.doc = null; // XML документ this.serializer = new XMLSerializer(); // Из Document в XML this.xmlTemplate = null; // Свойство, в которое будет записан Document из строки } createXmlFromString = memoizeOne((xmlTreeString: string): XMLDocument => { try { // На 20% быстрее, чем DOMParser.parsefromstring() this.xmlTemplate = document.implementation.createDocument(null, null, null); this.xmlTemplate.append(this.xmlTemplate.createElement('root')); this.xmlTemplate .getElementsByTagName('root')[0] .insertAdjacentHTML('beforeend', xmlTreeString); } catch (err) { // eslint-disable-next-line console.error(err); } return this.xmlTemplate as XMLDocument; }); generateNewXmlDocument = (xmlTreeString: string) => { const doc = this.createXmlFromString(xmlTreeString); this.doc = doc; return doc; }; getXmlDocument = memoizeOne((xmlTreeString: string) => { let treeString = xmlTreeString; if (typeof treeString !== 'string') { treeString = '<root></root>'; } if (isEqual(treeString, this.xmlTreeString)) { return this.doc; } this.xmlTreeString = xmlTreeString; return this.generateNewXmlDocument(treeString); }); getTreeStringFromDOM = (xmlDOM: XMLDocument) => { this.doc = xmlDOM; const treeString = this.serializer.serializeToString(this.doc); this.xmlTreeString = treeString; return this.xmlTreeString; }; /** Очистка кэша xml-дерева */ static resetTreeCache = () => { window.xmlTreeCache = null; }; static getXmlTreeFromString = (xmlTreeString: string) => { if (!window.xmlTreeCache) { window.xmlTreeCache = new XmlTreeCache(); } return window.xmlTreeCache.getXmlDocument(xmlTreeString); }; - Assistant