Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

241
Visualizações
Search for substring in an object using full string

There are full paths to files like

libs/shared/util/something/tsconfig.spec.json
apps/project/backend/subfolder/project.json
this/has/no/match.json
root-file.json

and I need to check if the file is inside of a path in the given projects object:

const projects = {
    "just-a-library": "libs/shared/util/something",
    "project-backend": "apps/project/backend"
}

My problem is, that it will never fully match, but only the beginning of the string - as there are optional subfolders and of course filenames. If there is a match, it should return the object key.

So for libs/shared/util/something/tsconfig.spec.json it should return just-a-library and this/has/no/match.json there should be no result.

I can't use string.includes(substring) or string.indexOf(substring) as this is the other way round: The full string is the filepath input and I'm searching for a possible substring element.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

If I get the task right...

const paths = [
  "libs/shared/util/something/tsconfig.spec.json",
  "apps/project/backend/subfolder/project.json",
  "this/has/no/match.json",
  "root-file.json",
];

const projects = {
  "just-a-library": "libs/shared/util/something",
  "project-backend": "apps/project/backend",
};

paths.forEach((path) => {
  const result = Object.keys(projects).find((k) => path.startsWith(projects[k])) || "[Not found]";
  console.log(`${path} - ${result}`);
});

Output:

libs/shared/util/something/tsconfig.spec.json - just-a-library
apps/project/backend/subfolder/project.json - project-backend
root-file.json - [Not found]
this/has/no/match.json - [Not found]
about 4 years ago · Juan Pablo Isaza Relatório

0

If you break input projects and file paths down, you can build a directory via an object. Once that's created scan through the object to find any matches.

Added some templating to show directory.

const projects = {
  "just-a-library": "libs/shared/util/something",
  "project-backend": "apps/project/backend"
};

const inputs = [
  "libs/shared/util/something/tsconfig.spec.json",
  "apps/project/backend/subfolder/project.json",
  "this/has/no/match.json",
  "root-file.json"
];

const createFolders = input => input.split(/\//g);
const directory = {};
const project = "__PROJECT__";
const directoryView = document.getElementById("directory");

const projectFolders = {};
Object.keys(projects).forEach(key => {
  projectFolders[key] = createFolders(projects[key]);
});

Object.keys(projectFolders).forEach(key => {
  let pwd = directory;
  for (let i = 0; i < projectFolders[key].length; i++) {
    if (!pwd[projectFolders[key][i]]) {
      pwd[projectFolders[key][i]] = {};
    }
    pwd = pwd[projectFolders[key][i]];
  }
  pwd[project] = key;
});

directoryView.innerText = JSON.stringify(directory, null, 2);

const inputFolders = inputs.map(entry => createFolders(entry));

for (let i = 0; i < inputFolders.length; i++) {
  let pwd = directory;
  const path = [];
  for (let j = 0; j < inputFolders[i].length; j++) {
    if (!pwd[inputFolders[i][j]] && j !== inputFolders[i].length - 1) {
      if (pwd === directory) {
        console.log("invalid input path:", inputFolders[i].join("/"));
      } else {
        console.log("file:", inputFolders[i].join("/"), "\nmatching project:", pwd[project]);
      }
      break;
    } else {
      pwd = pwd[inputFolders[i][j]];
      path.push(inputFolders[i][j]);
    }
  }
}
<pre id="directory" style="width:100px"></pre>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda