UNPKG

rehype-citation

Version:

rehype plugin to add citation and bibliography from bibtex files

4,121 lines (4,096 loc) 913 kB
import { parse as parse$8, format as format$8 } from '@citation-js/name'; import require$$0 from 'child_process'; import path from 'path'; import Url from 'url'; import Stream from 'stream'; import http from 'http'; import punycode from 'punycode'; import https from 'https'; import zlib from 'zlib'; import { parse as parse$9, format as format$9 } from '@citation-js/date'; import CSL from 'citeproc'; import yaml from 'js-yaml'; function currentVersion() { return this.log.length; } function retrieveVersion(versnum = 1) { if (versnum <= 0 || versnum > this.currentVersion()) { return null; } else { const [data, options] = this.log[versnum - 1]; const image = new this.constructor(JSON.parse(data), JSON.parse(options)); image.log = this.log.slice(0, versnum); return image; } } function undo(number = 1) { return this.retrieveVersion(this.currentVersion() - number); } function retrieveLastVersion() { return this.retrieveVersion(this.currentVersion()); } function save() { this.log.push([JSON.stringify(this.data), JSON.stringify(this._options)]); return this; } var log = { __proto__: null, currentVersion: currentVersion, retrieveVersion: retrieveVersion, retrieveLastVersion: retrieveLastVersion, undo: undo, save: save }; const formats$4 = ['real', 'string']; const types$1 = ['json', 'html', 'string', 'rtf']; const styles = ['csl', 'bibtex', 'bibtxt', 'citation-*', 'ris', 'ndjson']; const wrapperTypes = ['string', 'function']; function validateOutputOptions(options) { if (typeof options !== 'object') { throw new TypeError('Options not an object!'); } const { format, type, style, lang, append, prepend } = options; if (format && !formats$4.includes(format)) { throw new TypeError(`Option format ("${format}") should be one of: ${formats$4}`); } else if (type && !types$1.includes(type)) { throw new TypeError(`Option type ("${type}") should be one of: ${types$1}`); } else if (style && !styles.includes(style) && !/^citation/.test(style)) { throw new TypeError(`Option style ("${style}") should be one of: ${styles}`); } else if (lang && typeof lang !== 'string') { throw new TypeError(`Option lang should be a string, but is a ${typeof lang}`); } else if (prepend && !wrapperTypes.includes(typeof prepend)) { throw new TypeError(`Option prepend should be a string or a function, but is a ${typeof prepend}`); } else if (append && !wrapperTypes.includes(typeof append)) { throw new TypeError(`Option append should be a string or a function, but is a ${typeof append}`); } if (/^citation/.test(style) && type === 'json') { throw new Error(`Combination type/style of json/citation-* is not valid: ${type}/${style}`); } return true; } function validateOptions(options) { if (typeof options !== 'object') { throw new TypeError('Options should be an object'); } if (options.output) { validateOutputOptions(options.output); } else if (options.maxChainLength && typeof options.maxChainLength !== 'number') { throw new TypeError('Option maxChainLength should be a number'); } else if (options.forceType && typeof options.forceType !== 'string') { throw new TypeError('Option forceType should be a string'); } else if (options.generateGraph != null && typeof options.generateGraph !== 'boolean') { throw new TypeError('Option generateGraph should be a boolean'); } else if (options.strict != null && typeof options.strict !== 'boolean') { throw new TypeError('Option strict should be a boolean'); } else if (options.target != null && typeof options.target !== 'string') { throw new TypeError('Option target should be a boolean'); } return true; } const defaultOptions = { format: 'real', type: 'json', style: 'csl', lang: 'en-US' }; function options(options, log) { validateOutputOptions(options); if (log) { this.save(); } Object.assign(this._options, options); return this; } var options$1 = { __proto__: null, options: options, defaultOptions: defaultOptions }; function typeOf(thing) { switch (thing) { case undefined: return 'Undefined'; case null: return 'Null'; default: return thing.constructor.name; } } function dataTypeOf(thing) { switch (typeof thing) { case 'string': return 'String'; case 'object': if (Array.isArray(thing)) { return 'Array'; } else if (typeOf(thing) === 'Object') { return 'SimpleObject'; } else if (typeOf(thing) !== 'Null') { return 'ComplexObject'; } default: return 'Primitive'; } } var dataType = { __proto__: null, typeOf: typeOf, dataTypeOf: dataTypeOf }; function applyGraph(entry, graph) { if (entry._graph) { const index = graph.findIndex(({ type }) => type === '@else/list+object'); if (index !== -1) { graph.splice(index + 1, 0, ...entry._graph.slice(0, -1)); } } entry._graph = graph; return entry; } function removeGraph(entry) { delete entry._graph; return entry; } var graph = { __proto__: null, applyGraph: applyGraph, removeGraph: removeGraph }; const logger = { _output(level, scope, msg) { this._log.push(scope, msg); if (this._levels.indexOf(level) < this._levels.indexOf(this.level)) { return; } this._console.log(scope, ...msg); }, _console: null, _log: [], _levels: ['http', 'debug', 'unmapped', 'info', 'warn', 'error', 'silent'], level: 'silent' }; for (const level of logger._levels) { logger[level] = (scope, ...msg) => logger._output(level, scope, msg); } if (typeof console.Console === 'function') { logger._console = new console.Console(process.stderr); } else { logger._console = console; } const types = {}; const dataTypes = {}; const unregExts = {}; function parseNativeTypes(input, dataType) { switch (dataType) { case 'Array': if (input.length === 0 || input.every(entry => type(entry) === '@csl/object')) { return '@csl/list+object'; } else { return '@else/list+object'; } case 'SimpleObject': case 'ComplexObject': return '@csl/object'; default: return '@invalid'; } } function matchType(typeList = [], data) { for (const type of typeList) { if (types[type].predicate(data)) { return matchType(types[type].extensions, data) || type; } } } function type(input) { const dataType = dataTypeOf(input); if (dataType === 'Array' && input.length === 0) { return parseNativeTypes(input, dataType); } const match = matchType(dataTypes[dataType], input); return match || parseNativeTypes(input, dataType); } function addTypeParser(format, { dataType, predicate, extends: extend }) { let extensions = []; if (format in unregExts) { extensions = unregExts[format]; delete unregExts[format]; logger.debug('[core]', `Subclasses "${extensions}" finally registered to parent type "${format}"`); } const object = { predicate, extensions }; types[format] = object; if (extend) { const parentTypeParser = types[extend]; if (parentTypeParser) { parentTypeParser.extensions.push(format); } else { if (!unregExts[extend]) { unregExts[extend] = []; } unregExts[extend].push(format); logger.debug('[core]', `Subclass "${format}" is waiting on parent type "${extend}"`); } } else { const typeList = dataTypes[dataType] || (dataTypes[dataType] = []); typeList.push(format); } } function hasTypeParser(type) { return Object.prototype.hasOwnProperty.call(types, type); } function removeTypeParser(type) { delete types[type]; const typeLists = [...Object.keys(dataTypes).map(key => dataTypes[key]), ...Object.keys(types).map(type => types[type].extensions).filter(list => list.length > 0)]; typeLists.forEach(typeList => { const index = typeList.indexOf(type); if (index > -1) { typeList.splice(index, 1); } }); } function listTypeParser() { return Object.keys(types); } function treeTypeParser() { const attachNode = name => ({ name, children: types[name].extensions.map(attachNode) }); return { name: 'Type tree', children: Object.keys(dataTypes).map(name => ({ name, children: dataTypes[name].map(attachNode) })) }; } const typeMatcher = /^(?:@(.+?))(?:\/(?:(.+?)\+)?(?:(.+)))?$/; // @ts-nocheck function _defineProperty$7(obj, key, value) { key = _toPropertyKey$7(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$7(t) { var i = _toPrimitive$7(t, 'string'); return 'symbol' == typeof i ? i : i + ''; } function _toPrimitive$7(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); } class TypeParser { constructor(data) { _defineProperty$7(this, 'validDataTypes', ['String', 'Array', 'SimpleObject', 'ComplexObject', 'Primitive']); this.data = data; } validateDataType() { const dataType = this.data.dataType; if (dataType && !this.validDataTypes.includes(dataType)) { throw new RangeError(`dataType was ${dataType}; expected one of ${this.validDataTypes}`); } } validateParseType() { const predicate = this.data.predicate; if (predicate && !(predicate instanceof RegExp || typeof predicate === 'function')) { throw new TypeError(`predicate was ${typeof predicate}; expected RegExp or function`); } } validateTokenList() { const tokenList = this.data.tokenList; if (tokenList && typeof tokenList !== 'object') { throw new TypeError(`tokenList was ${typeof tokenList}; expected object or RegExp`); } } validatePropertyConstraint() { const propertyConstraint = this.data.propertyConstraint; if (propertyConstraint && typeof propertyConstraint !== 'object') { throw new TypeError(`propertyConstraint was ${typeof propertyConstraint}; expected array or object`); } } validateElementConstraint() { const elementConstraint = this.data.elementConstraint; if (elementConstraint && typeof elementConstraint !== 'string') { throw new TypeError(`elementConstraint was ${typeof elementConstraint}; expected string`); } } validateExtends() { const extend = this.data.extends; if (extend && typeof extend !== 'string') { throw new TypeError(`extends was ${typeof extend}; expected string`); } } validate() { if (this.data === null || typeof this.data !== 'object') { throw new TypeError(`typeParser was ${typeof this.data}; expected object`); } this.validateDataType(); this.validateParseType(); this.validateTokenList(); this.validatePropertyConstraint(); this.validateElementConstraint(); this.validateExtends(); } parseTokenList() { let tokenList = this.data.tokenList; if (!tokenList) { return []; } else if (tokenList instanceof RegExp) { tokenList = { token: tokenList }; } const { token, split = /\s+/, trim = true, every = true } = tokenList; const trimInput = input => trim ? input.trim() : input; const testTokens = every ? 'every' : 'some'; const predicate = input => trimInput(input).split(split)[testTokens](part => token.test(part)); return [predicate]; } parsePropertyConstraint() { const constraints = [].concat(this.data.propertyConstraint || []); return constraints.map(({ props, match, value }) => { props = [].concat(props); switch (match) { case 'any': case 'some': return input => props.some(prop => prop in input && (!value || value(input[prop]))); case 'none': return input => !props.some(prop => prop in input && (!value || value(input[prop]))); case 'every': default: return input => props.every(prop => prop in input && (!value || value(input[prop]))); } }); } parseElementConstraint() { const constraint = this.data.elementConstraint; return !constraint ? [] : [input => input.every(entry => type(entry) === constraint)]; } parsePredicate() { if (this.data.predicate instanceof RegExp) { return [this.data.predicate.test.bind(this.data.predicate)]; } else if (this.data.predicate) { return [this.data.predicate]; } else { return []; } } getCombinedPredicate() { const predicates = [...this.parsePredicate(), ...this.parseTokenList(), ...this.parsePropertyConstraint(), ...this.parseElementConstraint()]; if (predicates.length === 0) { return () => true; } else if (predicates.length === 1) { return predicates[0]; } else { return input => predicates.every(predicate => predicate(input)); } } getDataType() { if (this.data.dataType) { return this.data.dataType; } else if (this.data.predicate instanceof RegExp) { return 'String'; } else if (this.data.tokenList) { return 'String'; } else if (this.data.elementConstraint) { return 'Array'; } else { return 'Primitive'; } } get dataType() { return this.getDataType(); } get predicate() { return this.getCombinedPredicate(); } get extends() { return this.data.extends; } } class DataParser { constructor(parser, { async } = {}) { this.parser = parser; this.async = async; } validate() { const parser = this.parser; if (typeof parser !== 'function') { throw new TypeError(`parser was ${typeof parser}; expected function`); } } } class FormatParser { constructor(format, parsers = {}) { this.format = format; if (parsers.parseType) { this.typeParser = new TypeParser(parsers.parseType); } if (parsers.parse) { this.dataParser = new DataParser(parsers.parse, { async: false }); } if (parsers.parseAsync) { this.asyncDataParser = new DataParser(parsers.parseAsync, { async: true }); } } validateFormat() { const format = this.format; if (!typeMatcher.test(format)) { throw new TypeError(`format name was "${format}"; didn't match expected pattern`); } } validate() { this.validateFormat(); if (this.typeParser) { this.typeParser.validate(); } if (this.dataParser) { this.dataParser.validate(); } if (this.asyncDataParser) { this.asyncDataParser.validate(); } } } var parser = { __proto__: null, TypeParser: TypeParser, DataParser: DataParser, FormatParser: FormatParser }; function ownKeys$6(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$6(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys$6(Object(t), !0).forEach(function (r) { _defineProperty$6(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys$6(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty$6(obj, key, value) { key = _toPropertyKey$6(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$6(t) { var i = _toPrimitive$6(t, 'string'); return 'symbol' == typeof i ? i : i + ''; } function _toPrimitive$6(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); } const NAME = 1; const NAME_LIST = 2; const DATE = 3; const TYPE$3 = 4; const entryTypes = { article: true, 'article-journal': true, 'article-magazine': true, 'article-newspaper': true, bill: true, book: true, broadcast: true, chapter: true, classic: true, collection: true, dataset: true, document: true, entry: true, 'entry-dictionary': true, 'entry-encyclopedia': true, event: true, figure: true, graphic: true, hearing: true, interview: true, legal_case: true, legislation: true, manuscript: true, map: true, motion_picture: true, musical_score: true, pamphlet: true, 'paper-conference': true, patent: true, performance: true, periodical: true, personal_communication: true, post: true, 'post-weblog': true, regulation: true, report: true, review: true, 'review-book': true, software: true, song: true, speech: true, standard: true, thesis: true, treaty: true, webpage: true, 'journal-article': 'article-journal', 'book-chapter': 'chapter', 'posted-content': 'manuscript', 'proceedings-article': 'paper-conference', dissertation: 'thesis' }; const fieldTypes$2 = { author: NAME_LIST, chair: NAME_LIST, 'collection-editor': NAME_LIST, compiler: NAME_LIST, composer: NAME_LIST, 'container-author': NAME_LIST, contributor: NAME_LIST, curator: NAME_LIST, director: NAME_LIST, editor: NAME_LIST, 'editorial-director': NAME_LIST, 'executive-producer': NAME_LIST, guest: NAME_LIST, host: NAME_LIST, interviewer: NAME_LIST, illustrator: NAME_LIST, narrator: NAME_LIST, organizer: NAME_LIST, 'original-author': NAME_LIST, performer: NAME_LIST, producer: NAME_LIST, 'reviewed-author': NAME_LIST, recipient: NAME_LIST, 'script-writer': NAME_LIST, 'series-creator': NAME_LIST, translator: NAME_LIST, accessed: DATE, 'available-date': DATE, container: DATE, 'event-date': DATE, issued: DATE, 'original-date': DATE, submitted: DATE, type: TYPE$3, categories: 'object', custom: 'object', id: ['string', 'number'], language: 'string', journalAbbreviation: 'string', shortTitle: 'string', abstract: 'string', annote: 'string', archive: 'string', archive_collection: 'string', archive_location: 'string', 'archive-place': 'string', authority: 'string', 'call-number': 'string', 'chapter-number': 'string', 'citation-number': 'string', 'citation-key': 'string', 'citation-label': 'string', 'collection-number': 'string', 'collection-title': 'string', 'container-title': 'string', 'container-title-short': 'string', dimensions: 'string', division: 'string', DOI: 'string', edition: ['string', 'number'], event: 'string', 'event-title': 'string', 'event-place': 'string', 'first-reference-note-number': 'string', genre: 'string', ISBN: 'string', ISSN: 'string', issue: ['string', 'number'], jurisdiction: 'string', keyword: 'string', locator: 'string', medium: 'string', note: 'string', number: ['string', 'number'], 'number-of-pages': 'string', 'number-of-volumes': ['string', 'number'], 'original-publisher': 'string', 'original-publisher-place': 'string', 'original-title': 'string', page: 'string', 'page-first': 'string', 'part-number': ['string', 'number'], 'part-title': 'string', PMCID: 'string', PMID: 'string', printing: 'string', publisher: 'string', 'publisher-place': 'string', references: 'string', 'reviewed-title': 'string', 'reviewed-genre': 'string', scale: 'string', section: 'string', source: 'string', status: 'string', supplement: ['string', 'number'], title: 'string', 'title-short': 'string', URL: 'string', version: 'string', volume: ['string', 'number'], 'volume-title': 'string', 'volume-title-short': 'string', 'year-suffix': 'string' }; function correctName(name, bestGuessConversions) { if (typeof name === 'object' && name !== null && (name.literal || name.given || name.family)) { if (name.ORCID || name.orcid || name._ORCID) { name = _objectSpread$6({ _orcid: name.ORCID || name.orcid || name._ORCID }, name); delete name.ORCID; delete name.orcid; delete name._ORCID; } return name; } else if (!bestGuessConversions) { return undefined; } else if (typeof name === 'string') { return parse$8(name); } } function correctNameList(nameList, bestGuessConversions) { if (nameList instanceof Array) { const names = nameList.map(name => correctName(name, bestGuessConversions)).filter(Boolean); return names.length ? names : undefined; } } function correctDateParts(dateParts, bestGuessConversions) { if (dateParts.every(part => typeof part === 'number')) { return dateParts; } else if (!bestGuessConversions || dateParts.some(part => isNaN(parseInt(part)))) { return undefined; } else { return dateParts.map(part => parseInt(part)); } } function correctDate(date, bestGuessConversions) { const dp = 'date-parts'; if (typeof date !== 'object' || date === null) { return undefined; } else if (date[dp] instanceof Array && date[dp].every(part => part instanceof Array)) { const range = date[dp].map(dateParts => correctDateParts(dateParts, bestGuessConversions)).filter(Boolean); return range.length ? _objectSpread$6(_objectSpread$6({}, date), {}, { 'date-parts': range }) : undefined; } else if (date instanceof Array && date.every(part => part[dp] instanceof Array)) { const range = date.map(dateParts => correctDateParts(dateParts[dp], bestGuessConversions)).filter(Boolean); return range.length ? { 'date-parts': range } : undefined; } else if (date[dp] instanceof Array) { const dateParts = correctDateParts(date[dp], bestGuessConversions); return dateParts && { 'date-parts': [dateParts] }; } else if ('literal' in date || 'raw' in date) { return date; } } function correctType(type, bestGuessConversions) { type = correctField('language', type, bestGuessConversions); if (entryTypes[type] === true) { return type; } if (bestGuessConversions) { if (type in entryTypes) { return entryTypes[type]; } else if (type.toLowerCase() !== type) { return correctType(type.toLowerCase(), bestGuessConversions); } } return undefined; } function correctField(fieldName, value, bestGuessConversions) { const fieldType = [].concat(fieldTypes$2[fieldName]); switch (fieldTypes$2[fieldName]) { case NAME: return correctName(value, bestGuessConversions); case NAME_LIST: return correctNameList(value, bestGuessConversions); case DATE: return correctDate(value, bestGuessConversions); case TYPE$3: return correctType(value, bestGuessConversions); } if (bestGuessConversions) { if (typeof value === 'string' && fieldType.includes('number') && !fieldType.includes('string') && !isNaN(+value)) { return parseFloat(value); } else if (typeof value === 'number' && fieldType.includes('string') && !fieldType.includes('number')) { return value.toString(); } else if (Array.isArray(value) && value.length) { return correctField(fieldName, value[0], bestGuessConversions); } } if (fieldType.includes(typeof value)) { return value; } } function parseCsl(data, bestGuessConversions = true) { return data.map(function (entry) { const clean = {}; for (const field in entry) { const correction = correctField(field, entry[field], bestGuessConversions); if (correction !== undefined) { clean[field] = correction; } } return clean; }); } var csl = { __proto__: null, clean: parseCsl }; function ownKeys$5(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$5(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys$5(Object(t), !0).forEach(function (r) { _defineProperty$5(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys$5(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty$5(obj, key, value) { key = _toPropertyKey$5(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$5(t) { var i = _toPrimitive$5(t, 'string'); return 'symbol' == typeof i ? i : i + ''; } function _toPrimitive$5(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 upgradeCsl(item) { if (Array.isArray(item)) { return item.map(upgradeCsl); } item = _objectSpread$5({}, item); if ('event' in item) { item['event-title'] = item.event; delete item.event; } if (item.type === 'book' && 'version' in item) { item.type = 'software'; } return item; } function downgradeCsl(item) { if (Array.isArray(item)) { return item.map(downgradeCsl); } item = _objectSpread$5({}, item); if ('event-title' in item) { item.event = item['event-title']; delete item['event-title']; } if (item.type === 'software') { item.type = 'book'; } return item; } function deepCopy(value, seen = new Set()) { if (typeof value !== 'object' || value === null || value.constructor !== Object && value.constructor !== Array) { return value; } if (seen.has(value)) { throw new TypeError('Recursively copying circular structure'); } seen.add(value); let copy; if (value.constructor === Array) { copy = value.map(value => deepCopy(value, seen)); } else { const object = {}; for (const key in value) { object[key] = deepCopy(value[key], seen); } copy = object; } seen.delete(value); return copy; } var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; function createCommonjsModule(fn) { var module = { exports: {} }; return fn(module, module.exports), module.exports; } var conversions = {}; var lib$2 = conversions; function sign(x) { return x < 0 ? -1 : 1; } function evenRound(x) { // Round x to the nearest integer, choosing the even integer if it lies halfway between two. if (x % 1 === 0.5 && (x & 1) === 0) { // [even number].5; round down (i.e. floor) return Math.floor(x); } else { return Math.round(x); } } function createNumberConversion(bitLength, typeOpts) { if (!typeOpts.unsigned) { --bitLength; } const lowerBound = typeOpts.unsigned ? 0 : -Math.pow(2, bitLength); const upperBound = Math.pow(2, bitLength) - 1; const moduloVal = typeOpts.moduloBitLength ? Math.pow(2, typeOpts.moduloBitLength) : Math.pow(2, bitLength); const moduloBound = typeOpts.moduloBitLength ? Math.pow(2, typeOpts.moduloBitLength - 1) : Math.pow(2, bitLength - 1); return function (V, opts) { if (!opts) opts = {}; let x = +V; if (opts.enforceRange) { if (!Number.isFinite(x)) { throw new TypeError("Argument is not a finite number"); } x = sign(x) * Math.floor(Math.abs(x)); if (x < lowerBound || x > upperBound) { throw new TypeError("Argument is not in byte range"); } return x; } if (!isNaN(x) && opts.clamp) { x = evenRound(x); if (x < lowerBound) x = lowerBound; if (x > upperBound) x = upperBound; return x; } if (!Number.isFinite(x) || x === 0) { return 0; } x = sign(x) * Math.floor(Math.abs(x)); x = x % moduloVal; if (!typeOpts.unsigned && x >= moduloBound) { return x - moduloVal; } else if (typeOpts.unsigned) { if (x < 0) { x += moduloVal; } else if (x === -0) { // don't return negative zero return 0; } } return x; }; } conversions["void"] = function () { return undefined; }; conversions["boolean"] = function (val) { return !!val; }; conversions["byte"] = createNumberConversion(8, { unsigned: false }); conversions["octet"] = createNumberConversion(8, { unsigned: true }); conversions["short"] = createNumberConversion(16, { unsigned: false }); conversions["unsigned short"] = createNumberConversion(16, { unsigned: true }); conversions["long"] = createNumberConversion(32, { unsigned: false }); conversions["unsigned long"] = createNumberConversion(32, { unsigned: true }); conversions["long long"] = createNumberConversion(32, { unsigned: false, moduloBitLength: 64 }); conversions["unsigned long long"] = createNumberConversion(32, { unsigned: true, moduloBitLength: 64 }); conversions["double"] = function (V) { const x = +V; if (!Number.isFinite(x)) { throw new TypeError("Argument is not a finite floating-point value"); } return x; }; conversions["unrestricted double"] = function (V) { const x = +V; if (isNaN(x)) { throw new TypeError("Argument is NaN"); } return x; }; // not quite valid, but good enough for JS conversions["float"] = conversions["double"]; conversions["unrestricted float"] = conversions["unrestricted double"]; conversions["DOMString"] = function (V, opts) { if (!opts) opts = {}; if (opts.treatNullAsEmptyString && V === null) { return ""; } return String(V); }; conversions["ByteString"] = function (V, opts) { const x = String(V); let c = undefined; for (let i = 0; (c = x.codePointAt(i)) !== undefined; ++i) { if (c > 255) { throw new TypeError("Argument is not a valid bytestring"); } } return x; }; conversions["USVString"] = function (V) { const S = String(V); const n = S.length; const U = []; for (let i = 0; i < n; ++i) { const c = S.charCodeAt(i); if (c < 0xD800 || c > 0xDFFF) { U.push(String.fromCodePoint(c)); } else if (0xDC00 <= c && c <= 0xDFFF) { U.push(String.fromCodePoint(0xFFFD)); } else { if (i === n - 1) { U.push(String.fromCodePoint(0xFFFD)); } else { const d = S.charCodeAt(i + 1); if (0xDC00 <= d && d <= 0xDFFF) { const a = c & 0x3FF; const b = d & 0x3FF; U.push(String.fromCodePoint((2 << 15) + (2 << 9) * a + b)); ++i; } else { U.push(String.fromCodePoint(0xFFFD)); } } } } return U.join(''); }; conversions["Date"] = function (V, opts) { if (!(V instanceof Date)) { throw new TypeError("Argument is not a Date object"); } if (isNaN(V)) { return undefined; } return V; }; conversions["RegExp"] = function (V, opts) { if (!(V instanceof RegExp)) { V = new RegExp(V); } return V; }; var utils = createCommonjsModule(function (module) { module.exports.mixin = function mixin(target, source) { const keys = Object.getOwnPropertyNames(source); for (let i = 0; i < keys.length; ++i) { Object.defineProperty(target, keys[i], Object.getOwnPropertyDescriptor(source, keys[i])); } }; module.exports.wrapperSymbol = Symbol("wrapper"); module.exports.implSymbol = Symbol("impl"); module.exports.wrapperForImpl = function (impl) { return impl[module.exports.wrapperSymbol]; }; module.exports.implForWrapper = function (wrapper) { return wrapper[module.exports.implSymbol]; }; }); var mappingTable = [ [ [ 0, 44 ], "disallowed_STD3_valid" ], [ [ 45, 46 ], "valid" ], [ [ 47, 47 ], "disallowed_STD3_valid" ], [ [ 48, 57 ], "valid" ], [ [ 58, 64 ], "disallowed_STD3_valid" ], [ [ 65, 65 ], "mapped", [ 97 ] ], [ [ 66, 66 ], "mapped", [ 98 ] ], [ [ 67, 67 ], "mapped", [ 99 ] ], [ [ 68, 68 ], "mapped", [ 100 ] ], [ [ 69, 69 ], "mapped", [ 101 ] ], [ [ 70, 70 ], "mapped", [ 102 ] ], [ [ 71, 71 ], "mapped", [ 103 ] ], [ [ 72, 72 ], "mapped", [ 104 ] ], [ [ 73, 73 ], "mapped", [ 105 ] ], [ [ 74, 74 ], "mapped", [ 106 ] ], [ [ 75, 75 ], "mapped", [ 107 ] ], [ [ 76, 76 ], "mapped", [ 108 ] ], [ [ 77, 77 ], "mapped", [ 109 ] ], [ [ 78, 78 ], "mapped", [ 110 ] ], [ [ 79, 79 ], "mapped", [ 111 ] ], [ [ 80, 80 ], "mapped", [ 112 ] ], [ [ 81, 81 ], "mapped", [ 113 ] ], [ [ 82, 82 ], "mapped", [ 114 ] ], [ [ 83, 83 ], "mapped", [ 115 ] ], [ [ 84, 84 ], "mapped", [ 116 ] ], [ [ 85, 85 ], "mapped", [ 117 ] ], [ [ 86, 86 ], "mapped", [ 118 ] ], [ [ 87, 87 ], "mapped", [ 119 ] ], [ [ 88, 88 ], "mapped", [ 120 ] ], [ [ 89, 89 ], "mapped", [ 121 ] ], [ [ 90, 90 ], "mapped", [ 122 ] ], [ [ 91, 96 ], "disallowed_STD3_valid" ], [ [ 97, 122 ], "valid" ], [ [ 123, 127 ], "disallowed_STD3_valid" ], [ [ 128, 159 ], "disallowed" ], [ [ 160, 160 ], "disallowed_STD3_mapped", [ 32 ] ], [ [ 161, 167 ], "valid", [ ], "NV8" ], [ [ 168, 168 ], "disallowed_STD3_mapped", [ 32, 776 ] ], [ [ 169, 169 ], "valid", [ ], "NV8" ], [ [ 170, 170 ], "mapped", [ 97 ] ], [ [ 171, 172 ], "valid", [ ], "NV8" ], [ [ 173, 173 ], "ignored" ], [ [ 174, 174 ], "valid", [ ], "NV8" ], [ [ 175, 175 ], "disallowed_STD3_mapped", [ 32, 772 ] ], [ [ 176, 177 ], "valid", [ ], "NV8" ], [ [ 178, 178 ], "mapped", [ 50 ] ], [ [ 179, 179 ], "mapped", [ 51 ] ], [ [ 180, 180 ], "disallowed_STD3_mapped", [ 32, 769 ] ], [ [ 181, 181 ], "mapped", [ 956 ] ], [ [ 182, 182 ], "valid", [ ], "NV8" ], [ [ 183, 183 ], "valid" ], [ [ 184, 184 ], "disallowed_STD3_mapped", [ 32, 807 ] ], [ [ 185, 185 ], "mapped", [ 49 ] ], [ [ 186, 186 ], "mapped", [ 111 ] ], [ [ 187, 187 ], "valid", [ ], "NV8" ], [ [ 188, 188 ], "mapped", [ 49, 8260, 52 ] ], [ [ 189, 189 ], "mapped", [ 49, 8260, 50 ] ], [ [ 190, 190 ], "mapped", [ 51, 8260, 52 ] ], [ [ 191, 191 ], "valid", [ ], "NV8" ], [ [ 192, 192 ], "mapped", [ 224 ] ], [ [ 193, 193 ], "mapped", [ 225 ] ], [ [ 194, 194 ], "mapped", [ 226 ] ], [ [ 195, 195 ], "mapped", [ 227 ] ], [ [ 196, 196 ], "mapped", [ 228 ] ], [ [ 197, 197 ], "mapped", [ 229 ] ], [ [ 198, 198 ], "mapped", [ 230 ] ], [ [ 199, 199 ], "mapped", [ 231 ] ], [ [ 200, 200 ], "mapped", [ 232 ] ], [ [ 201, 201 ], "mapped", [ 233 ] ], [ [ 202, 202 ], "mapped", [ 234 ] ], [ [ 203, 203 ], "mapped", [ 235 ] ], [ [ 204, 204 ], "mapped", [ 236 ] ], [ [ 205, 205 ], "mapped", [ 237 ] ], [ [ 206, 206 ], "mapped", [ 238 ] ], [ [ 207, 207 ], "mapped", [ 239 ] ], [ [ 208, 208 ], "mapped", [ 240 ] ], [ [ 209, 209 ], "mapped", [ 241 ] ], [ [ 210, 210 ], "mapped", [ 242 ] ], [ [ 211, 211 ], "mapped", [ 243 ] ], [ [ 212, 212 ], "mapped", [ 244 ] ], [ [ 213, 213 ], "mapped", [ 245 ] ], [ [ 214, 214 ], "mapped", [ 246 ] ], [ [ 215, 215 ], "valid", [ ], "NV8" ], [ [ 216, 216 ], "mapped", [ 248 ] ], [ [ 217, 217 ], "mapped", [ 249 ] ], [ [ 218, 218 ], "mapped", [ 250 ] ], [ [ 219, 219 ], "mapped", [ 251 ] ], [ [ 220, 220 ], "mapped", [ 252 ] ], [ [ 221, 221 ], "mapped", [ 253 ] ], [ [ 222, 222 ], "mapped", [ 254 ] ], [ [ 223, 223 ], "deviation", [ 115, 115 ] ], [ [ 224, 246 ], "valid" ], [ [ 247, 247 ], "valid", [ ], "NV8" ], [ [ 248, 255 ], "valid" ], [ [ 256, 256 ], "mapped", [ 257 ] ], [ [ 257, 257 ], "valid" ], [ [ 258, 258 ], "mapped", [ 259 ] ], [ [ 259, 259 ], "valid" ], [ [ 260, 260 ], "mapped", [ 261 ] ], [ [ 261, 261 ], "valid" ], [ [ 262, 262 ], "mapped", [ 263 ] ], [ [ 263, 263 ], "valid" ], [ [ 264, 264 ], "mapped", [ 265 ] ], [ [ 265, 265 ], "valid" ], [ [ 266, 266 ], "mapped", [ 267 ] ], [ [ 267, 267 ], "valid" ], [ [ 268, 268 ], "mapped", [ 269 ] ], [ [ 269, 269 ], "valid" ], [ [ 270, 270 ], "mapped", [ 271 ] ], [ [ 271, 271 ], "valid" ], [ [ 272, 272 ], "mapped", [ 273 ] ], [ [ 273, 273 ], "valid" ], [ [ 274, 274 ], "mapped", [ 275 ] ], [ [ 275, 275 ], "valid" ], [ [ 276, 276 ], "mapped", [ 277 ] ], [ [ 277, 277 ], "valid" ], [ [ 278, 278 ], "mapped", [ 279 ] ], [ [ 279, 279 ], "valid" ], [ [ 280, 280 ], "mapped", [ 281 ] ], [ [ 281, 281 ], "valid" ], [ [ 282, 282 ], "mapped", [ 283 ] ], [ [ 283, 283 ], "valid" ], [ [ 284, 284 ], "mapped", [ 285 ] ], [ [ 285, 285 ], "valid" ], [ [ 286, 286 ], "mapped", [ 287 ] ], [ [ 287, 287 ], "valid" ], [ [ 288, 288 ], "mapped", [ 289 ] ], [ [ 289, 289 ], "valid" ], [ [ 290, 290 ], "mapped", [ 291 ] ], [ [ 291, 291 ], "valid" ], [ [ 292, 292 ], "mapped", [ 293 ] ], [ [ 293, 293 ], "valid" ], [ [ 294, 294 ], "mapped", [ 295 ] ], [ [ 295, 295 ], "valid" ], [ [ 296, 296 ], "mapped", [ 297 ] ], [ [ 297, 297 ], "valid" ], [ [ 298, 298 ], "mapped", [ 299 ] ], [ [ 299, 299 ], "valid" ], [ [ 300, 300 ], "mapped", [ 301 ] ], [ [ 301, 301 ], "valid" ], [ [ 302, 302 ], "mapped", [ 303 ] ], [ [ 303, 303 ], "valid" ], [ [ 304, 304 ], "mapped", [ 105, 775 ] ], [ [ 305, 305 ], "valid" ], [ [ 306, 307 ], "mapped", [ 105, 106 ] ], [ [ 308, 308 ], "mapped", [ 309 ] ], [ [ 309, 309 ], "valid" ], [ [ 310, 310 ], "mapped", [ 311 ] ], [ [ 311, 312 ], "valid" ], [ [ 313, 313 ], "mapped", [ 314 ] ], [ [ 314, 314 ], "valid" ], [ [ 315, 315 ], "mapped", [ 316 ] ], [ [ 316, 316 ], "valid" ], [ [ 317, 317 ], "mapped", [ 318 ] ], [ [ 318, 318 ], "valid" ], [ [ 319, 320 ], "mapped", [ 108, 183 ] ], [ [ 321, 321 ], "mapped", [ 322 ] ], [ [ 322, 322 ], "valid" ], [ [ 323, 323 ], "mapped", [ 324 ] ], [ [ 324, 324 ], "valid" ], [ [ 325, 325 ], "mapped", [ 326 ] ], [ [ 326, 326 ], "valid" ], [ [ 327, 327 ], "mapped", [ 328 ] ], [ [ 328, 328 ], "valid" ], [ [ 329, 329 ], "mapped", [ 700, 110 ] ], [ [ 330, 330 ], "mapped", [ 331 ] ], [ [ 331, 331 ], "valid" ], [ [ 332, 332 ], "mapped", [ 333 ] ], [ [ 333, 333 ], "valid" ], [ [ 334, 334 ], "mapped", [ 335 ] ], [ [ 335, 335 ], "valid" ], [ [ 336, 336 ], "mapped", [ 337 ] ], [ [ 337, 337 ], "valid" ], [ [ 338, 338 ], "mapped", [ 339 ] ], [ [ 339, 339 ], "valid" ], [ [ 340, 340 ], "mapped", [ 341 ] ], [ [ 341, 341 ], "valid" ], [ [ 342, 342 ], "mapped", [ 343 ] ], [ [ 343, 343 ], "valid" ], [ [ 344, 344 ], "mapped", [ 345 ] ], [ [ 345, 345 ], "valid" ], [ [ 346, 346 ], "mapped", [ 347 ] ], [ [ 347, 347 ], "valid" ], [ [ 348, 348 ], "mapped", [ 349 ] ], [ [ 349, 349 ], "valid" ], [ [ 350, 350 ], "mapped", [ 351 ] ], [ [ 351, 351 ], "valid" ], [ [ 352, 352 ], "mapped", [ 353 ] ], [ [ 353, 353 ], "valid" ], [ [ 354, 354 ], "mapped", [ 355 ] ], [ [ 355, 355 ], "valid" ], [ [ 356, 356 ], "mapped", [ 357 ] ], [ [ 357, 357 ], "valid" ], [ [ 358, 358 ], "mapped", [ 359 ] ], [ [ 359, 359 ], "valid" ], [ [ 360, 360 ], "mapped", [ 361 ] ], [ [ 361, 361 ], "valid" ], [ [ 362, 362 ], "mapped", [ 363 ] ], [ [ 363, 363 ], "valid" ], [ [ 364, 364 ], "mapped", [ 365 ] ], [ [ 365, 365 ], "valid" ], [ [ 366, 366 ], "mapped", [ 367 ] ], [ [ 367, 367 ], "valid" ], [ [ 368, 368 ], "mapped", [ 369 ] ], [ [ 369, 369 ], "valid" ], [ [ 370, 370 ], "mapped", [ 371 ] ], [ [ 371, 371 ], "valid" ], [ [ 372, 372 ], "mapped", [ 373 ] ], [ [ 373, 373 ], "valid" ], [ [ 374, 374 ], "mapped", [ 375 ] ], [ [ 375, 375 ], "valid" ], [ [ 376, 376 ], "mapped", [ 255 ] ], [ [ 377, 377 ], "mapped", [ 378 ] ], [ [ 378, 378 ], "valid" ], [ [ 379, 379 ], "mapped", [ 380 ] ], [ [ 380, 380 ], "valid" ], [ [ 381, 381 ], "mapped", [ 382 ] ], [ [ 382, 382 ], "valid" ], [ [ 383, 383 ], "mapped", [ 115 ] ], [ [ 384, 384 ], "valid" ], [ [ 385, 385 ], "mapped", [ 595 ] ], [ [ 386, 386 ], "mapped", [ 387 ] ], [ [ 387, 387 ], "valid" ], [ [ 388, 388 ], "mapped", [ 389 ] ], [ [ 389, 389 ], "valid" ], [ [ 390, 390 ], "mapped", [ 596 ] ], [ [ 391, 391 ], "mapped", [ 392 ] ], [ [ 392, 392 ], "valid" ], [ [ 393, 393 ], "mapped", [ 598 ] ], [ [ 394, 394 ], "mapped", [ 599 ] ], [ [ 395, 395 ], "mapped", [ 396 ] ], [ [ 396, 397 ], "valid" ], [ [ 398, 398 ], "mapped", [ 477 ] ], [ [ 399, 399 ], "mapped", [ 601 ] ], [ [ 400, 400 ], "mapped", [ 603 ] ], [ [ 401, 401 ], "mapped", [ 402 ] ], [ [ 402, 402 ], "valid" ], [ [ 403, 403 ], "mapped", [ 608 ] ], [ [ 404, 404 ], "mapped", [ 611 ] ], [ [ 405, 405 ], "valid" ], [ [ 406, 406 ], "mapped", [ 617 ] ], [ [ 407, 407 ], "mapped", [ 616 ] ], [ [ 408, 408 ], "mapped", [ 409 ] ], [ [ 409, 411 ], "valid" ], [ [ 412, 412 ], "mapped", [ 623 ] ], [ [ 413, 413 ], "mapped", [ 626 ] ], [ [ 414, 414 ], "valid" ], [ [ 415, 415 ], "mapped", [ 629 ] ], [ [ 416, 416 ], "mapped", [ 417 ] ], [ [ 417, 417 ], "valid" ], [ [ 418, 418 ], "mapped", [ 419 ] ], [ [ 419, 419 ], "valid" ], [ [ 420, 420 ], "mapped", [ 421 ] ], [ [ 421, 421 ], "valid" ], [ [ 422, 422 ], "mapped", [ 640 ] ], [ [ 423, 423 ], "mapped", [ 424 ] ], [ [ 424, 424 ], "valid" ], [ [ 425, 425 ], "mapped", [ 643 ] ], [ [ 426, 427 ], "valid" ], [ [ 428, 428 ], "mapped", [ 429 ] ], [ [ 429, 429 ], "valid" ], [ [ 430, 430 ], "mapped", [ 648 ] ], [ [ 431, 431 ], "mapped", [ 432 ] ], [ [ 432, 432 ], "valid" ], [ [ 433, 433 ], "mapped", [ 650 ] ], [ [ 434, 434 ], "mapped", [ 651 ] ], [ [ 435, 435 ], "mapped", [ 436 ] ], [ [ 436, 436 ], "valid" ], [ [ 437, 437 ], "mapped", [ 438 ] ], [ [ 438, 438 ], "valid" ], [ [ 439, 439 ], "mapped", [ 658 ] ], [ [ 440, 440 ], "mapped", [ 441 ] ], [ [ 441, 443 ], "valid" ], [ [ 444, 444 ], "mapped", [ 445 ] ], [ [ 445, 451 ], "valid" ], [ [ 452, 454 ], "mapped", [ 100, 382 ] ], [ [ 455, 457 ], "mapped", [ 108, 106 ] ], [ [ 458, 460 ], "mapped", [ 110, 106 ] ], [ [ 461, 461 ], "mapped", [ 462 ] ], [ [ 462, 462 ], "valid" ], [ [ 463, 463 ], "mapped", [ 464 ] ], [ [ 464, 464 ], "valid" ], [ [ 465, 465 ], "mapped", [ 466 ] ], [ [ 466, 466 ], "valid" ], [ [ 467, 467 ], "mapped", [ 468 ] ], [ [ 468, 468 ], "valid" ], [ [ 469, 469 ], "mapped", [ 470 ] ], [ [ 470, 470 ], "valid" ], [ [ 471, 471 ], "mapped", [ 472 ] ], [ [ 472, 472 ], "valid" ], [ [ 473, 473 ], "mapped", [ 474 ] ], [ [ 474, 474 ], "valid" ], [ [ 475, 475 ], "mapped", [ 476 ] ], [ [ 476, 477 ], "valid" ], [ [ 478, 478 ], "mapped", [ 479 ] ], [ [ 479, 479 ], "valid" ], [ [ 480, 480 ], "mapped", [ 481 ] ], [ [ 481, 481 ], "valid" ], [ [ 482, 482 ], "mapped", [ 483 ] ], [ [ 483, 483 ], "valid" ], [ [ 484, 484 ], "mapped", [ 485 ] ], [ [ 485, 485 ], "valid" ], [ [ 486, 486 ], "mapped", [ 487 ] ], [ [ 487, 487 ], "valid" ], [ [ 488, 488 ], "mapped", [ 489 ] ], [ [ 489, 489 ], "valid" ], [ [ 490, 490 ], "mapped", [ 491 ] ], [ [ 491, 491 ], "valid" ], [ [ 492, 492 ], "mapped", [ 493 ] ], [ [ 493, 493 ], "valid" ], [ [ 494, 494 ], "mapped", [ 495 ] ], [ [ 495, 496 ], "valid" ], [ [ 497, 499 ], "mapped", [ 100, 122 ] ], [ [ 500, 500 ], "mapped", [ 501 ] ], [ [ 501, 501 ], "valid" ], [ [ 502, 502 ], "mapped", [ 405 ] ], [ [ 503, 503 ], "mapped", [ 447 ] ], [ [ 504, 504 ], "mapped", [ 505 ] ], [ [ 505, 505 ], "valid" ], [ [ 506, 506 ], "mapped", [ 507 ] ], [ [ 507, 507 ], "valid" ], [ [ 508, 508 ], "mapped", [ 509 ] ], [ [ 509, 509 ], "valid" ], [ [ 510, 510 ], "mapped", [ 511 ] ], [ [ 511, 511 ], "valid" ], [ [ 512, 512 ], "mapped", [ 513 ] ], [ [ 513, 513 ], "valid" ], [ [ 514, 514 ], "mapped", [ 515 ] ], [ [ 515, 515 ], "valid" ], [ [ 516, 516 ], "mapped", [ 517 ] ], [ [ 517, 517 ], "valid" ], [ [ 518, 518 ], "mapped",