Skip to content

Commit

Permalink
fix: merge array random and array random functions
Browse files Browse the repository at this point in the history
  • Loading branch information
201flaviosilva committed Oct 19, 2022
1 parent 237cbf4 commit 8c1d166
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ This file was based on [this template](https://gist.github.com/juampynr/4c18214a
- [Move `toggleFullScreen` to `DOM/toggleFullScreen`](https://github.com/201flaviosilva-labs/javascript-utils/commit/2c0e9f8d293c68f36e9297b70c18321678e40921);
- [Only export `getDate` function from `getDate` file](https://github.com/201flaviosilva-labs/javascript-utils/commit/2c0e9f8d293c68f36e9297b70c18321678e40921);
- [Removed `nand`, `nor`, `not` and `xnor`](https://github.com/201flaviosilva-labs/javascript-utils/commit/2473d089c7a699650abfce108425ee0d479ce7e7) #88;
- [Move array functions to a folder](https://github.com/201flaviosilva-labs/javascript-utils/pull/92/commits/237cbf42f22131dd83c5126107f21b32ce33e232) #89;


## [1.2.9] - 02-09-2022
Expand Down
2 changes: 0 additions & 2 deletions src/Array/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { findBigObject } from "./findBigObject.js";
import { findLowObject } from "./findLowObject.js";
import { isSorted } from "./isSorted.js";
import { moveLeft, moveRight } from "./Move.js";
import { random } from "./random.js";
import { shuffle } from "./shuffle.js";
import { sortAscending } from "./sortAscending.js";
import { sortAscendingObject } from "./sortAscendingObject.js";
Expand All @@ -18,7 +17,6 @@ export {
findLowObject,
isSorted,
moveLeft, moveRight,
random,
shuffle,
sortAscending, sortAscendingObject,
sortDescending, sortDescendingObject,
Expand Down
13 changes: 0 additions & 13 deletions src/Array/random.js

This file was deleted.

10 changes: 8 additions & 2 deletions src/Array/shuffle.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@ import { randomInt } from "../randomNumber";
import { clone } from "../clone";

/**
* This function will return a new array with the original items in random positions. (not change the original array)
* This function will randomize the position of the array items. (change the original array!)
*
* To not mutate the original array pass in the `mutateOriginal` argument false, and this function will return a new array with the original items in random positions. (not changing the original array)
*
* @example shuffle(["A", "B", "C"]); // ["B","A","C"]
* @example shuffle([1,2,3,4,5,6,7,8,9]); // [8,5,1,4,3,6,9,2,7]
* @example shuffle([{a:1},{b:2},{c:3}]); // [{a:1},{c:3},{b:2}]
*
* @param {any[]} array - the array with the items to randomize
* @param {boolean} mutateOriginal - the array with the items to randomize
* @returns {any[]}
*/
export function shuffle(array) {
export function shuffle(array, mutateOriginal = true) {
if (mutateOriginal) return array.sort(() => Math.random() - 0.5);

// -- Mutate the original array
const copy = clone(array);
const shuffled = [];

Expand Down

0 comments on commit 8c1d166

Please sign in to comment.