Last active
January 20, 2020 15:10
-
-
Save deepakshrma/73aa118f04aa658462b0c88ded5a1d38 to your computer and use it in GitHub Desktop.
[tillWhen] Weird Part: How to break the LOOP in JavaScript
This file contains hidden or 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
const _ = {}; | |
// fn: (any[], function, () => boolean) -> any[] | |
_.tillWhen = (data, fn, predicate) => { | |
if (!data || !Array.isArray(data)) return data; | |
let index = 0; | |
let arr = []; | |
do { | |
arr.push(fn(data[index])); | |
} while (index < data.length && !predicate(data[index++])); | |
return arr; | |
}; | |
module.exports = _ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment