fix citation logic

This commit is contained in:
Cherrie Chang 2024-12-18 08:02:49 -05:00
parent 5e878d381f
commit f2d1a03401
4 changed files with 67 additions and 51 deletions

1
.gitignore vendored
View File

@ -11,3 +11,4 @@ coverage/
dist.zip dist.zip
packages/jspsych/README.md packages/jspsych/README.md
.turbo .turbo
.env

View File

@ -21,19 +21,22 @@ export default function generateCitations() {
// Try to find CITATION.cff file and look for preferred-citation // Try to find CITATION.cff file and look for preferred-citation
const citationCff = (() => { const citationCff = (() => {
let rawCff; let rawCff;
try { const getCff = (path) => {
// look for CITATION.cff in the current directory rawCff = fs.readFileSync(path, "utf-8").toString();
rawCff = fs.readFileSync("./CITATION.cff", "utf-8").toString();
const cffData = yaml.parse(rawCff); const cffData = yaml.parse(rawCff);
if (cffData["preferred-citation"]) { if (cffData["preferred-citation"]) {
preferredCitation = true; preferredCitation = true;
} }
return yaml.stringify(rawCff); return yaml.stringify(rawCff);
};
try {
// look for CITATION.cff in the current directory
return getCff("./CITATION.cff");
} catch (error) { } catch (error) {
try { try {
// look for CITATION.cff in the root of the repository // look for CITATION.cff in the root of the repository
rawCff = fs.readFileSync(path.join(appRootPath.path, "CITATION.cff"), "utf-8").toString(); return getCff(path.join(appRootPath.path, "CITATION.cff"));
return yaml.stringify(rawCff);
} catch (error) { } catch (error) {
console.warn( console.warn(
`No CITATION.cff file found: ${error.message}. If you would like to include a citation, please create a CITATION.cff file in the root of your repository.` `No CITATION.cff file found: ${error.message}. If you would like to include a citation, please create a CITATION.cff file in the root of your repository.`

View File

@ -262,33 +262,35 @@ export class JsPsych {
} }
getCitations( getCitations(
plugins: Array<Class<JsPsychPlugin<any>> | Class<JsPsychExtension>>, plugins: Array<Class<JsPsychPlugin<any>> | Class<JsPsychExtension>> = [],
format?: string format: "apa" | "bibtex" = "apa"
) { ) {
format = format ? format.toLowerCase() : "apa"; const formatOptions = ["apa", "bibtex"];
const jsPsychCitations: any = "__CITATIONS__";
format = format.toLowerCase() as "apa" | "bibtex";
// Check if plugins is an array // Check if plugins is an array
if (!Array.isArray(plugins)) { if (!Array.isArray(plugins)) {
throw new Error("Expected array of plugins/extensions"); throw new Error("Expected array of plugins/extensions");
} }
// Check if array is empty
else if (plugins.length == 0) {
console.log(
format == "apa"
? "de Leeuw, J. R., Gilbert, R. A., & Luchterhandt, B. (2023). jsPsych: Enabling an Open-Source Collaborative Ecosystem of Behavioral Experiments. Journal of Open Source Software, 8(85), 5351. https://doi.org/10.21105/joss.05351 "
: '@article{Leeuw2023jsPsych, author = {de Leeuw, Joshua R. and Gilbert, Rebecca A. and Luchterhandt, Bj{\\" o}rn}, journal = {Journal of Open Source Software}, doi = {10.21105/joss.05351}, issn = {2475-9066}, number = {85}, year = {2023}, month = {may 11}, pages = {5351}, publisher = {Open Journals}, title = {jsPsych: Enabling an {Open}-{Source} {Collaborative} {Ecosystem} of {Behavioral} {Experiments}}, url = {https://joss.theoj.org/papers/10.21105/joss.05351}, volume = {8}, } '
);
return;
}
// Check if format is supported // Check if format is supported
else if (!Object.keys(plugins[0]["info"].citations).includes(format)) { else if (!formatOptions.includes(format)) {
throw new Error("Unsupported citation format"); throw new Error("Unsupported citation format");
} else { }
const pluginsSet = new Set(plugins); // Print citations
plugins = Array.from(pluginsSet); else {
plugins.map((plugin) => { const jsPsychCitation = jsPsychCitations[format];
let pluginCitations = plugin["info"].citations; const citationSet = new Set([jsPsychCitation]);
console.log(format == "apa" ? `${pluginCitations.apa}` : `${pluginCitations.bibtex}`);
}); for (const plugin of plugins) {
try {
const pluginCitation = plugin["info"].citations[format];
citationSet.add(pluginCitation);
} catch {
console.error(`${plugin} does not have citation in ${format} format.`);
}
}
const citationList = Array.from(citationSet).join("\n");
return citationList;
} }
} }

View File

@ -1,50 +1,55 @@
import { JsPsych } from "../../src/JsPsych"; import { initJsPsych } from "../../dist/index.js";
import { TestExtension } from "../extensions/TestExtension"; import { TestExtension } from "../extensions/TestExtension";
import TestPlugin from "../TestPlugin"; import TestPlugin from "../TestPlugin";
const jsPsychApaCitation =
"de Leeuw, J. R., Gilbert, R. A., & Luchterhandt, B. (2023). jsPsych: Enabling an Open-Source Collaborative Ecosystem of Behavioral Experiments. Journal of Open Source Software, 8(85), 5351. https://doi.org/10.21105/joss.05351";
const jsPsychBibtexCitation =
'@misc{LeeuwjsPsych, author = {de Leeuw, Joshua R. and Gilbert, Rebecca A. and Luchterhandt, Bj{\\" o}rn}, doi = {10.5281/zenodo.7702307}, title = {jsPsych: Enabling an {Open}-{Source} {Collaborative} {Ecosystem} of {Behavioral} {Experiments}}, } @article{Leeuw2023jsPsych, author = {de Leeuw, Joshua R. and Gilbert, Rebecca A. and Luchterhandt, Bj{\\" o}rn}, journal = {Journal of Open Source Software}, doi = {10.21105/joss.05351}, issn = {2475-9066}, number = {85}, year = {2023}, month = {may 11}, pages = {5351}, publisher = {Open Journals}, title = {jsPsych: Enabling an {Open}-{Source} {Collaborative} {Ecosystem} of {Behavioral} {Experiments}}, url = {https://joss.theoj.org/papers/10.21105/joss.05351}, volume = {8}, } ';
const testPluginApaCitation = "Test plugin APA citation"; const testPluginApaCitation = "Test plugin APA citation";
const testPluginBibtexCitation = "Test plugin BibTeX citation"; const testPluginBibtexCitation = "Test plugin BibTeX citation";
const testExtensionApaCitation = "Test extension APA citation"; const testExtensionApaCitation = "Test extension APA citation";
let jspsych: JsPsych; let jsPsych;
let consoleLogSpy: jest.SpyInstance;
let consoleWarnSpy: jest.SpyInstance;
beforeEach(() => { beforeEach(() => {
jspsych = new JsPsych(); jsPsych = initJsPsych();
consoleLogSpy = jest.spyOn(console, "log").mockImplementation();
consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation();
});
afterEach(() => {
jest.restoreAllMocks();
}); });
describe("citing not using an array", () => { describe("citing not using an array", () => {
test("citing nothing", () => { test("citing nothing", () => {
expect(() => jspsych.getCitations(null)).toThrow("Expected array of plugins/extensions"); const citation = jsPsych.getCitations();
console.log(citation);
expect(citation).toMatch(jsPsychApaCitation);
});
test("citing without input and with valid format", () => {
jsPsych.getCitations(null, "apa");
expect(consoleLogSpy.mock.calls).toHaveLength(1);
expect(consoleLogSpy.mock.calls[0][0]).toBe(jsPsychApaCitation);
}); });
test("citing without input and with invalid format", () => { test("citing without input and with invalid format", () => {
expect(() => jspsych.getCitations(null, "apa")).toThrow("Expected array of plugins/extensions"); expect(() => jspsych.getCitations(null, "dummyTex")).toThrow("Unsupported citation format");
}); });
}); });
describe("citing using an array in different formats", () => { describe("citing using an array in different formats", () => {
test("citing empty array with APA format", () => { test("citing empty array with APA format", () => {
jspsych.getCitations([], "apa"); jspsych.getCitations([], "apa");
expect(consoleWarnSpy.mock.calls[0][0]).toBe("No plugins/extensions provided"); expect(consoleLogSpy.mock.calls).toHaveLength(1);
expect(consoleLogSpy.mock.calls[0][0]).toBe(jsPsychApaCitation);
}); });
test("citing empty array with BibTeX format", () => { test("citing empty array with BibTeX format", () => {
jspsych.getCitations([], "bibtex"); jspsych.getCitations([], "bibtex");
expect(consoleWarnSpy.mock.calls[0][0]).toBe("No plugins/extensions provided"); expect(consoleLogSpy.mock.calls).toHaveLength(1);
expect(consoleLogSpy.mock.calls[0][0]).toBe(jsPsychBibtexCitation);
}); });
test("citing empty array without format", () => { test("citing empty array without format", () => {
jspsych.getCitations([]); jspsych.getCitations([]);
expect(consoleWarnSpy.mock.calls[0][0]).toBe("No plugins/extensions provided"); expect(consoleLogSpy.mock.calls).toHaveLength(1);
expect(consoleLogSpy.mock.calls[0][0]).toBe(jsPsychApaCitation);
}); });
test("citing one plugin with valid format in all caps", () => { test("citing one plugin with valid format in all caps", () => {
jspsych.getCitations([TestPlugin], "APA"); const citation = jsPsych.getCitations([TestPlugin], "APA");
expect(consoleLogSpy.mock.calls[0][0]).toBe(testPluginApaCitation); expect(citation).toBe(jsPsychApaCitation + "\n" + testPluginApaCitation + "\n");
}); });
test("citing with unsupported format", () => { test("citing with unsupported format", () => {
expect(() => jspsych.getCitations([TestPlugin], "DummyTex")).toThrow( expect(() => jspsych.getCitations([TestPlugin], "DummyTex")).toThrow(
@ -56,22 +61,27 @@ describe("citing using an array in different formats", () => {
describe("citing mix of valid plugins/extensions", () => { describe("citing mix of valid plugins/extensions", () => {
test("citing a plugin", () => { test("citing a plugin", () => {
jspsych.getCitations([TestPlugin]); jspsych.getCitations([TestPlugin]);
expect(consoleLogSpy.mock.calls[0][0]).toBe(testPluginApaCitation); expect(consoleLogSpy.mock.calls).toHaveLength(2);
// expect(consoleLogSpy.mock.calls[0][0]).toBe(jsPsychApaCitation);
expect(consoleLogSpy.mock.calls[1][0]).toBe(testPluginApaCitation);
}); });
test("citing a plugin in BibTeX", () => { test("citing a plugin in BibTeX", () => {
jspsych.getCitations([TestPlugin], "bibtex"); jspsych.getCitations([TestPlugin], "bibtex");
expect(consoleLogSpy.mock.calls[0][0]).toBe(testPluginBibtexCitation); expect(consoleLogSpy.mock.calls).toHaveLength(2);
expect(consoleLogSpy.mock.calls[0][0]).toBe(jsPsychBibtexCitation);
expect(consoleLogSpy.mock.calls[1][0]).toBe(testPluginBibtexCitation);
}); });
test("citing multiple plugins", () => { test("citing multiple of the same plugins", () => {
jspsych.getCitations([TestPlugin, TestPlugin]); jspsych.getCitations([TestPlugin, TestPlugin]);
expect(consoleLogSpy.mock.calls).toHaveLength(2); expect(consoleLogSpy.mock.calls).toHaveLength(2);
expect(consoleLogSpy.mock.calls[0][0]).toBe(testPluginApaCitation); expect(consoleLogSpy.mock.calls[0][0]).toBe(jsPsychApaCitation);
expect(consoleLogSpy.mock.calls[1][0]).toBe(testPluginApaCitation); expect(consoleLogSpy.mock.calls[1][0]).toBe(testPluginApaCitation);
}); });
test("citing mix of plugins and extensions", () => { test("citing mix of plugins and extensions", () => {
jspsych.getCitations([TestPlugin, TestExtension]); jspsych.getCitations([TestPlugin, TestExtension]);
expect(consoleLogSpy.mock.calls).toHaveLength(2); expect(consoleLogSpy.mock.calls).toHaveLength(3);
expect(consoleLogSpy.mock.calls[0][0]).toBe(testPluginApaCitation); expect(consoleLogSpy.mock.calls[0][0]).toBe(jsPsychApaCitation);
expect(consoleLogSpy.mock.calls[1][0]).toBe(testExtensionApaCitation); expect(consoleLogSpy.mock.calls[1][0]).toBe(testPluginApaCitation);
expect(consoleLogSpy.mock.calls[2][0]).toBe(testExtensionApaCitation);
}); });
}); });