Created
July 6, 2020 21:25
-
-
Save friendlyanon/60e2a5f90e2b5b065f941355f2a2de49 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function drop(iterator, amount) { | |
for (let i = 0; !(i >= amount); ++i) { | |
if (iterator.next().done) { | |
break; | |
} | |
} | |
return iterator; | |
} | |
function* sliceLazy(iterable, start, end) { | |
let counter = end - start; | |
if (counter === 0) { | |
return; | |
} | |
for (const value of drop(iterable[Symbol.iterator](), start)) { | |
yield value; | |
if (--counter === 0) { | |
break; | |
} | |
} | |
} | |
function* splitLazy(string, delimiter, limit = -1) { | |
switch (limit >>>= 0) { | |
case 0: return; | |
case 1: return void (yield string); | |
} | |
let i = -1; | |
for (let count = 1; ;) { | |
const start = i; | |
if ((i = string.indexOf(delimiter, i + 1)) === -1) { | |
i = start; | |
break; | |
} | |
yield string.slice(start, i); | |
i += delimiter.length; | |
if (++count >= limit) { | |
break; | |
} | |
} | |
return void (yield string.slice(i)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment