unit.js 970 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. 'use strict';
  2. /**
  3. * https://github.com/TrySound/postcss-value-parser/blob/fc679a7e17877841ff9fe455722280b65abd4f28/lib/unit.js
  4. * parse node -> number and unit
  5. */
  6. const minus = '-'.charCodeAt(0);
  7. const plus = '+'.charCodeAt(0);
  8. const dot = '.'.charCodeAt(0);
  9. module.exports = function unit(value) {
  10. let pos = 0;
  11. const length = value.length;
  12. let dotted = false;
  13. let containsNumber = false;
  14. let code;
  15. let number = '';
  16. while (pos < length) {
  17. code = value.charCodeAt(pos);
  18. if (code >= 48 && code <= 57) {
  19. number += value[pos];
  20. containsNumber = true;
  21. } else if (code === dot) {
  22. if (dotted) {
  23. break;
  24. }
  25. dotted = true;
  26. number += value[pos];
  27. } else if (code === plus || code === minus) {
  28. if (pos !== 0) {
  29. break;
  30. }
  31. number += value[pos];
  32. } else {
  33. break;
  34. }
  35. pos += 1;
  36. }
  37. return containsNumber ? {
  38. number,
  39. unit: value.slice(pos),
  40. } : false;
  41. };