jspsych 8.0.1 → 8.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jspsych",
3
- "version": "8.0.1",
3
+ "version": "8.0.3",
4
4
  "description": "Behavioral experiments in a browser",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
@@ -48,7 +48,7 @@
48
48
  },
49
49
  "devDependencies": {
50
50
  "@fontsource/open-sans": "4.5.3",
51
- "@jspsych/config": "^3.0.0",
51
+ "@jspsych/config": "^3.0.1",
52
52
  "@types/dom-mediacapture-record": "^1.0.11",
53
53
  "base64-inline-loader": "^2.0.1",
54
54
  "css-loader": "^6.6.0",
@@ -48,7 +48,7 @@ export class KeyboardListenerAPI {
48
48
  private rootKeydownListener(e: KeyboardEvent) {
49
49
  // Iterate over a static copy of the listeners set because listeners might add other listeners
50
50
  // that we do not want to be included in the loop
51
- for (const listener of Array.from(this.listeners)) {
51
+ for (const listener of [...this.listeners]) {
52
52
  listener(e);
53
53
  }
54
54
  this.heldKeys.add(this.toLowerCaseIfInsensitive(e.key));
@@ -12,7 +12,7 @@ export function setSeed(seed: string = Math.random().toString()) {
12
12
  return seed;
13
13
  }
14
14
 
15
- export function repeat(array, repetitions, unpack = false) {
15
+ export function repeat(array: any, repetitions: any, unpack = false) {
16
16
  const arr_isArray = Array.isArray(array);
17
17
  const rep_isArray = Array.isArray(repetitions);
18
18
 
@@ -77,7 +77,7 @@ export function repeat(array, repetitions, unpack = false) {
77
77
  return out;
78
78
  }
79
79
 
80
- export function shuffle(array: Array<any>) {
80
+ export function shuffle<T>(array: Array<T>) {
81
81
  if (!Array.isArray(array)) {
82
82
  console.error("Argument to shuffle() must be an array.");
83
83
  }
@@ -101,7 +101,7 @@ export function shuffle(array: Array<any>) {
101
101
  return copy_array;
102
102
  }
103
103
 
104
- export function shuffleNoRepeats(arr: Array<any>, equalityTest: (a: any, b: any) => boolean) {
104
+ export function shuffleNoRepeats<T>(arr: Array<T>, equalityTest: (a: T, b: T) => boolean) {
105
105
  if (!Array.isArray(arr)) {
106
106
  console.error("First argument to shuffleNoRepeats() must be an array.");
107
107
  }
@@ -143,7 +143,10 @@ export function shuffleNoRepeats(arr: Array<any>, equalityTest: (a: any, b: any)
143
143
  return random_shuffle;
144
144
  }
145
145
 
146
- export function shuffleAlternateGroups(arr_groups, random_group_order = false) {
146
+ export function shuffleAlternateGroups<T extends any[]>(
147
+ arr_groups: Array<T>,
148
+ random_group_order = false
149
+ ) {
147
150
  const n_groups = arr_groups.length;
148
151
  if (n_groups == 1) {
149
152
  console.warn(
@@ -178,7 +181,7 @@ export function shuffleAlternateGroups(arr_groups, random_group_order = false) {
178
181
  return out;
179
182
  }
180
183
 
181
- export function sampleWithoutReplacement(arr, size) {
184
+ export function sampleWithoutReplacement<T>(arr: Array<T>, size: number) {
182
185
  if (!Array.isArray(arr)) {
183
186
  console.error("First argument to sampleWithoutReplacement() must be an array");
184
187
  }
@@ -189,7 +192,7 @@ export function sampleWithoutReplacement(arr, size) {
189
192
  return shuffle(arr).slice(0, size);
190
193
  }
191
194
 
192
- export function sampleWithReplacement(arr, size, weights?) {
195
+ export function sampleWithReplacement<T>(arr: Array<T>, size: number, weights?: number[]) {
193
196
  if (!Array.isArray(arr)) {
194
197
  console.error("First argument to sampleWithReplacement() must be an array");
195
198
  }
@@ -301,6 +304,21 @@ export function sampleExGaussian(
301
304
  return s;
302
305
  }
303
306
 
307
+ type RandomWordsOptions = {
308
+ min?: number;
309
+ max?: number;
310
+ exactly?: number;
311
+ maxLength?: number;
312
+ wordsPerString?: number;
313
+ seperator?: string;
314
+ formatter?: (word: string, index: number) => string;
315
+ join?: string;
316
+ };
317
+
318
+ type RandomWordsResult<T extends RandomWordsOptions> = T extends { join: string }
319
+ ? string
320
+ : string[];
321
+
304
322
  /**
305
323
  * Generate one or more random words.
306
324
  *
@@ -311,8 +329,9 @@ export function sampleExGaussian(
311
329
  *
312
330
  * @returns An array of words or a single string, depending on parameter choices.
313
331
  */
314
- export function randomWords(opts) {
315
- return rw(opts);
332
+ export function randomWords<T extends RandomWordsOptions>(opts: T) {
333
+ // there is a type incompatibility here because `random-words` uses overloads rather than generics
334
+ return rw(opts) as RandomWordsResult<T>;
316
335
  }
317
336
 
318
337
  // Box-Muller transformation for a random sample from normal distribution with mean = 0, std = 1
@@ -325,8 +344,8 @@ function randn_bm() {
325
344
  return Math.sqrt(-2.0 * Math.log(u)) * Math.cos(2.0 * Math.PI * v);
326
345
  }
327
346
 
328
- function unpackArray(array) {
329
- const out = {};
347
+ function unpackArray(array: object[]) {
348
+ const out: Record<string, any> = {};
330
349
 
331
350
  for (const x of array) {
332
351
  for (const key of Object.keys(x)) {