Passed
Push — master ( 78b380...a2100d )
by Rafael
01:41
created

parser.js ➔ ???   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
c 2
b 0
f 0
nc 1
dl 0
loc 13
rs 9.4285
nop 0
1
export class Parser {
2
	constructor() {
3
		this.regex = /(-?\d+px)|(rgb\(.+\))/g;
4
		this.propertyOrder = [ 'horizontal-position', 'vertical-position', 'blur-radius', 'spread-radius' ];
5
6
		this.default = {
7
			inset: false,
8
			'horizontal-position': 0,
9
			'vertical-position': 0,
10
			'blur-radius': 0,
11
			'spread-radius': 0,
12
			color: false
13
		};
14
	}
15
16
	/**
17
	 * Loop through a result and set properties accordingly.
18
	 *
19
	 * @since 1.0.0
20
	 *
21
	 * @param  {string} string CSS rule.
22
	 * @return {object}        Property values.
23
	 */
24
	parse( string ) {
25
		let setting = this.default,
26
			matches = string.match( this.regex ),
27
			propertyIndex = 0;
28
29
		setting.inset = -1 !== string.indexOf( 'inset' );
30
		if ( ! matches || 2 >= matches.length ) {
31
			return false;
32
		}
33
34
		for ( let match of matches ) {
35
			if ( -1 !== match.indexOf( 'rgb' ) ) {
36
				setting.color = match;
37
			} else if ( 'inset' === match ) {
38
				setting.inset = true;
39
			} else if ( -1 !== match.indexOf( 'px' ) ) {
40
				setting[this.propertyOrder[propertyIndex]] = parseInt( match );
41
				propertyIndex++;
42
			}
43
		}
44
45
		return setting;
46
	}
47
}
48
49
export { Parser as default };
50