leading-zero.js 860 B

12345678910111213141516171819202122232425262728293031323334353637
  1. module.exports = {
  2. name: 'leading-zero',
  3. syntax: ['css', 'less', 'sass', 'scss'],
  4. accepts: { boolean: [true, false] },
  5. /**
  6. * Processes tree node.
  7. * @param {node} node
  8. */
  9. process: function(node) {
  10. if (!node.is('number')) return;
  11. if (this.getValue('leading-zero')) {
  12. if (node.content[0] === '.')
  13. node.content = '0' + node.content;
  14. } else {
  15. node.content = node.content.replace(/^0+(?=\.)/, '');
  16. }
  17. },
  18. /**
  19. * Detects the value of an option at the tree node.
  20. *
  21. * @param {node} node
  22. */
  23. detect: function(node) {
  24. if (!node.is('number')) return;
  25. if (node.content.match(/^\.[0-9]+/)) {
  26. return false;
  27. } else if (node.content.match(/^0\.[0-9]+/)) {
  28. return true;
  29. }
  30. }
  31. };