-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support react nodes in conjuction function (#311)
- Loading branch information
1 parent
2ba197b
commit 6a43347
Showing
8 changed files
with
188 additions
and
75 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
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
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
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
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
This file was deleted.
Oops, something went wrong.
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,86 @@ | ||
import { conjuction, Locale } from "utils"; | ||
|
||
describe("conjuction", () => { | ||
it("should return a string", () => { | ||
expect.assertions(1); | ||
expect(conjuction([], Locale.EN)).toBe(""); | ||
}); | ||
|
||
it("should return a string a s", () => { | ||
expect.assertions(1); | ||
expect(conjuction(["a"], Locale.EN)).toBe("a"); | ||
}); | ||
|
||
it("should return a string b", () => { | ||
expect.assertions(1); | ||
expect(conjuction(["a", "b"], Locale.EN)).toBe("a, b"); | ||
}); | ||
|
||
it("should return a string c", () => { | ||
expect.assertions(1); | ||
expect(conjuction(["a", "b", "c"], Locale.EN)).toBe("a, b, c"); | ||
}); | ||
|
||
it("should return valid conjunction for spanish", () => { | ||
expect.assertions(1); | ||
expect(conjuction(["a", "b", "c"], Locale.ES)).toBe("a, b y c"); | ||
}); | ||
|
||
it("should return valid conjunction for french", () => { | ||
expect.assertions(1); | ||
expect(conjuction(["a", "b", "c"], "fr")).toBe("a, b et c"); | ||
}); | ||
|
||
it("should return valid nodeList conjunction for spanish and react component", () => { | ||
expect.assertions(1); | ||
expect( | ||
conjuction( | ||
[ | ||
<a key={"1"} href="1"> | ||
1 | ||
</a>, | ||
"b", | ||
"c", | ||
], | ||
Locale.ES | ||
) | ||
).toStrictEqual([ | ||
<a key={"1"} href="1"> | ||
1 | ||
</a>, | ||
", ", | ||
"b", | ||
" y ", | ||
"c", | ||
]); | ||
}); | ||
|
||
it("should return valid nodeList disjunction for spanish and react component", () => { | ||
expect.assertions(1); | ||
expect( | ||
conjuction( | ||
[ | ||
<a key={"1"} href="1"> | ||
a | ||
</a>, | ||
<a key={"2"} href="2"> | ||
b | ||
</a>, | ||
"c", | ||
], | ||
Locale.ES, | ||
{ type: "disjunction" } | ||
) | ||
).toStrictEqual([ | ||
<a key={"1"} href="1"> | ||
a | ||
</a>, | ||
", ", | ||
<a key={"2"} href="2"> | ||
b | ||
</a>, | ||
" o ", | ||
"c", | ||
]); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,9 +1,41 @@ | ||
export function conjuction(items: (string | undefined)[]): string { | ||
const list: string[] = []; | ||
items.forEach((item) => { | ||
if (item) { | ||
list.push(item); | ||
import { ReactNode } from "react"; | ||
|
||
export function conjuction( | ||
value: ReactNode[] | string, | ||
locale: string, | ||
options: Intl.ListFormatOptions = { type: "unit" } | ||
): ReactNode[] | string { | ||
const serializedValue: Array<string> = []; | ||
const reactNodes = new Map<string, ReactNode>(); | ||
|
||
let index = 0; | ||
for (const item of value) { | ||
let serializedItem; | ||
if (typeof item === "object") { | ||
serializedItem = String(index); | ||
reactNodes.set(serializedItem, item); | ||
} else { | ||
serializedItem = String(item); | ||
} | ||
}); | ||
return new Intl.ListFormat("en-US", { type: "unit" }).format(list); | ||
serializedValue.push(serializedItem); | ||
index++; | ||
} | ||
|
||
try { | ||
const listFormat = new Intl.ListFormat(locale, options); | ||
const formattedParts = listFormat.formatToParts(serializedValue); | ||
const result = formattedParts.map((part) => | ||
part.type === "literal" | ||
? part.value | ||
: reactNodes.get(part.value) ?? part.value | ||
); | ||
|
||
if (reactNodes.size > 0) { | ||
return result; | ||
} else { | ||
return result.join(""); | ||
} | ||
} catch (error) { | ||
return String(value); | ||
} | ||
} |