Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from 201flaviosilva-labs/89-move-array-functio…
…ns-to-a-folder refactor: Move array functions to a folder #89
- Loading branch information
Showing
22 changed files
with
134 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* Check if all Elements of the array are equal | ||
* | ||
* @example | ||
* allEqual([0,0,0,0]) // true | ||
* allEqual([0,0,0,1]) // false | ||
* allEqual([0,"a","a"]) // false | ||
* allEqual([[1,1],[1,1]]) // true | ||
* allEqual([[1,1],[1,0]]) // false | ||
* allEqual([{a:"b",c:1},{a:"b"}]) // false | ||
* allEqual([{a:"b",c:1},{a:"b",c:1}]) // true | ||
* allEqual([{a:"b",c:1},{a:"b",c:1},{a:"c",c:1}]) // false | ||
* allEqual([{a:"b",c:1},{a:"b",c:1},{a:"b",c:1}]) // true | ||
* | ||
* @param {any} arr - the array to check all elements | ||
* @returns {boolean} true if all elements of the array ara equal | ||
*/ | ||
export function allEqual(arr) { return arr.every(v => JSON.stringify(v) === JSON.stringify(arr[0])); }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
import { randomInt } from "./randomNumber"; | ||
import { randomInt } from "../randomNumber"; | ||
|
||
/** | ||
* Returns a random item from the array | ||
* | ||
* @see {@link https://www.w3schools.com/python/ref_random_choice.asp} | ||
* | ||
* @example arrayChoice(["A", "B", "C"]); // "B" | ||
* @example arrayChoice([10, 5, 123]); // 10 | ||
* @example choice(["A", "B", "C"]); // "B" | ||
* @example choice([10, 5, 123]); // 10 | ||
* | ||
* @param {any[]} array - the array to select a random item | ||
* @returns {any} | ||
*/ | ||
export function arrayChoice(array) { | ||
export function choice(array) { | ||
return array[randomInt(array.length)]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { allEqual } from "./allEqual.js"; | ||
import { choice } from "./choice.js"; | ||
import { findBigObject } from "./findBigObject.js"; | ||
import { findLowObject } from "./findLowObject.js"; | ||
import { isSorted } from "./isSorted.js"; | ||
import { moveLeft, moveRight } from "./Move.js"; | ||
import { shuffle } from "./shuffle.js"; | ||
import { sortAscending } from "./sortAscending.js"; | ||
import { sortAscendingObject } from "./sortAscendingObject.js"; | ||
import { sortDescending } from "./sortDescending.js"; | ||
import { sortDescendingObject } from "./sortDescendingObject.js"; | ||
|
||
export { | ||
allEqual, | ||
choice, | ||
findBigObject, | ||
findLowObject, | ||
isSorted, | ||
moveLeft, moveRight, | ||
shuffle, | ||
sortAscending, sortAscendingObject, | ||
sortDescending, sortDescendingObject, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* Check if the given array is sorted fom lowest to highest | ||
* | ||
* @example | ||
* isSorted([]) // true | ||
* isSorted([0,0,0,0]) // true | ||
* isSorted([2,1,4]) // false | ||
* isSorted([3,2,1]) // false | ||
* isSorted([1,2,3]) // true | ||
* isSorted(["B","A","D"]) // false | ||
* isSorted(["A","B","C"]) // true | ||
* | ||
* @param {any} arr - the array to check | ||
* @returns {boolean} true if the array is sorted | ||
*/ | ||
export function isSorted(arr = []) { | ||
for (let i = 0; i < arr.length - 1; i++) { | ||
if (arr[i] > arr[i + 1]) return false; | ||
} | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { randomInt } from "../randomNumber"; | ||
import { clone } from "../clone"; | ||
|
||
/** | ||
* 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, mutateOriginal = true) { | ||
if (mutateOriginal) return array.sort(() => Math.random() - 0.5); | ||
|
||
// -- Mutate the original array | ||
const copy = clone(array); | ||
const shuffled = []; | ||
|
||
while (copy.length) { | ||
const index = randomInt(copy.length); | ||
shuffled.push(copy[index]); | ||
copy.splice(index, 1); | ||
} | ||
|
||
return shuffled; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.