Comparing version 0.1.0 to 0.1.1
40
index.js
@@ -0,1 +1,24 @@ | ||
function addSymbolIterator(obj) { | ||
try { | ||
obj[Symbol.iterator] = function () { | ||
return this; | ||
}; | ||
} catch (error) { | ||
// pass | ||
} | ||
} | ||
function addSymbolIteratorFallback(obj) { | ||
obj["@@iterator"] = function () { | ||
return this; | ||
}; | ||
} | ||
function wrapNextFunction(next) { | ||
const iter = { next }; | ||
addSymbolIterator(iter); | ||
addSymbolIteratorFallback(iter); | ||
return iter; | ||
} | ||
function isArray(data) { | ||
@@ -45,12 +68,12 @@ try { | ||
let len = data.length; | ||
return { | ||
next: () => (i++ < len ? { value: data[i], done: false } : { done: true }) | ||
}; | ||
const next = () => | ||
i++ < len ? { value: data[i], done: false } : { done: true }; | ||
return wrapNextFunction(next); | ||
} | ||
function getOrCreateIterator(data) { | ||
if (hasNext(data)) { | ||
return data; | ||
} else if (hasSymbolIterator(data)) { | ||
if (hasSymbolIterator(data)) { | ||
return data[Symbol.iterator](); | ||
} else if (hasNext(data)) { | ||
return wrapNextFunction(data.next); | ||
} else if (hasIterator(data)) { | ||
@@ -67,2 +90,4 @@ return getIterator(data); | ||
module.exports = { | ||
addSymbolIterator, | ||
addSymbolIteratorFallback, | ||
isArray, | ||
@@ -74,4 +99,5 @@ hasNext, | ||
createIterator, | ||
getOrCreateIterator | ||
getOrCreateIterator, | ||
wrapNextFunction | ||
}; | ||
} |
{ | ||
"name": "iter-fun", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Fun with Iterables", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -38,2 +38,18 @@ # iter-fun | ||
const iter = createIterator(obj); | ||
import { addSymbolIterator } from "iter-fun"; | ||
const obj = { next: () => {...} }; | ||
addSymbolIterator(obj); | ||
// modifies the object in-place adding a [Symbol.iterator] key | ||
import { addSymbolIteratorFallback } from "iter-fun"; | ||
const obj = { next: () => {...} }; | ||
addSymbolIteratorFallback(obj); | ||
// modifies the object in-place adding a "@@iterator" string key | ||
// helpful if you are running in an old browser | ||
import { wrapNextFunction } from "iter-fun"; | ||
const next = () => { ... }; | ||
const iter = wrapNextFunction(next); | ||
// converts a next function into a fully functioning iterable | ||
``` |
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
11166
90
55