obliterator
Advanced tools
Comparing version 1.4.0 to 1.5.0
@@ -20,3 +20,3 @@ /** | ||
*/ | ||
module.exports = function forEach(iterable, callback) { | ||
function forEach(iterable, callback) { | ||
var iterator, k, i, l, s; | ||
@@ -78,2 +78,81 @@ | ||
return; | ||
} | ||
/** | ||
* Same function as the above `forEach` but will yield `null` when the target | ||
* does not have keys. | ||
* | ||
* @param {any} iterable - Iterable value. | ||
* @param {function} callback - Callback function. | ||
*/ | ||
forEach.forEachWithNullKeys = function(iterable, callback) { | ||
var iterator, k, i, l, s; | ||
if (!iterable) | ||
throw new Error('obliterator/forEachWithNullKeys: invalid iterable.'); | ||
if (typeof callback !== 'function') | ||
throw new Error('obliterator/forEachWithNullKeys: expecting a callback.'); | ||
// The target is an array or a string or function arguments | ||
if ( | ||
Array.isArray(iterable) || | ||
(ARRAY_BUFFER_SUPPORT && ArrayBuffer.isView(iterable)) || | ||
typeof iterable === 'string' || | ||
iterable.toString() === '[object Arguments]' | ||
) { | ||
for (i = 0, l = iterable.length; i < l; i++) | ||
callback(iterable[i], null); | ||
return; | ||
} | ||
// The target is a Set | ||
if (iterable instanceof Set) { | ||
iterable.forEach(function(value) { | ||
callback(value, null); | ||
}); | ||
return; | ||
} | ||
// The target has a #.forEach method | ||
if (typeof iterable.forEach === 'function') { | ||
iterable.forEach(callback); | ||
return; | ||
} | ||
// The target is iterable | ||
if ( | ||
SYMBOL_SUPPORT && | ||
Symbol.iterator in iterable && | ||
typeof iterable.next !== 'function' | ||
) { | ||
iterable = iterable[Symbol.iterator](); | ||
} | ||
// The target is an iterator | ||
if (typeof iterable.next === 'function') { | ||
iterator = iterable; | ||
i = 0; | ||
while ((s = iterator.next(), s.done !== true)) { | ||
callback(s.value, null); | ||
i++; | ||
} | ||
return; | ||
} | ||
// The target is a plain object | ||
for (k in iterable) { | ||
if (iterable.hasOwnProperty(k)) { | ||
callback(iterable[k], k); | ||
} | ||
} | ||
return; | ||
}; | ||
/** | ||
* Exporting. | ||
*/ | ||
module.exports = forEach; |
{ | ||
"name": "obliterator", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"description": "Higher order iterator library for JavaScript.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -169,2 +169,16 @@ [![Build Status](https://travis-ci.org/Yomguithereal/obliterator.svg)](https://travis-ci.org/Yomguithereal/obliterator) | ||
Optionally, one can use the `forEachWithNullKeys` function to iterate over mixed values but with the twist that iterables without proper keys (lists, sets etc.), will yield `null` instead of an index key. | ||
```js | ||
import {forEachWithNullKeys} from 'obliterator/foreach'; | ||
const set = new Set(['apple', 'banana']); | ||
forEach(set, (value, key) => { | ||
console.log(key, value); | ||
}); | ||
>>> null, 'apple' | ||
>>> null, 'banana' | ||
``` | ||
## map | ||
@@ -171,0 +185,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25966
693
320