Skip to content

Commit e5566ec

Browse files
authored
Merge pull request #8410 from nbogie/fix-types-for-shuffle
Fix type for shuffle() and add type test.
2 parents 62e2651 + 923418f commit e5566ec

3 files changed

Lines changed: 31 additions & 3 deletions

File tree

src/utilities/utility_functions.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ function utilityFunctions(p5, fn){
676676
*
677677
* @method shuffle
678678
* @param {Array} array array to shuffle.
679-
* @param {Boolean} [bool] if `true`, shuffle the original array in place. Defaults to `false`.
679+
* @param {Boolean} [modify] if `true`, shuffle the original array in place. Defaults to `false`.
680680
* @return {Array} shuffled array.
681681
*
682682
* @example
@@ -759,9 +759,9 @@ function utilityFunctions(p5, fn){
759759
* </code>
760760
* </div>
761761
*/
762-
fn.shuffle = function (arr, bool) {
762+
fn.shuffle = function (arr, modify) {
763763
const isView = ArrayBuffer && ArrayBuffer.isView && ArrayBuffer.isView(arr);
764-
arr = bool || isView ? arr : arr.slice();
764+
arr = modify || isView ? arr : arr.slice();
765765

766766
let rnd,
767767
tmp,

test/types/generics.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ function setup() {
1111

1212
// The types should fail if the result of random() is any
1313
logMessage(message);
14+
15+
testShuffleMaintainsType();
1416
}
1517

1618
// From: https://stackoverflow.com/a/50375286/62076
@@ -29,3 +31,23 @@ type NotAny<T> = IsStrictlyAny<T> extends true ? never : T
2931
function logMessage(message: NotAny<{ content: string }>) {
3032
console.log(message)
3133
}
34+
35+
36+
37+
/** test that shuffle(arr) preserves arr type in return, not just any[] */
38+
function testShuffleMaintainsType(){
39+
40+
type Expect<T extends true> = T;
41+
type Equal<X, Y> =
42+
(<T>() => T extends X ? 1 : 2) extends
43+
(<T>() => T extends Y ? 1 : 2) ? true : false;
44+
45+
const shuffleResult1 = shuffle(["a", "b", "c"]);
46+
const shuffleResult2 = shuffle(["a", 10, null]);
47+
//check the signature with the optional boolean type-checks, too
48+
const shuffleResult3 = shuffle([10, 20, 30], true);
49+
50+
type ShuffleTest1 = Expect<Equal<typeof shuffleResult1, string[]>>;
51+
type ShuffleTest2 = Expect<Equal<typeof shuffleResult2, (string|number|null)[]>>;
52+
type ShuffleTest3 = Expect<Equal<typeof shuffleResult3, number[]>>;
53+
}

utils/patch.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ export function applyPatches() {
4444
"random<T>(choices: readonly T[]): T;"
4545
);
4646

47+
replace(
48+
['p5.d.ts', 'global.d.ts'],
49+
'shuffle(array: any[], modify?: boolean): any[];',
50+
'shuffle<T>(array: T[], modify?: boolean): T[];'
51+
);
52+
4753
replace(
4854
'p5.d.ts',
4955
'textToContours(str: string, x: number, y: number, options?: { sampleFactor?: number; simplifyThreshold?: number }): object[][];',

0 commit comments

Comments
 (0)