mirror of
https://github.com/mozilla/pdf.js.git
synced 2026-08-06 22:37:23 +02:00
Add helper methods for populating dateActionsCache/scandCache in the src/scripting_api/util.js file
Rather than inlining all of that code, by moving it into separate helper methods we can utilize `Map.prototype.getOrInsertComputed()` instead. Also, make a couple of the existing class fields actually private.
This commit is contained in:
parent
5f181fd8b0
commit
f1f1782f38
@ -17,13 +17,15 @@
|
||||
import { PDFObject } from "./pdf_object.js";
|
||||
|
||||
class Util extends PDFObject {
|
||||
#createDateActionsBound = this.#createDateActions.bind(this);
|
||||
|
||||
#createScandDataBound = this.#createScandData.bind(this);
|
||||
|
||||
#dateActionsCache = null;
|
||||
|
||||
constructor(data) {
|
||||
super(data);
|
||||
#scandCache = null;
|
||||
|
||||
this._scandCache = new Map();
|
||||
this._months = [
|
||||
#months = [
|
||||
"January",
|
||||
"February",
|
||||
"March",
|
||||
@ -37,7 +39,8 @@ class Util extends PDFObject {
|
||||
"November",
|
||||
"December",
|
||||
];
|
||||
this._days = [
|
||||
|
||||
#days = [
|
||||
"Sunday",
|
||||
"Monday",
|
||||
"Tuesday",
|
||||
@ -46,8 +49,13 @@ class Util extends PDFObject {
|
||||
"Friday",
|
||||
"Saturday",
|
||||
];
|
||||
this.MILLISECONDS_IN_DAY = 86400000;
|
||||
this.MILLISECONDS_IN_WEEK = 604800000;
|
||||
|
||||
MILLISECONDS_IN_DAY = 86400000;
|
||||
|
||||
MILLISECONDS_IN_WEEK = 604800000;
|
||||
|
||||
constructor(data) {
|
||||
super(data);
|
||||
|
||||
// used with crackURL
|
||||
this._externalCall = data.externalCall;
|
||||
@ -71,7 +79,7 @@ class Util extends PDFObject {
|
||||
let i = 0;
|
||||
return args[0].replaceAll(
|
||||
pattern,
|
||||
function (match, nDecSep, cFlags, nWidth, nPrecision, cConvChar) {
|
||||
function (_, nDecSep, cFlags, nWidth, nPrecision, cConvChar) {
|
||||
// cConvChar must be one of d, f, s, x
|
||||
if (
|
||||
cConvChar !== "d" &&
|
||||
@ -217,12 +225,12 @@ class Util extends PDFObject {
|
||||
}
|
||||
|
||||
const handlers = {
|
||||
mmmm: data => this._months[data.month],
|
||||
mmm: data => this._months[data.month].substring(0, 3),
|
||||
mmmm: data => this.#months[data.month],
|
||||
mmm: data => this.#months[data.month].substring(0, 3),
|
||||
mm: data => (data.month + 1).toString().padStart(2, "0"),
|
||||
m: data => (data.month + 1).toString(),
|
||||
dddd: data => this._days[data.dayOfWeek],
|
||||
ddd: data => this._days[data.dayOfWeek].substring(0, 3),
|
||||
dddd: data => this.#days[data.dayOfWeek],
|
||||
ddd: data => this.#days[data.dayOfWeek].substring(0, 3),
|
||||
dd: data => data.day.toString().padStart(2, "0"),
|
||||
d: data => data.day.toString(),
|
||||
yyyy: data => data.year.toString().padStart(4, "0"),
|
||||
@ -251,7 +259,7 @@ class Util extends PDFObject {
|
||||
|
||||
const patterns =
|
||||
/(mmmm|mmm|mm|m|dddd|ddd|dd|d|yyyy|yy|HH|H|hh|h|MM|M|ss|s|tt|t|\\.)/g;
|
||||
return cFormat.replaceAll(patterns, function (match, pattern) {
|
||||
return cFormat.replaceAll(patterns, function (_, pattern) {
|
||||
return pattern in handlers
|
||||
? handlers[pattern](data)
|
||||
: pattern.charCodeAt(1);
|
||||
@ -337,16 +345,12 @@ class Util extends PDFObject {
|
||||
return buf.join("");
|
||||
}
|
||||
|
||||
#tryToGuessDate(cFormat, cDate) {
|
||||
// We use the format to know the order of day, month, year, ...
|
||||
#createDateActions(cFormat) {
|
||||
const actions = [];
|
||||
|
||||
let actions = (this.#dateActionsCache ||= new Map()).get(cFormat);
|
||||
if (!actions) {
|
||||
actions = [];
|
||||
this.#dateActionsCache.set(cFormat, actions);
|
||||
cFormat.replaceAll(
|
||||
/(d+)|(m+)|(y+)|(H+)|(M+)|(s+)/g,
|
||||
function (_match, d, m, y, H, M, s) {
|
||||
function (_, d, m, y, H, M, s) {
|
||||
if (d) {
|
||||
actions.push((n, data) => {
|
||||
if (n >= 1 && n <= 31) {
|
||||
@ -401,8 +405,18 @@ class Util extends PDFObject {
|
||||
return "";
|
||||
}
|
||||
);
|
||||
|
||||
return actions;
|
||||
}
|
||||
|
||||
#tryToGuessDate(cFormat, cDate) {
|
||||
// We use the format to know the order of day, month, year, ...
|
||||
|
||||
const actions = (this.#dateActionsCache ??= new Map()).getOrInsertComputed(
|
||||
cFormat,
|
||||
this.#createDateActionsBound
|
||||
);
|
||||
|
||||
const number = /\d+/g;
|
||||
let i = 0;
|
||||
let array;
|
||||
@ -442,27 +456,9 @@ class Util extends PDFObject {
|
||||
return this._scand(cFormat, cDate);
|
||||
}
|
||||
|
||||
_scand(cFormat, cDate, strict = false) {
|
||||
if (typeof cDate !== "string") {
|
||||
return new Date(cDate);
|
||||
}
|
||||
|
||||
if (cDate === "") {
|
||||
return new Date();
|
||||
}
|
||||
|
||||
switch (cFormat) {
|
||||
case 0:
|
||||
return this.scand("D:yyyymmddHHMMss", cDate);
|
||||
case 1:
|
||||
return this.scand("yyyy.mm.dd HH:MM:ss", cDate);
|
||||
case 2:
|
||||
return this.scand("m/d/yy h:MM:ss tt", cDate);
|
||||
}
|
||||
|
||||
if (!this._scandCache.has(cFormat)) {
|
||||
const months = this._months;
|
||||
const days = this._days;
|
||||
#createScandData(cFormat) {
|
||||
const months = this.#months,
|
||||
days = this.#days;
|
||||
|
||||
const handlers = {
|
||||
mmmm: {
|
||||
@ -596,9 +592,7 @@ class Util extends PDFObject {
|
||||
/(mmmm|mmm|mm|m|dddd|ddd|dd|d|yyyy|yy|HH|H|hh|h|MM|M|ss|s|tt|t)/g;
|
||||
const actions = [];
|
||||
|
||||
const re = escapedFormat.replaceAll(
|
||||
patterns,
|
||||
function (match, patternElement) {
|
||||
const re = escapedFormat.replaceAll(patterns, function (_, patternElement) {
|
||||
const { pattern, action } = handlers[patternElement];
|
||||
actions.push(action);
|
||||
// If the format is "Hm", then /\d{1,2}\d{1,2}/ is ambiguous so we use
|
||||
@ -606,14 +600,33 @@ class Util extends PDFObject {
|
||||
return pattern.includes(",")
|
||||
? `(?=${pattern})\\${actions.length}`
|
||||
: pattern;
|
||||
});
|
||||
|
||||
return [re, actions];
|
||||
}
|
||||
|
||||
_scand(cFormat, cDate, strict = false) {
|
||||
if (typeof cDate !== "string") {
|
||||
return new Date(cDate);
|
||||
}
|
||||
if (cDate === "") {
|
||||
return new Date();
|
||||
}
|
||||
|
||||
switch (cFormat) {
|
||||
case 0:
|
||||
return this.scand("D:yyyymmddHHMMss", cDate);
|
||||
case 1:
|
||||
return this.scand("yyyy.mm.dd HH:MM:ss", cDate);
|
||||
case 2:
|
||||
return this.scand("m/d/yy h:MM:ss tt", cDate);
|
||||
}
|
||||
|
||||
const [re, actions] = (this.#scandCache ??= new Map()).getOrInsertComputed(
|
||||
cFormat,
|
||||
this.#createScandDataBound
|
||||
);
|
||||
|
||||
this._scandCache.set(cFormat, [re, actions]);
|
||||
}
|
||||
|
||||
const [re, actions] = this._scandCache.get(cFormat);
|
||||
|
||||
const matches = new RegExp(`^${re}$`, "g").exec(cDate);
|
||||
if (!matches || matches.length !== actions.length + 1) {
|
||||
return strict ? null : this.#tryToGuessDate(cFormat, cDate);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user