Created
August 3, 2021 18:51
-
-
Save marlonklc/82e709dd9b7b6f7e754a675ea812c2ae to your computer and use it in GitHub Desktop.
My own unflatten
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const _ = require('lodash') | |
const SETTINGS = { | |
header_background_color: value(/whitelabel\.header_background_color/g), | |
footer_background_color: value(/whitelabel\.footer_background_color/g), | |
footer_sections: array(/whitelabel\.footer_sections\../g, 2), | |
footer_sections_title: value(/whitelabel\.footer_sections\..\.title/g), | |
footer_sections_links: array(/whitelabel\.footer_sections\..\.links\../g, 4), | |
footer_sections_links_title: value(/whitelabel\.footer_sections\..\.links\..\.title/g), | |
footer_sections_links_tooltip: value(/whitelabel\.footer_sections\..\.links\..\.tooltip/g), | |
footer_sections_links_link: value(/whitelabel\.footer_sections\..\.links\..\.link/g), | |
} | |
function array(regex, position) { | |
return { regex, position } | |
} | |
function value(regex) { | |
return { regex } | |
} | |
class WhitelabelService { | |
extract(setting) { | |
const settings = _.get(setting, 'ox_specific.qx_settings', []) | |
const headerBackgroundColor = this._findSettingValue(settings, SETTINGS.header_background_color.regex) | |
const footerBackgroundColor = this._findSettingValue(settings, SETTINGS.footer_background_color.regex) | |
const sections = this._getArrayOfSettings(settings, SETTINGS.footer_sections.regex) | |
const splitedSections = this._splitByPosition(sections, SETTINGS.footer_sections.position) | |
const result = splitedSections.map(section => { | |
const sectionTitle = this._findSettingValue(section, SETTINGS.footer_sections_title.regex) | |
const links = this._getArrayOfSettings(section, SETTINGS.footer_sections_links.regex) | |
const splitedLinks = this._splitByPosition(links, SETTINGS.footer_sections_links.position) | |
const resultLinks = splitedLinks.map(link => { | |
const linkTitle = this._findSettingValue(link, SETTINGS.footer_sections_links_title.regex) | |
const linkTooltip = this._findSettingValue(link, SETTINGS.footer_sections_links_tooltip.regex) | |
const linkLink = this._findSettingValue(link, SETTINGS.footer_sections_links_link.regex) | |
return { | |
title: linkTitle, | |
tooltip: linkTooltip, | |
link: linkLink | |
} | |
}) | |
return { | |
title: sectionTitle, | |
links: resultLinks | |
} | |
}) | |
return result | |
} | |
_splitByPosition(settings, position) { | |
const result = [] | |
settings.forEach(section => { | |
const split = section.wx_settingsname.split('.') | |
if (split.length - 1 < position) return | |
const index = split[position] | |
if (!result[index]) { | |
result[index] = [] | |
} | |
result[index].push(section) | |
}) | |
return result | |
} | |
_findSettingValue(settings, regex) { | |
const foundSetting = settings.find(item => item.wx_settingsname.match(regex)) | |
return this._getSettingValue(foundSetting) | |
} | |
_getSettingValue(setting) { | |
return _.get(setting, 'wx_activevalue.wx_value', '') | |
} | |
_getArrayOfSettings(settings, regex) { | |
return settings.filter(item => item.wx_settingsname.match(regex)) || [] | |
} | |
} | |
module.exports = new WhitelabelService(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment