1.0.2 • Published 6 years ago

unresolve v1.0.2

Weekly downloads
2
License
ISC
Repository
github
Last release
6 years ago

unresolve

Guess an import identifier that might require.resolve() to the same file path but starts with a package name.

API

This module exports one function:

unresolve(path, opt)

Expects path to be a traditional, mundane file system path of some file inside a node module. Returns a promise for a string that might be an import identifier that starts with a package name and would require.resolve() to path.

opt is an optional options object that supports these keys:

  • srcFile: If non-empty, path is expected to be a relative path and will be resolved relative to the parent directory of opt.srcFile.

Usage

from test/usage.js:

var self = require('../package.json').name, other = 'bluebird',
  othMain = require.resolve(other),
  othReadme = require.resolve(other + '/README.md'),
  othDir = dirname(othReadme) + '/',
  othSub = othMain.slice(othDir.length);

thenEqual(unresolve(othMain),   other + '/' + othSub);
thenEqual(unresolve(othReadme), other + '/README.md');
thenEqual(unresolve(othDir),    other);

thenEqual(unresolve(module.filename), self + '/test/usage.js');
thenEqual(unresolve(dirname(module.filename)), self + '/test');
thenEqual(unresolve('../', { srcFile: module.filename }), self);

Known issues

  • Needs more/better tests and docs.

 

License

ISC