@unlev/exeq 0.5.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +27 -3
- package/dist/index.d.ts +27 -3
- package/dist/index.js +54 -23
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +53 -23
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -2093,39 +2093,56 @@ import {
|
|
|
2093
2093
|
} from "pdf-lib";
|
|
2094
2094
|
|
|
2095
2095
|
// src/utils/formulaResolver.ts
|
|
2096
|
+
function parseDate(value) {
|
|
2097
|
+
const s = value.trim();
|
|
2098
|
+
if (!s) return null;
|
|
2099
|
+
if (/^\d+(\.\d+)?$/.test(s)) {
|
|
2100
|
+
const serial = parseFloat(s);
|
|
2101
|
+
if (serial > 59) {
|
|
2102
|
+
const utc = new Date(Math.round((serial - 25569) * 864e5));
|
|
2103
|
+
if (!isNaN(utc.getTime())) {
|
|
2104
|
+
return new Date(utc.getUTCFullYear(), utc.getUTCMonth(), utc.getUTCDate());
|
|
2105
|
+
}
|
|
2106
|
+
}
|
|
2107
|
+
}
|
|
2108
|
+
const iso = s.match(/^(\d{4})-(\d{2})-(\d{2})$/);
|
|
2109
|
+
if (iso) return new Date(+iso[1], +iso[2] - 1, +iso[3]);
|
|
2110
|
+
const d = new Date(s);
|
|
2111
|
+
return isNaN(d.getTime()) ? null : d;
|
|
2112
|
+
}
|
|
2096
2113
|
var BUILTIN_TRANSFORMS = {
|
|
2097
2114
|
// Date transforms (expects a parseable date string)
|
|
2098
2115
|
month: (v) => {
|
|
2099
|
-
const d =
|
|
2100
|
-
return
|
|
2116
|
+
const d = parseDate(v);
|
|
2117
|
+
return d ? String(d.getMonth() + 1) : "";
|
|
2101
2118
|
},
|
|
2102
2119
|
month2: (v) => {
|
|
2103
|
-
const d =
|
|
2104
|
-
return
|
|
2120
|
+
const d = parseDate(v);
|
|
2121
|
+
return d ? String(d.getMonth() + 1).padStart(2, "0") : "";
|
|
2105
2122
|
},
|
|
2106
2123
|
monthname: (v) => {
|
|
2107
|
-
const d =
|
|
2108
|
-
return
|
|
2124
|
+
const d = parseDate(v);
|
|
2125
|
+
return d ? d.toLocaleString("en", { month: "long" }) : "";
|
|
2109
2126
|
},
|
|
2110
2127
|
monthshort: (v) => {
|
|
2111
|
-
const d =
|
|
2112
|
-
return
|
|
2128
|
+
const d = parseDate(v);
|
|
2129
|
+
return d ? d.toLocaleString("en", { month: "short" }) : "";
|
|
2113
2130
|
},
|
|
2114
2131
|
day: (v) => {
|
|
2115
|
-
const d =
|
|
2116
|
-
return
|
|
2132
|
+
const d = parseDate(v);
|
|
2133
|
+
return d ? String(d.getDate()) : "";
|
|
2117
2134
|
},
|
|
2118
2135
|
day2: (v) => {
|
|
2119
|
-
const d =
|
|
2120
|
-
return
|
|
2136
|
+
const d = parseDate(v);
|
|
2137
|
+
return d ? String(d.getDate()).padStart(2, "0") : "";
|
|
2121
2138
|
},
|
|
2122
2139
|
year: (v) => {
|
|
2123
|
-
const d =
|
|
2124
|
-
return
|
|
2140
|
+
const d = parseDate(v);
|
|
2141
|
+
return d ? String(d.getFullYear()) : "";
|
|
2125
2142
|
},
|
|
2126
2143
|
year2: (v) => {
|
|
2127
|
-
const d =
|
|
2128
|
-
return
|
|
2144
|
+
const d = parseDate(v);
|
|
2145
|
+
return d ? String(d.getFullYear()).slice(-2) : "";
|
|
2129
2146
|
},
|
|
2130
2147
|
// String transforms
|
|
2131
2148
|
upper: (v) => v.toUpperCase(),
|
|
@@ -2154,15 +2171,27 @@ var BUILTIN_TRANSFORMS = {
|
|
|
2154
2171
|
};
|
|
2155
2172
|
var FORMULA_RE = /\{\{(.+?)\}\}/g;
|
|
2156
2173
|
var PIPE_RE = /^(.+?)\s*\|\s*(.+)$/;
|
|
2157
|
-
function
|
|
2174
|
+
function todayIso() {
|
|
2175
|
+
const d = /* @__PURE__ */ new Date();
|
|
2176
|
+
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}-${String(d.getDate()).padStart(2, "0")}`;
|
|
2177
|
+
}
|
|
2178
|
+
function resolveFormula(formula, fields, customTransforms, values) {
|
|
2158
2179
|
const transforms = { ...BUILTIN_TRANSFORMS, ...customTransforms };
|
|
2159
2180
|
return formula.replace(FORMULA_RE, (_match, expr) => {
|
|
2160
2181
|
const pipeMatch = expr.match(PIPE_RE);
|
|
2161
2182
|
const sourceLabel = (pipeMatch ? pipeMatch[1] : expr).trim();
|
|
2162
2183
|
const transformName = pipeMatch ? pipeMatch[2].trim() : null;
|
|
2163
|
-
const
|
|
2164
|
-
|
|
2165
|
-
|
|
2184
|
+
const lowerLabel = sourceLabel.toLowerCase();
|
|
2185
|
+
const sourceField = fields.find((f) => f.label.toLowerCase() === lowerLabel) || fields.find((f) => f.id === sourceLabel);
|
|
2186
|
+
let rawValue = sourceField ? sourceField.value || "" : void 0;
|
|
2187
|
+
if (rawValue === void 0 && values) {
|
|
2188
|
+
const key = Object.keys(values).find((k) => k.toLowerCase() === lowerLabel);
|
|
2189
|
+
if (key !== void 0) rawValue = values[key] || "";
|
|
2190
|
+
}
|
|
2191
|
+
if (rawValue === void 0 && (lowerLabel === "today" || lowerLabel === "now")) {
|
|
2192
|
+
rawValue = todayIso();
|
|
2193
|
+
}
|
|
2194
|
+
if (rawValue === void 0) return "";
|
|
2166
2195
|
if (!transformName) return rawValue;
|
|
2167
2196
|
const fn = transforms[transformName];
|
|
2168
2197
|
if (!fn) return rawValue;
|
|
@@ -2173,10 +2202,10 @@ function resolveFormula(formula, fields, customTransforms) {
|
|
|
2173
2202
|
}
|
|
2174
2203
|
});
|
|
2175
2204
|
}
|
|
2176
|
-
function resolveAllFormulas(fields, customTransforms) {
|
|
2205
|
+
function resolveAllFormulas(fields, customTransforms, values) {
|
|
2177
2206
|
return fields.map((f) => {
|
|
2178
2207
|
if (!f.formula) return f;
|
|
2179
|
-
const computed = resolveFormula(f.formula, fields, customTransforms);
|
|
2208
|
+
const computed = resolveFormula(f.formula, fields, customTransforms, values);
|
|
2180
2209
|
return computed !== f.value ? { ...f, value: computed } : f;
|
|
2181
2210
|
});
|
|
2182
2211
|
}
|
|
@@ -2383,7 +2412,7 @@ async function createPdfBuilder(defaults = {}) {
|
|
|
2383
2412
|
doc,
|
|
2384
2413
|
async addRecord(opts) {
|
|
2385
2414
|
const pageSize = opts.pageSize ?? defaults.pageSize;
|
|
2386
|
-
let fields = opts.resolveFormulas ? resolveAllFormulas(opts.fields, opts.customTransforms) : opts.fields;
|
|
2415
|
+
let fields = opts.resolveFormulas ? resolveAllFormulas(opts.fields, opts.customTransforms, opts.formulaValues) : opts.fields;
|
|
2387
2416
|
if (opts.calibration) {
|
|
2388
2417
|
fields = applyCalibration(
|
|
2389
2418
|
fields,
|
|
@@ -3105,6 +3134,7 @@ export {
|
|
|
3105
3134
|
isRedactField,
|
|
3106
3135
|
isSignatureField,
|
|
3107
3136
|
isTextLikeField,
|
|
3137
|
+
parseDate,
|
|
3108
3138
|
postPdfToCallback,
|
|
3109
3139
|
preserveFieldValues,
|
|
3110
3140
|
renderPdfPages,
|