Move the toRoman helper function out of the Util scope

Compared to all the other (static) methods in `Util`, the `toRoman` one looks slightly out of place. Even more so considering that `Util` is being exposed through `pdfjsLib`, where access to a Roman numerals conversion method doesn't make much sense.
This commit is contained in:
Jonas Jenwald 2018-07-10 10:45:25 +02:00
parent c1c49badff
commit 8e76d26e5b
2 changed files with 40 additions and 38 deletions

View File

@ -16,7 +16,7 @@
import {
bytesToString, createPromiseCapability, createValidAbsoluteUrl, FormatError,
info, InvalidPDFException, isBool, isString, MissingDataException, shadow,
stringToPDFString, stringToUTF8String, unreachable, Util, warn,
stringToPDFString, stringToUTF8String, toRomanNumerals, unreachable, warn,
XRefParseException
} from '../shared/util';
import {
@ -310,7 +310,7 @@ var Catalog = (function CatalogClosure() {
break;
case 'R':
case 'r':
currentLabel = Util.toRoman(currentIndex, style === 'r');
currentLabel = toRomanNumerals(currentIndex, style === 'r');
break;
case 'A':
case 'a':

View File

@ -839,45 +839,46 @@ var Util = (function UtilClosure() {
return result;
};
var ROMAN_NUMBER_MAP = [
'', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM',
'', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC',
'', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX'
];
/**
* Converts positive integers to (upper case) Roman numerals.
* @param {integer} number - The number that should be converted.
* @param {boolean} lowerCase - Indicates if the result should be converted
* to lower case letters. The default is false.
* @return {string} The resulting Roman number.
*/
Util.toRoman = function Util_toRoman(number, lowerCase) {
assert(Number.isInteger(number) && number > 0,
'The number should be a positive integer.');
var pos, romanBuf = [];
// Thousands
while (number >= 1000) {
number -= 1000;
romanBuf.push('M');
}
// Hundreds
pos = (number / 100) | 0;
number %= 100;
romanBuf.push(ROMAN_NUMBER_MAP[pos]);
// Tens
pos = (number / 10) | 0;
number %= 10;
romanBuf.push(ROMAN_NUMBER_MAP[10 + pos]);
// Ones
romanBuf.push(ROMAN_NUMBER_MAP[20 + number]);
var romanStr = romanBuf.join('');
return (lowerCase ? romanStr.toLowerCase() : romanStr);
};
return Util;
})();
const ROMAN_NUMBER_MAP = [
'', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM',
'', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC',
'', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX'
];
/**
* Converts positive integers to (upper case) Roman numerals.
* @param {integer} number - The number that should be converted.
* @param {boolean} lowerCase - Indicates if the result should be converted
* to lower case letters. The default value is `false`.
* @return {string} The resulting Roman number.
*/
function toRomanNumerals(number, lowerCase = false) {
assert(Number.isInteger(number) && number > 0,
'The number should be a positive integer.');
let pos, romanBuf = [];
// Thousands
while (number >= 1000) {
number -= 1000;
romanBuf.push('M');
}
// Hundreds
pos = (number / 100) | 0;
number %= 100;
romanBuf.push(ROMAN_NUMBER_MAP[pos]);
// Tens
pos = (number / 10) | 0;
number %= 10;
romanBuf.push(ROMAN_NUMBER_MAP[10 + pos]);
// Ones
romanBuf.push(ROMAN_NUMBER_MAP[20 + number]);
const romanStr = romanBuf.join('');
return (lowerCase ? romanStr.toLowerCase() : romanStr);
}
var PDFStringTranslateTable = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0x2D8, 0x2C7, 0x2C6, 0x2D9, 0x2DD, 0x2DB, 0x2DA, 0x2DC, 0, 0, 0, 0, 0, 0, 0,
@ -1025,6 +1026,7 @@ export {
UnexpectedResponseException,
UnknownErrorException,
Util,
toRomanNumerals,
XRefParseException,
FormatError,
arrayByteLength,