1
0
Fork
You've already forked foreachain
0
The chainable forEach().
  • JavaScript 100%
2021年12月05日 17:09:11 +01:00
.gitignore Remove build setup 2020年01月12日 03:23:13 +01:00
foreachain.browser.js Rename files 2021年03月20日 10:44:09 +01:00
foreachain.common.js Rename files 2021年03月20日 10:44:09 +01:00
foreachain.d.ts Add type definitions 2021年03月20日 10:39:54 +01:00
foreachain.module.js Rename files 2021年03月20日 10:44:09 +01:00
LICENSE Update year in LICENSE 2021年03月20日 09:57:28 +01:00
package.json 2.0.2 2021年12月05日 17:09:11 +01:00
README.md Update licence link in README 2021年12月05日 17:07:50 +01:00

foreachain

The chainable forEach().

[2,3,5].forEachain(v => {console.log(v)})
 .map(v => v * 3)
 .forEach(v => {console.log(v)});

Why?

I don't really know. I just imagined that there can be a task like this:

  1. Output array data, element by element
  2. Modify array
  3. Output data again

And this can't be done in JavaScript in one long chained function call.

...well, OK, it can be done like this:

[2,3,5].map(v => {console.log(v);return v})
 .map(v => v * 3)
 .forEach(v => {console.log(v)});

But I find the first map call unreadable and confusing. So, this is my solution.

How?

  1. npm i foreachain
    

    or

    <script src="https://cdn.jsdelivr.net/npm/foreachain/foreachain.browser.min.js"></script>
    

    or (for modern browsers)

    <script type="module">
     import foreachain from 'https://cdn.jsdelivr.net/npm/foreachain/foreachain.module.min.js';
    </script>
    
  2. // if you're not in browser
    import foreachain from 'foreachain';
    /* DO NOT overwrite the original forEach.
     * This is unethical.
     *
     * ...and will break your code. */
    [].constructor.prototype.forEachain = foreachain;
    
  3. [2,3,5].forEachain(v => {console.log(v)})
     .map(v => v * 3)
     .forEach(v => {console.log(v)});
    

Is this necessary to have as a separate package?

If you think that the answer should be "No", you can copy the source code over to your codebase.

List of times I found use for a chainable forEach

I'm still looking...

Licence

MIT © 2020-2021 Nikita Karamov