Skip to content

Commit c284aba

Browse files
authored
Merge pull request #32 from MADE-Apps/feature/improvements
Added new functions and helpers across packages
2 parents 64647c8 + 1bc3131 commit c284aba

File tree

12 files changed

+263
-71
lines changed

12 files changed

+263
-71
lines changed

MADE.Collections/package-lock.json

Lines changed: 30 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

MADE.Collections/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@
3737
},
3838
"homepage": "https://github.com/MADE-Apps/MADE.js#readme",
3939
"devDependencies": {
40-
"@types/jest": "^27.4.0",
41-
"jest": "^27.4.7",
42-
"ts-jest": "^27.1.2",
43-
"typescript": "^4.5.4"
40+
"@types/jest": "^27.4.1",
41+
"jest": "^27.5.1",
42+
"ts-jest": "^27.1.4",
43+
"typescript": "^4.6.3"
4444
}
4545
}

MADE.Collections/src/array/index.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export function potentialIndexOf<T>(array: T[], value: T, predicate: (value: T,
124124
}
125125

126126
/**
127-
*Inserts an item to the specified array at the potential index determined by the predicate.
127+
* Inserts an item to the specified array at the potential index determined by the predicate.
128128
* @param {T[]} array - The array of items the item should be inserted into.
129129
* @param {T} value - The object to insert into the array.
130130
* @param {(value: T, item: T) => boolean} predicate - The function to determine the position of the item based on the provided value.
@@ -137,3 +137,17 @@ export function insertAtPotentialIndex<T>(array: T[], value: T, predicate: (valu
137137
}
138138
return potentialIndex;
139139
}
140+
141+
/**
142+
* Creates a new array from the specified and shuffles the elements randomly.
143+
* @param {T[]} array - The array of items to shuffle.
144+
* @return {T[]} The new shuffled array of items.
145+
*/
146+
export function shuffle<T>(array: T[]): T[] {
147+
const result = array.slice();
148+
for (let i = result.length - 1; i > 0; i--) {
149+
const j = Math.floor(Math.random() * (i + 1));
150+
[result[i], result[j]] = [result[j], result[i]];
151+
}
152+
return result;
153+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { shuffle } from "../../src";
2+
3+
describe("when shuffling items", () => {
4+
it("should shuffle item order randomly", () => {
5+
// Arrange
6+
const items = [1, 2, 3, 4, 5];
7+
8+
// Act
9+
var shuffled = shuffle(items);
10+
11+
// Assert
12+
expect(shuffled).not.toEqual([1, 2, 3, 4, 5]);
13+
});
14+
15+
it("should contain same items after shuffle", () => {
16+
// Arrange
17+
const items = [1, 2, 3, 4, 5];
18+
19+
// Act
20+
var shuffled = shuffle(items);
21+
22+
// Assert
23+
expect(shuffled).toContain(1);
24+
expect(shuffled).toContain(2);
25+
expect(shuffled).toContain(3);
26+
expect(shuffled).toContain(4);
27+
expect(shuffled).toContain(5);
28+
});
29+
});

MADE.Data.Converters/package-lock.json

Lines changed: 30 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

MADE.Data.Converters/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@
3636
},
3737
"homepage": "https://github.com/MADE-Apps/MADE.js#readme",
3838
"devDependencies": {
39-
"@types/jest": "^27.4.0",
40-
"jest": "^27.4.7",
41-
"ts-jest": "^27.1.2",
42-
"typescript": "^4.5.4"
39+
"@types/jest": "^27.4.1",
40+
"jest": "^27.5.1",
41+
"ts-jest": "^27.1.4",
42+
"typescript": "^4.6.3"
4343
}
4444
}

MADE.Data.Converters/src/date/index.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,29 @@ export function toCurrentAge(startingDate: Date): number {
1313
return yearDiff;
1414
}
1515

16+
/**
17+
* Gets the day suffix for the specified date, i.e. st, nd, rd, or th.
18+
* @param {Date} date - The date to get a day suffix for.
19+
* @return {string} The day suffix as a string.
20+
*/
21+
export function toDaySuffix(date: Date): string {
22+
const day = date.getDate();
23+
switch (day) {
24+
case 1:
25+
case 21:
26+
case 31:
27+
return "st";
28+
case 2:
29+
case 22:
30+
return "nd";
31+
case 3:
32+
case 23:
33+
return "rd";
34+
default:
35+
return "th";
36+
}
37+
}
38+
1639
/**
1740
* Rounds the date value to its nearest hour determined by the half hour of each hour.
1841
* @param {Date} date - The date to round to the nearest hour.

0 commit comments

Comments
 (0)