JS替换对象中的占位符值

时间:2018-04-03 07:40:20

标签: javascript templates object lodash

我有模板,其中占位符指定如下。 (.event.id.event.properties.file,...)

瞧,我想用模板中的占位符替换下面event - 对象的值。

var templates = [
  {
    a:"1",
    a2: {some:"other Obj"},
    a3: ["or","array"],
  
    b: ".event.id",
    b2: "'.event.name' and more strings",
  },
  {
    dontKnowKeys: "of where the .event will be in",
    mightNotBeHere: "no placeholder here",
    butHereAgain: ".event"
    andSomeDeep: ".event.properties.file is Full"
  }
];

var event = {
  id: "1234-uuid-something",
  name: "I Am an awesome Event",
  properties: {
    file: "/root/foo",
    type: "error"
    }
  };


// First thought (pretty dirty, huh?)
JSON.parse(JSON.stringify(templates).replace(someMagicRegex, event))

// Second thought
templates = templates.map((tpl) => {
  return _.values(tpl).replace('.event.([a-z]+)',event["$1"]);
};

但这对properties.file密钥不起作用。

此外,如果层次结构任意长度.event.properties.file.cwd.path

,该怎么办?

毕竟我想拥有以下物品:

var out = [
  {
    a:"1",
    a2: {some:"other Obj"},
    a3: ["or","array"],
  
    b: "1234-uuid-something",
    b2: "'I Am an awesome Event' and more strings",
  },
  {
    dontKnowKeys: "of where the {\"id\":\"1234-uuid-something\",\"name\":\"I Am an  awesome Event\",\"properties\":{\"file\":\"/root/foot\",\"type\":\"error\"}} will be in",
    mightNotBeHere: "no placeholder here",
    butHereAgain: "{\"id\":\"1234-uuid-something\",\"name\":\"I Am an  awesome Event\",\"properties\":{\"file\":\"/root/foot\",\"type\":\"error\"}}"
    andSomeDeep: "/root/foo is Full"
  }
];

1 个答案:

答案 0 :(得分:1)

为你试过一个解决方案。试试

注意:这仅适用于当前具有简单数组和模块的模板结构。对象。密钥选择后正则表达式需要空间。根据你的需要改变它。

var templates = [
  {
    a: "1",
    a2: {some: "other Obj"},
    a3: ["or", "array"],

    b: ".event.id",
    b2: "' .event.name ' and more strings",
  },
  {
    dontKnowKeys: "of where the .event will be in",
    mightNotBeHere: "no placeholder here",
    butHereAgain: ".event.properties.type.depth.key",
    andSomeDeep: ".event.properties.file is Full"
  }
];

var event = {
  id: "1234-uuid-something",
  name: "I Am an awesome Event",
  properties: {
    file: "/root/foo",
    type: {depth:{key:"i am deep"}}
  }
};


// First thought (pretty dirty, huh?)
//JSON.parse(JSON.stringify(templates).replace(someMagicRegex, event))

// Second thought
templates = templates.map((tpl) => {
  return handleObject(tpl);
});

function handleObject(obj) {
  for (let keyname in obj) {
    obj[keyname] = checkTypes(obj[keyname])
  }
  return obj;
}

// assume you have objects and arrays also
function checkTypes(value) {
  if (typeof value === 'string') {
       return replaceVal(value);
  } else if (_.isArray(value)) {
    //assume array has only strings
     return value.map((d)=>replaceVal(d))
  } else if (_.isObject(value)) {
    return handleObject(value);
  }
}


function replaceVal(str) {
  //console.log(str);
  return str.replace(/.event.([^\s]+)/, (match,capture)=>_.get(event,capture))
}

console.log(templates);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.5/lodash.min.js"></script>