Skip to content

Commit

Permalink
fix code and test code
Browse files Browse the repository at this point in the history
  • Loading branch information
201flaviosilva committed Feb 1, 2023
1 parent 698b501 commit 220aa14
Show file tree
Hide file tree
Showing 32 changed files with 559 additions and 127 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This file was based on [this template](https://gist.github.com/juampynr/4c18214a
### [1.3.2] - 00-02-2023

#### Added
- Add tests to the code;

#### Changed
- Organize project;
Expand Down
6 changes: 2 additions & 4 deletions src/Maths/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ import { divideEvenly, divideEvenlyWithSpread } 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";
import { isEven, isOdd } from "./isEvenOdd.js";
import { isMultipleOf } from "./isMultipleOf.js";
import { isNarcissisticNumber } from "./isNarcissisticNumber.js";
import { lerp } from "./lerp.js";
import { makeNegative } from "./makeNegative.js";
import { map } from "./map.js";
import { median } from "./median.js";
import { mode } from "./mode.js";
import { percentage } from "./percentage.js";
import { multiplication } from "./multiplication.js";
import { negative } from "./negative.js";
import { percentage } from "./percentage.js";
import { radiansToDegrees } from "./radiansToDegrees.js";
import { range } from "./range.js";
import { subtraction } from "./subtraction.js";
Expand All @@ -40,7 +39,6 @@ export {
isMultipleOf,
isNarcissisticNumber,
lerp,
makeNegative,
map,
median,
mode,
Expand Down
22 changes: 12 additions & 10 deletions src/Maths/lerp.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
/**
* Return the value between 2 value based in a given percentage (decimal midpoint)
* Return the value between 2 values based on a given percentage (decimal midpoint)
* using linear interpolation.
*
* @example
* lerp(0, 0, 100); // 0
* lerp(0.5, 0, 100); // 50
* lerp(1, 0, 100); // 100
* @param {number} value - The value (decimal point)
* @param {number} min - Minimum value
* @param {number} max - Maximum value
* @returns {number} The result of the function
* @see {@link https://en.wikipedia.org/wiki/Linear_interpolation}
* lerp(0, 0, 100); // returns 0
* lerp(0.5, 0, 100); // returns 50
* lerp(1, 0, 100); // returns 100
*
* @param {number} value - The decimal value used for interpolation
* @param {number} min - The minimum value
* @param {number} max - The maximum value
* @returns {number} The result of the interpolation
*
* @see {@link https://en.wikipedia.org/wiki/Linear_interpolation} for more information on linear interpolation
*
* @function lerp
* @memberof Maths
Expand Down
15 changes: 0 additions & 15 deletions src/Maths/makeNegative.js

This file was deleted.

26 changes: 12 additions & 14 deletions src/Maths/map.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
/**
* Re-maps a number from one range to another
* @see {@link https://gist.github.com/xposedbones/75ebaef3c10060a3ee3b246166caab56#gistcomment-2951694}
*
* @example
* // value, range1, range2
* // map(value, x1, y1, x2, y2)
* map(-10, 0, 100, 0, 1000); // -100
* map(0, 0, 100, 0, 1000); // 0
* map(10, 0, 100, 0, 1000); // 100
* map(50, 0, 100, 0, 1000); // 500
* map(1000, 0, 100, 0, 1000) // 10000
* // map(value, fromRangeStart, fromRangeEnd, toRangeStart, toRangeEnd)
* map(-10, 0, 100, 0, 1000) // returns -100
* map(0, 0, 100, 0, 1000) // returns 0
* map(10, 0, 100, 0, 1000) // returns 100
* map(50, 0, 100, 0, 1000) // returns 500
* map(1000, 0, 100, 0, 1000) // returns 10000
*
* @param {number} value
* @param {number} start1 - Start of the range 1
* @param {number} end1 - End of the range 1
* @param {number} start2 - Start of the range 2
* @param {number} end2 - End of the range 2
* @returns {number} the value un the range
* @param {number} value - The number to be re-mapped
* @param {number} fromRangeStart - The start of the range the number is currently in
* @param {number} fromRangeEnd - The end of the range the number is currently in
* @param {number} toRangeStart - The start of the range the number should be mapped to
* @param {number} toRangeEnd - The end of the range the number should be mapped to
* @returns {number} - The re-mapped number
*
* @function map
* @memberof Maths
Expand Down
30 changes: 17 additions & 13 deletions src/Maths/mode.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
/**
* Returns the most repeated number
* Returns the most repeated element in an array
*
* @example mode(1,2,2,3,4) // 2
* @example
* mode([1, 2, 2, 3, 4]); // 2
* mode(["apple", "banana", "banana", "cherry"]); // "banana"
*
* @param {...number} numbers - the numbers to get the median
* @returns {number}
* @param {Array} args - the elements to get the mode
* @returns {*}
*
* @function mode
* @memberof Maths
*/
export function mode(...numbers) {
export function mode(...args) {
if (args.length === 1) return args[0];

const mode = {};
let max = 0;
let max = args[0];
let count = 0;

for (let i = 0; i < numbers.length; i++) {
const num = numbers[i];
for (let i = 0; i < args.length; i++) {
const el = args[i];

if (mode[num]) mode[num]++;
else mode[num] = 1;
if (mode[el]) mode[el]++;
else mode[el] = 1;

if (count < mode[num]) {
max = num;
count = mode[num];
if (count < mode[el]) {
max = el;
count = mode[el];
}
}

Expand Down
16 changes: 9 additions & 7 deletions src/Maths/multiplication.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
/**
* Calculates a multiplication of all givens numbers
* Calculates the product of all given numbers
*
* @example multiplication(1,2); // 2
* @example multiplication(1,2,-5,2.4,-6.5,0.5); // 78
* @example multiplication(1,2,3,4,5,6,7,8,9,0); // 0
* @example multiplication(-1,2,3,4,5,6,7,8,-9); // 362880
* @example
* multiplication(1, 2); // returns 2
* multiplication(1, 2, -5, 2.4, -6.5, 0.5); // returns 78
* multiplication(1, 2, 3, 4, 5, 6, 7, 8, 9, 0); // returns 0
* multiplication(-1, 2, 3, 4, 5, 6, 7, 8, -9); // returns 362880
* multiplication(...[-1, 2, 3, 4, 5, 6, 7, 8, -9]); // returns 362880
*
* @param {...number} numbers - the numbers to multiplication
* @returns {number}
* @param {...number} numbers - The numbers to be multiplied
* @returns {number} The product of all given numbers
*
* @function multiplication
* @memberof Maths
Expand Down
16 changes: 10 additions & 6 deletions src/Maths/negative.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
/**
* Convertes the given number in a negative number
* Converts the given number to its negative equivalent, unless it's already negative or zero.
*
* @example negative(5); // -5
* @example negative(-35); // -35
* @example
* negative(10) // -10
* negative(0) // 0
* negative(-5) // -5
*
* @param {number} num - number to convert
* @returns {number} fixed number
* @param {number} number - The number to be converted
* @returns {number} - The negative equivalent of the input number, or 0 if the input was 0.
*
* @function negative
* @memberof Maths
*/
export function negative(num) { return Math.abs(num) * -1; }
export function negative(number) {
return number === 0 ? 0 : -Math.abs(number);
};
16 changes: 8 additions & 8 deletions src/Maths/percentage.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/**
* Return the value between 2 value based in a given percentage (decimal midpoint)
* Calculates the percentage of a given value in relation to a total value
*
* @example
* percentage(10, 100); // 10
* percentage(40, 40); // 100
* percentage(40, 20); // 200
* @param {number} value - the value to compare
* @param {number} total - total value
* @returns {number} the result
* percentage(10, 100); // returns 10
* percentage(40, 40); // returns 100
* percentage(40, 20); // returns 200
*
* @param {number} value - The value to be calculated as a percentage
* @param {number} total - The total value to be used as reference
* @returns {number} The calculated percentage
*
* @function percentage
* @memberof Maths
Expand Down
10 changes: 5 additions & 5 deletions src/Maths/radiansToDegrees.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* Convert the given radians value in degrees
* Converts a given radian value to degrees
*
* @example
* radiansToDegrees(1.58) // 57.29577951308232
* radiansToDegrees(2.5) // 143.2394487827058
* radiansToDegrees(1.58) // returns: 90.52733163067008
* radiansToDegrees(2.5) // returns: 143.2394487827058
*
* @param {number} r - radians value to convert in degrees
* @returns {number} converted the given radian in degrees
* @param {number} r - The radian value to be converted to degrees
* @returns {number} The converted radian value in degrees
*
* @function radiansToDegrees
* @memberof Maths
Expand Down
27 changes: 15 additions & 12 deletions src/Maths/range.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
/**
* Return a array of number Between the 2 given values,
* Returns an array of numbers between the `start` and `end` parameters,
* incrementing by the `step` parameter.
* Optionally, the values within the specified `skip` range can be skipped.
*
* @example range(1, 5); // [ 1, 2, 3, 4, 5 ]
* @example range(0, 100, 10); // [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
* @example range(0, 100, 100) // [0, 100]
* @example range(1, 100, 100) // [1]
* @example range(0, 10, 1, [{start:2,end:8}]) // [0, 1, 9, 10]
* @example range(0, 10, 1, [{start:2,end:4}, {start:7,end:8},]) // [0, 1, 5, 6, 9, 10]
* @example
* range(1, 5); // [1, 2, 3, 4, 5]
* range(0, 100, 10); // [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
* range(0, 100, 100) // [0, 100]
* range(1, 100, 100) // [1]
* range(0, 10, 1, [{start:2, end:8}]) // [0, 1, 9, 10]
* range(0, 10, 1, [{start:2, end:4}, {start:7, end:8}]) // [0, 1, 5, 6, 9, 10]
*
* @param {number} start - start point to add numbers
* @param {number} end - end point to stop adding numbers
* @param {number} [step=1] - The value to increment
* @param {number[]} [skip=[]] - The values to skip
* @returns {number[]} Array of number
* @param {number} start - The starting point of the range
* @param {number} end - The ending point of the range
* @param {number} [step=1] - The increment value
* @param {Array<{start: number, end: number}>} [skip=[]] - The range of values to skip
* @returns {number[]} An array of numbers
*
* @function range
* @memberof Maths
Expand Down
5 changes: 5 additions & 0 deletions src/Maths/subtraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* @example subtraction(1,2,-5,2.4,-6.5,0.5); // 7.6
* @example subtraction(1,2,3,4,5,6,7,8,9,0); // -43
* @example subtraction(-1,2,3,4,5,6,7,8,-9); // -27
* @example subtraction(...[-1,2,3,4,5,6,7,8,-9]); // -27
*
* @param {...number} numbers - the numbers to subtraction
* @returns {number}
Expand All @@ -19,6 +20,10 @@ export function subtraction(...numbers) {

let result = numbers[0];
for (let i = 1; i < numbers.length; i++) {
if (typeof numbers[i] !== "number") {
console.error(`The element at position ${i} is not a number`);
return NaN;
}
result -= numbers[i];
}
return result;
Expand Down
1 change: 1 addition & 0 deletions src/Maths/sum.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* @example sum(1,2,-5,2.4,-6.5); // -6.1
* @example sum(1,2,3,4,5,6,7,8,9,0); // 45
* @example sum(-1,2,3,4,5,6,7,8,-9); // 25
* @example sum(...[-1,2,3,4,5,6,7,8,-9]); // 25
*
* @param {...number} numbers - the numbers to sum
* @returns {number}
Expand Down
49 changes: 24 additions & 25 deletions src/Physics/topDownCarMovimentation.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
import { clamp } from "../Maths/clamp.js";

/**
* Updates a position, rotation and speed of a car in the top down view
* Updates the position, rotation, and speed of a car in a top-down view.
*
* The algorithm is based on {@link https://www.youtube.com/watch?v=Rs_rAxEsAvI}.
*
* Algorithm based in {@link https://www.youtube.com/watch?v=Rs_rAxEsAvI}
* @see {@link https://www.youtube.com/watch?v=Rs_rAxEsAvI}
*
* @see example -> {@link https://codesandbox.io/s/top-down-car-movimentation-sobjn0?file=/src/index.js}
* @see example -> {@link https://codesandbox.io/s/top-down-car-movimentation-sobjn0}
*
* @param {Object} state - the current state of the car
* @param {Object} state.keys - the objet with the input keys
* @param {boolean} [state.keys.forward=false] true if the forward key is pressed
* @param {boolean} [state.keys.left=false] true if the left key is pressed
* @param {boolean} [state.keys.right=false] true if the right key is pressed
* @param {boolean} [state.keys.reverse=false] true if the reverse key is pressed
* @param {number} state.x - current x position of the car
* @param {number} state.y - current y position of the car
* @param {number} state.speed - current car speed
* @param {number} state.acceleration - acceleration value per update
* @param {number} [state.maxSpeed=Infinity] - the limit speed of the car
* @param {number} [state.friction=0] - friction of the car (0 to 1)
* @param {number} state.rotation - current car rotation
* @param {number} state.rotationSpeed - car rotation speed
* @param {Object} state.bounds - limits the maximum position on the axes
* @param {Object} state.bounds.x - limits the maximum position in the x axis
* @param {number} [state.bounds.x.min=-Infinity] - minimum limit on the x axis
* @param {number} [state.bounds.x.max=Infinity] - max limit on the x axis
* @param {Object} state.bounds.y - limits the maximum position in the y axis
* @param {number} [state.bounds.y.min=-Infinity] - minimum limit on the y axis
* @param {number} [state.bounds.y.max=Infinity] - max limit on the y axis
* @param {Object} state.keys - the object containing the input keys
* @param {boolean} [state.keys.forward=false] - true if the forward key is pressed
* @param {boolean} [state.keys.left=false] - true if the left key is pressed
* @param {boolean} [state.keys.right=false] - true if the right key is pressed
* @param {boolean} [state.keys.reverse=false] - true if the reverse key is pressed
* @param {number} state.x - the current x position of the car
* @param {number} state.y - the current y position of the car
* @param {number} state.speed - the current car speed
* @param {number} state.acceleration - the acceleration value per update
* @param {number} [state.maxSpeed=Infinity] - the maximum speed limit of the car
* @param {number} [state.friction=0] - the friction of the car (0 to 1)
* @param {number} state.rotation - the current car rotation
* @param {number} state.rotationSpeed - the car rotation speed
* @param {Object} state.bounds - the maximum position limits on the axes
* @param {Object} state.bounds.x - the maximum x axis position limits
* @param {number} [state.bounds.x.min=-Infinity] - the minimum x axis limit
* @param {number} [state.bounds.x.max=Infinity] - the maximum x axis limit
* @param {Object} state.bounds.y - the maximum y axis position limits
* @param {number} [state.bounds.y.min=-Infinity] - the minimum y axis limit
* @param {number} [state.bounds.y.max=Infinity] - the maximum y axis limit
* @returns {Object}
*/
export function topDownCarMovimentation({
Expand Down
18 changes: 10 additions & 8 deletions tests/Arrays/findBigObject.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ import { Arrays } from "../../src/index.js";
const { findBigObject } = Arrays;

describe("Arrays/findBigObject.js", () => {
const mockArray = [{ a: 1, b: 100 }, { a: 0, b: 50 }, { a: 0, b: 200 }];
describe("findBigObject", () => {
const mockArray = [{ a: 1, b: 100 }, { a: 0, b: 50 }, { a: 0, b: 200 }];

it("should return the object with the biggest given property value", () => {
expect(findBigObject(mockArray, "b")).toEqual({ a: 0, b: 200 });
expect(findBigObject(mockArray, "a")).toEqual({ a: 1, b: 100 });
});
it("should return the object with the biggest given property value", () => {
expect(findBigObject(mockArray, "b")).toEqual({ a: 0, b: 200 });
expect(findBigObject(mockArray, "a")).toEqual({ a: 1, b: 100 });
});

it("should return only the value of the biggest given property value", () => {
expect(findBigObject(mockArray, "b", true)).toBe(200);
expect(findBigObject(mockArray, "a", true)).toBe(1);
it("should return only the value of the biggest given property value", () => {
expect(findBigObject(mockArray, "b", true)).toBe(200);
expect(findBigObject(mockArray, "a", true)).toBe(1);
});
});
});

0 comments on commit 220aa14

Please sign in to comment.