um-react/src/util/splitN.ts
Jixun Wu 263f4c2b6a
Some checks failed
continuous-integration/drone/push Build is failing
feat: kwm v2 key import ui
2023-06-17 02:45:31 +01:00

21 lines
454 B
TypeScript

export function splitN(str: string, sep: string, maxN: number) {
if (maxN <= 1) {
return [str];
}
const chunks: string[] = [];
const lenSep = sep.length;
let searchIdx = 0;
for (; maxN > 1; maxN--) {
const nextIdx = str.indexOf(sep, searchIdx);
if (nextIdx === -1) {
break;
}
chunks.push(str.slice(searchIdx, nextIdx));
searchIdx = nextIdx + lenSep;
}
chunks.push(str.slice(searchIdx));
return chunks;
}