0

Using Electron and electron-store to add files' simplified executable names and their full paths from showOpenDialog to config.json. Selecting the same file causes repeating entries in config.json. For some reason (or rather missing code), app thinks they're different paths.

function addTool() {
    dialog.showOpenDialog({
            title: 'Select tool executable.',
            filters: [{
                name: 'Tool start file',
                extensions: ['exe', 'jar']
            }],
            properties: ['openFile']
        },
        (exeFromDialog) => {
            var var_exeToolPath = exeFromDialog.join(); //removes square brackets
            var var_toolName = path.basename(var_exeToolPath).split(/[/._-]/g)[0];
            //path.basename removes path until file, split+regex takes only first part until first character (one of ._/)

            const tools = appConfig.get('tools');
            const newTool = [...(tools || []), {
                "toolName": var_toolName,
                "toolPath": var_exeToolPath
            }];
            appConfig.set('tools', newTool);

        })
}

This is how config.json looks when you open the same file few times:

{
    "winPosition": {
        "x": 1497,
        "y": 410,
        "width": 203,
        "height": 603
    },
    "exePOEPath": [
        "C:\\Program Files (x86)\\Grinding Gear Games\\Path of Exile\\PathOfExile_x64.exe"
    ],
    "tools": [
        {
            "toolName": "tool1",
            "toolPath": "D:\\tool1.exe"
        },
        {
            "toolName": "tool1",
            "toolPath": "D:\\tool1.exe"
        },
        {
            "toolName": "tool1",
            "toolPath": "D:\\tool1.exe"
        }
    ]
}

1 Answers1

0

Ultimately it comes to the question How to remove duplicates from your array

This part of your code will always add the new value, it doesn't check for duplicates

const newTool = [...(tools || []), {
  toolName: var_toolName,
  toolPath: var_exeToolPath
}]

So it should be improved to something like the following:

newTool = newTool.filter((item, pos, self) =>
  self.find(other => other.toolName === item.toolName) === item
)

I would prefer using [...new Set([newTool])] but you store Objects which are compared by reference thus duplicates cannot be eliminated by Set

pergy
  • 5,285
  • 1
  • 22
  • 36