jspsych 7.3.0 → 7.3.2

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": "7.3.0",
3
+ "version": "7.3.2",
4
4
  "description": "Behavioral experiments in a browser",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
package/src/JsPsych.ts CHANGED
@@ -781,7 +781,14 @@ export class JsPsych {
781
781
  for (const param in trial.type.info.parameters) {
782
782
  // check if parameter is complex with nested defaults
783
783
  if (trial.type.info.parameters[param].type === ParameterType.COMPLEX) {
784
- if (trial.type.info.parameters[param].array === true) {
784
+ // check if parameter is undefined and has a default value
785
+ if (typeof trial[param] === "undefined" && trial.type.info.parameters[param].default) {
786
+ trial[param] = trial.type.info.parameters[param].default;
787
+ }
788
+ // if parameter is an array, iterate over each entry after confirming that there are
789
+ // entries to iterate over. this is common when some parameters in a COMPLEX type have
790
+ // default values and others do not.
791
+ if (trial.type.info.parameters[param].array === true && Array.isArray(trial[param])) {
785
792
  // iterate over each entry in the array
786
793
  trial[param].forEach(function (ip, i) {
787
794
  // check each parameter in the plugin description
@@ -789,13 +796,7 @@ export class JsPsych {
789
796
  if (typeof trial[param][i][p] === "undefined" || trial[param][i][p] === null) {
790
797
  if (typeof trial.type.info.parameters[param].nested[p].default === "undefined") {
791
798
  console.error(
792
- "You must specify a value for the " +
793
- p +
794
- " parameter (nested in the " +
795
- param +
796
- " parameter) in the " +
797
- trial.type +
798
- " plugin."
799
+ `You must specify a value for the ${p} parameter (nested in the ${param} parameter) in the ${trial.type.info.name} plugin.`
799
800
  );
800
801
  } else {
801
802
  trial[param][i][p] = trial.type.info.parameters[param].nested[p].default;
@@ -809,11 +810,7 @@ export class JsPsych {
809
810
  else if (typeof trial[param] === "undefined" || trial[param] === null) {
810
811
  if (typeof trial.type.info.parameters[param].default === "undefined") {
811
812
  console.error(
812
- "You must specify a value for the " +
813
- param +
814
- " parameter in the " +
815
- trial.type.info.name +
816
- " plugin."
813
+ `You must specify a value for the ${param} parameter in the ${trial.type.info.name} plugin.`
817
814
  );
818
815
  } else {
819
816
  trial[param] = trial.type.info.parameters[param].default;
@@ -102,15 +102,15 @@ export class MediaAPI {
102
102
  }
103
103
  );
104
104
  };
105
- request.onerror = function (e) {
105
+ request.onerror = (e) => {
106
106
  let err: ProgressEvent | string = e;
107
- if (this.status == 404) {
107
+ if (request.status == 404) {
108
108
  err = "404";
109
109
  }
110
110
  callback_error({ source: source, error: err });
111
111
  };
112
- request.onloadend = function (e) {
113
- if (this.status == 404) {
112
+ request.onloadend = (e) => {
113
+ if (request.status == 404) {
114
114
  callback_error({ source: source, error: "404" });
115
115
  }
116
116
  };
@@ -176,24 +176,24 @@ export class MediaAPI {
176
176
  return;
177
177
  }
178
178
 
179
- for (var i = 0; i < images.length; i++) {
180
- var img = new Image();
181
-
182
- img.onload = function () {
179
+ for (let i = 0; i < images.length; i++) {
180
+ const img = new Image();
181
+ const src = images[i];
182
+ img.onload = () => {
183
183
  n_loaded++;
184
- callback_load(img.src);
184
+ callback_load(src);
185
185
  if (n_loaded === images.length) {
186
186
  callback_complete();
187
187
  }
188
188
  };
189
189
 
190
- img.onerror = function (e) {
191
- callback_error({ source: img.src, error: e });
190
+ img.onerror = (e) => {
191
+ callback_error({ source: src, error: e });
192
192
  };
193
193
 
194
- img.src = images[i];
194
+ img.src = src;
195
195
 
196
- this.img_cache[images[i]] = img;
196
+ this.img_cache[src] = img;
197
197
  this.preload_requests.push(img);
198
198
  }
199
199
  }
@@ -221,9 +221,9 @@ export class MediaAPI {
221
221
  const request = new XMLHttpRequest();
222
222
  request.open("GET", video, true);
223
223
  request.responseType = "blob";
224
- request.onload = function () {
225
- if (this.status === 200 || this.status === 0) {
226
- const videoBlob = this.response;
224
+ request.onload = () => {
225
+ if (request.status === 200 || request.status === 0) {
226
+ const videoBlob = request.response;
227
227
  video_buffers[video] = URL.createObjectURL(videoBlob); // IE10+
228
228
  n_loaded++;
229
229
  callback_load(video);
@@ -232,15 +232,15 @@ export class MediaAPI {
232
232
  }
233
233
  }
234
234
  };
235
- request.onerror = function (e) {
235
+ request.onerror = (e) => {
236
236
  let err: ProgressEvent | string = e;
237
- if (this.status == 404) {
237
+ if (request.status == 404) {
238
238
  err = "404";
239
239
  }
240
240
  callback_error({ source: video, error: err });
241
241
  };
242
- request.onloadend = function (e) {
243
- if (this.status == 404) {
242
+ request.onloadend = (e) => {
243
+ if (request.status == 404) {
244
244
  callback_error({ source: video, error: "404" });
245
245
  }
246
246
  };
@@ -120,6 +120,7 @@ export function shuffleNoRepeats(arr: Array<any>, equalityTest: (a: any, b: any)
120
120
  }
121
121
 
122
122
  const random_shuffle = shuffle(arr);
123
+
123
124
  for (let i = 0; i < random_shuffle.length - 1; i++) {
124
125
  if (equalityTest(random_shuffle[i], random_shuffle[i + 1])) {
125
126
  // neighbors are equal, pick a new random neighbor to swap (not the first or last element, to avoid edge cases)
@@ -128,7 +129,8 @@ export function shuffleNoRepeats(arr: Array<any>, equalityTest: (a: any, b: any)
128
129
  while (
129
130
  equalityTest(random_shuffle[i + 1], random_shuffle[random_pick]) ||
130
131
  equalityTest(random_shuffle[i + 1], random_shuffle[random_pick + 1]) ||
131
- equalityTest(random_shuffle[i + 1], random_shuffle[random_pick - 1])
132
+ equalityTest(random_shuffle[i + 1], random_shuffle[random_pick - 1]) ||
133
+ equalityTest(random_shuffle[i], random_shuffle[random_pick])
132
134
  ) {
133
135
  random_pick = Math.floor(Math.random() * (random_shuffle.length - 2)) + 1;
134
136
  }