npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

omnimap

v1.0.1

Published

Map over multiple array/object without thinking too much.

Downloads

25

Readme

Omnimap

Map over multiple arrays/object without thinking too much. Callback function follows the general input arguments of Array.prototype.map.

Usage

Array

'use strict';
const omnimap = require('omnimap');
const result = omnimap([1,2,3], [4,5,6], function(a, b, idx, arr) {
  // idx is the current idx iteration index
  return a + b;
});
console.log(result); // [5, 7, 9];

You can do three arrays as well. Well, just give it any number of arrays, and it'll map through.

'use strict';
const omnimap = require('omnimap');
const result = omnimap([1,2,3], [4,5,6], [7,8,9], function(a, b, c, idx, arr) {
  // idx is the current idx iteration index
  return a + b + c;
});
console.log(result); // [12, 15, 18];

Object

As with all object looping in js, the order of the outcome is not guaranteed.

'use strict';
const omnimap = require('omnimap');
const a = { a: 1, b: 2, c: 3 };
const b = { a: 4, b: 5, c: 6 };
const result = omnimap(a, b, function(a, b, key, arr) {
  // key is the currently iterating object key
  return a + b;
});
console.log(result); // { a: 5, b: 7, c: 9 };

For object mapping, we have a convenient tool that converts the result object into a key-less array.

console.log(result.toArray()); // [5, 7, 9];

Optional lazy evaluation

If you don't pass a callback function as the last argument, it will return a evaluator which you can call with a callback.

'use strict';
const omnimap = require('omnimap');
const map = omnimap([1,2,3], [4,5,6]);
const result = map(function(a, b, idx, arr) {
  // idx is the current idx iteration index
  return a + b;
});
console.log(result); // [5, 7, 9];

The real reason why I made this

Promise.

function doSomethingLethal(payload) {
  return someLethalPromiseFunc(payload);
}

const omnimap = require('omnimap');
const controllers = [
  doSomethingLethal,
  doSomethingLethal,
  doSomethingLethal,
];
const payloads = [
  { msg: 'Hello' },
  { msg: 'World' },
  { msg: '世界' }
];

const task = omnimap(controllers, payloads, function(controller, payload, idx) {
  payload.id = idx;
  return controller(payload);
});

Promise.all(task).then(function(results) {
  console.log(results);
});

Caveats

  1. The maximum iteration count is always the length of the first enumerable given. Make sure that the length of following enumerables are less than or equal to the length of the first enumerable. It would fail node.js VM (undefined reference errors).
  2. Object mapping returns its own instance, OmnimapObject, rather than a plain object. This is to hide .toArray() method behind the prototype. However, it still is compatible with all Object.prototype methods.
  3. Because of the Caveat#2, testing methods such as chai.assert.deepEqual would fail. You will have to iterate over each key manually, and assert each member individually.