site/node_modules/@citation-js/plugin-bibtex/lib/input/entries.js

77 lines
3.6 KiB
JavaScript
Raw Permalink Normal View History

2024-10-14 06:09:33 +00:00
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.parse = parse;
exports.parseBibtex = parseBibtex;
var _config = _interopRequireDefault(require("../config.js"));
var _index = require("../mapping/index.js");
var _value = require("./value.js");
var _constants = require("./constants.js");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function validate(entries, requirements) {
const problems = [];
for (const {
type,
label,
properties
} of entries) {
if (type in requirements) {
const missing = [];
for (const field of requirements[type]) {
if (Array.isArray(field) && !field.some(field => field in properties)) {
missing.push(field.join('/'));
} else if (typeof field === 'string' && !(field in properties)) {
missing.push(field);
}
}
if (missing.length) {
problems.push([label, `missing fields: ${missing.join(', ')}`]);
}
} else {
problems.push([label, `invalid type: "${type}"`]);
}
}
if (problems.length) {
throw new RangeError(['Invalid entries:'].concat(problems.map(([label, problem]) => ` - ${label} has ${problem}`)).join('\n'));
}
}
function parseEntryValues(entry) {
const output = {};
if ('language' in entry.properties) {
output.language = (0, _value.parse)(entry.properties.language, 'language');
}
for (const property in entry.properties) {
const value = entry.properties[property];
if (value === '') {
continue;
}
output[property] = (0, _value.parse)(value + '', property, output.language);
}
for (const property in entry.annotations) {
for (const annotation in entry.annotations[property]) {
output[property + '+an:' + annotation] = (0, _value.parseAnnotation)(entry.annotations[property][annotation]);
}
}
return _objectSpread(_objectSpread({}, entry), {}, {
properties: output
});
}
function parse(entries) {
if (_config.default.parse.strict) {
validate(entries, _constants.required.biblatex);
}
return (0, _index.parse)(entries.map(parseEntryValues));
}
function parseBibtex(entries) {
if (_config.default.parse.strict) {
validate(entries, _constants.required.bibtex);
}
return (0, _index.parseBibtex)(entries.map(parseEntryValues));
}