I am working with JS and have a problem where I have two strings such as:
var test = "This is a {feature1} and {feature2} boy.";
var data = "This is a very good and smart boy.";
Considering whatever word is wrapped in {} is a variable, substituting the appropriate value of the variables will convert test to data.
The problem statement is we need to find the values of every word in case if the problem has a solution, otherwise, return false from the method.
So, in the above example, we have a return value as
{
feature1: 'very good',
feature2: 'smart'
}
So far, I have tried the below code which seems to be working though, but it seems a bit complex for this operation.
function matchAndGetVar(test, data) {
function multiSplit(data, splitters) {
function helper(dataArray, splitter) {
return dataArray.reduce((acc, curr) => {
acc.push(...curr.split(splitter));
return acc;
}, []);
}
return splitters.reduce((acc, curr) => helper(acc, curr), [data]).map((f, index) => {
if (index % 2 === 0) return f;
return '{' + f + '}';
});
}
var splittedTestData = multiSplit(test, ['{', '}']);
return splittedTestData.reduce((acc, curr, index) => {
if (curr.startsWith("{") && curr.endsWith("}")) {}
else if (index === 0) {
if (data.startsWith(curr)) {
data = data.substr(curr.length);
} else {
return false;
}
} else if (splittedTestData[index] === "") {
if (acc)
acc[splittedTestData[index - 1].substr(1, splittedTestData[index - 1].length - 2)] = data;
data = "";
} else {
const index1 = data.indexOf(splittedTestData[index]);
if (index1 === -1) return false;
if (acc)
acc[splittedTestData[index - 1].substr(1, splittedTestData[index - 1].length - 2)] = data.substr(0, index1);
data = data.substr(index1 + splittedTestData[index].length);
}
return acc;
}, {});
}
var test = "This is a {feature1} and {feature2} boy.";
var data = "This is a very good and smart boy.";
console.log(matchAndGetVar(test, data));
Can anyone suggest something simpler approach or any modification to the implementation?
helpermeans nothing. Every function is a helper. Name your functions with verbs that represent what they do.