Skip to content

Commit

Permalink
Implemented default descriptions for extension type and version; unkn…
Browse files Browse the repository at this point in the history
…owns are now removed from variables with multiple descriptions; JsPsychExtensionInfo interface updated to allow version and data.
  • Loading branch information
Bankminer78 committed Jul 8, 2024
1 parent 631150e commit 3389ffc
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 6 deletions.
2 changes: 2 additions & 0 deletions packages/jspsych/src/modules/extensions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export interface JsPsychExtensionInfo {
name: string;
version: string;
data: Record<string, any>;
}

export interface JsPsychExtension {
Expand Down
49 changes: 48 additions & 1 deletion packages/metadata/src/VariablesMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ export class VariablesMap {
*/
private variables: { [key: string]: VariableFields };

/**
* Flag that determines if the extension variables' default descriptions have been generated in the metadata.
**/
extensionDefaultFlag: boolean = false;

/**
* Creates the VariablesMap bycalling generateDefaultVariables() method to
* generate the basic metadata common to every dataset_description.json file.
Expand All @@ -48,6 +53,37 @@ export class VariablesMap {
this.generateDefaultVariables();
}

generateDefaultExtensionVariables(): void {
if (this.extensionDefaultFlag) {
return;
}

const extension_type_var: VariableFields = {
"@type": "PropertyValue",
name: "extension_type",
description: {
default: "unknown",
jsPsych: "The name(s) of the extension(s) used in the trial.",
},
value: "string",
};
this.setVariable(extension_type_var);

const extension_version_var: VariableFields = {
"@type": "PropertyValue",
name: "extension_version",
description: {
default: "unknown",
jsPsych: "The version(s) of the extension(s) used in the trial.",
},
value: "numeric",
};

this.setVariable(extension_version_var);

this.extensionDefaultFlag = true;
}

/**
* Generates the default variables shared between every JsPsych experiment and fills in
* with default descriptions according to JsPsych documentation.
Expand Down Expand Up @@ -111,7 +147,12 @@ export class VariablesMap {
variable["description"] = description[key];
} else if (numKeys == 2) {
delete description["default"]; // deletes default

// Delete the property if its value is "unknown"
for (const descKey in description as object) {
if (description[descKey] === "unknown") {
delete description[descKey];
}
}
if (Object.keys(description).length == 1) {
// error checking that it reduced to one key
const key = Object.keys(description)[0];
Expand All @@ -120,6 +161,12 @@ export class VariablesMap {
} else if (numKeys > 2) {
// deletes default
delete description["default"];
// Delete the property if its value is "unknown"
for (const descKey in description as object) {
if (description[descKey] === "unknown") {
delete description[descKey];
}
}
}

var_list.push(variable);
Expand Down
29 changes: 24 additions & 5 deletions packages/metadata/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ export default class JsPsychMetadata {
return res;
}

/**
* Generates the default descriptions for extension_type and extension_version in metadata. Should be called after
* default metadata is generated, and only should be called once.
**/
generateDefaultExtensionVariables(): void {
this.variables.generateDefaultExtensionVariables();
}

/**
* Method that creates an author. This method can also be used to overwrite existing authors
* with the same name in order to update fields.
Expand Down Expand Up @@ -302,7 +310,16 @@ export default class JsPsychMetadata {
const pluginType = observation["trial_type"];
const extensionType = observation["extension_type"]; // fix for non-list (single item extension)
const extensionVersion = observation["extension_version"];
const ignored_fields = new Set(["trial_type", "trial_index", "time_elapsed"]);

if (extensionType) this.generateDefaultExtensionVariables(); // After first call, generation is stopped.

const ignored_fields = new Set([
"trial_type",
"trial_index",
"time_elapsed",
"extenstion_type",
"extension_version",
]);

for (const variable in observation) {
const value = observation[variable];
Expand All @@ -312,11 +329,13 @@ export default class JsPsychMetadata {
if (ignored_fields.has(variable)) this.updateFields(variable, value, typeof value);
else {
await this.generateMetadata(variable, value, pluginType, version);
extensionType
? extensionType.forEach((ext, index) =>
if (extensionType) {
await Promise.all(
extensionType.map((ext, index) =>
this.generateMetadata(variable, value, ext, extensionVersion[index])
) // verify
: console.log("No extensionType");
)
);
}
}
}
}
Expand Down

0 comments on commit 3389ffc

Please sign in to comment.