.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / stylelint / lib / utils / getUnitFromValueNode.js
1 /* @flow */
2 "use strict";
3
4 const _ = require("lodash");
5 const blurInterpolation = require("./blurInterpolation");
6 const isStandardSyntaxValue = require("./isStandardSyntaxValue");
7 const valueParser = require("postcss-value-parser");
8
9 /**
10  * Get unit from value node
11  *
12  * Returns `null` if the unit is not found.
13  */
14 module.exports = function(node /*: Object*/) /*: ?string*/ {
15   if (!node || (node && !node.value)) {
16     return null;
17   }
18
19   const value = blurInterpolation(node.value, "")
20     // ignore hack unit
21     .replace("\\0", "")
22     .replace("\\9", "")
23     // ignore decimal place
24     .replace(".", "");
25
26   if (
27     node.type !== "word" ||
28     !isStandardSyntaxValue(value) ||
29     !_.isFinite(parseInt(value)) ||
30     node.value[0] === "#"
31   ) {
32     return null;
33   }
34
35   const parsedUnit = valueParser.unit(value);
36
37   if (!parsedUnit) {
38     return null;
39   }
40
41   return parsedUnit.unit;
42 };