Skip to content

Commit ea2d924

Browse files
committed
feat: add substringBetween function to extract substring between specified needles
1 parent dbe49be commit ea2d924

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

packages/util/src/__tests__/string.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,30 @@ describe('util', () => {
175175
expect(result).toEqual(input);
176176
});
177177
});
178+
179+
describe('#substringBetween', () => {
180+
it('should return "123" for string "test/123/test/456" with needles "/" and "/"', () => {
181+
const input = 'test/123/test/456';
182+
const result = string.substringBetween(input, '/', '/');
183+
expect(result).toEqual('123');
184+
});
185+
186+
it('should return "123" for string "test/123" with needles "/" and "/"', () => {
187+
const input = 'test/123';
188+
const result = string.substringBetween(input, '/', '/');
189+
expect(result).toEqual('123');
190+
});
191+
192+
it('should return original string "123" when start needle not found for "123" with needles "/" and "/"', () => {
193+
const input = '123';
194+
const result = string.substringBetween(input, '/', '/');
195+
expect(result).toEqual('123');
196+
});
197+
198+
it('should return "123/test" for string "test[123/test]456" with needles "[" and "]"', () => {
199+
const input = 'test[123/test]456';
200+
const result = string.substringBetween(input, '[', ']');
201+
expect(result).toEqual('123/test');
202+
});
203+
});
178204
});

packages/util/src/string.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ export function substringAfter(value: string, delimiter: string | RegExp): strin
175175
}
176176

177177
/**
178-
* Get the substring between the first occurrence of the specified start and end string.
178+
* Get the substring between the last occurrence of the specified start and end string.
179179
* If the end string is not found the substring after the start string is returned.
180180
* If the start string is not found but the end of the string is found the substring before the end string is returned.
181181
* @param value value to search in
@@ -196,6 +196,27 @@ export function substringBetweenLast(value: string, start: string, end: string):
196196
return value;
197197
}
198198

199+
/**
200+
* Returns the first occurrence of the substring between the specified start and end needles.
201+
* If the start needle is not found, returns the original string.
202+
* If the end needle is not found, returns the substring from after the start needle to the end of the string.
203+
* @param value Input string
204+
* @param start The starting needle
205+
* @param end The ending needle
206+
*/
207+
export function substringBetween(value: string, start: string, end: string): string {
208+
const startIndex = value.indexOf(start);
209+
if (startIndex === -1) {
210+
return value;
211+
}
212+
const startPos = startIndex + start.length;
213+
const endIndex = value.indexOf(end, startPos);
214+
if (endIndex === -1) {
215+
return value.substring(startPos);
216+
}
217+
return value.substring(startPos, endIndex);
218+
}
219+
199220
/**
200221
* Joins array parts together in one or more strings based on the max size of the string
201222
* @param parts

0 commit comments

Comments
 (0)