Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ticky/fisher-yates-map
🎲 A map implementation which calls the callback for each element in a random order
https://github.com/ticky/fisher-yates-map
fisher-yates fisher-yates-map map
Last synced: 18 days ago
JSON representation
🎲 A map implementation which calls the callback for each element in a random order
- Host: GitHub
- URL: https://github.com/ticky/fisher-yates-map
- Owner: ticky
- License: mit
- Created: 2017-06-16T23:08:50.000Z (over 7 years ago)
- Default Branch: develop
- Last Pushed: 2024-08-06T08:48:35.000Z (3 months ago)
- Last Synced: 2024-10-07T02:42:04.834Z (about 1 month ago)
- Topics: fisher-yates, fisher-yates-map, map
- Language: JavaScript
- Homepage:
- Size: 385 KB
- Stars: 1
- Watchers: 3
- Forks: 1
- Open Issues: 18
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Fisher-Yates Map
[![npm](https://img.shields.io/npm/v/fisher-yates-map.svg?maxAge=2592000)](https://www.npmjs.com/package/fisher-yates-map) ![fisher-yates-map](https://img.shields.io/npm/l/fisher-yates-map.svg?maxAge=2592000) [![Build Status](https://travis-ci.org/ticky/fisher-yates-map.svg?branch=develop)](https://travis-ci.org/ticky/fisher-yates-map) [![codecov](https://codecov.io/gh/ticky/fisher-yates-map/branch/develop/graph/badge.svg)](https://codecov.io/gh/ticky/fisher-yates-map)
A map implementation which calls the callback for each element in a random order
## Usage
`fyMap` is a simple map function which accepts an array and a callback;
```js
import fyMap from 'fisher-yates-map';const myArray = ['meow', 'purr', 'nya'];
const output = fyMap(myArray, (item, index, array) => {
console.log(`cat ${index + 1} ${item}s!`);
return `${item}~!`;
});
// => 'cat 2 purrs!'
// => 'cat 1 meows!'
// => 'cat 3 nyas!'console.log(output);
// => ['meow~!', 'purr~!', 'nya~!']
```The output will be the same as calling the equivalent built-in `Array.prototype.map` method, however, each item will be sent to the callback in a random order.
### With Babel Function Bind
If you're using [`babel-plugin-transform-function-bind`](http://babeljs.io/docs/plugins/transform-function-bind/), you can also call `fyMap` with an alternate syntax;
```js
const output = myArray::fyMap((item, index, array) => {
console.log(`cat ${index + 1} ${item}s!`);
return `${item}~!`;
});
```More information about this syntax extension can be found [on Babel's website](http://babeljs.io/docs/plugins/transform-function-bind/).
## Why?
This could be useful in situations where the order of operations doesn't matter, or you want to enforce that per-item operations not rely on prior results. It could also be useful if you have a large number of operations which you'd like to scatter the application of.
## Caveats
`fyMap` doesn't implement the standard `Array.prototype.map` handling of `this`. If that doesn't work for you, you should probably use something else!
Performance is also imperfect - if you need something as fast as `Array.prototype.map` or a traditional loop, this isn't it.