类型检测

原文来自:snippets.barretlee.com,只是为了自己学习收集特意fork了一遍。如有侵权,联系删除:i@webcliwn.net

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
////
// A collection of function for advanced type checking
// @author Hugo Giraudel
////

@function is-number($value) {
@return type-of($value) == 'number';
}

@function is-time($value) {
@return is-number($value) and index('ms' 's', unit($value)) != null;
}

@function is-duration($value) {
@return is-time($value);
}

@function is-angle($value) {
@return is-number($value) and index('deg' 'rad' 'grad' 'turn', unit($value)) != null;
}

@function is-frequency($value) {
@return is-number($value) and index('Hz' 'kHz', unit($value)) != null;
}

@function is-integer($value) {
@return is-number($value) and round($value) == $value;
}

@function is-relative-length($value) {
@return is-number($value) and index('em' 'ex' 'ch' 'rem' 'vw' 'vh' 'vmin' 'vmax', unit($value)) != null;
}

@function is-absolute-length($value) {
@return is-number($value) and index('cm' 'mm' 'in' 'px' 'pt' 'pc', unit($value)) != null;
}

@function is-percentage($value) {
@return is-number($value) and unit($value) == '%';
}

@function is-length($value) {
@return is-relative-length($value) or is-absolute-length($value);
}

@function is-resolution($value) {
@return is-number($value) and index('dpi' 'dpcm' 'dppx', unit($value)) != null;
}

@function is-position($value) {
@return is-length($value) or is-percentage($value) or index('top' 'right' 'bottom' 'left' 'center', $value) != null;
}