Skip to content

Commit

Permalink
#1270: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
JoernBerkefeld committed Apr 18, 2024
1 parent 63e7235 commit 4c6be6d
Showing 1 changed file with 60 additions and 79 deletions.
139 changes: 60 additions & 79 deletions lib/util/replaceContentBlockReference.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,88 +38,69 @@ export const ReplaceContentBlockReference = {
* @returns {string} replaced string
*/
replaceContentBlockReference(str, from, to) {
const regexById_double = /ContentBlockById\(\s*"([0-9]+)"\s*\)/gim;
const regexByKey_double = /ContentBlockByKey\(\s*"([a-z0-9-/._]+)"\s*\)/gim;
const regexByName_double = /ContentBlockByName\(\s*"([ a-z0-9-\\._]+)"\s*\)/gim;

const regexById_single = /ContentBlockById\(\s*'([0-9]+)'\s*\)/gim;
const regexByKey_single = /ContentBlockByKey\(\s*'([a-z0-9-/._]+)'\s*\)/gim;
const regexByName_single = /ContentBlockByName\(\s*'([ a-z0-9-\\._]+)'\s*\)/gim;

switch (from) {
case 'id': {
const doubleReplaced = str.replaceAll(regexById_double, (match, id) => {
const asset = this._getAssetById(id);
return this._replaceWith(asset, to);
});
return doubleReplaced.replaceAll(regexById_single, (match, id) => {
const asset = this._getAssetById(id);
return this._replaceWith(asset, to);
});
}
case 'key': {
const doubleReplaced = str.replaceAll(regexByKey_double, (match, key) => {
const asset = this._getAssetByKey(key);
return this._replaceWith(asset, to);
});
return doubleReplaced.replaceAll(regexByKey_single, (match, key) => {
const asset = this._getAssetByKey(key);
return this._replaceWith(asset, to);
});
}
case 'name': {
const doubleReplaced = str.replaceAll(regexByName_double, (match, pathName) => {
const asset = this._getAssetByPathName(pathName);
return this._replaceWith(asset, to);
});
return doubleReplaced.replaceAll(regexByName_single, (match, pathName) => {
const asset = this._getAssetByPathName(pathName);
return this._replaceWith(asset, to);
});
}
}
},
/**
*
* @private
* @param {string} pathName r__folder_Path +'/'+ name
* @returns {{id: number, key: string, pathName: string}} asset object
*/
_getAssetByPathName(pathName) {
// TODO
return { id: 9999, key: 'my-key', pathName: pathName };
},
/**
*
* @private
* @param {number} id id of asset
* @returns {{id: number, key: string, pathName: string}} asset object
*/
_getAssetById(id) {
// TODO
return {
id: id,
key: 'my-key',
pathName: `Content Builder\\Release 2 - BUILD\\Content Blocks Library\\NEW\\03. Header and banner\\Referred contentblock\\RS_Dev_Header_blue`,
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,
],
name: [
/ContentBlockByName\(\s*"([ a-z0-9-\\._]+)"\s*\)/gim,
/ContentBlockByName\(\s*'([ a-z0-9-\\._]+)'\s*\)/gim,
],
};
let result = str;
for (const regex of regexBy[from]) {
result = result.replaceAll(regex, (match, identifier) => {
const asset = this.getAssetBy[from](identifier);
return this._replaceWith(asset, to);
});
}
return result;
},
/**
*
* @private
* @param {string} key customerKey field of asset
* @returns {{id: number, key: string, pathName: string}} asset object
*/
_getAssetByKey(key) {
// TODO
return {
id: 9999,
key: key,
pathName: `Content Builder\\Release 2 - BUILD\\Content Blocks Library\\NEW\\03. Header and banner\\Referred contentblock\\RS_Dev_Header_blue`,
};
_getAssetBy: {
/**
*
* @private
* @param {string} pathName r__folder_Path +'/'+ name
* @returns {{id: number, key: string, pathName: string}} asset object
*/
name(pathName) {
// TODO
return { id: 9999, key: 'my-key', pathName: pathName };
},
/**
*
* @private
* @param {number} id id of asset
* @returns {{id: number, key: string, pathName: string}} asset object
*/
id(id) {
// TODO
return {
id: id,
key: 'my-key',
pathName: `Content Builder\\Release 2 - BUILD\\Content Blocks Library\\NEW\\03. Header and banner\\Referred contentblock\\RS_Dev_Header_blue`,
};
},
/**
*
* @private
* @param {string} key customerKey field of asset
* @returns {{id: number, key: string, pathName: string}} asset object
*/
key(key) {
// TODO
return {
id: 9999,
key: key,
pathName: `Content Builder\\Release 2 - BUILD\\Content Blocks Library\\NEW\\03. Header and banner\\Referred contentblock\\RS_Dev_Header_blue`,
};
},
},

/* const fixedString = testStringId.replaceAll(regexById, replaceIdWithName);
*/
/**
*
* @private
Expand Down

0 comments on commit 4c6be6d

Please sign in to comment.