Açıklama Yok

index.js 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. 'use strict';
  2. var path = require('path');
  3. var isAbsolute = require('is-absolute');
  4. var pathRoot = require('path-root');
  5. var MapCache = require('map-cache');
  6. var cache = new MapCache();
  7. module.exports = function(filepath) {
  8. if (typeof filepath !== 'string') {
  9. throw new TypeError('parse-filepath expects a string');
  10. }
  11. if (cache.has(filepath)) {
  12. return cache.get(filepath);
  13. }
  14. var obj = {};
  15. if (typeof path.parse === 'function') {
  16. obj = path.parse(filepath);
  17. obj.extname = obj.ext;
  18. obj.basename = obj.base;
  19. obj.dirname = obj.dir;
  20. obj.stem = obj.name;
  21. } else {
  22. define(obj, 'root', function() {
  23. return pathRoot(this.path);
  24. });
  25. define(obj, 'extname', function() {
  26. return path.extname(filepath);
  27. });
  28. define(obj, 'ext', function() {
  29. return this.extname;
  30. });
  31. define(obj, 'name', function() {
  32. return path.basename(filepath, this.ext);
  33. });
  34. define(obj, 'stem', function() {
  35. return this.name;
  36. });
  37. define(obj, 'base', function() {
  38. return this.name + this.ext;
  39. });
  40. define(obj, 'basename', function() {
  41. return this.base;
  42. });
  43. define(obj, 'dir', function() {
  44. var dir = path.dirname(filepath);
  45. if (dir === '.') {
  46. return (filepath[0] === '.') ? dir : '';
  47. } else {
  48. return dir;
  49. }
  50. });
  51. define(obj, 'dirname', function() {
  52. return this.dir;
  53. });
  54. }
  55. obj.path = filepath;
  56. define(obj, 'absolute', function() {
  57. return path.resolve(this.path);
  58. });
  59. define(obj, 'isAbsolute', function() {
  60. return isAbsolute(this.path);
  61. });
  62. cache.set(filepath, obj);
  63. return obj;
  64. };
  65. function define(obj, prop, fn) {
  66. var cached;
  67. Object.defineProperty(obj, prop, {
  68. configurable: true,
  69. enumerable: true,
  70. set: function(val) {
  71. cached = val;
  72. },
  73. get: function() {
  74. return cached || (cached = fn.call(obj));
  75. }
  76. });
  77. }