Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/convert dblp xml as static method #24

Merged
merged 3 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ class OpenReviewClient {

if (emails) {
const fullResponse = { profiles: [], count: 0 };
const batches = this.tools.splitArray(emails, this.RESPONSE_SIZE);
const batches = Tools.splitArray(emails, this.RESPONSE_SIZE);
let data;
for (let emailBatch of batches) {
data = await this._handleResponse(fetch(this.profilesSearchUrl, {
Expand Down Expand Up @@ -380,7 +380,7 @@ class OpenReviewClient {

if (ids) {
const fullResponse = { profiles: [], count: 0 };
const batches = this.tools.splitArray(ids, this.RESPONSE_SIZE);
const batches = Tools.splitArray(ids, this.RESPONSE_SIZE);
let data;
for (let batch of batches) {
data = await this._handleResponse(fetch(this.profilesSearchUrl, {
Expand Down Expand Up @@ -1574,4 +1574,4 @@ class OpenReviewClient {

}

module.exports = { OpenReviewClient };
module.exports = { OpenReviewClient, Tools };
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openreview/client",
"version": "0.0.19",
"version": "0.0.20",
"description": "Node.js client library for OpenReview's academic publishing API",
"main": "index.js",
"scripts": {
Expand Down
18 changes: 9 additions & 9 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const assert = require('assert');
const { OpenReviewClient } = require('../index');
const { OpenReviewClient, Tools } = require('../index');

describe('OpenReview Client', function () {
this.beforeAll(async function () {
Expand Down Expand Up @@ -531,10 +531,10 @@ describe('OpenReview Client', function () {
}
};

let res = this.superClient.tools.getPreferredName(fakeProfile);
let res = Tools.getPreferredName(fakeProfile);
assert.equal(res, 'AnotherFirst AnotherMiddle AnotherLast');

res = this.superClient.tools.getPreferredName(fakeProfile, true);
res = Tools.getPreferredName(fakeProfile, true);
assert.equal(res, 'AnotherLast');
});

Expand Down Expand Up @@ -665,19 +665,19 @@ describe('OpenReview Client', function () {
});

it('should test prettyId', async function () {
let res = this.superClient.tools.prettyId('ICLR.cc/2023/Conference');
let res = Tools.prettyId('ICLR.cc/2023/Conference');
assert.equal(res, 'ICLR 2023 Conference');

res = this.superClient.tools.prettyId('ICLR.cc/2023/Conference/-/Blind_Submission');
res = Tools.prettyId('ICLR.cc/2023/Conference/-/Blind_Submission');
assert.equal(res, 'ICLR 2023 Conference Blind Submission');

res = this.superClient.tools.prettyId('ICLR.cc/2023/Conference/-/Revision');
res = Tools.prettyId('ICLR.cc/2023/Conference/-/Revision');
assert.equal(res, 'ICLR 2023 Conference Revision');

res = this.superClient.tools.prettyId('ICLR.cc/2023/Conference/-/Meta_Review', true);
res = Tools.prettyId('ICLR.cc/2023/Conference/-/Meta_Review', true);
assert.equal(res, 'Meta Review');

res = this.superClient.tools.prettyId('');
res = Tools.prettyId('');
assert.equal(res, '');
});

Expand Down Expand Up @@ -783,7 +783,7 @@ describe('OpenReview Client', function () {
];

for (let i = 0; i < dblpXmls.length; i++) {
const note = this.superClient.tools.convertDblpXmlToNote(dblpXmls[i]);
const note = Tools.convertDblpXmlToNote(dblpXmls[i]);
const resolvedNote = resolved[i];
if (resolvedNote.pdate) {
assert.equal(note.pdate, resolvedNote.pdate);
Expand Down
107 changes: 60 additions & 47 deletions tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,29 @@ class Tools {
this.subdomainsCache = {};
}

/**
* Returns the type of a variable
*
* @static
* @param {any} variable - The variable to get the type of
* @returns {string} The type of the variable
*/
static variableType(variable) {
if (variable === null) return 'null';
if (Array.isArray(variable)) {
return 'array';
}
return typeof variable;
}

/**
* Checks if domain is a TLD
*
* @private
* @param {string} domain - domain to check
* @returns {boolean} true if domain is a TLD, false otherwise
*/
_isTLD(domain) {
#isTLD(domain) {
return isValid(domain) && !getDomain(domain);
}

Expand All @@ -49,10 +64,10 @@ class Tools {
*
* @example
*
* _getSubdomains('iesl.cs.umass.edu')
* #getSubdomains('iesl.cs.umass.edu')
* returns ['iesl.cs.umass.edu', 'cs.umass.edu', 'umass.edu']
*/
_getSubdomains(domain) {
#getSubdomains(domain) {
if (this.subdomainsCache[domain]) {
return this.subdomainsCache[domain];
}
Expand All @@ -63,7 +78,7 @@ class Tools {
const domains = domainComponents.map((_, index) => domainComponents.slice(index).join('.'));
const validDomains = new Set();
for (const domain of domains) {
if (!this._isTLD(domain)) {
if (!this.#isTLD(domain)) {
validDomains.add(this.duplicateDomains[domain] || domain);
}
}
Expand All @@ -72,12 +87,12 @@ class Tools {
return subdomains;
}

_infoFunctionBuilder(policyFunction) {
#infoFunctionBuilder(policyFunction) {
return (profile, nYears) => {
const result = policyFunction(profile, nYears);
const domains = new Set();
for (const domain of result.domains) {
const subdomains = this._getSubdomains(domain);
const subdomains = this.#getSubdomains(domain);
for (const subdomain of subdomains) {
domains.add(subdomain);
}
Expand All @@ -96,12 +111,12 @@ class Tools {
/**
* Splits an array into chunks of a given size.
*
* @private
* @static
* @param {Array} arr - Array to split.
* @param {number} size - Size of the chunks.
* @returns {Array} Array of chunks.
*/
splitArray(arr, size) {
static splitArray(arr, size) {
const result = [];
for (let i = 0; i < arr.length; i += size) {
result.push(arr.slice(i, i + size));
Expand Down Expand Up @@ -134,11 +149,12 @@ class Tools {
/**
* Takes an Invitation or Group ID and returns a pretty version of it.
*
* @static
* @param {string} id - Invitation or Group ID.
* @param {boolean} onlyLast - If true, only the last part of the ID will be returned.
* @returns {string} Pretty version of the ID.
*/
prettyId(id, onlyLast) {
static prettyId(id, onlyLast) {
if (!id) {
return '';
} else if (id.indexOf('~') === 0 && id.length > 1) {
Expand Down Expand Up @@ -219,11 +235,12 @@ class Tools {
/**
* Accepts an OpenReview profile object and retrieves the user's preferred name or the first listed name.
*
* @static
* @param {object} profile - The OpenReview profile object.
* @param {boolean} [lastNameOnly=false] - A boolean indicating whether to return only the last name or the full name.
* @returns {string} - The user's preferred name or the first listed name.
*/
getPreferredName(profile, lastNameOnly=false) {
static getPreferredName(profile, lastNameOnly=false) {
const names = profile.content.names;
const preferredNames = names.filter(name => name.preferred);
const preferredName = preferredNames.length > 0 ? preferredNames[0] : names[0];
Expand Down Expand Up @@ -424,11 +441,11 @@ class Tools {

let infoFunction;
if (typeof policy === 'function') {
infoFunction = this._infoFunctionBuilder(policy);
infoFunction = this.#infoFunctionBuilder(policy);
} else if (policy === 'neurips') {
infoFunction = this._infoFunctionBuilder(this.getNeuripsProfileInfo);
infoFunction = this.#infoFunctionBuilder(this.getNeuripsProfileInfo);
} else {
infoFunction = this._infoFunctionBuilder(this.getProfileInfo);
infoFunction = this.#infoFunctionBuilder(this.getProfileInfo);
}

const authorDomains = new Set();
Expand Down Expand Up @@ -662,43 +679,39 @@ class Tools {
};
}

#variableType(variable) {
if (variable === null) return 'null';
if (Array.isArray(variable)) {
return 'array';
}
return typeof variable;
}

convertDblpXmlToNote(dblpXml) {
/**
* Converts a dblp xml to a note object.
*
* @static
* @param {string} dblpXml - The dblp xml.
* @returns {object} The note object.
*
*/
static convertDblpXmlToNote(dblpXml) {
const removeDigitsRegEx = /\s\d{4}$/;
const removeTrailingPeriod = /\.$/;

if (!this.xmlParser) {
this.xmlParser = new XMLParser({ ignoreAttributes: false });
}

if (!this.entryTypes) {
this.entryTypes = [
'article',
'book',
'booklet',
'conference',
'inbook',
'incollection',
'inproceedings',
'manual',
'mastersthesis',
'misc',
'phdthesis',
'proceedings',
'techreport',
'unpublished'
];
}
const xmlParser = new XMLParser({ ignoreAttributes: false });

const entryTypes = [
'article',
'book',
'booklet',
'conference',
'inbook',
'incollection',
'inproceedings',
'manual',
'mastersthesis',
'misc',
'phdthesis',
'proceedings',
'techreport',
'unpublished'
];

const getRawDataValue = rawData => {
const rawDataType = this.#variableType(rawData);
const rawDataType = this.variableType(rawData);
if (rawDataType === 'object') {
return rawData['#text'];
} else {
Expand All @@ -716,7 +729,7 @@ class Tools {

const entryToData = entryElement => {
const data = {};
data.type = this.entryTypes.find(type => entryElement[type]) || 'misc';
data.type = entryTypes.find(type => entryElement[type]) || 'misc';
const rawData = entryElement[data.type];
data.key = rawData['@_key'];
data.publtype = rawData['@_publtype'];
Expand Down Expand Up @@ -797,7 +810,7 @@ class Tools {

let dblpJson;
try {
dblpJson = this.xmlParser.parse(dblpXml);
dblpJson = xmlParser.parse(dblpXml);
} catch (err) {
throw new OpenReviewError({
message: 'Something went wrong parsing the dblp xml',
Expand Down