Skip to content

Commit

Permalink
Merge branch 'NBO/use_alphabetic_order_in_scenario_list'
Browse files Browse the repository at this point in the history
  • Loading branch information
nborde-CSM committed Oct 12, 2021
2 parents 7d402f2 + 8096138 commit 9649f52
Show file tree
Hide file tree
Showing 3 changed files with 260 additions and 21 deletions.
36 changes: 15 additions & 21 deletions src/utils/SortScenarioListUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,23 @@ const clone = rfdc();

export const sortScenarioList = (scenarioList) => {
const sortedList = [];
let hasMovedScenarios = true;
while (hasMovedScenarios && scenarioList.length !== 0) {
const scenarioListCopy = clone(scenarioList);
hasMovedScenarios = false;
for (let i = 0; i < scenarioListCopy.length; ++i) {
const scenario = scenarioListCopy[i];
if (scenario.parentId === null) {
let scenarioListCopy = clone(scenarioList);
scenarioListCopy.sort((a, b) => a.name.toUpperCase() < b.name.toUpperCase() ? -1 : 1);

const buildScenarioTree = (idParent, depth) => {
const scenarioListToFilter = [];
for (const scenario of scenarioListCopy) {
if (scenario.parentId === idParent) {
scenario.depth = depth;
sortedList.push(scenario);
scenario.depth = 0;
const scenarioId = scenarioList.findIndex((current) => current.id === scenario.id);
scenarioList.splice(scenarioId, 1);
} else {
const parentScenarioId = scenario.parentId;
const parentPosInSortedList = sortedList.findIndex((current) => current.id === parentScenarioId);
if (parentPosInSortedList !== -1) {
scenario.depth = sortedList[parentPosInSortedList].depth + 1;
sortedList.splice(parentPosInSortedList + 1, 0, scenario);
const scenarioId = scenarioList.findIndex((current) => current.id === scenario.id);
scenarioList.splice(scenarioId, 1);
hasMovedScenarios = true;
}
scenarioListToFilter.push(scenario);
buildScenarioTree(scenario.id, depth + 1);
}
}
}
scenarioListCopy = scenarioListCopy.filter(scenario => !scenarioListToFilter.includes(scenario));
};

buildScenarioTree(null, 0);

return sortedList;
};
228 changes: 228 additions & 0 deletions src/utils/__test__/ScenarioListData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
// Copyright (c) Cosmo Tech.
// Licensed under the MIT license.

export const scenarioList = [
{
id: 's-1v9g5kzkv6o3',
name: 'PROD-8310',
parentId: null
},
{
id: 's-nn4le5eq42v6',
name: 'SHAM - Test 1',
parentId: null
},
{
id: 's-2xdq7wwd620l',
name: 'SHAM - Test 2',
parentId: null
},
{
id: 's-0n1r7pqwpd9g',
name: 'SHAM - Test 3',
parentId: null
},
{
id: 's-onr272m71o0z',
name: 'SHAM Test 5',
parentId: null
},
{
id: 's-j8wkvnn803zp',
name: 'SHAM - Test 6',
parentId: null
},
{
id: 's-5lx5zk9k6zgw',
name: 'THU - Test',
parentId: null
},
{
id: 's-p7xypk75kdyk',
name: 'JREY - Test refresh parameters',
parentId: null
},
{
id: 's-q4v57lx22dm4',
name: 'NBO - test abc1',
parentId: null
},
{
id: 's-ynryernd0xw1',
name: 'SHAM - Test 2-1',
parentId: 's-2xdq7wwd620l'
},
{
id: 's-gm3evxqwzzmj',
name: 'PROD-8311',
parentId: 's-1v9g5kzkv6o3'
},
{
id: 's-0nlpdyr305o3',
name: 'PROD-8109',
parentId: 's-1v9g5kzkv6o3'
},
{
id: 's-2xmgyyynzjv8',
name: 'VMI - Test metrics',
parentId: 's-1v9g5kzkv6o3'
},
{
id: 's-82y5ew9216rl',
name: 'THU - Test 2',
parentId: 's-5lx5zk9k6zgw'
},
{
id: 's-52q26jd8w0n3',
name: 'NBO - test efgh',
parentId: 's-q4v57lx22dm4'
},
{
id: 's-9zqz7m3p2gg5',
name: 'NBO - test abbcd',
parentId: 's-q4v57lx22dm4'
},
{
id: 's-7rqr96eyvkx9',
name: 'NBO - test lmnop',
parentId: 's-q4v57lx22dm4'
},
{
id: 's-9zqq2p36vrp1',
name: 'NBO - test abc3',
parentId: 's-q4v57lx22dm4'
},
{
id: 's-5mqqk55n239z',
name: 'NBO - abc2',
parentId: 's-9zqz7m3p2gg5'
},
{
id: 's-gk111e3v47mm',
name: 'NBO - test abc0',
parentId: 's-52q26jd8w0n3'
}
];

export const expectedSortedScenariolist = [
{
id: 's-p7xypk75kdyk',
name: 'JREY - Test refresh parameters',
parentId: null,
depth: 0
},
{
id: 's-q4v57lx22dm4',
name: 'NBO - test abc1',
parentId: null,
depth: 0
},
{
id: 's-9zqz7m3p2gg5',
name: 'NBO - test abbcd',
parentId: 's-q4v57lx22dm4',
depth: 1
},
{
id: 's-5mqqk55n239z',
name: 'NBO - abc2',
parentId: 's-9zqz7m3p2gg5',
depth: 2
},
{
id: 's-9zqq2p36vrp1',
name: 'NBO - test abc3',
parentId: 's-q4v57lx22dm4',
depth: 1
},
{
id: 's-52q26jd8w0n3',
name: 'NBO - test efgh',
parentId: 's-q4v57lx22dm4',
depth: 1
},
{
id: 's-gk111e3v47mm',
name: 'NBO - test abc0',
parentId: 's-52q26jd8w0n3',
depth: 2
},
{
id: 's-7rqr96eyvkx9',
name: 'NBO - test lmnop',
parentId: 's-q4v57lx22dm4',
depth: 1
},
{
id: 's-1v9g5kzkv6o3',
name: 'PROD-8310',
parentId: null,
depth: 0
},
{
id: 's-0nlpdyr305o3',
name: 'PROD-8109',
parentId: 's-1v9g5kzkv6o3',
depth: 1
},
{
id: 's-gm3evxqwzzmj',
name: 'PROD-8311',
parentId: 's-1v9g5kzkv6o3',
depth: 1
},
{
id: 's-2xmgyyynzjv8',
name: 'VMI - Test metrics',
parentId: 's-1v9g5kzkv6o3',
depth: 1
},
{
id: 's-nn4le5eq42v6',
name: 'SHAM - Test 1',
parentId: null,
depth: 0
},
{
id: 's-2xdq7wwd620l',
name: 'SHAM - Test 2',
parentId: null,
depth: 0
},
{
id: 's-ynryernd0xw1',
name: 'SHAM - Test 2-1',
parentId: 's-2xdq7wwd620l',
depth: 1
},
{
id: 's-0n1r7pqwpd9g',
name: 'SHAM - Test 3',
parentId: null,
depth: 0
},
{
id: 's-j8wkvnn803zp',
name: 'SHAM - Test 6',
parentId: null,
depth: 0
},
{
id: 's-onr272m71o0z',
name: 'SHAM Test 5',
parentId: null,
depth: 0
},
{
id: 's-5lx5zk9k6zgw',
name: 'THU - Test',
parentId: null,
depth: 0
},
{
id: 's-82y5ew9216rl',
name: 'THU - Test 2',
parentId: 's-5lx5zk9k6zgw',
depth: 1
}
];
17 changes: 17 additions & 0 deletions src/utils/__test__/SortScenarioListUtils.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) Cosmo Tech.
// Licensed under the MIT license.

import { scenarioList, expectedSortedScenariolist } from './ScenarioListData.js';
import { sortScenarioList } from '../SortScenarioListUtils';

describe('sortScenarioList', () => {
test('sort scenario list with right depth with an unsorted list', () => {
const sortedScenarioList = sortScenarioList(scenarioList);
expect(sortedScenarioList).toStrictEqual(expectedSortedScenariolist);
});

test('sort scenario list function with empty list', () => {
const sortedScenarioList = sortScenarioList([]);
expect(sortedScenarioList).toStrictEqual([]);
});
});

0 comments on commit 9649f52

Please sign in to comment.