UNPKG

3.1 kBJavaScriptView Raw
1'use strict'
2
3const isPlainObj = require('is-plain-obj')
4const mapObj = require('map-obj')
5
6const { removeFalsy } = require('./utils/remove_falsy')
7
8// Removes default values (empty objects and arrays) from the configuration.
9const simplifyConfig = function ({
10 build: { environment, processing: { css, html, images, js, ...processing } = {}, services, ...build } = {},
11 functions,
12 plugins,
13 headers,
14 redirects,
15 context = {},
16 ...config
17}) {
18 const buildA = {
19 ...build,
20 ...simplifyEnvironment(environment),
21 ...removeEmptyObject(
22 {
23 ...processing,
24 ...removeEmptyObject(css, 'css'),
25 ...removeEmptyObject(html, 'html'),
26 ...removeEmptyObject(images, 'images'),
27 ...removeEmptyObject(js, 'js'),
28 },
29 'processing',
30 ),
31 ...removeEmptyObject(services, 'services'),
32 }
33 return removeFalsy({
34 ...config,
35 ...removeEmptyObject(simplifyFunctions(functions), 'functions'),
36 ...removeEmptyObject(buildA, 'build'),
37 ...removeEmptyArray(plugins, 'plugins'),
38 ...removeEmptyArray(headers, 'headers'),
39 ...removeEmptyArray(simplifyRedirects(redirects), 'redirects'),
40 ...removeEmptyObject(simplifyContexts(context), 'context'),
41 })
42}
43
44const simplifyEnvironment = function (environment) {
45 return Array.isArray(environment)
46 ? removeEmptyArray(environment, 'environment')
47 : removeEmptyObject(environment, 'environment')
48}
49
50const simplifyContexts = function (contextProps) {
51 return mapObj(contextProps, simplifyContextProps)
52}
53
54const simplifyContextProps = function (context, contextConfig) {
55 return [context, simplifyConfig(contextConfig)]
56}
57
58const simplifyFunctions = function (functions) {
59 return isPlainObj(functions) ? Object.entries(functions).reduce(simplifyFunction, {}) : functions
60}
61
62const simplifyFunction = function (functions, [key, value]) {
63 return { ...functions, ...removeEmptyObject(value, key) }
64}
65
66const simplifyRedirects = function (redirects) {
67 return Array.isArray(redirects) ? redirects.map(simplifyRedirect) : redirects
68}
69
70const simplifyRedirect = function (redirect) {
71 if (!isPlainObj(redirect)) {
72 return redirect
73 }
74
75 const { force, proxy, query, conditions, headers, ...redirectA } = redirect
76 return {
77 ...redirectA,
78 ...removeDefaultValue(force, 'force', false),
79 ...removeDefaultValue(proxy, 'proxy', false),
80 ...removeEmptyObject(query, 'query'),
81 ...removeEmptyObject(conditions, 'conditions'),
82 ...removeEmptyObject(headers, 'headers'),
83 }
84}
85
86const removeDefaultValue = function (value, propName, defaultValue) {
87 return value === defaultValue ? {} : { [propName]: value }
88}
89
90const removeEmptyObject = function (object, propName) {
91 if (!isPlainObj(object)) {
92 return {}
93 }
94
95 const objectA = removeFalsy(object)
96 return Object.keys(objectA).length === 0 ? {} : { [propName]: objectA }
97}
98
99const removeEmptyArray = function (array, propName) {
100 if (!Array.isArray(array)) {
101 return {}
102 }
103
104 return array.length === 0 ? {} : { [propName]: array }
105}
106
107module.exports = { simplifyConfig, removeEmptyObject, removeEmptyArray }