Skip to content

Commit d2915f0

Browse files
author
Roman Snapko
committed
Refactor step name generation to use alphabetical indexing
1 parent 074e747 commit d2915f0

2 files changed

Lines changed: 25 additions & 15 deletions

File tree

packages/react-ui/src/app/features/builder/blocks-selector/block-selector-utils.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
Trigger,
1414
TriggerType,
1515
} from '@openops/shared';
16-
1716
import { createDefaultOptionSettings } from '../step-settings/split-settings/utils';
1817

1918
const defaultCode = `export const code = async (inputs) => {
@@ -37,13 +36,7 @@ const getStepName = (block: StepMetadata, flowVersion: FlowVersion) => {
3736
if (block.type === TriggerType.BLOCK) {
3837
return 'trigger';
3938
}
40-
const baseName = 'step_';
41-
let number = 1;
42-
const steps = flowHelper.getAllSteps(flowVersion.trigger);
43-
while (steps.some((step) => step.name === `${baseName}${number}`)) {
44-
number++;
45-
}
46-
return `${baseName}${number}`;
39+
return flowHelper.findAvailableStepName(flowVersion, 'step');
4740
};
4841

4942
const getDefaultStep = ({

packages/shared/src/lib/flows/flow-helper.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ type GetStepFromSubFlow = {
4646
const actionSchemaValidator = TypeCompiler.Compile(SingleActionSchema);
4747
const triggerSchemaValidation = TypeCompiler.Compile(TriggerWithOptionalId);
4848

49+
const ALPHABET_LENGTH = 26;
50+
const A_CHAR_CODE = 'a'.charCodeAt(0);
51+
4952
export function buildBlockActionKey(
5053
blockName: string,
5154
actionName: string,
@@ -1096,16 +1099,30 @@ function doesActionHaveChildren(
10961099
return false;
10971100
}
10981101

1102+
function indexToAlphabetical(index: number): string {
1103+
let n = index + 1;
1104+
let result = '';
1105+
while (n > 0) {
1106+
n--;
1107+
result = String.fromCharCode(A_CHAR_CODE + (n % ALPHABET_LENGTH)) + result;
1108+
n = Math.floor(n / ALPHABET_LENGTH);
1109+
}
1110+
return result;
1111+
}
1112+
10991113
function findUnusedName(names: string[], stepPrefix: string): string {
1100-
let availableNumber = 1;
1101-
let availableName = `${stepPrefix}_${availableNumber}`;
1114+
const prefix = `${stepPrefix}_`;
1115+
const tails = new Set(
1116+
names
1117+
.filter((n) => n.startsWith(prefix))
1118+
.map((n) => n.slice(prefix.length)),
1119+
);
11021120

1103-
while (names.includes(availableName)) {
1104-
availableNumber++;
1105-
availableName = `${stepPrefix}_${availableNumber}`;
1121+
let index = 0;
1122+
while (tails.has(indexToAlphabetical(index))) {
1123+
index++;
11061124
}
1107-
1108-
return availableName;
1125+
return `${stepPrefix}_${indexToAlphabetical(index)}`;
11091126
}
11101127

11111128
function findAvailableStepName(

0 commit comments

Comments
 (0)