index.js 754 B

1234567891011121314151617181920212223242526272829303132
  1. 'use strict';
  2. var path = require('path');
  3. function replaceExt(npath, ext) {
  4. if (typeof npath !== 'string') {
  5. return npath;
  6. }
  7. if (npath.length === 0) {
  8. return npath;
  9. }
  10. var nFileName = path.basename(npath, path.extname(npath)) + ext;
  11. var nFilepath = path.join(path.dirname(npath), nFileName);
  12. // Because `path.join` removes the head './' from the given path.
  13. // This removal can cause a problem when passing the result to `require` or
  14. // `import`.
  15. if (startsWithSingleDot(npath)) {
  16. return '.' + path.sep + nFilepath;
  17. }
  18. return nFilepath;
  19. }
  20. function startsWithSingleDot(fpath) {
  21. var first2chars = fpath.slice(0, 2);
  22. return first2chars === '.' + path.sep || first2chars === './';
  23. }
  24. module.exports = replaceExt;