Skip to content

Open-source document signature platform. Not affiliated with DocuSign.

TypeScript
1.3 KB
Raw Blame
import assert from "node:assert/strict";
import { test } from "node:test";

// Firefox 115 ESR has neither Intl.Segmenter nor String.prototype.toWellFormed.
// Remove both before the helpers load, so this file runs the code path those
// browsers take. Each test file runs in its own process.
Reflect.deleteProperty(Intl, "Segmenter");
Reflect.deleteProperty(String.prototype, "toWellFormed");

const { getInitials } = await import("./initials.ts");
const { truncateToWholeCharacters } = await import("./text-truncate.ts");

function isWellFormed(value: string): boolean {
  return !/[\uD800-\uDFFF]/.test(
    value.replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g, ""),
  );
}

test("initials load and stay whole without Intl.Segmenter", () => {
  assert.equal(typeof Intl.Segmenter, "undefined");
  assert.equal(getInitials("Robin Casey πŸ˜€", { max: 4 }), "RC");
  assert.equal(getInitials("Robin Casey D", { max: 4 }), "RCD");
  assert.equal(getInitials("πŸ‘©πŸ½β€πŸ’»", { max: 4 }), "πŸ‘©πŸ½β€πŸ’»");
  assert.equal(getInitials("\ud83d Robin", { max: 4 }), "R");
});

test("typed initials are cut at whole characters without Intl.Segmenter", () => {
  const cut = truncateToWholeCharacters("ABπŸ˜€C", 3);

  assert.ok(isWellFormed(cut), JSON.stringify(cut));
  assert.equal(cut, "AB");
});