-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
49 lines (45 loc) · 1.29 KB
/
utils.js
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
function validate(config, debug) {
const validate = require('jsonschema').validate
const schema = require(`./schema.json`)
if (debug) {
const required =
schema.properties.cep.properties.extensions.items.required
required.push('debug')
}
const validation = validate(config, schema)
const valid = validation.valid
if (!valid) {
const errors = validation.errors.map((error) => {
const stack = error.stack.replace('instance', 'package')
const words = stack.split(' ')
const path = words[0].toString()
return `${path}\n ${words.slice(1).join(' ')}`
}).join('\n')
throw errors
}
}
function capitalise(key) {
key = key.toLowerCase()
if (key.includes('min')) return 'MinSize'
if (key.includes('max')) return 'MaxSize'
if (key.includes('screen')) return 'ScreenPercentage'
return key.replace(/^\w/, (c) => c.toUpperCase())
}
function format(xml) {
const xmlFormat = require('xml-formatter')
return xmlFormat(xml, { collapseContent: true })
}
function transform(key) {
key = key.toLowerCase()
if (key.includes('dis')) return 'Disabled'
const dark = key.includes('dark')
const roll = key.includes('roll')
const newKey = !roll ? 'Normal' : 'RollOver'
return dark ? `Dark${newKey}` : newKey
}
module.exports = {
validate,
capitalise,
format,
transform,
}