Skip to content

Instantly share code, notes, and snippets.

@parakhjain23
Last active February 1, 2024 12:28
Show Gist options
  • Save parakhjain23/36be8124fb32aff8cfedc3ebeabfc491 to your computer and use it in GitHub Desktop.
Save parakhjain23/36be8124fb32aff8cfedc3ebeabfc491 to your computer and use it in GitHub Desktop.
Plugin Fucntions
// 1. Function to convert from old json to new json
export function convertToNewStructure(array: Array<InputFieldsType>, location: string = 'root') {
const result: NewJsonType = {
steps: {
[location]: []
},
blocks: {}
}
function processItem(item: InputFieldsType) {
item.key =
location === 'root'
? `${item.key !== undefined ? item.key : item.value}`
: `${location}.${item.key !== undefined ? item.key : item.value}`
if (item?.type === 'input groups') {
result.steps[item.key?.toString()] = []
item?.children?.forEach((child) => {
const key = `${item?.key}.${child?.key}`
result?.steps?.[item?.key?.toString()]?.push(key)
child.key = key
processItem(child)
})
// eslint-disable-next-line
const { children, list, altersDynamicFields, ...itemWithoutChildren } = item
result.blocks[item.key] = itemWithoutChildren
} else {
// eslint-disable-next-line
const { list, altersDynamicFields, ...itemWithoutChildren } = item
result.blocks[item.key] = itemWithoutChildren
}
result.blocks[item.key].dependsOn = extractUsedVariables(item?.source, 'context.inputData')
}
array.forEach((item) => {
result.steps?.[location]?.push(`${location === 'root' ? '' : `${location}.`}${item.key !== undefined ? item.key : item.value}`)
processItem(item)
})
return result
}
// 2. Function to extract Used variable from the source
// usage( extractUsedVariables(jsCode, ‘context.inputData’) )
export function extractUsedVariables(jsCode: string = '', basePrefix: string = 'context.inputData') {
const optionalChainingRegexPart = '\\??\\.'
const prefixRegex = `${basePrefix.replace('.', `(${optionalChainingRegexPart})`)}(${optionalChainingRegexPart})`
const regex = new RegExp(`${prefixRegex}([a-zA-Z_][a-zA-Z0-9_]*)`, 'g')
const matches: string[] = []
let match
// eslint-disable-next-line
while ((match = regex.exec(jsCode)) !== null) {
matches.push(match[3]) // Adjust index based on number of capture groups in regex
}
return [...new Set(matches)]
}
// 3. Get Required field from json
// need to check more
function extractRequiredFields(json) {
const requiredFields = {};
function processStep(stepKey) {
requiredFields[stepKey] = [];
// Process each key in the current step
json.steps[stepKey].forEach(key => {
const block = json.blocks[key];
if (block && block.required) {
requiredFields[stepKey].push(key);
}
// Recursive call for nested steps
if (json.steps[key]) {
processStep(key);
}
});
}
// Start processing from the root
processStep('root');
return requiredFields;
}
const keyFieldDisableOrVisibleHe = (depandsOn1, flag1) => {
return depandsOn1.every(key => {
return flag1[key] === "SUCCESS" || flag1[key] === "MOUNTED"
})
}
const keyApiCallKarDu = (depandsOn1, flag1) => {
return depandsOn1.every(key => {
return flag1[key] === "SUCCESS"
})
}
const getKeysToSetFalseInFlagWhenValueChange = (key1, blocks1) => {
let arrToReturn = []
arrToReturn = [...arrToReturn, ...(blocks1[key1].usedId || [])]
blocks1[key1]?.usedId?.forEach(keyName => {
arrToReturn = [...arrToReturn, ...getKeysToSetFalseWhenValueChange(keyName, blocks1)]
});
return [...new Set(arrToReturn)];
}
const getUsedInJsonFromBlocksDependsOn = (blocks1) => {
const valueToReturn = {}
Object.keys(blocks1).forEach(keyName => {
blocks1[keyName].dependsOn.forEach(keyNameToAppend => {
if (typeof valueToReturn[keyNameToAppend] === 'object') {
valueToReturn[keyNameToAppend].add(keyName)
} else {
valueToReturn[keyNameToAppend] = new Set([keyName])
}
})
})
return valueToReturn
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment