Skip to content

Instantly share code, notes, and snippets.

@deepakshrma
Last active January 20, 2020 15:10
Show Gist options
  • Save deepakshrma/73aa118f04aa658462b0c88ded5a1d38 to your computer and use it in GitHub Desktop.
Save deepakshrma/73aa118f04aa658462b0c88ded5a1d38 to your computer and use it in GitHub Desktop.
[tillWhen] Weird Part: How to break the LOOP in JavaScript
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