utils.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. "use strict";
  2. const {
  3. inspect
  4. } = require('util');
  5. const _ = require('lodash');
  6. const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
  7. exports.createAssetsFilter = createAssetsFilter;
  8. function createAssetsFilter(excludePatterns) {
  9. const excludeFunctions = _(excludePatterns).castArray().compact().map(pattern => {
  10. if (typeof pattern === 'string') {
  11. pattern = new RegExp(pattern, 'u');
  12. }
  13. if (_.isRegExp(pattern)) {
  14. return asset => pattern.test(asset);
  15. }
  16. if (!_.isFunction(pattern)) {
  17. throw new TypeError(`Pattern should be either string, RegExp or a function, but "${inspect(pattern, {
  18. depth: 0
  19. })}" got.`);
  20. }
  21. return pattern;
  22. }).value();
  23. if (excludeFunctions.length) {
  24. return asset => _.every(excludeFunctions, fn => fn(asset) !== true);
  25. } else {
  26. return () => true;
  27. }
  28. }
  29. /**
  30. * @desc get string of current time
  31. * format: dd/MMM HH:mm
  32. * */
  33. exports.defaultTitle = function () {
  34. const time = new Date();
  35. const year = time.getFullYear();
  36. const month = MONTHS[time.getMonth()];
  37. const day = time.getDate();
  38. const hour = `0${time.getHours()}`.slice(-2);
  39. const minute = `0${time.getMinutes()}`.slice(-2);
  40. const currentTime = `${day} ${month} ${year} at ${hour}:${minute}`;
  41. return `${process.env.npm_package_name || 'Webpack Bundle Analyzer'} [${currentTime}]`;
  42. };