Improve types by removing nullability from return value

This commit is contained in:
Andras Schmelczer 2025-07-12 12:14:48 +01:00
commit 7d160cd9fd
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C

View file

@ -44,6 +44,25 @@ export type History = (typeof HISTORY_VALUES)[number];
export interface TextWithCursors { export interface TextWithCursors {
/** The document's entire content as a string */ /** The document's entire content as a string */
text: string; text: string;
/**
* Array of cursor positions within the text. Can be empty if there are no cursors to track.
* Each cursor has a unique ID and position.
*/
cursors: CursorPosition[];
}
/**
* Represents a text document with associated cursor positions.
*
* This interface is used both as input to reconcile functions (to specify where
* cursors are positioned in the original documents) and as output (with cursors
* automatically repositioned after merging).
*/
export interface TextWithOptionalCursors {
/** The document's entire content as a string */
text: string;
/** /**
* Array of cursor positions within the text. Can be null, undefined, or empty * Array of cursor positions within the text. Can be null, undefined, or empty
* if there are no cursors to track. Each cursor has a unique ID and position. * if there are no cursors to track. Each cursor has a unique ID and position.
@ -60,6 +79,7 @@ export interface TextWithCursors {
export interface CursorPosition { export interface CursorPosition {
/** Unique identifier for the cursor (can be any number, must be unique within the document) */ /** Unique identifier for the cursor (can be any number, must be unique within the document) */
id: number; id: number;
/** Character position in the text, 0-based index from the beginning of the document */ /** Character position in the text, 0-based index from the beginning of the document */
position: number; position: number;
} }
@ -74,11 +94,13 @@ export interface CursorPosition {
export interface TextWithCursorsAndHistory { export interface TextWithCursorsAndHistory {
/** The merged document's entire content */ /** The merged document's entire content */
text: string; text: string;
/** /**
* Array of cursor positions within the merged text. Can be null, undefined, or empty * Array of cursor positions within the merged text. Can empty if there are no cursors to track.
* if there are no cursors to track. All cursors are automatically repositioned. * All cursors are automatically repositioned from the left and right documents.
*/ */
cursors: null | undefined | CursorPosition[]; cursors: CursorPosition[];
/** /**
* Detailed provenance information showing the origin of each text span in the result. * Detailed provenance information showing the origin of each text span in the result.
* Each span indicates whether it was unchanged, added from left, added from right, etc. * Each span indicates whether it was unchanged, added from left, added from right, etc.
@ -96,10 +118,8 @@ export interface TextWithCursorsAndHistory {
export interface SpanWithHistory { export interface SpanWithHistory {
/** The text content of this span */ /** The text content of this span */
text: string; text: string;
/**
* The origin of this text span: "Unchanged" (from original), "AddedFromLeft", /** The origin of this text span in the merge result */
* "AddedFromRight", "RemovedFromLeft", or "RemovedFromRight"
*/
history: History; history: History;
} }
@ -135,8 +155,8 @@ let isInitialised = false;
*/ */
export function reconcile( export function reconcile(
original: string, original: string,
left: string | TextWithCursors, left: string | TextWithOptionalCursors,
right: string | TextWithCursors, right: string | TextWithOptionalCursors,
tokenizer: BuiltinTokenizer = 'Word' tokenizer: BuiltinTokenizer = 'Word'
): TextWithCursors { ): TextWithCursors {
init(); init();
@ -189,8 +209,8 @@ export function reconcile(
*/ */
export function reconcileWithHistory( export function reconcileWithHistory(
original: string, original: string,
left: string | TextWithCursors, left: string | TextWithOptionalCursors,
right: string | TextWithCursors, right: string | TextWithOptionalCursors,
tokenizer: BuiltinTokenizer = 'Word' tokenizer: BuiltinTokenizer = 'Word'
): TextWithCursorsAndHistory { ): TextWithCursorsAndHistory {
init(); init();
@ -243,7 +263,9 @@ function init() {
isInitialised = true; isInitialised = true;
} }
function toWasmTextWithCursors(text: string | TextWithCursors): wasmTextWithCursors { function toWasmTextWithCursors(
text: string | TextWithOptionalCursors
): wasmTextWithCursors {
const isInputString = typeof text === 'string'; const isInputString = typeof text === 'string';
const leftText = isInputString ? text : text.text; const leftText = isInputString ? text : text.text;
const leftCursors = isInputString ? [] : (text.cursors ?? []); const leftCursors = isInputString ? [] : (text.cursors ?? []);