@citation-js/plugin-isbn
Version:
Citation.js plugin to fetch ISBN metadata
124 lines (123 loc) • 3.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.GOOGLE_BOOKS_API_VERSION = void 0;
exports.format = format;
exports.parse = parse;
var _core = require("@citation-js/core");
var name = _interopRequireWildcard(require("@citation-js/name"));
var date = _interopRequireWildcard(require("@citation-js/date"));
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
const GOOGLE_PROPS = [{
source: 'printType',
target: 'type',
convert: {
toTarget(type) {
return {
BOOK: 'book',
MAGAZINE: 'article-magazine'
}[type];
},
toSource(type) {
if (type.slice(0, 7) === 'article') {
return 'MAGAZINE';
} else {
return 'BOOK';
}
}
}
}, {
source: 'authors',
target: 'author',
convert: {
toTarget(authors) {
return authors.map(name.parse);
},
toSource(authors) {
return authors.map(name.format);
}
}
}, {
source: 'canonicalVolumeLink',
target: 'URL'
}, {
source: 'categories',
target: 'keyword',
convert: {
toTarget(array) {
return array.join(',');
},
toSource(list) {
return list.split(',');
}
}
}, {
source: 'description',
target: 'abstract'
}, {
source: 'dimensions',
target: 'dimensions',
convert: {
toTarget({
height,
width,
thickness
}) {
return `${height} x ${width} x ${thickness} cm`;
},
toSource(list) {}
}
}, {
source: 'industryIdentifiers',
target: ['ISBN', 'ISSN', 'DOI', 'PMID', 'PMCID'],
convert: {
toTarget(ids) {
return ['ISBN_13', 'ISSN'].map(id => (ids.find(({
type
}) => type === id) || {}).identifier);
},
toSource(isbn, issn, ...other) {
return [isbn && {
type: isbn.length === 13 ? 'ISBN_13' : 'ISBN_10',
identifier: isbn
}, issn && {
type: 'ISSN',
identifier: issn
}, ...other.map(identifier => ({
type: 'OTHER',
identifier
}))].filter(Boolean);
}
}
}, 'language', {
source: 'pageCount',
target: 'number-of-pages'
}, 'publisher', 'title', {
source: 'publishedDate',
target: 'issued',
convert: {
toTarget: date.parse,
toSource: date.format
}
}];
const translator = new _core.util.Translator(GOOGLE_PROPS);
function parse(volume) {
if (volume.volumeInfo.language === 'un') {
delete volume.volumeInfo.language;
}
if (volume.volumeInfo.pageCount === 0) {
delete volume.volumeInfo.pageCount;
}
return translator.convertToTarget(volume.volumeInfo);
}
function format(records) {
return {
kind: 'books#volumes',
totalItems: records.length,
items: records.map(translator.convertToSource)
};
}
const GOOGLE_BOOKS_API_VERSION = 'v1';
exports.GOOGLE_BOOKS_API_VERSION = GOOGLE_BOOKS_API_VERSION;