unit.js 933 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. var minus = '-'.charCodeAt(0);
  2. var plus = '+'.charCodeAt(0);
  3. var dot = '.'.charCodeAt(0);
  4. module.exports = function (value) {
  5. var pos = 0;
  6. var length = value.length;
  7. var dotted = false;
  8. var containsNumber = false;
  9. var code;
  10. var number = '';
  11. while (pos < length) {
  12. code = value.charCodeAt(pos);
  13. if (code >= 48 && code <= 57) {
  14. number += value[pos];
  15. containsNumber = true;
  16. } else if (code === dot) {
  17. if (dotted) {
  18. break;
  19. }
  20. dotted = true;
  21. number += value[pos];
  22. } else if (code === plus || code === minus) {
  23. if (pos !== 0) {
  24. break;
  25. }
  26. number += value[pos];
  27. } else {
  28. break;
  29. }
  30. pos += 1;
  31. }
  32. return containsNumber ? {
  33. number: number,
  34. unit: value.slice(pos)
  35. } : false;
  36. };