Skip to content
Open
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
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ Zotero translation architecture and others.
Item utility functions require:
- Calling `Zotero.Schema.init(json)` with the JSON from `schema.json` from Zotero schema repo
- Calling `Zotero.Date.init(json)` with the JSON from `resource/dateFormats.json`
- Loading `resource/zoteroTypeSchemaData.js` before `cachedTypes.js` or in Node.js running
- Running
```js
let CachedTypes = require('./cachedTypes')
CachedTypes.setTypeSchema(require('./resource/zoteroTypeSchemaData'))
import CachedTypes from './cachedTypes.js'
import { ZOTERO_TYPE_SCHEMA } from './resource/zoteroTypeSchemaData.js'

CachedTypes.setTypeSchema(ZOTERO_TYPE_SCHEMA)
```
- Implementing `Zotero.localeCompare()`; a simple implementation would be
```js
Expand Down
57 changes: 27 additions & 30 deletions cachedTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@
* Emulates very small parts of cachedTypes.js and itemFields.js APIs for use with connector
*/

(function() {
import Utilities from "./utilities.js";

var TypeSchema;
let TypeSchema;
if (typeof ZOTERO_TYPE_SCHEMA != 'undefined') {
TypeSchema = ZOTERO_TYPE_SCHEMA;
}

var CachedTypes = new function() {
const CachedTypes = new function() {
const schemaTypes = ["itemTypes", "creatorTypes", "fields"];
var typeData = {};
var itemTypes, creatorTypes, fields;
let typeData = {};
let itemTypes, creatorTypes, fields;

this.setTypeSchema = function(typeSchema) {
TypeSchema = typeSchema;
Expand All @@ -46,7 +46,7 @@ var CachedTypes = new function() {
// attach IDs and make referenceable by either ID or name
for (let i = 0; i < schemaTypes.length; i++) {
let schemaType = schemaTypes[i];
typeData[schemaType] = Zotero.Utilities.deepCopy(TypeSchema[schemaType]);
typeData[schemaType] = Utilities.deepCopy(TypeSchema[schemaType]);
for (let id in TypeSchema[schemaType]) {
let entry = typeData[schemaType][id];
entry.unshift(parseInt(id, 10));
Expand All @@ -73,17 +73,17 @@ var CachedTypes = new function() {
}

getID(idOrName) {
var type = this.type[idOrName];
const type = this.type[idOrName];
return (type ? type[0]/* id */ : false);
}

getName(idOrName) {
var type = this.type[idOrName];
const type = this.type[idOrName];
return (type ? type[1]/* name */ : false);
}

getLocalizedString(idOrName) {
var type = this.type[idOrName];
const type = this.type[idOrName];
return (type ? type[2]/* localizedString */ : false);
}
}
Expand All @@ -100,34 +100,34 @@ var CachedTypes = new function() {
}

getTypesForItemType(idOrName) {
var itemType = itemTypes[idOrName];
const itemType = itemTypes[idOrName];
if(!itemType) return false;

var itemCreatorTypes = itemType[3]; // creatorTypes
const itemCreatorTypes = itemType[3]; // creatorTypes
if (!itemCreatorTypes
// TEMP: 'note' and 'attachment' have an array containing false
|| (itemCreatorTypes.length == 1 && !itemCreatorTypes[0])) {
return [];
}
var n = itemCreatorTypes.length;
var outputTypes = new Array(n);
const n = itemCreatorTypes.length;
const outputTypes = new Array(n);

for(var i=0; i<n; i++) {
var creatorType = creatorTypes[itemCreatorTypes[i]];
for(let i=0; i<n; i++) {
const creatorType = creatorTypes[itemCreatorTypes[i]];
outputTypes[i] = {"id":creatorType[0]/* id */,
"name":creatorType[1]/* name */};
}
return outputTypes;
};

getPrimaryIDForType(idOrName) {
var itemType = itemTypes[idOrName];
const itemType = itemTypes[idOrName];
if(!itemType) return false;
return itemType[3]/* creatorTypes */[0];
};

isValidForItemType(creatorTypeID, itemTypeID) {
let itemType = itemTypes[itemTypeID];
const itemType = itemTypes[itemTypeID];
return itemType[3]/* creatorTypes */.includes(creatorTypeID);
};
})();
Expand All @@ -138,7 +138,7 @@ var CachedTypes = new function() {
}

isValidForType(fieldIdOrName, typeIdOrName) {
var field = fields[fieldIdOrName], itemType = itemTypes[typeIdOrName];
const field = fields[fieldIdOrName], itemType = itemTypes[typeIdOrName];

// mimics itemFields.js
if(!field || !itemType) return false;
Expand All @@ -152,16 +152,16 @@ var CachedTypes = new function() {
};

getFieldIDFromTypeAndBase(typeIdOrName, fieldIdOrName) {
var baseField = fields[fieldIdOrName], itemType = itemTypes[typeIdOrName];
let baseField = fields[fieldIdOrName], itemType = itemTypes[typeIdOrName];

if(!baseField || !itemType) return false;

// get as ID
baseField = baseField[0]/* id */;

// loop through base fields for item type
var baseFields = itemType[5];
for(var i in baseFields) {
const baseFields = itemType[5];
for(let i in baseFields) {
if(baseFields[i] === baseField) {
return i;
}
Expand All @@ -171,23 +171,20 @@ var CachedTypes = new function() {
};

getBaseIDFromTypeAndField(typeIdOrName, fieldIdOrName) {
var field = fields[fieldIdOrName], itemType = itemTypes[typeIdOrName];
const field = fields[fieldIdOrName], itemType = itemTypes[typeIdOrName];
if(!field || !itemType) {
throw new Error("Invalid field or type ID");
}

var baseField = itemType[5]/* baseFields */[field[0]/* id */];
const baseField = itemType[5]/* baseFields */[field[0]/* id */];
return baseField ? baseField : false;
};

getItemTypeFields(typeIdOrName) {
return itemTypes[typeIdOrName][4]/* fields */.slice();
};
})();
}
if (typeof module !== 'undefined') {
module.exports = CachedTypes
} else if (typeof Zotero !== 'undefined') {
Object.assign(Zotero, CachedTypes);
}
})();
}();

export { CachedTypes };
export default CachedTypes;
57 changes: 26 additions & 31 deletions date.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
***** END LICENSE BLOCK *****
*/

(function() {
import Utilities from "./utilities.js";

var Utilities_Date = new function(){
const Utilities_Date = new function() {
this.isSQLDate = isSQLDate;
this.isSQLDateTime = isSQLDateTime;
this.sqlHasYear = sqlHasYear;
Expand Down Expand Up @@ -94,7 +94,7 @@ var Utilities_Date = new function(){
let months = _monthsWithEnglish.short.map(m => m.toLowerCase())
.concat(_monthsWithEnglish.long.map(m => m.toLowerCase()));
// TODO: Switch back to native RegExp in Fx102 when Unicode property escapes are supported
_monthRe = Zotero.Utilities.XRegExp("(.*)(?:^|[^\\p{L}])(" + months.join("|") + ")[^ ]*(?: (.*)$|$)", "iu");
_monthRe = Utilities.XRegExp("(.*)(?:^|[^\\p{L}])(" + months.join("|") + ")[^ ]*(?: (.*)$|$)", "iu");
};


Expand Down Expand Up @@ -183,12 +183,12 @@ var Utilities_Date = new function(){
var seconds = date.getSeconds();
}

year = Zotero.Utilities.lpad(year, '0', 4);
month = Zotero.Utilities.lpad(month + 1, '0', 2);
day = Zotero.Utilities.lpad(day, '0', 2);
hours = Zotero.Utilities.lpad(hours, '0', 2);
minutes = Zotero.Utilities.lpad(minutes, '0', 2);
seconds = Zotero.Utilities.lpad(seconds, '0', 2);
year = Utilities.lpad(year, '0', 4);
month = Utilities.lpad(month + 1, '0', 2);
day = Utilities.lpad(day, '0', 2);
hours = Utilities.lpad(hours, '0', 2);
minutes = Utilities.lpad(minutes, '0', 2);
seconds = Utilities.lpad(seconds, '0', 2);

return year + '-' + month + '-' + day + ' '
+ hours + ':' + minutes + ':' + seconds;
Expand All @@ -215,12 +215,12 @@ var Utilities_Date = new function(){
var minutes = date.getUTCMinutes();
var seconds = date.getUTCSeconds();

year = Zotero.Utilities.lpad(year, '0', 4);
month = Zotero.Utilities.lpad(month + 1, '0', 2);
day = Zotero.Utilities.lpad(day, '0', 2);
hours = Zotero.Utilities.lpad(hours, '0', 2);
minutes = Zotero.Utilities.lpad(minutes, '0', 2);
seconds = Zotero.Utilities.lpad(seconds, '0', 2);
year = Utilities.lpad(year, '0', 4);
month = Utilities.lpad(month + 1, '0', 2);
day = Utilities.lpad(day, '0', 2);
hours = Utilities.lpad(hours, '0', 2);
minutes = Utilities.lpad(minutes, '0', 2);
seconds = Utilities.lpad(seconds, '0', 2);

return year + '-' + month + '-' + day + 'T'
+ hours + ':' + minutes + ':' + seconds + 'Z';
Expand Down Expand Up @@ -275,7 +275,7 @@ var Utilities_Date = new function(){
};

if (typeof string == 'string' || typeof string == 'number') {
string = Zotero.Utilities.trimInternal(string.toString());
string = Utilities.trimInternal(string.toString());
}

// skip empty things
Expand Down Expand Up @@ -601,11 +601,11 @@ var Utilities_Date = new function(){
var date = this.strToDate(str);

if(date.year) {
var dateString = Zotero.Utilities.lpad(date.year, "0", 4);
var dateString = Utilities.lpad(date.year, "0", 4);
if (parseInt(date.month) == date.month) {
dateString += "-"+Zotero.Utilities.lpad(date.month+1, "0", 2);
dateString += "-"+Utilities.lpad(date.month+1, "0", 2);
if(date.day) {
dateString += "-"+Zotero.Utilities.lpad(date.day, "0", 2);
dateString += "-"+Utilities.lpad(date.day, "0", 2);
}
}
return dateString;
Expand Down Expand Up @@ -654,9 +654,9 @@ var Utilities_Date = new function(){

parts.month = typeof parts.month != "undefined" ? parts.month + 1 : '';

var multi = (parts.year ? Zotero.Utilities.lpad(parts.year, '0', 4) : '0000') + '-'
+ Zotero.Utilities.lpad(parts.month, '0', 2) + '-'
+ (parts.day ? Zotero.Utilities.lpad(parts.day, '0', 2) : '00')
var multi = (parts.year ? Utilities.lpad(parts.year, '0', 4) : '0000') + '-'
+ Utilities.lpad(parts.month, '0', 2) + '-'
+ (parts.day ? Utilities.lpad(parts.day, '0', 2) : '00')
+ ' '
+ str;
return multi;
Expand Down Expand Up @@ -946,13 +946,8 @@ var Utilities_Date = new function(){
}
}
return _localeDateOrder;
}
}

if (typeof module != 'undefined') {
module.exports = Utilities_Date;
} else if (typeof Zotero != 'undefined') {
Zotero.Date = Utilities_Date;
}
};
};

})();
export { Utilities_Date };
export default Utilities_Date;
24 changes: 10 additions & 14 deletions openurl.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
***** END LICENSE BLOCK *****
*/

(function() {
import Utilities from "./utilities.js";
import Utilities_Item from "../utilities_item.js";

var OpenURL = new function() {
const OpenURL = new function() {
this.createContextObject = createContextObject;
this.parseContextObject = parseContextObject;

Expand Down Expand Up @@ -148,7 +149,7 @@ var OpenURL = new function() {

if(item.creators && item.creators.length) {
// encode first author as first and last
let firstCreator = Zotero.Utilities.Item.getFirstCreatorFromItemJSON(item);
let firstCreator = Utilities_Item.getFirstCreatorFromItemJSON(item);
if(item.itemType == "patent") {
_mapTag(firstCreator.firstName, "invfirst");
_mapTag(firstCreator.lastName, "invlast");
Expand Down Expand Up @@ -348,7 +349,7 @@ var OpenURL = new function() {
var type = "author";
}

item.creators.push(_cloneIfNecessary(Zotero.Utilities.cleanAuthor(value, type, value.indexOf(",") !== -1), item));
item.creators.push(_cloneIfNecessary(Utilities.cleanAuthor(value, type, value.indexOf(",") !== -1), item));
} else if(key == "rft.aucorp") {
complexAu.push(_cloneIfNecessary({lastName:value, isInstitution:true}, item));
} else if(key == "rft.isbn" && !item.ISBN) {
Expand Down Expand Up @@ -408,7 +409,7 @@ var OpenURL = new function() {
} else if(key == "rft.subject") {
item.tags.push(value);
} else if(key == "rft.type") {
if(Zotero.Utilities.Item.itemTypeExists(value)) item.itemType = value;
if(Utilities_Item.itemTypeExists(value)) item.itemType = value;
} else if(key == "rft.source") {
item.publicationTitle = value;
}
Expand Down Expand Up @@ -446,13 +447,8 @@ var OpenURL = new function() {
}

return item;
}
}

if (typeof module != 'undefined') {
module.exports = OpenURL;
} else if (typeof Zotero != 'undefined') {
Zotero.OpenURL = OpenURL;
}
};
};

})();
export { OpenURL };
export default OpenURL;
Loading