-
-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ (grapher) allow line breaks after hyphens (#3689)
- Loading branch information
1 parent
23f14e3
commit 0ba25b7
Showing
4 changed files
with
104 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
packages/@ourworldindata/components/src/TextWrap/TextWrapUtils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#! /usr/bin/env jest | ||
|
||
import { joinFragments, splitIntoFragments } from "./TextWrapUtils" | ||
|
||
it("splits text correctly into fragments", () => { | ||
expect(splitIntoFragments("")).toEqual([]) | ||
expect(splitIntoFragments("word")).toEqual([ | ||
{ text: "word", separator: "" }, | ||
]) | ||
expect(splitIntoFragments("an example line")).toEqual([ | ||
{ text: "an", separator: " " }, | ||
{ text: "example", separator: " " }, | ||
{ text: "line", separator: "" }, | ||
]) | ||
expect(splitIntoFragments("high-income countries")).toEqual([ | ||
{ text: "high-income", separator: " " }, | ||
{ text: "countries", separator: "" }, | ||
]) | ||
expect(splitIntoFragments("high-income countries", [" ", "-"])).toEqual([ | ||
{ text: "high", separator: "-" }, | ||
{ text: "income", separator: " " }, | ||
{ text: "countries", separator: "" }, | ||
]) | ||
}) | ||
|
||
it("splits and joins text correctly into fragments", () => { | ||
const examples = [ | ||
"", | ||
"word", | ||
"an example line", | ||
"an example spaced out text", | ||
"an example-with-hyphens", | ||
"an example - with - a - differennt - kind-of - hyphen", | ||
"hyphen at the end -", | ||
"a mixed-bag - ok", | ||
] | ||
for (const text of examples) { | ||
expect(joinFragments(splitIntoFragments(text))).toEqual(text) | ||
expect(joinFragments(splitIntoFragments(text, [" ", "-"]))).toEqual( | ||
text | ||
) | ||
} | ||
}) |
32 changes: 32 additions & 0 deletions
32
packages/@ourworldindata/components/src/TextWrap/TextWrapUtils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { isEmpty } from "@ourworldindata/utils" | ||
|
||
export type Fragment = { | ||
text: string | ||
separator: string | ||
} | ||
|
||
export function splitIntoFragments( | ||
text: string, | ||
separators = [" "] | ||
): Fragment[] { | ||
if (isEmpty(text)) return [] | ||
const fragments: Fragment[] = [] | ||
let currText = "" | ||
for (const char of text) { | ||
if (separators.includes(char)) { | ||
fragments.push({ text: currText, separator: char }) | ||
currText = "" | ||
} else { | ||
currText += char | ||
} | ||
} | ||
fragments.push({ text: currText, separator: "" }) | ||
return fragments | ||
} | ||
|
||
export function joinFragments(fragments: Fragment[]): string { | ||
return fragments | ||
.map(({ text, separator }) => text + separator) | ||
.join("") | ||
.trim() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters