extension.js 923 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. 'use strict';
  2. var path = require('path');
  3. function getLongExtension(basename) {
  4. if (basename[basename.length - 1] === '.') {
  5. return null;
  6. }
  7. var startIndex = (basename[0] === '.') ? 1 : 0;
  8. var dotIndex = basename.indexOf('.', startIndex);
  9. if (dotIndex <= startIndex) {
  10. return null;
  11. }
  12. return basename.slice(dotIndex);
  13. }
  14. function getPossibleExtensions(longExtension) {
  15. var arr = [longExtension];
  16. var len = longExtension.length;
  17. var startIndex = 1;
  18. while (startIndex < len) {
  19. var dotIndex = longExtension.indexOf('.', startIndex);
  20. if (dotIndex < 0) {
  21. break;
  22. }
  23. arr.push(longExtension.slice(dotIndex));
  24. startIndex = dotIndex + 1;
  25. }
  26. return arr;
  27. }
  28. module.exports = function(input) {
  29. var basename = path.basename(input);
  30. var longExtension = getLongExtension(basename);
  31. if (!longExtension) {
  32. return;
  33. }
  34. return getPossibleExtensions(longExtension);
  35. };