bbc/unicode-bidirectional

View on GitHub
src/weak/rule/es.js

Summary

Maintainability
A
35 mins
Test Coverage
import includes from 'lodash.includes';

// http://unicode.org/reports/tr9/#W4
// [1]: A single European separator between two European
//      numbers changes to a European number.
// [2]: A single common separator between two
//      numbers of the same type changes to that type.
// [3]: (Otherwise make no changes)
function es(types) {
  if (types.size < 3) return types;

  const isNumber = t => includes(['AN', 'EN'], t);
  const first = types.take(1);
  const middle = types.skip(2).zipWith((curr, prevOne, prevTwo) => {
    if (curr === 'EN' && curr === prevTwo && prevOne === 'ES') { // [1]
      return 'EN';
    } else if (prevOne === 'CS' && isNumber(curr) && curr === prevTwo) { // [2]
      return curr;
    } else { // [3]
      return prevOne;
    }
  }, types.skip(1), types);
  const last = types.last();
  return first.concat(middle).push(last);
}

export default es;