From d824aa5e7a313202f7614fba3192a4774f637ab3 Mon Sep 17 00:00:00 2001 From: joshunrau Date: Tue, 27 Aug 2024 12:40:05 -0400 Subject: [PATCH 1/4] improve randomization typing --- packages/jspsych/src/modules/randomization.ts | 37 ++++++++++++++----- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/packages/jspsych/src/modules/randomization.ts b/packages/jspsych/src/modules/randomization.ts index 6343d762..d45a73fa 100644 --- a/packages/jspsych/src/modules/randomization.ts +++ b/packages/jspsych/src/modules/randomization.ts @@ -12,7 +12,7 @@ export function setSeed(seed: string = Math.random().toString()) { return seed; } -export function repeat(array, repetitions, unpack = false) { +export function repeat(array: any, repetitions: any, unpack = false) { const arr_isArray = Array.isArray(array); const rep_isArray = Array.isArray(repetitions); @@ -77,7 +77,7 @@ export function repeat(array, repetitions, unpack = false) { return out; } -export function shuffle(array: Array) { +export function shuffle(array: Array) { if (!Array.isArray(array)) { console.error("Argument to shuffle() must be an array."); } @@ -101,7 +101,7 @@ export function shuffle(array: Array) { return copy_array; } -export function shuffleNoRepeats(arr: Array, equalityTest: (a: any, b: any) => boolean) { +export function shuffleNoRepeats(arr: Array, equalityTest: (a: T, b: T) => boolean) { if (!Array.isArray(arr)) { console.error("First argument to shuffleNoRepeats() must be an array."); } @@ -143,7 +143,10 @@ export function shuffleNoRepeats(arr: Array, equalityTest: (a: any, b: any) return random_shuffle; } -export function shuffleAlternateGroups(arr_groups, random_group_order = false) { +export function shuffleAlternateGroups( + arr_groups: Array, + random_group_order = false +) { const n_groups = arr_groups.length; if (n_groups == 1) { console.warn( @@ -178,7 +181,7 @@ export function shuffleAlternateGroups(arr_groups, random_group_order = false) { return out; } -export function sampleWithoutReplacement(arr, size) { +export function sampleWithoutReplacement(arr: Array, size: number) { if (!Array.isArray(arr)) { console.error("First argument to sampleWithoutReplacement() must be an array"); } @@ -189,7 +192,7 @@ export function sampleWithoutReplacement(arr, size) { return shuffle(arr).slice(0, size); } -export function sampleWithReplacement(arr, size, weights?) { +export function sampleWithReplacement(arr: Array, size: number, weights?: number[]) { if (!Array.isArray(arr)) { console.error("First argument to sampleWithReplacement() must be an array"); } @@ -301,6 +304,17 @@ export function sampleExGaussian( return s; } +type RandomWordsOptions = { + min?: number; + max?: number; + exactly?: number; + maxLength?: number; + wordsPerString?: number; + seperator?: string; + formatter?: (word: string, index: number) => string; + join?: string; +}; + /** * Generate one or more random words. * @@ -311,8 +325,11 @@ export function sampleExGaussian( * * @returns An array of words or a single string, depending on parameter choices. */ -export function randomWords(opts) { - return rw(opts); +export function randomWords( + opts: T +): T extends { join: string } ? string : string[] { + // there is a type incompatibility here because `random-words` uses overloads rather than generics + return rw(opts) as any; } // Box-Muller transformation for a random sample from normal distribution with mean = 0, std = 1 @@ -325,8 +342,8 @@ function randn_bm() { return Math.sqrt(-2.0 * Math.log(u)) * Math.cos(2.0 * Math.PI * v); } -function unpackArray(array) { - const out = {}; +function unpackArray(array: object[]) { + const out: Record = {}; for (const x of array) { for (const key of Object.keys(x)) { From 1e807c741ed72e69604bdb0cd5f99d6b6c91e5fb Mon Sep 17 00:00:00 2001 From: joshunrau Date: Tue, 27 Aug 2024 12:41:03 -0400 Subject: [PATCH 2/4] update contributors file --- contributors.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contributors.md b/contributors.md index 3eae9831..4bc62a97 100644 --- a/contributors.md +++ b/contributors.md @@ -64,4 +64,4 @@ The following people have contributed to the development of jsPsych by writing c * Reto Wyss - https://github.com/retowyss * Shaobin Jiang - https://github.com/Shaobin-Jiang * Haotian Tu - https://github.com/thtTNT - +* Joshua Unrau - https://github.com/joshunrau \ No newline at end of file From 67b4a05878e7b308642e39bcd3be32db3be6939c Mon Sep 17 00:00:00 2001 From: joshunrau Date: Tue, 27 Aug 2024 12:50:00 -0400 Subject: [PATCH 3/4] clean up randomWords signature --- packages/jspsych/src/modules/randomization.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/jspsych/src/modules/randomization.ts b/packages/jspsych/src/modules/randomization.ts index d45a73fa..e58ebaff 100644 --- a/packages/jspsych/src/modules/randomization.ts +++ b/packages/jspsych/src/modules/randomization.ts @@ -315,6 +315,10 @@ type RandomWordsOptions = { join?: string; }; +type RandomWordsResult = T extends { join: string } + ? string + : string[]; + /** * Generate one or more random words. * @@ -325,11 +329,9 @@ type RandomWordsOptions = { * * @returns An array of words or a single string, depending on parameter choices. */ -export function randomWords( - opts: T -): T extends { join: string } ? string : string[] { +export function randomWords(opts: T) { // there is a type incompatibility here because `random-words` uses overloads rather than generics - return rw(opts) as any; + return rw(opts) as RandomWordsResult; } // Box-Muller transformation for a random sample from normal distribution with mean = 0, std = 1 From 860bbaf1c1995ee531f7b459749ad736099d4e57 Mon Sep 17 00:00:00 2001 From: joshunrau Date: Tue, 27 Aug 2024 12:53:22 -0400 Subject: [PATCH 4/4] add changeset --- .changeset/young-wolves-grow.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/young-wolves-grow.md diff --git a/.changeset/young-wolves-grow.md b/.changeset/young-wolves-grow.md new file mode 100644 index 00000000..6d4dd13d --- /dev/null +++ b/.changeset/young-wolves-grow.md @@ -0,0 +1,5 @@ +--- +"jspsych": patch +--- + +Improve type definitions for randomization module