Skip to content
Astera Technology

Map locale rows without losing extra languages

Require two declared labels and preserve unrecognised locales separately.

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.

Each locale row has a nonempty locale and text, with no duplicate locale. Require en and zh-HK, mapping them to en and zhHK fields. Keep other locale rows in unmapped. The function copies text; it cannot establish whether the two labels mean the same thing.

Editable synthetic mapping cases
CaseInput record (JSON)Expected output (JSON)
Pair supplied locale text{"entries":[{"locale":"en","text":"Front"},{"locale":"zh-HK","text":"前台"}]}{"ok":true,"value":{"en":"Front","zhHK":"前台","unmapped":[]}}
Reject duplicate locale{"entries":[{"locale":"en","text":"Front"},{"locale":"en","text":"Desk"},{"locale":"zh-HK","text":"前台"}]}{"ok":false,"error":"LOCALE_DUPLICATE"}
Preserve another locale{"entries":[{"locale":"en","text":"Front"},{"locale":"zh-HK","text":"前台"},{"locale":"fr","text":"Bureau"}]}{"ok":true,"value":{"en":"Front","zhHK":"前台","unmapped":[{"locale":"fr","text":"Bureau"}]}}
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 localisedLabels(i){
  const fail=error=>({ok:false,error});if(!i||!Array.isArray(i.entries))return fail('LOCALE_ENTRIES');
  const found=new Map(),unmapped=[];
  for(const e of i.entries){
    if(!e||typeof e.locale!=='string'||!e.locale||typeof e.text!=='string'||!e.text)return fail('LOCALE_VALUE');
    if(found.has(e.locale))return fail('LOCALE_DUPLICATE');found.set(e.locale,e.text);
    if(!['en','zh-HK'].includes(e.locale))unmapped.push({locale:e.locale,text:e.text});
  }
  if(!found.has('en')||!found.has('zh-HK'))return fail('LOCALE_MISSING');
  return {ok:true,value:{en:found.get('en'),zhHK:found.get('zh-HK'),unmapped}};
}

// Original synthetic cases; expected outputs are literal records.
const cases = [
  {
    "label": "Pair supplied locale text",
    "input": {
      "entries": [
        {
          "locale": "en",
          "text": "Front"
        },
        {
          "locale": "zh-HK",
          "text": "前台"
        }
      ]
    },
    "expected": {
      "ok": true,
      "value": {
        "en": "Front",
        "zhHK": "前台",
        "unmapped": []
      }
    }
  },
  {
    "label": "Reject duplicate locale",
    "input": {
      "entries": [
        {
          "locale": "en",
          "text": "Front"
        },
        {
          "locale": "en",
          "text": "Desk"
        },
        {
          "locale": "zh-HK",
          "text": "前台"
        }
      ]
    },
    "expected": {
      "ok": false,
      "error": "LOCALE_DUPLICATE"
    }
  },
  {
    "label": "Preserve another locale",
    "input": {
      "entries": [
        {
          "locale": "en",
          "text": "Front"
        },
        {
          "locale": "zh-HK",
          "text": "前台"
        },
        {
          "locale": "fr",
          "text": "Bureau"
        }
      ]
    },
    "expected": {
      "ok": true,
      "value": {
        "en": "Front",
        "zhHK": "前台",
        "unmapped": [
          {
            "locale": "fr",
            "text": "Bureau"
          }
        ]
      }
    }
  }
];
for (const sample of cases) {
  const actual = localisedLabels(sample.input);
  assert.deepEqual(actual, sample.expected);
  console.log(sample.label, JSON.stringify(actual));
}
Download runnable example ↓

Work through the boundary

What happens to the French Bureau label in the preservation case?

Show the worked answer

It remains {locale:"fr",text:"Bureau"} in unmapped, beside Front and 前台. It is neither overwritten nor substituted for the required zh-HK row.

Related resources