String interpolation. Find the objects prop that matches the string inside […] and replace with the value of the prop

I am working on a function that finds the objects prop that matches the string inside […] and replaces with the value of the prop.. it also has to ignore any strings inside of [[…]] and remove the outer square brackets

For example

interpolate(‘Hello [name] [[author]]’, {name: ‘foo’, author: ‘bar’})

Would result in ‘Hello foo [author]’

Here is my code so far

const person1 = { name: 'John', author: 'John & John'};
const person2 = { name: 'Jill', author: 'Jk Jowling' };
const person3 = { name: 'Jack', author: 'Wacko Jacko'};

const str1 = 'Hello [name] [[author]]';

// Find the objects prop that matches the string inside [...] and replace with the value of the prop
// Ignore the values inside double brackets [[...]]


const interpolate = (string, obj) => {
// Throw errors if incorrect arguments are passed in
if (!string || typeof string !== 'string') {
    throw new Error(`"string" is required`);
}

if (!obj || typeof obj !== 'object') {
    throw new Error(`"object" is required`);
}

Object.keys(obj).map((value) => {
    string
    .split(' ')
    .indexOf(`[${value}]`) === 1 || string.indexOf(`[[${value}]]`) === -1
  ? console.log(string.replaceAll(`[${value}]`, obj[value])) 
  : console.log(`One or more unidentifiers found in String: ${string}`)
})


// Working better but still need to work on conditionals

// Removed RegEx

// Used replaceAll to target multiple [string]


};

interpolate(str1, person1);
interpolate(str1, person2);
interpolate(str1, person3);

161 thoughts on “String interpolation. Find the objects prop that matches the string inside […] and replace with the value of the prop”

  1. Have you ever considered about adding a little bit more than just your articles?
    I mean, what you say is fundamental and everything. However think of if you added some great graphics or video clips to give your posts
    more, “pop”! Your content is excellent but with pics and video
    clips, this site could definitely be one of the greatest in its niche.
    Awesome blog!

    Reply
  2. I don’t know whether it’s just me or if everyone else encountering
    problems with your blog. It seems like some of the written text in your posts are running off the screen. Can somebody
    else please comment and let me know if this is happening to them too?
    This may be a problem with my internet browser because I’ve had this
    happen previously. Thanks

    Reply
  3. Awesome blog you have here but I was curious about if you knew
    of any community forums that cover the same topics talked about in this article?

    I’d really love to be a part of group where I can get opinions from other
    experienced individuals that share the same interest.

    If you have any recommendations, please let me
    know. Appreciate it!

    Reply
  4. I feel that is among the such a lot significant info for me.
    And i am glad reading your article. But wanna statement on few common things, The web site taste is ideal, the articles is actually great :
    D. Good job, cheers

    Reply
  5. Hey there! This post could not be written any better!

    Reading this post reminds me of my old room mate!
    He always kept chatting about this. I will forward this article to him.

    Pretty sure he will have a good read. Thank you for sharing!

    Reply
  6. [url=http://cheapviagramedicationsale.monster/]where to buy female viagra pill[/url] [url=http://ordercheapcialis5mg.monster/]tadalafil 20mg uk[/url] [url=http://bestcialis20noprescription.monster/]cialis discount pharmacy[/url] [url=http://sildenafilnh.online/]60 mg sildenafil[/url] [url=http://cheapviagra50mgcost.quest/]paypal viagra uk[/url] [url=http://viagraprice.online/]25 mg viagra cost[/url] [url=http://viagratabsdrugstore.quest/]cheap viagra canadian pharmacy[/url] [url=http://cheapcialisdrugsale.monster/]tadalafil buy india[/url] [url=http://bestcialis20mgrx.monster/]where can i buy cialis online in canada[/url] [url=http://genericcialistabletforsale.quest/]average cost cialis daily use[/url]

    Reply