( file: string )
| 223 | |
| 224 | // from the innermost node_modules the file names a package in |
| 225 | export const dependencyOf = ( |
| 226 | file: string |
| 227 | ): [root: string, name: string] | undefined => { |
| 228 | const starts = [...file.matchAll(nodeModulesSegment)] |
| 229 | for (let i = starts.length - 1; i >= 0; i--) { |
| 230 | packageInNodeModules.lastIndex = starts[i].index |
| 231 | const match = packageInNodeModules.exec(file) |
| 232 | if (match) { |
| 233 | return [ |
| 234 | file.slice(0, packageInNodeModules.lastIndex), |
| 235 | match[1].replace("\\", "/") |
| 236 | ] |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | // a leading .* would backtrack across a long path once per character |
| 242 | const nodeModulesSegment = /[/\\]node_modules(?=[/\\])/g |
no test coverage detected