rotate.mjs 749 B

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