# root-import-utils

> Functions for transforming import/require paths that have a prefix referring to the project root.

Latest version **1.1.0** (published 2017-03-03) · CC0-1.0 license · 0 weekly downloads

## Install

```sh
npm install root-import-utils
pnpm add root-import-utils
yarn add root-import-utils
bun add root-import-utils
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2017-03-03 |
| First published | 2016-09-06 |
| Weekly downloads | 0 |
| License | CC0-1.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Johnny Hauser |
| Maintainers | m59 |
| Keywords | import, require, transform, module, path, utilities, functions, project, root, relative |

## Links

- npm: https://www.npmjs.com/package/root-import-utils
- Repository: https://github.com/m59peacemaker/js-root-import-utils
- Homepage: https://github.com/m59peacemaker/js-root-import-utils#readme
- Issues: https://github.com/m59peacemaker/js-root-import-utils/issues
- npm.io page: https://npm.io/package/root-import-utils

## Dependencies (3)

- [path](https://npm.io/package/path.md) ^0.12.7
- [fj-pipe](https://npm.io/package/fj-pipe.md) ^1.1.0
- [regexp.escape](https://npm.io/package/regexp.escape.md) ^1.0.2

## Alternatives

- [base64url](https://npm.io/package/base64url.md) — 6.1M weekly downloads
- [get-installed-path](https://npm.io/package/get-installed-path.md) — 502.9K weekly downloads
- [@uppy/url](https://npm.io/package/@uppy/url.md) — 185.8K weekly downloads
- [@d3fc/d3fc-shape](https://npm.io/package/@d3fc/d3fc-shape.md) — 16.2K weekly downloads
- [localizer](https://npm.io/package/localizer.md) — 226 weekly downloads

## Recent versions

- 1.1.0 (latest) — 2017-03-03
- 1.0.5 — 2017-02-09
- 1.0.4 — 2016-11-13
- 1.0.3 — 2016-09-06
- 1.0.1 — 2016-09-06
- 1.0.0 — 2016-09-06

## README

# root-import-utils

Functions for transforming import/require paths that have a prefix referring to the project root.

## install

```sh
npm install root-import-utils
```

## usage

```js
const {
  hasPrefix,
  stripPrefix,
  getRelativePathToModule,
  getLongRelativePathToModule
} = require('root-import-utils')
```

## API

### `hasPrefix(prefix, path)`

```js
hasPrefix('~', '~/a/b.js') // -> true
```

- `prefix: string`
- `path: string`
- **returns**: `boolean`

### `stripPrefix(prefix, path)`

```js
stripPrefix('~', '~/a/b.js') // -> 'a/b.js'
```

- `prefix: string`
- `path: string`
- **returns**: `string` the path argument with prefix removed. The `/` after the prefix is also removed so that the remaining path is relative.

### `getRelativePathToModule(sourcePathFromRoot, modulePathFromRoot)`
### `getLongRelativePathToModule(sourcePathFromRoot, modulePathFromRoot)`

- `sourcePathFromRoot: string` relative path from project root to the file that has the import statement or require call
- `modulePathFromRoot: string` the module path of import/require with prefix removed so that it is a relative path from project root

`getLongRelativePathToModule()` simply prepends the relative path from sourcePathFromRoot to the project root to modulePathFromRoot, rather than calculating the most direct path from source to the target module. That means it will return a more verbose path in some situations. See the following example.

```js
getRelativePathToModule(    'a/b/c.js', 'a/b') // -> './'        // directly to module
getLongRelativePathToModule('a/b/c.js', 'a/b') // -> '../../a/b' // back to project root, forward to module
```

---
_Source: https://npm.io/package/root-import-utils · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
