@jim/spandex-ascii

JSR JSR Score

ASCII rendering for spatial indexes. Visualize in terminals, logs, or anywhere text works.

Requires: @jim/spandex (core library)

Install

deno add jsr:@jim/spandex jsr:@jim/spandex-ascii

Quick Example

import createMortonLinearScanIndex from '@jim/spandex/index/mortonlinearscan';
import { createA1Adapter } from '@jim/spandex/adapter/a1';
import { createRenderer } from '@jim/spandex-ascii';

const index = createMortonLinearScanIndex<'red' | 'blue'>();
const adapter = createA1Adapter(index);

adapter.insert('A1:C3', 'red');
adapter.insert('B2:D4', 'blue');

const { render } = createRenderer();
console.log(render(adapter, {
	legend: { R: 'red', B: 'blue' },
}));

Output:

    A   B   C   D
  โ”โ”โ”โ”โ”ณโ”โ”โ”โ”ณโ”โ”โ”โ”“   ยท
1 โ”ƒ R โ”ƒ R โ”ƒ R โ”ƒ
  โ”ฃโ”โ”โ”โ•‹โ”โ”โ”โ•‹โ”โ”โ”โ•‹โ”โ”โ”โ”“
2 โ”ƒ R โ”ƒ B โ”ƒ B โ”ƒ B โ”ƒ
  โ”ฃโ”โ”โ”โ•‹โ”โ”โ”โ•‹โ”โ”โ”โ•‹โ”โ”โ”โ”ซ
3 โ”ƒ R โ”ƒ B โ”ƒ B โ”ƒ B โ”ƒ
  โ”—โ”โ”โ”โ•‹โ”โ”โ”โ•‹โ”โ”โ”โ•‹โ”โ”โ”โ”ซ
4     โ”ƒ B โ”ƒ B โ”ƒ B โ”ƒ
  ยท   โ”—โ”โ”โ”โ”ปโ”โ”โ”โ”ปโ”โ”โ”โ”›

B = "blue"
R = "red"

Options:

// Grid only mode (useful for progression rendering)
const grid = render(index, { legend, gridOnly: true });
// Output has no legend or infinity annotations footer

Parsing

Round-trip support for testing or data import:

import { parse } from '@jim/spandex-ascii';

const grid = `
    A   B   C
  โ”โ”โ”โ”โ”ณโ”โ”โ”โ”ณโ”โ”โ”โ”“
0 โ”ƒ R โ”ƒ R โ”ƒ B โ”ƒ
  โ”—โ”โ”โ”โ”ปโ”โ”โ”โ”ปโ”โ”โ”โ”›

R = "RED"
B = "BLUE"
`;

const result = parse<string>(grid);
// result.grids[0].results = [[[0, 0, 1, 0], "RED"], [[2, 0, 2, 0], "BLUE"]]
// result.legend = { 'R': 'RED', 'B': 'BLUE' }

Progression Rendering

Visualize how an index changes over time:

const { renderProgression } = createRenderer();

const output = renderProgression(
	createMortonLinearScanIndex<'horizontal' | 'vertical'>,
	[
		{ params: {}, action: (idx) => idx.insert([-Infinity, 1, Infinity, 1], 'horizontal') },
		{ params: {}, action: (idx) => idx.insert([1, -Infinity, 1, Infinity], 'vertical') },
	],
	{ legend: { H: 'horizontal', V: 'vertical' } },
);

console.log(output);

Great for test documentation and debugging insertion sequences.

Why ASCII?

Use when: Logging, CI/CD output, terminal debugging, text-only docs

Trade-offs:

Comparison: For browser debugging with colors and large grids, use @jim/spandex-html.

Layout API

Compose multiple grids side-by-side:

const { renderLayout } = createRenderer();

const output = renderLayout(
	[
		{ source: index1, params: { legend: legend1 } },
		{ source: index2, params: { legend: legend2 } },
	],
	{
		spacing: 5, // Characters between grids
	},
);

Related

License

MIT