Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
generator-foreach
Advanced tools
Imagine you want to use forEach
inside a generator function:
var stuff = [1, 2, 3, 4, 5]
function * fn () {
stuff.forEach(something)
function something (item) {
console.log(item)
}
}
for (var i of fn());
It works ok, but what if you want to yield a value based on an item
?
var stuff = [1, 2, 3, 4, 5]
function * fn () {
stuff.forEach(something)
function something (item) {
yield item
}
}
for (var i of fn()) console.log(i)
Doesn't work anymore! You can't use yield
inside an ordinary function. So generator-foreach
comes into play:
var foreach = require('generator-foreach')
var stuff = [1, 2, 3, 4, 5]
function * fn () {
yield * foreach(stuff, something)
function * something (item) {
yield item
}
}
for (var i of fn()) console.log(i)
Do use it you need to remember a couple of things:
yield *
before foreach
;Use --harmony
or --harmony-generators --harmony-iteration
flags
/**
* basic example
*/
var foreach = require('generator-foreach')
function * gen (array) {
yield * foreach(array, function * (num) {
yield num + 1
})
}
for (var num of gen([1, 2, 3])) console.log(num)
Use --harmony
or --harmony-generators --harmony-iteration
flags
/**
* fancy recursive array flattening
*/
var foreach = require('generator-foreach')
function * value (val) {
yield val
}
function * flatten (array) {
yield * Array.isArray(array) ? foreach(array, flatten) : value(array)
}
var array = [1, 2, [3, [4, 5, [6, 7]]]]
for (var num of flatten(array)) console.log(num)
FAQs
forEach for generators
The npm package generator-foreach receives a total of 10 weekly downloads. As such, generator-foreach popularity was classified as not popular.
We found that generator-foreach demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.