Skip to content

Commit a389b78

Browse files
committed
Added StringUtils#join
1 parent 272bde1 commit a389b78

2 files changed

Lines changed: 67 additions & 0 deletions

File tree

src/utilities/string-utils.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,55 @@ describe("StringUtils", () => {
114114

115115
// #endregion isValidEmail
116116

117+
// -----------------------------------------------------------------------------------------
118+
// #region join()
119+
// -----------------------------------------------------------------------------------------
120+
121+
describe("join()", () => {
122+
describe("with the default separator", () => {
123+
type JoinTestTypes = [string[], string];
124+
125+
test.each<JoinTestTypes>([
126+
[[], ""],
127+
[["a"], "a"],
128+
[["a"], "a"],
129+
[["a", "b"], "a,b"],
130+
])(
131+
"when values is %p, returns %p",
132+
(values: string[], expected: string) => {
133+
// Arrange & Act
134+
const result: string = StringUtils.join(values);
135+
136+
// Assert
137+
expect(result).toBe(expected);
138+
}
139+
);
140+
});
141+
142+
describe("with a separator argument", () => {
143+
type JoinTestTypesWithSeparator = [string[], string, string];
144+
145+
test.each<JoinTestTypesWithSeparator>([
146+
[[], "", ""],
147+
[["a"], "", "a"],
148+
[["a"], ",", "a"],
149+
[["a", "b"], "", "ab"],
150+
[["a", "b"], " ", "a b"],
151+
])(
152+
"when values is %p and separator is %p, returns %p",
153+
(values: string[], separator: string, expected: string) => {
154+
// Arrange & Act
155+
const result: string = StringUtils.join(values, separator);
156+
157+
// Assert
158+
expect(result).toBe(expected);
159+
}
160+
);
161+
});
162+
});
163+
164+
// #endregion join
165+
117166
// -----------------------------------------------------------------------------------------
118167
// #region truncateRight()
119168
// -----------------------------------------------------------------------------------------

src/utilities/string-utils.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { CollectionUtils } from "./collection-utils";
12
import _ from "lodash";
23

34
// -----------------------------------------------------------------------------------------
@@ -48,6 +49,22 @@ const isEmpty = (value?: string): boolean =>
4849
const isValidEmail = (value?: string): boolean =>
4950
value != null && REGEX_VALID_EMAIL.test(value);
5051

52+
/**
53+
* Joins an array of strings into one string with a separator. If the array is empty, it will return an empty string.
54+
*
55+
* @default ""
56+
* @param {string[]} values Values to join into one string.
57+
* @param {string} [separator=","] String to seperate each of the given values.
58+
* @returns {string}
59+
*/
60+
const join = (values: string[], separator: string = ","): string => {
61+
if (CollectionUtils.isEmpty(values)) {
62+
return "";
63+
}
64+
65+
return values.join(separator);
66+
};
67+
5168
const truncateRight = (value: string, truncateAtPos: number): string => {
5269
if (value.length <= truncateAtPos) {
5370
return value;
@@ -73,6 +90,7 @@ export const StringUtils = {
7390
hasValue,
7491
isEmpty,
7592
isValidEmail,
93+
join,
7694
lowerFirst: _.lowerFirst,
7795
pad: _.pad,
7896
padEnd: _.padEnd,

0 commit comments

Comments
 (0)