rotate.cjs 780 B

1234567891011121314151617181920212223242526272829303132333435
  1. 'use strict';
  2. function rotateFromString(value, defaultValue = 0) {
  3. const units = value.replace(/^-?[0-9.]*/, "");
  4. function cleanup(value2) {
  5. while (value2 < 0) {
  6. value2 += 4;
  7. }
  8. return value2 % 4;
  9. }
  10. if (units === "") {
  11. const num = parseInt(value);
  12. return isNaN(num) ? 0 : cleanup(num);
  13. } else if (units !== value) {
  14. let split = 0;
  15. switch (units) {
  16. case "%":
  17. split = 25;
  18. break;
  19. case "deg":
  20. split = 90;
  21. }
  22. if (split) {
  23. let num = parseFloat(value.slice(0, value.length - units.length));
  24. if (isNaN(num)) {
  25. return 0;
  26. }
  27. num = num / split;
  28. return num % 1 === 0 ? cleanup(num) : 0;
  29. }
  30. }
  31. return defaultValue;
  32. }
  33. exports.rotateFromString = rotateFromString;