getDimension.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. 'use strict';
  2. const blurInterpolation = require('./blurInterpolation');
  3. const isStandardSyntaxValue = require('./isStandardSyntaxValue');
  4. const valueParser = require('postcss-value-parser');
  5. /**
  6. * Get Dimension from value node;
  7. * `unit` and `number` return null if neither is found
  8. *
  9. * @param {import('postcss-value-parser').Node} node
  10. *
  11. * @returns {{unit: null, number: null} | valueParser.Dimension}
  12. */
  13. module.exports = function getDimension(node) {
  14. if (!node || !node.value) {
  15. return {
  16. unit: null,
  17. number: null,
  18. };
  19. }
  20. // Ignore non-word nodes
  21. if (node.type !== 'word') {
  22. return {
  23. unit: null,
  24. number: null,
  25. };
  26. }
  27. // Ignore non standard syntax
  28. if (!isStandardSyntaxValue(node.value)) {
  29. return {
  30. unit: null,
  31. number: null,
  32. };
  33. }
  34. // Ignore HEX
  35. if (node.value.startsWith('#')) {
  36. return {
  37. unit: null,
  38. number: null,
  39. };
  40. }
  41. // Remove non standard stuff
  42. const value = blurInterpolation(node.value, '')
  43. // ignore hack unit
  44. .replace('\\0', '')
  45. .replace('\\9', '');
  46. const parsedUnit = valueParser.unit(value);
  47. if (!parsedUnit) {
  48. return {
  49. unit: null,
  50. number: null,
  51. };
  52. }
  53. return parsedUnit;
  54. };