string-remove-widows examples

Table of Contents

Quick Take

import { strict as assert } from "assert";
import { removeWidows } from "string-remove-widows";

const { ranges, res } = removeWidows(
  "Some text with many words on one line."
);

// see codsen.com/ranges/
assert.deepEqual(ranges, [[32, 33, " "]]);

assert.equal(
  res,
  "Some text with many words on one line."
);

Widow word removal from text within HTML

import { strict as assert } from "assert";
import { stripHtml } from "string-strip-html";
import { removeWidows } from "string-remove-widows";

const someHtml = `The quick brown fox jumps of the lazy dog.<div class="a">`;

// default widow word removal libs are not aware of HTML:
// -----------------------------------------------------------------------------

assert.equal(
  removeWidows(someHtml).res,
  `The quick brown fox jumps of the lazy dog.<div&nbsp;class="a">` // 😱
);

// luckily, removeWidows() consumes optional HTML tag locations
assert.equal(
  removeWidows(someHtml, {
    tagRanges: stripHtml(someHtml)
      // remove the third argument, what to insert ("&nbsp;" string in these cases)
      .ranges.map(([from, to]) => [from, to]),
  }).res,
  `The quick brown fox jumps of the lazy&nbsp;dog.<div class="a">` // ✅
);