Skip to content

[Replacement]: path-is-inside #1313

Backlog
Backlog

Description

@rickysinghmba

Package to replace

path-is-inside

Package link

https://npmx.dev/package/path-is-inside

Native replacement(s)

path.relative

Documentation

https://nodejs.org/api/path.html#pathrelativefrom-to

Availability

All supported Node.js versions

Rationale

Same job as is-path-inside, which was added with an e18e docs page in #1090 (#1009). ~15.6M weekly downloads, all on the latest version (1.0.2), which hasn't been published since 2016. It's used directly by about 3,300 packages, so the downloads aren't coming from a single dependent.

The is-path-inside snippet works here with one change: path-is-inside returns true when the two paths are equal, so an empty relation should count as inside. I checked that against the package's own logic and it matches, except for paths that aren't normalized: path-is-inside does a plain string prefix check, so pathIsInside('/a/../b', '/a') returns true, while path.relative correctly treats it as outside.

This could reuse the is-path-inside docs page, or get its own short page that links to it, as discussed in #1022.

Code example (optional)

import pathIsInside from 'path-is-inside' // [!code --]
import path from 'node:path' // [!code ++]

const pathIsInside = (childPath, parentPath) => { // [!code ++]
  const relation = path.relative(parentPath, childPath) // [!code ++]
  return ( // [!code ++]
    relation === '' || // [!code ++]
    (relation !== '..' && // [!code ++]
      !relation.startsWith(`..${path.sep}`) && // [!code ++]
      relation !== path.resolve(childPath)) // [!code ++]
  ) // [!code ++]
} // [!code ++]

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions