function swap(glyphs, rangeA, rangeB, reverseA, reverseB) {
  if (reverseA === void 0) {
    reverseA = false;
  }

  if (reverseB === void 0) {
    reverseB = false;
  }

  var end = glyphs.splice(rangeB[0] - (rangeB[1] - 1), rangeB[1]);

  if (reverseB) {
    end.reverse();
  }

  var start = glyphs.splice.apply(glyphs, [rangeA[0], rangeA[1]].concat(end));

  if (reverseA) {
    start.reverse();
  }

  glyphs.splice.apply(glyphs, [rangeB[0] - (rangeA[1] - 1), 0].concat(start));
  return glyphs;
}
This is a shaper for Arabic, and other cursive scripts.
It uses data from ArabicShaping.txt in the Unicode database,
compiled to a UnicodeTrie by generate-data.coffee.

The shaping state machine was ported from Harfbuzz.
https://github.com/behdad/harfbuzz/blob/master/src/hb-ot-shape-complex-arabic.cc