UNPKG

@shopgate/engage

Version:
40 lines 5.24 kB
function _typeof(obj){if(typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"){_typeof=function _typeof(obj){return typeof obj;};}else{_typeof=function _typeof(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj;};}return _typeof(obj);}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}function _defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor);}}function _createClass(Constructor,protoProps,staticProps){if(protoProps)_defineProperties(Constructor.prototype,protoProps);if(staticProps)_defineProperties(Constructor,staticProps);return Constructor;}/* eslint-disable class-methods-use-this */import get from'lodash/get';import appConfig,{writeToConfig,equalStructureComparator}from'@shopgate/pwa-common/helpers/config';/** * Parses a JSON object and resolves placeholders with values from the object. */export var ThemeConfigResolver=/*#__PURE__*/function(){/** * @param {Object} config The configuration to resolve references for. * @param {string} [delimiter='$.'] What the replaceable starts with. * @param {string} [endDelimiter='.$'] What the replaceable ends with. Useful within some strings. * @constructor */function ThemeConfigResolver(){var config=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{};var delimiter=arguments.length>1&&arguments[1]!==undefined?arguments[1]:'$.';var endDelimiter=arguments.length>2&&arguments[2]!==undefined?arguments[2]:'.$';_classCallCheck(this,ThemeConfigResolver);this.config=config;this.delimiter=delimiter;// Some references require an ending delimiter. E.g.: "size: $.theme.reference.value.$rem" this.endDelimiter=endDelimiter;}/** * Resolves the JSON configuration. * @returns {Object} */return _createClass(ThemeConfigResolver,[{key:"resolve",value:function resolve(){if(!this.configIsFilled()){return this.config;}return this.processObject(this.config);}/** * Takes the current theme config and resolves all page- and widget-setting refs */},{key:"resolveAll",value:function resolveAll(){this.config=appConfig.theme;writeToConfig({theme:this.resolve()},equalStructureComparator);}/** * Checks whether the config has any keys. * @returns {boolean} */},{key:"configIsFilled",value:function configIsFilled(){return Object.keys(this.config).length>0;}/** * @param {Object} input The input object to process. * @returns {Object} */},{key:"processObject",value:function processObject(input){var _this=this;var output={};Object.keys(input).forEach(function(key){var item=input[key];if(_this.isObject(item)){output[key]=_this.processObject(item);return;}if(Array.isArray(item)){output[key]=_this.processArray(item);return;}if(_this.isString(item)){output[key]=_this.processString(item);return;}output[key]=item;});return output;}/** * @param {Array} input The input array to process. * @returns {Array} */},{key:"processArray",value:function processArray(input){var _this2=this;return input.map(function(item){if(_this2.isObject(item)){return _this2.processObject(item);}if(Array.isArray(item)){return _this2.processArray(item);}if(_this2.isString(item)){return _this2.processString(item);}return item;});}/** * @param {string} input The input string to process. * @returns {string} */},{key:"processString",value:function processString(input){var _this3=this;// Replace all variable references in the given string if any exist var value=input;var replacementValue;var preRegex=new RegExp(".*".concat(// Escape delimiter to use within another reg exp. this.delimiter.replace(/[.*+?^${}()|[\]\\]/g,'\\$&')),'g');var postRegex=new RegExp(/[^a-zA-Z0-9_$.].*$/,'g');var partials=value.split(this.delimiter).map(function(partial,index){// The first entry can never contain references if(index===0){return partial;}var partialValue="".concat(_this3.delimiter).concat(partial);return partialValue.split(_this3.endDelimiter).map(function(s,i){// Can only have two parts with the first being either a path or plain text if(i>0){return s;}// Replace reference if this part contains one var path=s.replace(preRegex,'').replace(postRegex,'');replacementValue=get(_this3.config,path);return s.replace("".concat(_this3.delimiter).concat(path),replacementValue);}).join('');});value=partials.join('');// Keep the original type if it's not a combined value if(replacementValue!==undefined&&replacementValue.toString()===value){return replacementValue;}return value;}/** * Checks whether the props is a string. * @param {*} prop The property to check. * @returns {boolean} */},{key:"isString",value:function isString(prop){return typeof prop==='string';}/** * Whether the prop is an object. * @param {*} prop The prop to test. * @returns {boolean} */},{key:"isObject",value:function isObject(prop){return _typeof(prop)==='object'&&prop!==null&&prop.constructor===Object;}}]);}();/* eslint-enable class-methods-use-this */