time.js 753 B

12345678910111213141516171819202122232425262728293031
  1. var OptimizationLevel = require('../../../options/optimization-level').OptimizationLevel;
  2. var TIME_VALUE = /^(-?[\d.]+)(m?s)$/;
  3. var plugin = {
  4. level1: {
  5. value: function time(name, value, options) {
  6. if (!options.level[OptimizationLevel.One].replaceTimeUnits) {
  7. return value;
  8. }
  9. if (!TIME_VALUE.test(value)) {
  10. return value;
  11. }
  12. return value.replace(TIME_VALUE, function(match, val, unit) {
  13. var newValue;
  14. if (unit == 'ms') {
  15. newValue = parseInt(val) / 1000 + 's';
  16. } else if (unit == 's') {
  17. newValue = parseFloat(val) * 1000 + 'ms';
  18. }
  19. return newValue.length < match.length ? newValue : match;
  20. });
  21. }
  22. }
  23. };
  24. module.exports = plugin;