Back to Morse Tree
SOURCE / PINNED RELEASE

Made of little things.

Morse Tree

Release
52c388d28ec3…
Author-recorded commit
fd8192a198f7…
License
LICENSE
Author’s source reference
nostr://npub1eg5pfnws6tg6rtp33tugpppqtv5u05crmf983qp438cdycwzngys0gxzcs/wss%3A%2F%2Fgit.napplet.soy%2F/n-6ab5aa84ba1

Archive hash verified: 9d22ca7f5a712965…. The source-to-build association is the author’s claim; it has not been independently rebuilt.

tests/morse.test.ts
import assert from 'node:assert/strict';
import test from 'node:test';
import {
  LETTER_CODES,
  accuracy,
  classifyGap,
  classifyMark,
  codeAt,
  indexOf,
  letterAt,
  letterTreeCodes,
  schedule,
  treeCodes,
  treeSlots,
} from '../src/morse.ts';

test('every letter sits on its own node of a depth-four tree', () => {
  const codes = treeCodes();
  assert.equal(codes.length, 31);
  assert.equal(new Set(Object.values(LETTER_CODES)).size, 26);
  for (const [letter, code] of Object.entries(LETTER_CODES)) {
    assert.ok(codes.includes(code), letter);
    assert.equal(letterAt(code), letter);
  }
  const empty = codes.filter((code) => code !== '' && !letterAt(code));
  assert.deepEqual(empty.sort(), ['---.', '----', '.-.-', '..--'].sort());
});

test('dot is the left child and dash the right child', () => {
  assert.equal(codeAt(1, 0), '.');
  assert.equal(codeAt(1, 1), '-');
  assert.equal(codeAt(2, 1), '.-');
  for (const code of treeCodes()) assert.equal(codeAt(code.length, indexOf(code)), code);
  assert.equal(letterAt(codeAt(1, 0)), 'E');
  assert.equal(letterAt(codeAt(4, 15)), null);
});

test('timing uses one, three and seven dot gaps', () => {
  // S = ... , O = ---
  const so = schedule('SO');
  assert.deepEqual(so.tones.map((t) => [t.start, t.length]), [[0, 1], [2, 1], [4, 1], [8, 3], [12, 3], [16, 3]]);
  assert.equal(so.length, 19);
  const words = schedule('E E');
  assert.deepEqual(words.tones.map((t) => t.start), [0, 8]);
  assert.deepEqual(words.tones.map((t) => t.letter), [0, 2]);
  assert.equal(schedule(' ').tones.length, 0);
});

test('the letter tree drops the four empty nodes and packs the leaves', () => {
  const codes = letterTreeCodes();
  assert.equal(codes.length, 27);
  const { slots, count } = treeSlots();
  assert.equal(count, 13);
  assert.equal(slots.size, 27);
  // Leaves are the twelve four-symbol letters plus O, which has no children left.
  const leaves = codes.filter((code) => !codes.includes(code + '.') && !codes.includes(code + '-')).map(letterAt);
  assert.deepEqual(leaves.sort(), [...'BCFHJLOPQVXYZ'].sort());
  // Every child stays on its side of its parent, and no two nodes of a level touch.
  for (const code of codes) {
    if (code === '') continue;
    const parent = slots.get(code.slice(0, -1))!;
    const own = slots.get(code)!;
    assert.ok(code.endsWith('.') ? own < parent : own > parent, code);
  }
  for (let depth = 1; depth <= 4; depth += 1) {
    const level = codes.filter((code) => code.length === depth).map((code) => slots.get(code)!).sort((a, b) => a - b);
    for (let i = 1; i < level.length; i += 1) assert.ok(level[i] - level[i - 1] >= 1, `depth ${depth}`);
  }
});

test('a straight key splits marks and gaps at the ITU midpoints', () => {
  const unit = 100;
  assert.equal(classifyMark(10, unit), null);
  assert.equal(classifyMark(40, unit), '.');
  assert.equal(classifyMark(199, unit), '.');
  assert.equal(classifyMark(200, unit), '-');
  // A long hold is still a dash: Morse has no upper limit.
  assert.equal(classifyMark(5000, unit), '-');
  assert.equal(classifyGap(150, unit), 'symbol');
  assert.equal(classifyGap(200, unit), 'letter');
  assert.equal(classifyGap(499, unit), 'letter');
  assert.equal(classifyGap(500, unit), 'word');
  assert.equal(accuracy(100, unit, '.'), 1);
  assert.equal(accuracy(300, unit, '-'), 1);
  assert.equal(accuracy(150, unit, '.'), 0.5);
  assert.equal(accuracy(900, unit, '-'), 0);
});