Skip to content

Commit c3d4c22

Browse files
committed
Update split_between_tokens.ts
1 parent e82e1c3 commit c3d4c22

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/string/split_between_tokens.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { isEmpty } from '../array/mod.ts';
2+
3+
/**
4+
* This function spits a string between tokens.
5+
*
6+
* @param input The input string
7+
* @param tokens A list of char tokens
8+
* @returns The spited array
9+
*/
10+
export default function splitBetweenTokens(
11+
input : string,
12+
tokens : string[]
13+
) : string[] {
14+
if (!input) {
15+
return [];
16+
}
17+
if (isEmpty(tokens)) {
18+
return [input];
19+
}
20+
const escapedTokens = `\\${tokens.join('\\')}`;
21+
return input.split(new RegExp(`(?=[${escapedTokens}])|(?<=[${escapedTokens}]+)`));
22+
}

0 commit comments

Comments
 (0)