Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

for-each-break

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

for-each-break - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

VERSION.md

102

index.js

@@ -38,12 +38,13 @@ 'use strict';

* @param callback callback function (value, index, array)
* @param thisArg optional arg to use as this for the callback
* @param defaultReturn optional default return value
* @return {*}
*/
function forEach(callback, defaultReturn = UNDEFINED) {
function forEach(callback, thisArg = UNDEFINED, defaultReturn = UNDEFINED) {
const savedReturn = BREAK.clearDefault(defaultReturn);
const iMax = this.length;
let result = defaultReturn;
let result = thisArg;
for (let i = 0; i < iMax; i++) {
const value = this[i];
const returned = callback(value, i, this);
const returned = callback.call(thisArg, value, i, this);
if (returned === BREAK) {

@@ -59,2 +60,27 @@ result = BREAK.returned;

/**
* Execute forEach on array like object in reverse and return value using BREAK
*
* @this array like object over which to loop
* @param callback callback function (value, index, array)
* @param thisArg optional arg to use as this for the callback
* @param defaultReturn optional default return value
* @return {*}
*/
function forEachRight(callback, thisArg = UNDEFINED, defaultReturn = UNDEFINED) {
const savedReturn = BREAK.clearDefault(defaultReturn);
const iMax = this.length;
let result = thisArg;
for (let i = iMax; i--;) {
const value = this[i];
const returned = callback.call(thisArg, value, i, this);
if (returned === BREAK) {
result = BREAK.returned;
break;
}
}
BREAK.restoreDefault(savedReturn);
return result;
}
/**
* Execute map on array like object and return accumulated values,

@@ -66,5 +92,6 @@ * returning BREAK will terminate loop early,

* @param callback callback function (value, index, array)
* @param thisArg optional arg to use as this for the callback
* @return {*}
*/
function map(callback) {
function map(callback, thisArg = UNDEFINED) {
let result = [];

@@ -75,3 +102,3 @@ const savedReturn = BREAK.clearDefault(result);

const value = this[i];
const returned = callback(value, i, this);
const returned = callback.call(thisArg, value, i, this);
if (returned === BREAK) {

@@ -88,2 +115,29 @@ result = BREAK.returned;

/**
* Execute map on array like object in reverse and return accumulated values,
* returning BREAK will terminate loop early,
* BREAK(value) or RETURN(value) will return value as result of call
*
* @this array like object over which to loop
* @param callback callback function (value, index, array)
* @param thisArg optional arg to use as this for the callback
* @return {*}
*/
function mapRight(callback, thisArg = UNDEFINED) {
let result = [];
const savedReturn = BREAK.clearDefault(result);
const iMax = this.length;
for (let i = iMax; i--;) {
const value = this[i];
const returned = callback.call(thisArg, value, i, this);
if (returned === BREAK) {
result = BREAK.returned;
break;
}
result.push(returned);
}
BREAK.restoreDefault(savedReturn);
return result;
}
/**
* Execute filter on array like object and return accumulated values,

@@ -95,5 +149,6 @@ * returning BREAK or RETURN will terminate loop early,

* @param callback callback function (value, index, array)
* @param thisArg optional arg to use as this for the callback
* @return {*}
*/
function filter(callback) {
function filter(callback, thisArg = UNDEFINED) {
let result = [];

@@ -105,3 +160,3 @@ const savedReturn = BREAK.clearDefault(result);

const value = this[i];
const returned = callback(value, i, this);
const returned = callback.call(thisArg, value, i, this);
if (returned === BREAK) {

@@ -119,2 +174,32 @@ result = BREAK.returned;

/**
* Execute filter on array like object in reverse and return accumulated values,
* returning BREAK or RETURN will terminate loop early,
* BREAK(value) or RETURN(value) will return value as result of call
*
* @this array like object over which to loop
* @param callback callback function (value, index, array)
* @param thisArg optional arg to use as this for the callback
* @return {*}
*/
function filterRight(callback, thisArg = UNDEFINED) {
let result = [];
const savedReturn = BREAK.clearDefault(result);
const iMax = this.length;
let i = iMax;
for (let i = iMax; i--;) {
const value = this[i];
const returned = callback.call(thisArg, value, i, this);
if (returned === BREAK) {
result = BREAK.returned;
break;
}
if (returned) {
result.push(value);
}
}
BREAK.restoreDefault(savedReturn);
return result;
}
module.exports.BREAK = BREAK;

@@ -125,1 +210,4 @@ module.exports.RETURN = BREAK;

module.exports.filter = filter;
module.exports.forEachRight = forEachRight;
module.exports.mapRight = mapRight;
module.exports.filterRight = filterRight;

2

package.json
{
"name": "for-each-break",
"version": "0.1.0",
"version": "0.2.0",
"description": "forEach, map and filter with break and return emulation",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -5,4 +5,5 @@ # for-each-break

Small utility library implementing `forEach`, `map` and `filter` functions with break and return
emulation as if it was a regular `for` or `while` loop.
Small utility library implementing `forEach`, `map`, `filter`, `forEachRight`, `mapRight` and
`filterRight` functions with break and return emulation as if it was a regular `for` or `while`
loop.

@@ -17,2 +18,4 @@ To simulate break return `BREAK` or `RETURN`, to simulate `return` return `BREAK(value)` or

<!-- @formatter:off -->
* `BREAK` - used to break or return from loop, using `BREAK` to break out of loop is preferred

@@ -22,9 +25,11 @@ since it communicates clear intention

value is preferred since it communicates clear intention
* `forEach` - use to loop over array like object: `forEach.call(arrayLike, callback,
defaultReturn)`
* `map` - use to map array like object, with early break or return: `map.call(arrayLike,
callback)`
* `filter` - use to filter array like object, with early break or return:
`filter.call(arrayLike, callback)`
* `forEach` - use to loop over array like object: `forEach.call(arrayLike, callback, thisArg, defaultReturn)`
* `map` - use to map array like object, with early break or return: `map.call(arrayLike, callback, thisArg)`
* `filter` - use to filter array like object, with early break or return: `filter.call(arrayLike, callback, thisArg)`
* `forEachRight` - use to loop over array like object in reverse: `forEach.call(arrayLike, callback, thisArg, defaultReturn)`
* `mapRight` - use to map array like object in reverse, with early break or return: `map.call(arrayLike, callback, thisArg)`
* `filterRight` - use to filter array like object in reverse, with early break or return: `filter.call(arrayLike, callback, thisArg)`
<!-- @formatter:on -->
## Install

@@ -34,3 +39,3 @@

```sh
```
npm install for-each-break --save

@@ -37,0 +42,0 @@ ```

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc