Passed
Pull Request — master (#555)
by John
03:00
created

src/utils/SassGetGridConfig.js   A

Complexity

Total Complexity 8
Complexity/F 2.67

Size

Lines of Code 62
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 41
c 0
b 0
f 0
dl 0
loc 62
rs 10
wmc 8
mnd 5
bc 5
fnc 3
bpm 1.6666
cpm 2.6666
noi 1
1
/**
2
 * @copyright Copyright (c) 2018 John Molakvoæ <[email protected]>
3
 *
4
 * @author John Molakvoæ <[email protected]>
5
 *
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
const sass = require('node-sass')
24
const sassUtils = require('node-sass-utils')(sass)
25
26
// sass plugin to implement js configs into scss
27
const sassVars = require('../assets/grid-sizes')
28
29
// Allows us to use js variables in sass so we can havea global config
30
// https://github.com/planetflash/sharing_variables_js_sass/
31
const convertStringToSassDimension = function(result) {
32
	// Only attempt to convert strings
33
	if (typeof result !== 'string') {
34
		return result
35
	}
36
37
	const cssUnits = [
38
		'rem',
39
		'em',
40
		'vh',
41
		'vw',
42
		'vmin',
43
		'vmax',
44
		'ex',
45
		'%',
46
		'px',
47
		'cm',
48
		'mm',
49
		'in',
50
		'pt',
51
		'pc',
52
		'ch'
53
	]
54
	const parts = result.match(/[a-zA-Z]+|[0-9]+/g)
55
	const value = parts[0]
56
	const unit = parts[parts.length - 1]
57
	if (cssUnits.indexOf(unit) !== -1) {
58
		result = new sassUtils.SassDimension(parseInt(value, 10), unit)
59
	}
60
61
	return result
62
}
63
64
module.exports = function(keys) {
65
	keys = keys.getValue().split('.')
66
	var result = sassVars
67
68
	for (let i = 0; i < keys.length; i++) {
69
		result = result[keys[i]]
70
71
		// Convert to SassDimension if dimenssion
72
		if (typeof result === 'string') {
73
			result = convertStringToSassDimension(result)
74
		} else if (typeof result === 'object') {
75
			Object.keys(result).forEach(function(key) {
76
				var value = result[key]
0 ignored issues
show
Bug introduced by
The variable result is changed as part of the for loop for example by result.keys.i on line 69. Only the value of the last iteration will be visible in this function if it is called after the loop.
Loading history...
77
				result[key] = convertStringToSassDimension(value)
78
			})
79
		}
80
	}
81
82
	result = sassUtils.castToSass(result)
83
	return result
84
}
85