JSPM

filename-to-module-name

0.1.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2
  • Score
    100M100P100Q54348F
  • License MIT

Get the shortest name file can be required by in Node.js

Package Exports

  • filename-to-module-name

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (filename-to-module-name) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

npm

filename-to-module-name

Build Status Dependency Status

Returns the shortest module name for the given filename.

Here is a pseudocode, effectively the inverse of require.resolve algorithm used by Node:

1. If X is a directory,
   a. return basename(X)
   b. STOP
2. Let Y be X with ".js", ".json" or ".node" removed
3. If exists(Y), return basename(X). STOP
4. For E in [".js", ".json", ".node"],
   a. If X ends with E, return basename(Y). STOP
   b. If exists(Y + E), return basename(X). STOP
5. Return basename(X)

Example

var moduleName = require('filename-to-module-name');

moduleName('./index.js');
//=> "index"

API

filenameToModuleName(filename)

Returns the module name. filename should be a name of an existing file or directory.

Install

npm install filename-to-module-name

License

MIT