Blame view

node_modules/clean-css/lib/options/rounding-precision.js 1.92 KB
aaac7fed   liuqimichale   add
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  var override = require('../utils/override');
  
  var INTEGER_PATTERN = /^\d+$/;
  
  var ALL_UNITS = ['*', 'all'];
  var DEFAULT_PRECISION = 'off'; // all precision changes are disabled
  var DIRECTIVES_SEPARATOR = ','; // e.g. *=5,px=3
  var DIRECTIVE_VALUE_SEPARATOR = '='; // e.g. *=5
  
  function roundingPrecisionFrom(source) {
    return override(defaults(DEFAULT_PRECISION), buildPrecisionFrom(source));
  }
  
  function defaults(value) {
    return {
      'ch': value,
      'cm': value,
      'em': value,
      'ex': value,
      'in': value,
      'mm': value,
      'pc': value,
      'pt': value,
      'px': value,
      'q': value,
      'rem': value,
      'vh': value,
      'vmax': value,
      'vmin': value,
      'vw': value,
      '%': value
    };
  }
  
  function buildPrecisionFrom(source) {
    if (source === null || source === undefined) {
      return {};
    }
  
    if (typeof source == 'boolean') {
      return {};
    }
  
    if (typeof source == 'number' && source == -1) {
      return defaults(DEFAULT_PRECISION);
    }
  
    if (typeof source == 'number') {
      return defaults(source);
    }
  
    if (typeof source == 'string' && INTEGER_PATTERN.test(source)) {
      return defaults(parseInt(source));
    }
  
    if (typeof source == 'string' && source == DEFAULT_PRECISION) {
      return defaults(DEFAULT_PRECISION);
    }
  
    if (typeof source == 'object') {
      return source;
    }
  
    return source
      .split(DIRECTIVES_SEPARATOR)
      .reduce(function (accumulator, directive) {
        var directiveParts = directive.split(DIRECTIVE_VALUE_SEPARATOR);
        var name = directiveParts[0];
        var value = parseInt(directiveParts[1]);
  
        if (isNaN(value) || value == -1) {
          value = DEFAULT_PRECISION;
        }
  
        if (ALL_UNITS.indexOf(name) > -1) {
          accumulator = override(accumulator, defaults(value));
        } else {
          accumulator[name] = value;
        }
  
        return accumulator;
      }, {});
  }
  
  module.exports = {
    DEFAULT: DEFAULT_PRECISION,
    roundingPrecisionFrom: roundingPrecisionFrom
  };