Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cartesian-map

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cartesian-map

Gives cartesian product of lists obtained by applying callback to array elements

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

cartesian-map

This small library contains a map-like function that iterates over elements of an array (or over characters of a string), calls a user-provided function on each, and returns the cartesian product of the lists returned by the function for each of the elements.

Examples

Array.prototype.cartesianMap = require('cartesian-map');
String.prototype.cartesianMap = require('cartesian-map');

console.log([1, 2, 3].cartesianMap(() => [false, true]));
/*
[
  [ false, false, false ],
  [ false, false, true ],
  [ false, true, false ],
  [ false, true, true ],
  [ true, false, false ],
  [ true, false, true ],
  [ true, true, false ],
  [ true, true, true ]
]
*/

console.log([2, 11, 24, 39].cartesianMap((item, index) => item % 2 === 0 ? [item] : [item, item + index]));
// or - alternatively - omitting square brackets for single elements:
console.log([2, 11, 24, 39].cartesianMap((item, index) => item % 2 === 0 ? item : [item, item + index]));
/*
[
  [ 2, 11, 24, 39 ],
  [ 2, 11, 24, 42 ],
  [ 2, 12, 24, 39 ],
  [ 2, 12, 24, 42 ]
]
*/

console.log('Foo Bar'.cartesianMap((char) => char === char.toLowerCase() ? char : [char, char.toLowerCase()]));
/*
[ 'Foo Bar', 'Foo bar', 'foo Bar', 'foo bar' ]
*/

console.log('bar'.cartesianMap((char) => ['', char]));
/*
[ '', 'r', 'a', 'ar', 'b', 'br', 'ba', 'bar' ]
*/

Keywords

FAQs

Package last updated on 16 May 2020

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc