Allow words to carry source position information, stripped on manipulation
This commit is contained in:
parent
f3be6b40f9
commit
6ca93d6615
2 changed files with 22 additions and 4 deletions
|
@ -53,4 +53,15 @@ describe("Text Words", () => {
|
|||
])("Concat(%s, %s)", (left, right, expected) =>
|
||||
expect(Concat(left, right)).toEqual(expected)
|
||||
);
|
||||
|
||||
test.each([
|
||||
[null, { bare: "1", pos: 13 }, { text: "1", pos: 13 }],
|
||||
[null, { text: "2", pos: 13 }, { text: "2", pos: 13 }],
|
||||
[null, { html: "3", pos: 13 }, { html: "3", pos: 13 }],
|
||||
[{ bare: "4", pos: 13 }, { bare: "5", pos: 15 }, { text: "45" }],
|
||||
[{ html: "6", pos: 13 }, { html: "7", pos: 15 }, { html: "67" }],
|
||||
])(
|
||||
"Concat(%s, %s) strips source position when combining words",
|
||||
(left, right, expected) => expect(Concat(left, right)).toEqual(expected)
|
||||
);
|
||||
});
|
||||
|
|
15
src/words.ts
15
src/words.ts
|
@ -1,4 +1,6 @@
|
|||
import { escapeHtml } from "./helpers";
|
||||
import { escapeHtml } from './helpers';
|
||||
|
||||
export type SourcePos = number;
|
||||
|
||||
/**
|
||||
* A word whose value is text with provenance- this literal value appeared in the source code,
|
||||
|
@ -15,13 +17,15 @@ import { escapeHtml } from "./helpers";
|
|||
*/
|
||||
export type BareWord = {
|
||||
bare: string;
|
||||
pos?: SourcePos;
|
||||
};
|
||||
|
||||
/**
|
||||
* A word whose value is plain text, with no special provenance.
|
||||
* A word whose value is plain text
|
||||
*/
|
||||
export type TextWord = {
|
||||
text: string;
|
||||
pos?: SourcePos;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -78,9 +82,12 @@ export function AsHtml(word: TextPiece | ErrorResult): string {
|
|||
}
|
||||
|
||||
// safely concatenate text pieces, converting as needed
|
||||
export function Concat(left: TextPiece | null, right: TextPiece) {
|
||||
export function Concat(
|
||||
left: TextPiece | null,
|
||||
right: TextPiece
|
||||
): TextWord | HtmlWord {
|
||||
if (left === null) {
|
||||
return "bare" in right ? { text: right.bare } : right;
|
||||
return "bare" in right ? { text: right.bare, pos: right.pos } : right;
|
||||
}
|
||||
if ("html" in left || "html" in right) {
|
||||
return { html: AsHtml(left) + AsHtml(right) };
|
||||
|
|
Loading…
Reference in a new issue