Skip to content

Commit

Permalink
#1270: refactored to class
Browse files Browse the repository at this point in the history
  • Loading branch information
JoernBerkefeld committed Apr 19, 2024
1 parent 3830399 commit 10c030e
Showing 1 changed file with 26 additions and 29 deletions.
55 changes: 26 additions & 29 deletions lib/util/replaceContentBlockReference.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,47 +31,45 @@
/**
* Util that contains logger and simple util methods
*/
export const ReplaceContentBlockReference = {
export default class ReplaceContentBlockReference {
/** @type {{id: AssetItemIdSimpleMap, key: AssetItemSimpleMap, pathName: AssetItemSimpleMap}} */
assetCacheMap: {
static assetCacheMap = {
id: null,
key: null,
pathName: null,
},
};
/** @type {{id: RegExp[], key: RegExp[], pathName: RegExp[]}} */
static #regexBy = {
id: [/ContentBlockById\(\s*"([0-9]+)"\s*\)/gim, /ContentBlockById\(\s*'([0-9]+)'\s*\)/gim],
key: [
/ContentBlockByKey\(\s*"([a-z0-9-/._]+)"\s*\)/gim,
/ContentBlockByKey\(\s*'([a-z0-9-/._]+)'\s*\)/gim,
],
pathName: [
/ContentBlockByName\(\s*"([ a-z0-9-\\._]+)"\s*\)/gim,
/ContentBlockByName\(\s*'([ a-z0-9-\\._]+)'\s*\)/gim,
],
};

/**
*
* @param {string} str full code string
* @param {ContentBlockConversionTypes} from what to replace
* @param {ContentBlockConversionTypes} to what to replace with
* @returns {string} replaced string
*/
replaceContentBlockReference(str, from, to) {
/** @type {{id: RegExp[], key: RegExp[], pathName: RegExp[]}} */
const regexBy = {
id: [
/ContentBlockById\(\s*"([0-9]+)"\s*\)/gim,
/ContentBlockById\(\s*'([0-9]+)'\s*\)/gim,
],
key: [
/ContentBlockByKey\(\s*"([a-z0-9-/._]+)"\s*\)/gim,
/ContentBlockByKey\(\s*'([a-z0-9-/._]+)'\s*\)/gim,
],
pathName: [
/ContentBlockByName\(\s*"([ a-z0-9-\\._]+)"\s*\)/gim,
/ContentBlockByName\(\s*'([ a-z0-9-\\._]+)'\s*\)/gim,
],
};
static replaceContentBlockReference(str, from, to) {
let result = str;
for (const regex of regexBy[from]) {
for (const regex of this.#regexBy[from]) {
result = result.replaceAll(regex, (match, identifier) => {
const asset = this._getAssetBy[from](identifier);
return this._replaceWith(asset, to);
const asset = this.#getAssetBy[from](identifier);
return this.#replaceWith(asset, to);
});
}
return result;
},
}
/** @type {{id: Function, key: Function, pathName: Function}} */
_getAssetBy: {
static #getAssetBy = {
/**
*
* @private
Expand Down Expand Up @@ -110,15 +108,14 @@ export const ReplaceContentBlockReference = {
pathName: `Content Builder\\Release 2 - BUILD\\Content Blocks Library\\NEW\\03. Header and banner\\Referred contentblock\\RS_Dev_Header_blue`,
};
},
},
};
/**
*
* @private
* @param {AssetItemSimple} asset asset object
* @param {ContentBlockConversionTypes} to replace with
* @returns {string} replaced string
*/
_replaceWith(asset, to) {
static #replaceWith(asset, to) {
switch (to) {
case 'id': {
return `ContentBlockById("${asset.id}")`;
Expand All @@ -130,5 +127,5 @@ export const ReplaceContentBlockReference = {
return `ContentBlockByName("${asset.pathName}")`;
}
}
},
};
}
}

0 comments on commit 10c030e

Please sign in to comment.