matchPathPrefix()function

matchPathPrefix(target: PossiblePath, base: PossiblePath, caller: AnyCaller = matchPathPrefix): AbsolutePath | undefined
ParamType
targetPossiblePath
Path to match against base — relative paths resolve against base. required
basePossiblePath
Base path whose prefix is stripped from target. required
callerAnyCaller
Function to attribute a thrown error to (defaults to matchPathPrefix itself). Defaults to matchPathPrefix
Return
AbsolutePath | undefined
The remaining absolute path after stripping base, / for an exact match, or undefined if target is not under base.
Throws
RequiredError
If target or base is not a valid path.

Match and strip a base path prefix from a path using segment-aware pathname rules.

  • Both inputs must be absolute paths that begin with /.
  • Returns / when the paths are an exact match.

Examples

matchPathPrefix("/a/b", "/a") // "/b"