diff --git a/src/utils/composition.js b/src/utils/composition.js deleted file mode 100644 index a041e15..0000000 --- a/src/utils/composition.js +++ /dev/null @@ -1,95 +0,0 @@ -/** - * Copyright (c) 2021, RTE (http://www.rte-france.com) - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - */ - -export function convertCompositionStringToArray(compositionString) { - // Transform parentheses into arrays - const step1 = compositionString - .split(/([()])/) - .map((x) => x.trim()) - .join(' ') - .replace(/\)\s\)/g, '))') - .replace(/\(/g, '[') - .replace(/\)\s/g, '], ') - .replace(/\)/g, ']'); - - // Wrap whole string in an array - const step2 = '[' + step1 + ']'; - - // Replace whitespace separators with commas and escape values - const step3 = step2 - .replace(/[^[\],\s]+/g, '"$&"') - .replace(/" /g, '", ') - .replace(/,[\s]+]/g, ']'); - - // Parse as a JSON array - const compositionArray = JSON.parse(step3); - - // Wrap Single filters in a group - return compositionArray.map((element) => { - if (!Array.isArray(element) && element !== '||' && element !== '&&') { - return [element]; - } - return element; - }); -} - -export function convertCompositionArrayToString(compositionArray) { - const compositionStringArray = compositionArray.map((element) => { - if (Array.isArray(element)) { - if (element.length === 1) { - return element[0]; - } else { - return `(${convertCompositionArrayToString(element)})`; - } - } - return element; - }); - return compositionStringArray.join(' '); -} - -export const checkCompositionArrayValidity = ( - compositionArray, - isInner = false -) => { - let arrayOperation; - return compositionArray - .map((compositionElement, index) => { - if (index % 2 === 0) { - if (Array.isArray(compositionElement)) { - return checkCompositionArrayValidity( - compositionElement, - true - ); - } - return /filter\d+\b/.test(compositionElement); - } - if (index === 1) { - arrayOperation = compositionElement; - } - return ( - /(&&|\|\|)/.test(compositionElement) && - (!isInner || compositionElement === arrayOperation) - ); - }) - .reduce((acc, element) => acc && element); -}; - -export function getMaxDepthParentheses(logicString) { - let count = 0; - let maxCount = 0; - Array.from(logicString).forEach((char) => { - if (char === '(') { - count++; - } else if (char === ')') { - count--; - } - if (count > maxCount) { - maxCount = count; - } - }); - return maxCount; -} diff --git a/src/utils/functions.js b/src/utils/functions.ts similarity index 72% rename from src/utils/functions.js rename to src/utils/functions.ts index 75ad437..90b401f 100644 --- a/src/utils/functions.js +++ b/src/utils/functions.ts @@ -7,9 +7,12 @@ import { pick } from 'lodash'; +type GetMatcher = (target: T) => Parameters['find']>[0]; +type PickProps = Array; + /** * Copy properties from corresponding objects in a source array to objects in a target array. - * It returns the target array that contain modified objects + * It returns the target array that contains modified objects * * @param targetArray the target array to copy to inside objects * @param sourceArray the source array from which to copy properties of objects @@ -17,7 +20,12 @@ import { pick } from 'lodash'; * @param props properties names to copy, specified individually or in array * @return the target array */ -export const assignArray = (targetArray, sourceArray, matcher, ...props) => { +export function assignArray( + targetArray: T[], + sourceArray: T[], + matcher: GetMatcher, + ...props: PickProps +) { targetArray?.forEach((targetObj) => { const matcherOfTarget = matcher(targetObj); const sourceObj = sourceArray?.find(matcherOfTarget); @@ -26,4 +34,4 @@ export const assignArray = (targetArray, sourceArray, matcher, ...props) => { }); return targetArray; -}; +} diff --git a/src/utils/properties.js b/src/utils/properties.js deleted file mode 100644 index 8b8d472..0000000 --- a/src/utils/properties.js +++ /dev/null @@ -1,22 +0,0 @@ -/** - * Copyright (c) 2021, RTE (http://www.rte-france.com) - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - */ - -import { EquipmentProperties } from '../constants/equipmentDefinition'; - -export function getProperty(equipmentType, propertyName) { - return EquipmentProperties[equipmentType].find( - (property) => property.name === propertyName - ); -} - -export function getValuesOption(property) { - //TODO Intl - return property?.values?.map((value) => ({ - label: value, - value, - })); -}