Skip to content

Commit

Permalink
refactor: Move array functions to a folder #89
Browse files Browse the repository at this point in the history
  • Loading branch information
201flaviosilva committed Oct 19, 2022
1 parent 1974e18 commit 237cbf4
Show file tree
Hide file tree
Showing 21 changed files with 123 additions and 111 deletions.
18 changes: 18 additions & 0 deletions src/Array/allEqual.js
@@ -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])); };
8 changes: 4 additions & 4 deletions src/arrayChoice.js → src/Array/choice.js
@@ -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)];
}
12 changes: 6 additions & 6 deletions src/arrayFindBigObject.js → src/Array/findBigObject.js
Expand Up @@ -3,17 +3,17 @@
*
* @example
* const myArray = [{a:1, b:100}, {a: 0, b:50}, {a:0, b:200}]
* arrayFindBigObject(myArray, "b"); // {a:0, b:200}
* arrayFindBigObject(myArray, "a"); // {a:1, b:100}
* arrayGetMaxObjects(myArray, "b", true); // 200
* arrayGetMaxObjects(myArray, "a", true); // 1
* findBigObject(myArray, "b"); // {a:0, b:200}
* findBigObject(myArray, "a"); // {a:1, b:100}
* findBigObject(myArray, "b", true); // 200
* findBigObject(myArray, "a", true); // 1
*
* @param {any[]} array - The array to search
* @param {string} prop - The property to find the biggest element
* @param {boolean} [returnOnlyValue=false] - If true only returns the value of the given property with the biggest value
* @returns {any|number} - The biggest element in the array
*/
export function arrayFindBigObject(array, prop, returnOnlyValue = false) {
export function findBigObject(array, prop, returnOnlyValue = false) {
const biggest = Math.max(...array.map(o => o[prop]));
return returnOnlyValue ? biggest : array.find((e) => e[prop] === biggest);
}
Expand All @@ -26,7 +26,7 @@ export function arrayFindBigObject(array, prop, returnOnlyValue = false) {
* @returns {Object} - The biggest element in the array
* @deprecated
*/
export function arrayFindBigObjectDeprecated(array, prop) {
export function findBigObjectDeprecated(array, prop) {
const propValues = array.map((e) => e[prop]); // Get a new array only the props
const minPropValue = Math.max(...propValues); // Try to find the biggest value
return array.find((e) => e[prop] === minPropValue); // Find the biggest in the array
Expand Down
12 changes: 6 additions & 6 deletions src/arrayFindLowObject.js → src/Array/findLowObject.js
Expand Up @@ -3,18 +3,18 @@
*
* @example
* const myArray = [{a:1, b:100}, {a:10, b:50}, {a:0, b:200}]
* arrayFindLowObject(myArray, "b"); // {a:10, b:50}
* arrayFindLowObject(myArray, "a"); // {a:0, b:200}
* arrayGetMinObjects(myArray, "b", true); // 50
* arrayGetMinObjects(myArray, "a", true); // 0
* findLowObject(myArray, "b"); // {a:10, b:50}
* findLowObject(myArray, "a"); // {a:0, b:200}
* findLowObject(myArray, "b", true); // 50
* findLowObject(myArray, "a", true); // 0
*
* @param {Object[]} array - The array to search
* @param {string} prop - The property to find the lowest element
* @param {boolean} [returnOnlyValue=false] - If true only returns the value of the given property with the lowest value
* @returns {Object} - The lowest element in the array
*/

export function arrayFindLowObject(array, prop, returnOnlyValue = false) {
export function findLowObject(array, prop, returnOnlyValue = false) {
const lowest = Math.min(...array.map(o => o[prop]));
return returnOnlyValue ? lowest : array.find((e) => e[prop] === lowest);
}
Expand All @@ -27,7 +27,7 @@ export function arrayFindLowObject(array, prop, returnOnlyValue = false) {
* @returns {Object} - The lowest element in the array
* @deprecated
*/
export function arrayFindLowObjectDeprecated(array, prop) {
export function findLowObjectDeprecated(array, prop) {
const propValues = array.map((e) => e[prop]); // Get a new array only the props
const minPropValue = Math.min(...propValues); // Try to find the lowest value
return array.find((e) => e[prop] === minPropValue); // Find the lowest in the array
Expand Down
25 changes: 25 additions & 0 deletions src/Array/index.js
@@ -0,0 +1,25 @@
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 { random } from "./random.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,
random,
shuffle,
sortAscending, sortAscendingObject,
sortDescending, sortDescendingObject,
};
21 changes: 21 additions & 0 deletions src/Array/isSorted.js
@@ -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;
}
20 changes: 10 additions & 10 deletions src/arrayMove.js → src/Array/move.js
Expand Up @@ -2,16 +2,16 @@
* @description
* Move an array element to the right
*
* @example arrayMoveLeft([1,2,3,4,5]); // [5,1,2,3,4]
* @example arrayMoveLeft([1,2,3,4,5], 2); // [4,5,1,2,3]
* @example arrayMoveLeft(["a","b","c","d","e"], 7) // ["d","e","a","b","c"]
* @example arrayMoveLeft(["a","b"], 3) // ["b","a"]
* @example moveLeft([1,2,3,4,5]); // [5,1,2,3,4]
* @example moveLeft([1,2,3,4,5], 2); // [4,5,1,2,3]
* @example moveLeft(["a","b","c","d","e"], 7) // ["d","e","a","b","c"]
* @example moveLeft(["a","b"], 3) // ["b","a"]
*
* @param {any[]} array - The array to move
* @param {number} times - The number of times to move the array
* @returns {any[]}
*/
export function arrayMoveLeft(array = [], times = 1) {
export function moveLeft(array = [], times = 1) {
for (let t = 0; t < times; t++) {

let last = array[array.length - 1];
Expand All @@ -30,16 +30,16 @@ export function arrayMoveLeft(array = [], times = 1) {
* @description
* Move an array element to the left
*
* @example arrayMoveRight([1,2,3,4,5]); // [2,3,4,5,1]
* @example arrayMoveRight([1,2,3,4,5], 2); // [3,4,5,1,2]
* @example arrayMoveRight(["a","b","c","d","e"], 7) // ["c","d","e","a","b"]
* @example arrayMoveRight(["a","b"], 3) // ["b","a"]
* @example moveRight([1,2,3,4,5]); // [2,3,4,5,1]
* @example moveRight([1,2,3,4,5], 2); // [3,4,5,1,2]
* @example moveRight(["a","b","c","d","e"], 7) // ["c","d","e","a","b"]
* @example moveRight(["a","b"], 3) // ["b","a"]
*
* @param {any[]} array - The array to move
* @param {number} times - The number of times to move the array
* @returns {any[]}
*/
export function arrayMoveRight(array = [], times = 1) {
export function moveRight(array = [], times = 1) {
for (let t = 0; t < times; t++) {

let last = array[0]; // temp variable to store last changed value
Expand Down
13 changes: 13 additions & 0 deletions src/Array/random.js
@@ -0,0 +1,13 @@
/**
* This function will randomize the position of the array items. (change the original array!)
*
* @example random(["A", "B", "C"]); // ["B","A","C"]
* @example random([1,2,3,4,5,6,7,8,9]); // [8,5,1,4,3,6,9,2,7]
* @example random([{a:1},{b:2},{c:3}]); // [{a:1},{c:3},{b:2}]
*
* @param {any[]} array - the array with the items to randomize
* @returns {any[]} the array changed
*/
export function random(array) {
return array.sort(() => Math.random() - 0.5);
}
12 changes: 6 additions & 6 deletions src/arrayShuffle.js → src/Array/shuffle.js
@@ -1,17 +1,17 @@
import { randomInt } from "./randomNumber";
import { clone } from "./clone";
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)
*
* @example arrayShuffle(["A", "B", "C"]); // ["B","A","C"]
* @example arrayShuffle([1,2,3,4,5,6,7,8,9]); // [8,5,1,4,3,6,9,2,7]
* @example arrayShuffle([{a:1},{b:2},{c:3}]); // [{a:1},{c:3},{b:2}]
* @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
* @returns {any[]}
*/
export function arrayShuffle(array) {
export function shuffle(array) {
const copy = clone(array);
const shuffled = [];

Expand Down
4 changes: 2 additions & 2 deletions src/arraySortAscending.js → src/Array/sortAscending.js
Expand Up @@ -3,13 +3,13 @@
*
* @example
* const myArr = [10,4,2,7,1,0,11,4,2,3,5,8,4,3,0,6];
* const myNewSortedArr = arraySortAscending(myArr); // [0,0,1,2,2,3,3,4,4,4,5,6,7,8,10,11]
* const myNewSortedArr = sortAscending(myArr); // [0,0,1,2,2,3,3,4,4,4,5,6,7,8,10,11]
*
* @param {number[]} arr - the array to sort
* @param {boolean} [changeArr=false] - if true will change the original array
* @returns {number[]} A new Array sorted
*/
export function arraySortAscending(arr, changeArr = false) {
export function sortAscending(arr, changeArr = false) {
if (!changeArr) return [...arr].sort((a, b) => a - b);
return arr.sort((a, b) => a - b);
}
Expand Up @@ -5,4 +5,4 @@
* @param {string} prop - the property base to sort
* @returns {any[]} - a new sorted array by the given property
*/
export function arraySortAscendingObject(arr, prop) { return [...arr].sort((a, b) => a[prop] - b[prop]); }
export function sortAscendingObject(arr, prop) { return [...arr].sort((a, b) => a[prop] - b[prop]); }
2 changes: 1 addition & 1 deletion src/arraySortDescending.js → src/Array/sortDescending.js
Expand Up @@ -8,4 +8,4 @@
* @param {number[]} arr - the array to sort
* @returns {number[]} A new Array sorted
*/
export function arraySortDescending(arr) { return [...arr].sort((a, b) => b - a); }
export function sortDescending(arr) { return [...arr].sort((a, b) => b - a); }
Expand Up @@ -5,4 +5,4 @@
* @param {string} prop - the property base to sort
* @returns {any[]} - a new sorted array by the given property
*/
export function arraySortDescendingObject(arr, prop) { return [...arr].sort((a, b) => b[prop] - a[prop]); }
export function sortDescendingObject(arr, prop) { return [...arr].sort((a, b) => b[prop] - a[prop]); }
3 changes: 3 additions & 0 deletions src/Maths/factorial.js
Expand Up @@ -12,6 +12,9 @@
*
* @param {number} number - the number to factoring
* @returns {number} the result
*
* @function factorial
* @memberof Maths
*/
export function factorial(number) {
if (number <= 1) return number;
Expand Down
2 changes: 2 additions & 0 deletions src/Maths/index.js
Expand Up @@ -3,6 +3,7 @@ import { clamp } from "./clamp.js";
import { degreesToRadians } from "./degreesToRadians.js";
import { divideEvenly } from "./divideEvenly.js";
import { division } from "./division.js";
import { factorial } from "./factorial.js";
import { getPositionWithAngleDistance } from "./getPositionWithAngleDistance.js";
import { isEven, isOdd } from "./isEvenOdd.js";
import { invertedLerp } from "./invertedLerp.js";
Expand Down Expand Up @@ -30,6 +31,7 @@ export {
degreesToRadians,
divideEvenly,
division,
factorial,
getPositionWithAngleDistance,
invertedLerp,
isEven, isOdd,
Expand Down
2 changes: 1 addition & 1 deletion src/Maths/median.js
@@ -1,5 +1,5 @@
import { isEven } from "./isEvenOdd";
import { arraySortAscending } from "../arraySortAscending";
import { arraySortAscending } from "../Array/arraySortAscending";

/**
* Returns the median of the givens numbers
Expand Down
18 changes: 0 additions & 18 deletions src/arrayAllEqual.js

This file was deleted.

21 changes: 0 additions & 21 deletions src/arrayIsSorted.js

This file was deleted.

13 changes: 0 additions & 13 deletions src/arrayRandom.js

This file was deleted.

0 comments on commit 237cbf4

Please sign in to comment.