All files / lib crunchPermissionsPolicyHeader.js

86.67% Statements 13/15
80% Branches 8/10
100% Functions 3/3
86.67% Lines 13/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 362x 6x   74x   74x 72x     74x   76x 1x     75x         75x           75x     74x   74x        
module.exports = function crunchPermissionsPolicyHeader(headerValue) {
	return Object.entries(headerValue)
		.reduce((accumulator, [key, value]) => {
			let serializedValue = value
 
			if (!Array.isArray(value)) {
				serializedValue = value.split(' ')
			}
 
			serializedValue = serializedValue.map(item => {
				// Remove unnecessary quotes from tokens
				if (item.includes('*')) {
					return '*'
				}
 
				Iif (item === "'self'") {
					return 'self'
				}
 
				// Add quotes to all items that aren't tokens
				Iif (!['*', 'self'].includes(item) && !/^['"].*['"]$/) {
					return item
						.replace(/^['"]/, '"')
						.replace(/['"]$/, '"')
				}
 
				return item
			})
 
			accumulator.push(`${key}=(${serializedValue.join(' ')})`)
 
			return accumulator
		}, [])
		.join(',')
}