Skip to content

Commit 593126a

Browse files
committed
chore(core): add utils/getArrayForCommaSeparatedString
1 parent d01fbaa commit 593126a

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { describe, expect, it } from "vitest";
2+
3+
import { getArrayForCommaSeparatedString } from "./getArrayForCommaSeparatedString";
4+
5+
describe(getArrayForCommaSeparatedString.name, () => {
6+
it("should return empty array for empty string", () => {
7+
expect(getArrayForCommaSeparatedString("")).toEqual([]);
8+
});
9+
10+
it("should return empty array for null/undefined input", () => {
11+
expect(getArrayForCommaSeparatedString(null as unknown as string)).toEqual([]);
12+
expect(getArrayForCommaSeparatedString(undefined as unknown as string)).toEqual([]);
13+
});
14+
15+
it("should split comma separated string into array", () => {
16+
expect(getArrayForCommaSeparatedString("a,b,c")).toEqual(["a", "b", "c"]);
17+
});
18+
19+
it("should trim whitespace from array items", () => {
20+
expect(getArrayForCommaSeparatedString(" a , b , c ")).toEqual(["a", "b", "c"]);
21+
});
22+
23+
it("should handle single item", () => {
24+
expect(getArrayForCommaSeparatedString("a")).toEqual(["a"]);
25+
});
26+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* Converts a comma-separated string into an array of trimmed strings
3+
* @param str The comma-separated input string to split
4+
* @returns Array of trimmed strings split from the input
5+
*/
6+
export const getArrayForCommaSeparatedString = (str: string) =>
7+
typeof str === "string" && str.length > 0 ? str.split(",").map((item) => item.trim()) : [];

0 commit comments

Comments
 (0)