简体   繁体   English

使用完整字符串在 object 中搜索 substring

[英]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:我需要检查文件是否在给定项目 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.如果匹配,则应返回 object 密钥。

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.所以对于libs/shared/util/something/tsconfig.spec.json它应该返回just-a-librarythis/has/no/match.json应该没有结果。

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.我不能使用string.includes(substring)string.indexOf(substring)因为这是相反的方式:完整的字符串是文件路径输入,我正在搜索可能的 substring 元素。

If you break input projects and file paths down, you can build a directory via an object.如果您破坏输入项目和文件路径,您可以通过 object 构建一个目录。 Once that's created scan through the object to find any matches.创建完成后,扫描 object 以查找任何匹配项。

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>

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: 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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM