Skip to content

Commit 2cffe5e

Browse files
committed
fix: refactor to be more performant
1 parent 9057a60 commit 2cffe5e

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

pkg/js/transformer/jsontodsl.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ function sortByModule(aName: string, bName: string, aMeta?: Metadata, bMeta?: Me
394394
/* This function gets the modules from the JSON model and
395395
* returns them as an alphabetically sorted array
396396
*/
397-
export function getModulesFromJSON(model: Omit<AuthorizationModel, "id">): string[] {
397+
export function getModulesFromJSON2(model: Omit<AuthorizationModel, "id">): string[] {
398398
const schemaVersion = model?.schema_version || "1.1";
399399

400400
if (schemaVersion !== "1.2") {
@@ -407,21 +407,28 @@ export function getModulesFromJSON(model: Omit<AuthorizationModel, "id">): strin
407407
return [];
408408
}
409409

410-
const modules = model.type_definitions?.reduce((acc: (string | undefined)[], typeDef) => {
411-
const relations = typeDef?.metadata?.relations || {};
412-
Object.entries(relations).forEach(([_, relationDefinition]) => {
413-
acc.push(relationDefinition.module);
414-
});
410+
const modulesMap: Record<string, boolean> = {};
411+
model.type_definitions?.forEach(typeDef => {
412+
const key = typeDef.metadata?.module;
413+
if (key) {
414+
modulesMap[key] = true;
415+
}
415416

416-
return [...acc, typeDef.metadata?.module];
417-
}, []);
417+
for (const relationMeta in typeDef?.metadata?.relations) {
418+
const key = typeDef?.metadata?.relations[relationMeta]?.module;
419+
if (key) {
420+
modulesMap[key] = true;
421+
}
422+
}
423+
});
418424

419425
const conditions = model.conditions || {};
420-
const conditionModules = Object.entries(conditions).map(
421-
([_, conditionDefinition]) => conditionDefinition.metadata?.module,
422-
);
423-
424-
const filteredModules = [...modules, ...conditionModules].filter((module) => module !== undefined) as string[];
426+
for (const conditionMeta in conditions) {
427+
const key = conditions[conditionMeta].metadata?.module;
428+
if (key) {
429+
modulesMap[key] = true;
430+
}
431+
}
425432

426-
return [...new Set(filteredModules)].sort((a, b) => a.localeCompare(b));
433+
return Object.keys(modulesMap).sort();
427434
}

0 commit comments

Comments
 (0)