Skip to content
Astera Technology

Render a small text template with one-pass substitutions

Retain used and unused keys without expanding tokens inside values.

Download complete case notes and answer keys ↓

Source contract and expected records

This is an original, fully synthetic mapping example. Inputs are plain, acyclic JSON-shaped records with the fields shown; the canonical shape and rejection codes belong to this example. Adapt the contract before using a different source.

The custom template uses {key}, where keys start with a lowercase letter and contain lowercase letters, digits or underscores. Other braces are invalid. Each referenced key must have a string value. Replace tokens once, recording used keys in first-use order and unused input keys separately. Replacement values are literal text, not a second template.

Editable synthetic mapping cases
CaseInput record (JSON)Expected output (JSON)
Render original plain-text grammar{"template":"Hi {name}; ref {ref}.","values":{"name":"Ada","ref":"R1"}}{"ok":true,"value":{"rendered":"Hi Ada; ref R1.","usedKeys":["name","ref"],"unusedKeys":[]}}
Reject missing token value{"template":"Hi {name}; ref {ref}.","values":{"name":"Ada"}}{"ok":false,"error":"TEMPLATE_VALUE"}
Preserve replacement text literally{"template":"Hi {name}","values":{"name":"{ref}","ref":"R1"}}{"ok":true,"value":{"rendered":"Hi {ref}","usedKeys":["name"],"unusedKeys":["ref"]}}
Download editable CSV ↓

Run the local transformation

Run the complete example as a JavaScript module in Node.js. It contains the pure mapping function, all three inputs and literal expected outputs; assertions stop on a mismatch. A rejected case returns ok=false with an error code, rather than a partial canonical record.

Complete function and executable cases
import assert from 'node:assert/strict';

function textTemplate(i){
  const fail=error=>({ok:false,error});if(!i||typeof i.template!=='string'||!i.values||typeof i.values!=='object'||Array.isArray(i.values))return fail('TEMPLATE_SHAPE');
  const pattern=/\{([a-z][a-z0-9_]*)\}/g;
  if(/[{}]/.test(i.template.replace(pattern,'')))return fail('TEMPLATE_SYNTAX');
  const usedKeys=[];let error;
  const rendered=i.template.replace(pattern,(_,key)=>{
    if(!Object.hasOwn(i.values,key)||typeof i.values[key]!=='string'){error='TEMPLATE_VALUE';return '';}
    if(!usedKeys.includes(key))usedKeys.push(key);return i.values[key];
  });
  if(error)return fail(error);
  return {ok:true,value:{rendered,usedKeys,unusedKeys:Object.keys(i.values).filter(k=>!usedKeys.includes(k))}};
}

// Original synthetic cases; expected outputs are literal records.
const cases = [
  {
    "label": "Render original plain-text grammar",
    "input": {
      "template": "Hi {name}; ref {ref}.",
      "values": {
        "name": "Ada",
        "ref": "R1"
      }
    },
    "expected": {
      "ok": true,
      "value": {
        "rendered": "Hi Ada; ref R1.",
        "usedKeys": [
          "name",
          "ref"
        ],
        "unusedKeys": []
      }
    }
  },
  {
    "label": "Reject missing token value",
    "input": {
      "template": "Hi {name}; ref {ref}.",
      "values": {
        "name": "Ada"
      }
    },
    "expected": {
      "ok": false,
      "error": "TEMPLATE_VALUE"
    }
  },
  {
    "label": "Preserve replacement text literally",
    "input": {
      "template": "Hi {name}",
      "values": {
        "name": "{ref}",
        "ref": "R1"
      }
    },
    "expected": {
      "ok": true,
      "value": {
        "rendered": "Hi {ref}",
        "usedKeys": [
          "name"
        ],
        "unusedKeys": [
          "ref"
        ]
      }
    }
  }
];
for (const sample of cases) {
  const actual = textTemplate(sample.input);
  assert.deepEqual(actual, sample.expected);
  console.log(sample.label, JSON.stringify(actual));
}
Download runnable example ↓

Work through the boundary

If name contains "{ref}", will ref be substituted on a second pass?

Show the worked answer

No. "Hi {name}" becomes "Hi {ref}" literally. usedKeys contains name; ref remains unused. A missing referenced value instead rejects with TEMPLATE_VALUE.

Related resources