identityscope 0.0.0-stage → 0.28.4
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/LICENSE +21 -0
- package/README.md +916 -2
- package/SECURITY.md +56 -0
- package/dist/core/audit-readiness.d.ts +158 -0
- package/dist/core/audit-readiness.js +357 -0
- package/dist/core/audit-readiness.js.map +1 -0
- package/dist/core/certs.d.ts +68 -0
- package/dist/core/certs.js +132 -0
- package/dist/core/certs.js.map +1 -0
- package/dist/core/checks.d.ts +133 -0
- package/dist/core/checks.js +512 -0
- package/dist/core/checks.js.map +1 -0
- package/dist/core/cloud-rules.d.ts +20 -0
- package/dist/core/cloud-rules.js +44 -0
- package/dist/core/cloud-rules.js.map +1 -0
- package/dist/core/compare.d.ts +45 -0
- package/dist/core/compare.js +372 -0
- package/dist/core/compare.js.map +1 -0
- package/dist/core/conditions.d.ts +10 -0
- package/dist/core/conditions.js +201 -0
- package/dist/core/conditions.js.map +1 -0
- package/dist/core/credentials.d.ts +71 -0
- package/dist/core/credentials.js +211 -0
- package/dist/core/credentials.js.map +1 -0
- package/dist/core/datasets.d.ts +53 -0
- package/dist/core/datasets.js +596 -0
- package/dist/core/datasets.js.map +1 -0
- package/dist/core/definition.d.ts +32 -0
- package/dist/core/definition.js +301 -0
- package/dist/core/definition.js.map +1 -0
- package/dist/core/discover.d.ts +32 -0
- package/dist/core/discover.js +105 -0
- package/dist/core/discover.js.map +1 -0
- package/dist/core/engine.d.ts +18 -0
- package/dist/core/engine.js +290 -0
- package/dist/core/engine.js.map +1 -0
- package/dist/core/fields.d.ts +16 -0
- package/dist/core/fields.js +132 -0
- package/dist/core/fields.js.map +1 -0
- package/dist/core/governance-trail.d.ts +211 -0
- package/dist/core/governance-trail.js +557 -0
- package/dist/core/governance-trail.js.map +1 -0
- package/dist/core/http-transport.d.ts +66 -0
- package/dist/core/http-transport.js +247 -0
- package/dist/core/http-transport.js.map +1 -0
- package/dist/core/index.d.ts +13 -0
- package/dist/core/index.js +16 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/insights.d.ts +176 -0
- package/dist/core/insights.js +417 -0
- package/dist/core/insights.js.map +1 -0
- package/dist/core/joiner-evidence.d.ts +98 -0
- package/dist/core/joiner-evidence.js +254 -0
- package/dist/core/joiner-evidence.js.map +1 -0
- package/dist/core/lab/credential-map.d.ts +50 -0
- package/dist/core/lab/credential-map.js +163 -0
- package/dist/core/lab/credential-map.js.map +1 -0
- package/dist/core/lab/javadate.d.ts +7 -0
- package/dist/core/lab/javadate.js +307 -0
- package/dist/core/lab/javadate.js.map +1 -0
- package/dist/core/lab/jsonpath.d.ts +11 -0
- package/dist/core/lab/jsonpath.js +273 -0
- package/dist/core/lab/jsonpath.js.map +1 -0
- package/dist/core/lab/policy-preview.d.ts +43 -0
- package/dist/core/lab/policy-preview.js +78 -0
- package/dist/core/lab/policy-preview.js.map +1 -0
- package/dist/core/lab/rfc5646.d.ts +2 -0
- package/dist/core/lab/rfc5646.js +421 -0
- package/dist/core/lab/rfc5646.js.map +1 -0
- package/dist/core/lab/transform-impact.d.ts +63 -0
- package/dist/core/lab/transform-impact.js +138 -0
- package/dist/core/lab/transform-impact.js.map +1 -0
- package/dist/core/lab/transform-people.d.ts +61 -0
- package/dist/core/lab/transform-people.js +132 -0
- package/dist/core/lab/transform-people.js.map +1 -0
- package/dist/core/lab/transforms.d.ts +79 -0
- package/dist/core/lab/transforms.js +738 -0
- package/dist/core/lab/transforms.js.map +1 -0
- package/dist/core/lab/velocity.d.ts +33 -0
- package/dist/core/lab/velocity.js +874 -0
- package/dist/core/lab/velocity.js.map +1 -0
- package/dist/core/lab/workflow-health.d.ts +80 -0
- package/dist/core/lab/workflow-health.js +234 -0
- package/dist/core/lab/workflow-health.js.map +1 -0
- package/dist/core/lab/workflows.d.ts +87 -0
- package/dist/core/lab/workflows.js +568 -0
- package/dist/core/lab/workflows.js.map +1 -0
- package/dist/core/lifecycle-gaps.d.ts +192 -0
- package/dist/core/lifecycle-gaps.js +389 -0
- package/dist/core/lifecycle-gaps.js.map +1 -0
- package/dist/core/logs.d.ts +145 -0
- package/dist/core/logs.js +342 -0
- package/dist/core/logs.js.map +1 -0
- package/dist/core/lookup.d.ts +129 -0
- package/dist/core/lookup.js +289 -0
- package/dist/core/lookup.js.map +1 -0
- package/dist/core/model-insights.d.ts +247 -0
- package/dist/core/model-insights.js +474 -0
- package/dist/core/model-insights.js.map +1 -0
- package/dist/core/narrow.d.ts +23 -0
- package/dist/core/narrow.js +137 -0
- package/dist/core/narrow.js.map +1 -0
- package/dist/core/nerm.d.ts +154 -0
- package/dist/core/nerm.js +397 -0
- package/dist/core/nerm.js.map +1 -0
- package/dist/core/operations-watch.d.ts +232 -0
- package/dist/core/operations-watch.js +491 -0
- package/dist/core/operations-watch.js.map +1 -0
- package/dist/core/output/csv.d.ts +6 -0
- package/dist/core/output/csv.js +22 -0
- package/dist/core/output/csv.js.map +1 -0
- package/dist/core/output/html.d.ts +11 -0
- package/dist/core/output/html.js +66 -0
- package/dist/core/output/html.js.map +1 -0
- package/dist/core/output/xlsx.d.ts +3 -0
- package/dist/core/output/xlsx.js +130 -0
- package/dist/core/output/xlsx.js.map +1 -0
- package/dist/core/output/zip.d.ts +16 -0
- package/dist/core/output/zip.js +106 -0
- package/dist/core/output/zip.js.map +1 -0
- package/dist/core/ownership.d.ts +39 -0
- package/dist/core/ownership.js +100 -0
- package/dist/core/ownership.js.map +1 -0
- package/dist/core/patterns.d.ts +11 -0
- package/dist/core/patterns.js +31 -0
- package/dist/core/patterns.js.map +1 -0
- package/dist/core/preview.d.ts +96 -0
- package/dist/core/preview.js +173 -0
- package/dist/core/preview.js.map +1 -0
- package/dist/core/roles.d.ts +125 -0
- package/dist/core/roles.js +420 -0
- package/dist/core/roles.js.map +1 -0
- package/dist/core/tenant.d.ts +67 -0
- package/dist/core/tenant.js +194 -0
- package/dist/core/tenant.js.map +1 -0
- package/dist/core/threat-signals.d.ts +112 -0
- package/dist/core/threat-signals.js +250 -0
- package/dist/core/threat-signals.js.map +1 -0
- package/dist/core/threat-watch.d.ts +144 -0
- package/dist/core/threat-watch.js +282 -0
- package/dist/core/threat-watch.js.map +1 -0
- package/dist/core/transport.d.ts +82 -0
- package/dist/core/transport.js +312 -0
- package/dist/core/transport.js.map +1 -0
- package/dist/core/types.d.ts +137 -0
- package/dist/core/types.js +10 -0
- package/dist/core/types.js.map +1 -0
- package/dist/core/warnings.d.ts +236 -0
- package/dist/core/warnings.js +504 -0
- package/dist/core/warnings.js.map +1 -0
- package/dist/demo/tenant.d.ts +1572 -0
- package/dist/demo/tenant.js +1023 -0
- package/dist/demo/tenant.js.map +1 -0
- package/dist/node/attribute-history.d.ts +22 -0
- package/dist/node/attribute-history.js +86 -0
- package/dist/node/attribute-history.js.map +1 -0
- package/dist/node/changes.d.ts +20 -0
- package/dist/node/changes.js +78 -0
- package/dist/node/changes.js.map +1 -0
- package/dist/node/cli.d.ts +2 -0
- package/dist/node/cli.js +291 -0
- package/dist/node/cli.js.map +1 -0
- package/dist/node/config.d.ts +72 -0
- package/dist/node/config.js +203 -0
- package/dist/node/config.js.map +1 -0
- package/dist/node/credentials.d.ts +13 -0
- package/dist/node/credentials.js +24 -0
- package/dist/node/credentials.js.map +1 -0
- package/dist/node/delivery.d.ts +10 -0
- package/dist/node/delivery.js +105 -0
- package/dist/node/delivery.js.map +1 -0
- package/dist/node/doctor.d.ts +2 -0
- package/dist/node/doctor.js +151 -0
- package/dist/node/doctor.js.map +1 -0
- package/dist/node/drift.d.ts +31 -0
- package/dist/node/drift.js +56 -0
- package/dist/node/drift.js.map +1 -0
- package/dist/node/evidence.d.ts +42 -0
- package/dist/node/evidence.js +129 -0
- package/dist/node/evidence.js.map +1 -0
- package/dist/node/exceptions.d.ts +30 -0
- package/dist/node/exceptions.js +83 -0
- package/dist/node/exceptions.js.map +1 -0
- package/dist/node/history.d.ts +35 -0
- package/dist/node/history.js +74 -0
- package/dist/node/history.js.map +1 -0
- package/dist/node/index.d.ts +7 -0
- package/dist/node/index.js +8 -0
- package/dist/node/index.js.map +1 -0
- package/dist/node/keep.d.ts +9 -0
- package/dist/node/keep.js +39 -0
- package/dist/node/keep.js.map +1 -0
- package/dist/node/loader.d.ts +7 -0
- package/dist/node/loader.js +35 -0
- package/dist/node/loader.js.map +1 -0
- package/dist/node/log-files.d.ts +16 -0
- package/dist/node/log-files.js +93 -0
- package/dist/node/log-files.js.map +1 -0
- package/dist/node/monthly.d.ts +41 -0
- package/dist/node/monthly.js +108 -0
- package/dist/node/monthly.js.map +1 -0
- package/dist/node/oauth-login.d.ts +45 -0
- package/dist/node/oauth-login.js +121 -0
- package/dist/node/oauth-login.js.map +1 -0
- package/dist/node/packs.d.ts +32 -0
- package/dist/node/packs.js +43 -0
- package/dist/node/packs.js.map +1 -0
- package/dist/node/privileged-seen.d.ts +11 -0
- package/dist/node/privileged-seen.js +41 -0
- package/dist/node/privileged-seen.js.map +1 -0
- package/dist/node/recipients.d.ts +17 -0
- package/dist/node/recipients.js +51 -0
- package/dist/node/recipients.js.map +1 -0
- package/dist/node/runner.d.ts +51 -0
- package/dist/node/runner.js +298 -0
- package/dist/node/runner.js.map +1 -0
- package/dist/node/runtime.d.ts +12 -0
- package/dist/node/runtime.js +13 -0
- package/dist/node/runtime.js.map +1 -0
- package/dist/node/schedules.d.ts +87 -0
- package/dist/node/schedules.js +141 -0
- package/dist/node/schedules.js.map +1 -0
- package/dist/node/secrets.d.ts +37 -0
- package/dist/node/secrets.js +219 -0
- package/dist/node/secrets.js.map +1 -0
- package/dist/node/support.d.ts +14 -0
- package/dist/node/support.js +133 -0
- package/dist/node/support.js.map +1 -0
- package/dist/node/tenants.d.ts +93 -0
- package/dist/node/tenants.js +255 -0
- package/dist/node/tenants.js.map +1 -0
- package/dist/node/ui-page.d.ts +1 -0
- package/dist/node/ui-page.js +3 -0
- package/dist/node/ui-page.js.map +1 -0
- package/dist/node/ui-server.d.ts +48 -0
- package/dist/node/ui-server.js +2665 -0
- package/dist/node/ui-server.js.map +1 -0
- package/dist/node/write.d.ts +9 -0
- package/dist/node/write.js +53 -0
- package/dist/node/write.js.map +1 -0
- package/dist/reports/builtin.generated.d.ts +1 -0
- package/dist/reports/builtin.generated.js +49 -0
- package/dist/reports/builtin.generated.js.map +1 -0
- package/dist/reports/library.d.ts +6 -0
- package/dist/reports/library.js +18 -0
- package/dist/reports/library.js.map +1 -0
- package/launcher/IdentityScope.cmd +11 -0
- package/package.json +69 -4
- package/reports/access-profile-members.yaml +29 -0
- package/reports/access-profile-membership-counts.yaml +17 -0
- package/reports/access-profiles-without-owner.yaml +16 -0
- package/reports/access-request-audit-trail.yaml +36 -0
- package/reports/accounts-by-source.yaml +19 -0
- package/reports/active-identities-without-manager.yaml +22 -0
- package/reports/aggregation-failures.yaml +29 -0
- package/reports/aggregation-history.yaml +27 -0
- package/reports/all-sources.yaml +16 -0
- package/reports/application-accounts.yaml +32 -0
- package/reports/application-entitlements.yaml +31 -0
- package/reports/campaign-progress.yaml +23 -0
- package/reports/certification-decisions.yaml +31 -0
- package/reports/direct-reports-per-manager.yaml +20 -0
- package/reports/disabled-accounts-of-active-people.yaml +33 -0
- package/reports/duplicate-emails.yaml +16 -0
- package/reports/entitlement-holders.yaml +35 -0
- package/reports/entitlements-held-by-inactive-identities.yaml +31 -0
- package/reports/entitlements-without-owner.yaml +28 -0
- package/reports/identities-by-lifecycle-state.yaml +15 -0
- package/reports/identities-missing-key-attributes.yaml +27 -0
- package/reports/identities-with-processing-errors.yaml +19 -0
- package/reports/identity-attribute-mappings.yaml +27 -0
- package/reports/machine-accounts.yaml +28 -0
- package/reports/managers-and-direct-reports.yaml +23 -0
- package/reports/most-access.yaml +18 -0
- package/reports/new-identities.yaml +24 -0
- package/reports/old-passwords.yaml +35 -0
- package/reports/pending-reviews-by-reviewer.yaml +20 -0
- package/reports/person-access.yaml +31 -0
- package/reports/privileged-access.yaml +29 -0
- package/reports/provisioning-error-causes.yaml +27 -0
- package/reports/provisioning-errors.yaml +32 -0
- package/reports/provisioning-failures.yaml +28 -0
- package/reports/recent-access-requests.yaml +22 -0
- package/reports/recent-activity.yaml +32 -0
- package/reports/recent-leavers.yaml +34 -0
- package/reports/role-members.yaml +29 -0
- package/reports/role-membership-counts.yaml +18 -0
- package/reports/roles-without-owner.yaml +17 -0
- package/reports/sources-needing-attention.yaml +23 -0
- package/reports/stuck-access-requests.yaml +29 -0
- package/reports/team-access.yaml +30 -0
- package/reports/terminated-users-with-active-accounts.yaml +46 -0
- package/reports/uncorrelated-accounts.yaml +38 -0
- package/ui/js/00-setup.js +6 -0
- package/ui/js/01-small-helpers.js +143 -0
- package/ui/js/02-connection.js +150 -0
- package/ui/js/03-health-check.js +17 -0
- package/ui/js/04-library.js +135 -0
- package/ui/js/05-pickers.js +163 -0
- package/ui/js/06-report-view.js +157 -0
- package/ui/js/07-fields.js +85 -0
- package/ui/js/08-designer.js +360 -0
- package/ui/js/09-yaml.js +22 -0
- package/ui/js/10-instant-check.js +171 -0
- package/ui/js/11-lookup.js +161 -0
- package/ui/js/12-tenant.js +115 -0
- package/ui/js/13-source-configuration-and-diagnostics.js +64 -0
- package/ui/js/14-certifications.js +68 -0
- package/ui/js/15-monthly-report.js +24 -0
- package/ui/js/16-ownership.js +39 -0
- package/ui/js/17-compare-tenants.js +102 -0
- package/ui/js/18-audit.js +181 -0
- package/ui/js/19-insights.js +108 -0
- package/ui/js/20-role-mining.js +35 -0
- package/ui/js/21-share-reports.js +45 -0
- package/ui/js/22-partner-overview-all-customer-tenants.js +228 -0
- package/ui/js/23-in-app-guidance.js +217 -0
- package/ui/js/24-role-tools.js +220 -0
- package/ui/js/25-schedules.js +264 -0
- package/ui/js/26-start.js +7 -0
- package/ui/js/27-warnings.js +224 -0
- package/ui/js/28-audit-readiness.js +99 -0
- package/ui/js/29-lifecycle.js +182 -0
- package/ui/js/30-labs.js +265 -0
- package/ui/js/31-build.js +203 -0
- package/ui/js/32-lab-examples.js +148 -0
- package/ui/js/33-finder.js +89 -0
- package/ui/js/34-governance.js +129 -0
- package/ui/js/35-watch.js +266 -0
- package/ui/js/36-model.js +247 -0
- package/ui/js/37-logs-and-watch.js +224 -0
- package/ui/shell.html +112 -0
- package/ui/style.css +350 -0
|
@@ -0,0 +1,874 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A subset of the Apache Velocity Template Language, as ISC transforms use it:
|
|
3
|
+
* references ($a, ${a}, $!a, $a.b, $a.method(args), $a[0]), #set, #if/#elseif/#else/#end,
|
|
4
|
+
* comments, Java String methods, and the tools ISC puts in every transform's context
|
|
5
|
+
* ($__dateTool, $__mathTool, $__numberTool, $__esc). Follows Velocity 2 defaults: an
|
|
6
|
+
* undefined reference renders as its own text ($!ref renders nothing), and #if treats
|
|
7
|
+
* null, false, empty strings and empty lists as false.
|
|
8
|
+
*
|
|
9
|
+
* Anything this subset cannot do (#foreach, #macro, ...) raises an UnsupportedError so the
|
|
10
|
+
* lab can say "only ISC can evaluate this" instead of guessing.
|
|
11
|
+
*/
|
|
12
|
+
import { formatJavaDate, parseJavaDate } from './javadate.js';
|
|
13
|
+
export class UnsupportedError extends Error {
|
|
14
|
+
constructor(msg) {
|
|
15
|
+
super(msg);
|
|
16
|
+
this.name = 'UnsupportedError';
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export class TemplateError extends Error {
|
|
20
|
+
constructor(msg) {
|
|
21
|
+
super(msg);
|
|
22
|
+
this.name = 'TemplateError';
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const UNDEF = Symbol('undefined');
|
|
26
|
+
// ---------------------------------------------------------------- tools
|
|
27
|
+
const javaDate = (v) => (v instanceof Date ? v : typeof v === 'number' ? new Date(v) : null);
|
|
28
|
+
function num(v) {
|
|
29
|
+
if (typeof v === 'number')
|
|
30
|
+
return v;
|
|
31
|
+
if (typeof v === 'string' && v.trim() !== '' && !Number.isNaN(Number(v)))
|
|
32
|
+
return Number(v);
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
/** Velocity tools as documented for ISC transforms (GMT, en_US). */
|
|
36
|
+
export function tools(now) {
|
|
37
|
+
const dateTool = {
|
|
38
|
+
__tool: 'date',
|
|
39
|
+
date: now,
|
|
40
|
+
getDate: () => now,
|
|
41
|
+
getSystemTime: () => now.getTime(),
|
|
42
|
+
getSystemDate: () => now,
|
|
43
|
+
format: (a, b) => {
|
|
44
|
+
// format(date) | format(pattern, date)
|
|
45
|
+
if (b === undefined)
|
|
46
|
+
return javaDate(a) ? formatJavaDate(javaDate(a), "yyyy-MM-dd'T'HH:mm:ss.SSSZ") : null;
|
|
47
|
+
const d = javaDate(b) ?? (typeof b === 'string' ? parseIsoLoose(b) : null);
|
|
48
|
+
return d ? formatJavaDate(d, String(a)) : null;
|
|
49
|
+
},
|
|
50
|
+
toDate: (a, b) => (b === undefined ? (typeof a === 'string' ? parseIsoLoose(a) : javaDate(a)) : typeof b === 'string' ? parseJavaDate(b, String(a)) : javaDate(b)),
|
|
51
|
+
getYear: (d) => (javaDate(d) ?? now).getUTCFullYear(),
|
|
52
|
+
getMonth: (d) => (javaDate(d) ?? now).getUTCMonth(),
|
|
53
|
+
getDay: (d) => (javaDate(d) ?? now).getUTCDate(),
|
|
54
|
+
};
|
|
55
|
+
const mathTool = {
|
|
56
|
+
__tool: 'math',
|
|
57
|
+
add: (...xs) => arith(xs, (a, b) => a + b),
|
|
58
|
+
sub: (...xs) => arith(xs, (a, b) => a - b),
|
|
59
|
+
mul: (...xs) => arith(xs, (a, b) => a * b),
|
|
60
|
+
div: (...xs) => arith(xs, (a, b) => a / b, true),
|
|
61
|
+
mod: (a, b) => arith([a, b], (x, y) => x % y),
|
|
62
|
+
max: (...xs) => arith(xs, (a, b) => Math.max(a, b)),
|
|
63
|
+
min: (...xs) => arith(xs, (a, b) => Math.min(a, b)),
|
|
64
|
+
abs: (a) => (num(a) === null ? null : Math.abs(num(a))),
|
|
65
|
+
ceil: (a) => (num(a) === null ? null : Math.ceil(num(a))),
|
|
66
|
+
floor: (a) => (num(a) === null ? null : Math.floor(num(a))),
|
|
67
|
+
round: (a) => (num(a) === null ? null : Math.round(num(a))),
|
|
68
|
+
roundTo: (d, a) => (num(a) === null || num(d) === null ? null : Number(num(a).toFixed(num(d)))),
|
|
69
|
+
toNumber: (a) => num(a),
|
|
70
|
+
toInteger: (a) => (num(a) === null ? null : Math.trunc(num(a))),
|
|
71
|
+
getRandom: () => Math.random(),
|
|
72
|
+
};
|
|
73
|
+
const numberTool = {
|
|
74
|
+
__tool: 'number',
|
|
75
|
+
format: (pattern, value) => formatNumber(String(pattern), num(value)),
|
|
76
|
+
toNumber: (a) => num(a),
|
|
77
|
+
integer: (a) => (num(a) === null ? null : String(Math.trunc(num(a)))),
|
|
78
|
+
};
|
|
79
|
+
const esc = {
|
|
80
|
+
__tool: 'esc',
|
|
81
|
+
url: (s) => (s === null || s === undefined ? null : encodeURIComponent(String(s)).replace(/%20/g, '+').replace(/[!'()*]/g, (c) => '%' + c.charCodeAt(0).toString(16).toUpperCase())),
|
|
82
|
+
html: (s) => (s === null || s === undefined ? null : String(s).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"')),
|
|
83
|
+
xml: (s) => (s === null || s === undefined ? null : String(s).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''')),
|
|
84
|
+
javascript: (s) => (s === null || s === undefined ? null : JSON.stringify(String(s)).slice(1, -1).replace(/'/g, "\\'")),
|
|
85
|
+
java: (s) => (s === null || s === undefined ? null : JSON.stringify(String(s)).slice(1, -1)),
|
|
86
|
+
};
|
|
87
|
+
return { __dateTool: dateTool, __mathTool: mathTool, __numberTool: numberTool, __esc: esc };
|
|
88
|
+
}
|
|
89
|
+
function arith(xs, f, div = false) {
|
|
90
|
+
const ns = xs.map(num);
|
|
91
|
+
if (ns.some((n) => n === null) || !ns.length)
|
|
92
|
+
return null;
|
|
93
|
+
if (div && ns.slice(1).some((n) => n === 0))
|
|
94
|
+
return null;
|
|
95
|
+
const r = ns.reduce(f);
|
|
96
|
+
const allInt = ns.every(Number.isInteger) && !div;
|
|
97
|
+
return allInt ? Math.trunc(r) : r;
|
|
98
|
+
}
|
|
99
|
+
/** java.text.DecimalFormat subset: 0, #, grouping, decimals, and the named "integer"/"currency"/"percent". */
|
|
100
|
+
export function formatNumber(pattern, n) {
|
|
101
|
+
if (n === null)
|
|
102
|
+
return null;
|
|
103
|
+
if (pattern === 'integer')
|
|
104
|
+
return String(Math.round(n));
|
|
105
|
+
if (pattern === 'number')
|
|
106
|
+
return n.toLocaleString('en-US', { maximumFractionDigits: 3 });
|
|
107
|
+
if (pattern === 'currency')
|
|
108
|
+
return n.toLocaleString('en-US', { style: 'currency', currency: 'USD' });
|
|
109
|
+
if (pattern === 'percent')
|
|
110
|
+
return Math.round(n * 100) + '%';
|
|
111
|
+
const [ip, fp = ''] = pattern.split('.');
|
|
112
|
+
const minInt = (ip.match(/0/g) ?? []).length;
|
|
113
|
+
const minFrac = (fp.match(/0/g) ?? []).length;
|
|
114
|
+
const maxFrac = fp.replace(/[^0#]/g, '').length;
|
|
115
|
+
const grouping = ip.includes(',');
|
|
116
|
+
const s = Math.abs(n).toFixed(maxFrac);
|
|
117
|
+
let [i, f = ''] = s.split('.');
|
|
118
|
+
while (f.length > minFrac && f.endsWith('0'))
|
|
119
|
+
f = f.slice(0, -1);
|
|
120
|
+
i = i.padStart(minInt, '0');
|
|
121
|
+
if (grouping)
|
|
122
|
+
i = i.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
|
123
|
+
return (n < 0 ? '-' : '') + i + (f ? '.' + f : '');
|
|
124
|
+
}
|
|
125
|
+
export function parseIsoLoose(s) {
|
|
126
|
+
const t = Date.parse(s.replace(/([+-]\d{2})(\d{2})$/, '$1:$2'));
|
|
127
|
+
return Number.isFinite(t) ? new Date(t) : null;
|
|
128
|
+
}
|
|
129
|
+
// ---------------------------------------------------------------- Java String methods
|
|
130
|
+
function javaRegex(re, global = true) {
|
|
131
|
+
let flags = global ? 'g' : '';
|
|
132
|
+
let src = re;
|
|
133
|
+
const m = /^\(\?([imsx]+)\)/.exec(src);
|
|
134
|
+
if (m) {
|
|
135
|
+
src = src.slice(m[0].length);
|
|
136
|
+
if (m[1].includes('i'))
|
|
137
|
+
flags += 'i';
|
|
138
|
+
if (m[1].includes('s'))
|
|
139
|
+
flags += 's';
|
|
140
|
+
if (m[1].includes('m'))
|
|
141
|
+
flags += 'm';
|
|
142
|
+
}
|
|
143
|
+
src = src.replace(/\\p\{InCombiningDiacriticalMarks\}/g, '\\u0300-\\u036f').replace(/\\p\{Alpha\}/g, 'A-Za-z').replace(/\\p\{Punct\}/g, '!-\\/:-@\\[-`{-~');
|
|
144
|
+
if (/\\[pP]\{/.test(src))
|
|
145
|
+
flags += 'u';
|
|
146
|
+
if (/\(\?[<]?[=!]?[a-zA-Z]/.test(src) && !/\(\?<[a-zA-Z]/.test(src))
|
|
147
|
+
throw new UnsupportedError(`regular expression feature not available offline: ${re}`);
|
|
148
|
+
return new RegExp(src, flags);
|
|
149
|
+
}
|
|
150
|
+
export { javaRegex };
|
|
151
|
+
/** Java replacement strings use $1 and \\$; JavaScript uses $1 and $$. */
|
|
152
|
+
export const javaReplacement = (r) => r.replace(/\\\$/g, '$$$$').replace(/\\\\/g, '\\');
|
|
153
|
+
/** java.lang.String.split: regex delimiter, trailing empty strings removed. */
|
|
154
|
+
export function javaSplit(s, delim) {
|
|
155
|
+
const single = delim.length === 1 && !'.$|()[{^?*+\\'.includes(delim);
|
|
156
|
+
const parts = single ? s.split(delim) : s.split(javaRegex(delim, false).global ? javaRegex(delim) : new RegExp(javaRegex(delim, false).source, javaRegex(delim, false).flags));
|
|
157
|
+
if (parts.length > 1 && parts[0] === '' && !single) {
|
|
158
|
+
// Java removes a leading empty string only for a zero-width match at the start.
|
|
159
|
+
const re = javaRegex(delim, false);
|
|
160
|
+
const m = re.exec(s);
|
|
161
|
+
if (m && m.index === 0 && m[0] === '')
|
|
162
|
+
parts.shift();
|
|
163
|
+
}
|
|
164
|
+
while (parts.length > 1 && parts[parts.length - 1] === '')
|
|
165
|
+
parts.pop();
|
|
166
|
+
if (parts.length === 1 && parts[0] === '' && s === '')
|
|
167
|
+
return [''];
|
|
168
|
+
return parts;
|
|
169
|
+
}
|
|
170
|
+
function stringMethod(s, name, args) {
|
|
171
|
+
const a = args;
|
|
172
|
+
const str = (i) => (a[i] === null || a[i] === undefined ? 'null' : String(a[i]));
|
|
173
|
+
const int = (i) => {
|
|
174
|
+
const n = num(a[i]);
|
|
175
|
+
if (n === null)
|
|
176
|
+
throw new TemplateError(`${name}: argument ${i + 1} is not a number`);
|
|
177
|
+
return Math.trunc(n);
|
|
178
|
+
};
|
|
179
|
+
switch (name) {
|
|
180
|
+
case 'length':
|
|
181
|
+
return s.length;
|
|
182
|
+
case 'toLowerCase':
|
|
183
|
+
return s.toLowerCase();
|
|
184
|
+
case 'toUpperCase':
|
|
185
|
+
return s.toUpperCase();
|
|
186
|
+
case 'trim':
|
|
187
|
+
return s.replace(/^[\u0000- ]+|[\u0000- ]+$/g, '');
|
|
188
|
+
case 'strip':
|
|
189
|
+
return s.trim();
|
|
190
|
+
case 'isEmpty':
|
|
191
|
+
return s.length === 0;
|
|
192
|
+
case 'isBlank':
|
|
193
|
+
return s.trim().length === 0;
|
|
194
|
+
case 'substring': {
|
|
195
|
+
const b = int(0);
|
|
196
|
+
const e = a.length > 1 ? int(1) : s.length;
|
|
197
|
+
if (b < 0 || e > s.length || b > e)
|
|
198
|
+
throw new TemplateError(`StringIndexOutOfBoundsException: begin ${b}, end ${e}, length ${s.length}`);
|
|
199
|
+
return s.substring(b, e);
|
|
200
|
+
}
|
|
201
|
+
case 'charAt': {
|
|
202
|
+
const i = int(0);
|
|
203
|
+
if (i < 0 || i >= s.length)
|
|
204
|
+
throw new TemplateError(`StringIndexOutOfBoundsException: index ${i}, length ${s.length}`);
|
|
205
|
+
return s.charAt(i);
|
|
206
|
+
}
|
|
207
|
+
case 'indexOf':
|
|
208
|
+
return a.length > 1 ? s.indexOf(str(0), int(1)) : s.indexOf(str(0));
|
|
209
|
+
case 'lastIndexOf':
|
|
210
|
+
return s.lastIndexOf(str(0));
|
|
211
|
+
case 'contains':
|
|
212
|
+
return s.includes(str(0));
|
|
213
|
+
case 'startsWith':
|
|
214
|
+
return s.startsWith(str(0));
|
|
215
|
+
case 'endsWith':
|
|
216
|
+
return s.endsWith(str(0));
|
|
217
|
+
case 'equals':
|
|
218
|
+
return a[0] !== null && a[0] !== undefined && s === String(a[0]);
|
|
219
|
+
case 'equalsIgnoreCase':
|
|
220
|
+
return a[0] !== null && a[0] !== undefined && s.toLowerCase() === String(a[0]).toLowerCase();
|
|
221
|
+
case 'concat':
|
|
222
|
+
return s + str(0);
|
|
223
|
+
case 'replace':
|
|
224
|
+
return s.split(str(0)).join(str(1));
|
|
225
|
+
case 'replaceAll':
|
|
226
|
+
return s.replace(javaRegex(str(0)), javaReplacement(str(1)));
|
|
227
|
+
case 'replaceFirst':
|
|
228
|
+
return s.replace(javaRegex(str(0), false), javaReplacement(str(1)));
|
|
229
|
+
case 'matches':
|
|
230
|
+
return new RegExp(`^(?:${javaRegex(str(0), false).source})$`, javaRegex(str(0), false).flags).test(s);
|
|
231
|
+
case 'split':
|
|
232
|
+
return javaSplit(s, str(0));
|
|
233
|
+
case 'toString':
|
|
234
|
+
return s;
|
|
235
|
+
case 'hashCode':
|
|
236
|
+
return [...s].reduce((h, c) => (Math.imul(31, h) + c.charCodeAt(0)) | 0, 0);
|
|
237
|
+
case 'compareTo':
|
|
238
|
+
return s < str(0) ? -1 : s > str(0) ? 1 : 0;
|
|
239
|
+
default:
|
|
240
|
+
throw new UnsupportedError(`String.${name}() is not available offline`);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
function callMethod(target, name, args) {
|
|
244
|
+
if (target === null || target === undefined)
|
|
245
|
+
return UNDEF;
|
|
246
|
+
if (typeof target === 'string')
|
|
247
|
+
return stringMethod(target, name, args);
|
|
248
|
+
if (Array.isArray(target)) {
|
|
249
|
+
switch (name) {
|
|
250
|
+
case 'size':
|
|
251
|
+
return target.length;
|
|
252
|
+
case 'get': {
|
|
253
|
+
const i = num(args[0]);
|
|
254
|
+
if (i === null || i < 0 || i >= target.length)
|
|
255
|
+
throw new TemplateError(`IndexOutOfBoundsException: ${args[0]}`);
|
|
256
|
+
return target[i];
|
|
257
|
+
}
|
|
258
|
+
case 'isEmpty':
|
|
259
|
+
return target.length === 0;
|
|
260
|
+
case 'contains':
|
|
261
|
+
return target.some((x) => String(x) === String(args[0]));
|
|
262
|
+
case 'toString':
|
|
263
|
+
return `[${target.join(', ')}]`;
|
|
264
|
+
default:
|
|
265
|
+
throw new UnsupportedError(`List.${name}() is not available offline`);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
if (typeof target === 'number') {
|
|
269
|
+
if (name === 'toString')
|
|
270
|
+
return String(target);
|
|
271
|
+
if (name === 'intValue')
|
|
272
|
+
return Math.trunc(target);
|
|
273
|
+
throw new UnsupportedError(`Number.${name}() is not available offline`);
|
|
274
|
+
}
|
|
275
|
+
if (typeof target === 'object') {
|
|
276
|
+
const o = target;
|
|
277
|
+
const f = o[name];
|
|
278
|
+
if (typeof f === 'function')
|
|
279
|
+
return f(...args);
|
|
280
|
+
if (name === 'get' && args.length === 1)
|
|
281
|
+
return o[String(args[0])] ?? null;
|
|
282
|
+
if (name === 'getAttribute' && args.length === 1 && o['__attributes'])
|
|
283
|
+
return o['__attributes'][String(args[0])] ?? null;
|
|
284
|
+
if (/^get[A-Z]/.test(name) && !args.length) {
|
|
285
|
+
const p = name[3].toLowerCase() + name.slice(4);
|
|
286
|
+
if (p in o)
|
|
287
|
+
return o[p];
|
|
288
|
+
}
|
|
289
|
+
if (/^is[A-Z]/.test(name) && !args.length) {
|
|
290
|
+
const p = name[2].toLowerCase() + name.slice(3);
|
|
291
|
+
if (p in o)
|
|
292
|
+
return o[p];
|
|
293
|
+
}
|
|
294
|
+
throw new UnsupportedError(`.${name}() on ${o['__tool'] ? '$__' + o['__tool'] + 'Tool' : 'this object'} is not available offline`);
|
|
295
|
+
}
|
|
296
|
+
if (typeof target === 'boolean' && name === 'toString')
|
|
297
|
+
return String(target);
|
|
298
|
+
throw new UnsupportedError(`.${name}() is not available offline`);
|
|
299
|
+
}
|
|
300
|
+
function property(target, name) {
|
|
301
|
+
if (target === null || target === undefined)
|
|
302
|
+
return UNDEF;
|
|
303
|
+
if (typeof target === 'object' && !Array.isArray(target)) {
|
|
304
|
+
const o = target;
|
|
305
|
+
if (name in o && typeof o[name] !== 'function')
|
|
306
|
+
return o[name];
|
|
307
|
+
const getter = 'get' + name[0].toUpperCase() + name.slice(1);
|
|
308
|
+
if (typeof o[getter] === 'function')
|
|
309
|
+
return o[getter]();
|
|
310
|
+
if (o['__attributes'] && name in o['__attributes'])
|
|
311
|
+
return o['__attributes'][name];
|
|
312
|
+
return UNDEF;
|
|
313
|
+
}
|
|
314
|
+
if (typeof target === 'string' && (name === 'length' || name === 'empty'))
|
|
315
|
+
return name === 'length' ? UNDEF : target.length === 0;
|
|
316
|
+
if (Array.isArray(target) && name === 'size')
|
|
317
|
+
return UNDEF;
|
|
318
|
+
return UNDEF;
|
|
319
|
+
}
|
|
320
|
+
function lexExpr(src) {
|
|
321
|
+
const out = [];
|
|
322
|
+
let i = 0;
|
|
323
|
+
while (i < src.length) {
|
|
324
|
+
const c = src[i];
|
|
325
|
+
if (/\s/.test(c)) {
|
|
326
|
+
i++;
|
|
327
|
+
continue;
|
|
328
|
+
}
|
|
329
|
+
if (c === '"' || c === "'") {
|
|
330
|
+
let j = i + 1;
|
|
331
|
+
let s = '';
|
|
332
|
+
while (j < src.length && src[j] !== c) {
|
|
333
|
+
if (src[j] === '\\' && src[j + 1] === c) {
|
|
334
|
+
s += c;
|
|
335
|
+
j += 2;
|
|
336
|
+
continue;
|
|
337
|
+
}
|
|
338
|
+
s += src[j];
|
|
339
|
+
j++;
|
|
340
|
+
}
|
|
341
|
+
if (j >= src.length)
|
|
342
|
+
throw new TemplateError('unclosed string');
|
|
343
|
+
out.push({ t: c === '"' ? 'istr' : 'str', v: s });
|
|
344
|
+
i = j + 1;
|
|
345
|
+
continue;
|
|
346
|
+
}
|
|
347
|
+
if (/[0-9]/.test(c) || (c === '-' && /[0-9]/.test(src[i + 1] ?? '') && (!out.length || ['op', 'lp', 'comma', 'lb'].includes(out[out.length - 1].t)))) {
|
|
348
|
+
const m = /^-?[0-9]+(\.[0-9]+)?/.exec(src.slice(i));
|
|
349
|
+
out.push({ t: 'num', v: m[0] });
|
|
350
|
+
i += m[0].length;
|
|
351
|
+
continue;
|
|
352
|
+
}
|
|
353
|
+
if (c === '$') {
|
|
354
|
+
const r = readRef(src, i);
|
|
355
|
+
if (!r)
|
|
356
|
+
throw new TemplateError(`bad reference at "${src.slice(i, i + 10)}"`);
|
|
357
|
+
out.push({ t: 'ref', v: src.slice(i, r.end) });
|
|
358
|
+
i = r.end;
|
|
359
|
+
continue;
|
|
360
|
+
}
|
|
361
|
+
const two = src.slice(i, i + 2);
|
|
362
|
+
if (['==', '!=', '<=', '>=', '&&', '||'].includes(two)) {
|
|
363
|
+
out.push({ t: 'op', v: two });
|
|
364
|
+
i += 2;
|
|
365
|
+
continue;
|
|
366
|
+
}
|
|
367
|
+
if ('<>!+-*/%='.includes(c)) {
|
|
368
|
+
out.push({ t: 'op', v: c });
|
|
369
|
+
i++;
|
|
370
|
+
continue;
|
|
371
|
+
}
|
|
372
|
+
if (c === '(')
|
|
373
|
+
out.push({ t: 'lp', v: c });
|
|
374
|
+
else if (c === ')')
|
|
375
|
+
out.push({ t: 'rp', v: c });
|
|
376
|
+
else if (c === '[')
|
|
377
|
+
out.push({ t: 'lb', v: c });
|
|
378
|
+
else if (c === ']')
|
|
379
|
+
out.push({ t: 'rb', v: c });
|
|
380
|
+
else if (c === ',')
|
|
381
|
+
out.push({ t: 'comma', v: c });
|
|
382
|
+
else if (/[A-Za-z_]/.test(c)) {
|
|
383
|
+
const m = /^[A-Za-z_][A-Za-z0-9_]*/.exec(src.slice(i));
|
|
384
|
+
out.push({ t: 'word', v: m[0] });
|
|
385
|
+
i += m[0].length;
|
|
386
|
+
continue;
|
|
387
|
+
}
|
|
388
|
+
else if (c === '.' && src[i + 1] === '.') {
|
|
389
|
+
out.push({ t: 'op', v: '..' });
|
|
390
|
+
i += 2;
|
|
391
|
+
continue;
|
|
392
|
+
}
|
|
393
|
+
else
|
|
394
|
+
throw new TemplateError(`unexpected "${c}"`);
|
|
395
|
+
i++;
|
|
396
|
+
}
|
|
397
|
+
return out;
|
|
398
|
+
}
|
|
399
|
+
const WORD_OPS = { eq: '==', ne: '!=', lt: '<', gt: '>', le: '<=', ge: '>=', and: '&&', or: '||', not: '!' };
|
|
400
|
+
class ExprParser {
|
|
401
|
+
toks;
|
|
402
|
+
ev;
|
|
403
|
+
p = 0;
|
|
404
|
+
constructor(toks, ev) {
|
|
405
|
+
this.toks = toks;
|
|
406
|
+
this.ev = ev;
|
|
407
|
+
}
|
|
408
|
+
done() {
|
|
409
|
+
return this.p >= this.toks.length;
|
|
410
|
+
}
|
|
411
|
+
peek() {
|
|
412
|
+
const t = this.toks[this.p];
|
|
413
|
+
return t && t.t === 'word' && WORD_OPS[t.v] ? { t: 'op', v: WORD_OPS[t.v] } : t;
|
|
414
|
+
}
|
|
415
|
+
next() {
|
|
416
|
+
const t = this.peek();
|
|
417
|
+
this.p++;
|
|
418
|
+
return t;
|
|
419
|
+
}
|
|
420
|
+
parse() {
|
|
421
|
+
const v = this.or();
|
|
422
|
+
return v === UNDEF ? null : v;
|
|
423
|
+
}
|
|
424
|
+
or() {
|
|
425
|
+
let a = this.and();
|
|
426
|
+
while (this.peek()?.v === '||') {
|
|
427
|
+
this.next();
|
|
428
|
+
const b = this.and();
|
|
429
|
+
a = truthy(a) || truthy(b);
|
|
430
|
+
}
|
|
431
|
+
return a;
|
|
432
|
+
}
|
|
433
|
+
and() {
|
|
434
|
+
let a = this.cmp();
|
|
435
|
+
while (this.peek()?.v === '&&') {
|
|
436
|
+
this.next();
|
|
437
|
+
const b = this.cmp();
|
|
438
|
+
a = truthy(a) && truthy(b);
|
|
439
|
+
}
|
|
440
|
+
return a;
|
|
441
|
+
}
|
|
442
|
+
cmp() {
|
|
443
|
+
const a = this.add();
|
|
444
|
+
const op = this.peek();
|
|
445
|
+
if (op && op.t === 'op' && ['==', '!=', '<', '>', '<=', '>='].includes(op.v)) {
|
|
446
|
+
this.next();
|
|
447
|
+
const b = this.add();
|
|
448
|
+
return compare(op.v, a, b);
|
|
449
|
+
}
|
|
450
|
+
return a;
|
|
451
|
+
}
|
|
452
|
+
add() {
|
|
453
|
+
let a = this.mul();
|
|
454
|
+
while (this.peek()?.t === 'op' && ['+', '-'].includes(this.peek().v)) {
|
|
455
|
+
const op = this.next().v;
|
|
456
|
+
const b = this.mul();
|
|
457
|
+
if (op === '+' && (typeof a === 'string' || typeof b === 'string') && (num(a) === null || num(b) === null))
|
|
458
|
+
a = String(a === UNDEF ? '' : a) + String(b === UNDEF ? '' : b);
|
|
459
|
+
else
|
|
460
|
+
a = arith([a, b], op === '+' ? (x, y) => x + y : (x, y) => x - y);
|
|
461
|
+
}
|
|
462
|
+
return a;
|
|
463
|
+
}
|
|
464
|
+
mul() {
|
|
465
|
+
let a = this.unary();
|
|
466
|
+
while (this.peek()?.t === 'op' && ['*', '/', '%'].includes(this.peek().v)) {
|
|
467
|
+
const op = this.next().v;
|
|
468
|
+
const b = this.unary();
|
|
469
|
+
a = op === '*' ? arith([a, b], (x, y) => x * y) : op === '%' ? arith([a, b], (x, y) => x % y) : num(a) !== null && num(b) !== null && Number.isInteger(num(a)) && Number.isInteger(num(b)) && num(b) !== 0 ? Math.trunc(num(a) / num(b)) : arith([a, b], (x, y) => x / y, true);
|
|
470
|
+
}
|
|
471
|
+
return a;
|
|
472
|
+
}
|
|
473
|
+
unary() {
|
|
474
|
+
if (this.peek()?.v === '!') {
|
|
475
|
+
this.next();
|
|
476
|
+
return !truthy(this.unary());
|
|
477
|
+
}
|
|
478
|
+
return this.primary();
|
|
479
|
+
}
|
|
480
|
+
primary() {
|
|
481
|
+
const t = this.next();
|
|
482
|
+
if (!t)
|
|
483
|
+
throw new TemplateError('expression ends too early');
|
|
484
|
+
if (t.t === 'str')
|
|
485
|
+
return t.v;
|
|
486
|
+
if (t.t === 'istr')
|
|
487
|
+
return this.ev.render(t.v);
|
|
488
|
+
if (t.t === 'num')
|
|
489
|
+
return Number(t.v);
|
|
490
|
+
if (t.t === 'word') {
|
|
491
|
+
if (t.v === 'true')
|
|
492
|
+
return true;
|
|
493
|
+
if (t.v === 'false')
|
|
494
|
+
return false;
|
|
495
|
+
if (t.v === 'null')
|
|
496
|
+
return null;
|
|
497
|
+
throw new TemplateError(`unknown word "${t.v}"`);
|
|
498
|
+
}
|
|
499
|
+
if (t.t === 'ref')
|
|
500
|
+
return this.ev.refValue(t.v);
|
|
501
|
+
if (t.t === 'lp') {
|
|
502
|
+
const v = this.or();
|
|
503
|
+
if (this.next()?.t !== 'rp')
|
|
504
|
+
throw new TemplateError('missing )');
|
|
505
|
+
return v;
|
|
506
|
+
}
|
|
507
|
+
if (t.t === 'lb') {
|
|
508
|
+
const items = [];
|
|
509
|
+
if (this.peek()?.t === 'rb') {
|
|
510
|
+
this.next();
|
|
511
|
+
return items;
|
|
512
|
+
}
|
|
513
|
+
const first = this.or();
|
|
514
|
+
if (this.peek()?.v === '..') {
|
|
515
|
+
this.next();
|
|
516
|
+
const last = this.or();
|
|
517
|
+
if (this.next()?.t !== 'rb')
|
|
518
|
+
throw new TemplateError('missing ]');
|
|
519
|
+
const out = [];
|
|
520
|
+
const a = num(first);
|
|
521
|
+
const b = num(last);
|
|
522
|
+
for (let i = a; a <= b ? i <= b : i >= b; a <= b ? i++ : i--)
|
|
523
|
+
out.push(i);
|
|
524
|
+
return out;
|
|
525
|
+
}
|
|
526
|
+
items.push(first);
|
|
527
|
+
while (this.peek()?.t === 'comma') {
|
|
528
|
+
this.next();
|
|
529
|
+
items.push(this.or());
|
|
530
|
+
}
|
|
531
|
+
if (this.next()?.t !== 'rb')
|
|
532
|
+
throw new TemplateError('missing ]');
|
|
533
|
+
return items;
|
|
534
|
+
}
|
|
535
|
+
throw new TemplateError(`unexpected "${t.v}"`);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
function truthy(v) {
|
|
539
|
+
if (v === UNDEF || v === null || v === undefined || v === false)
|
|
540
|
+
return false;
|
|
541
|
+
if (v === '' || (Array.isArray(v) && v.length === 0))
|
|
542
|
+
return false;
|
|
543
|
+
return true;
|
|
544
|
+
}
|
|
545
|
+
function compare(op, a, b) {
|
|
546
|
+
const A = a === UNDEF ? null : a;
|
|
547
|
+
const B = b === UNDEF ? null : b;
|
|
548
|
+
if (op === '==' || op === '!=') {
|
|
549
|
+
let eq;
|
|
550
|
+
if (A === null || B === null)
|
|
551
|
+
eq = A === B;
|
|
552
|
+
else if (typeof A === 'number' && typeof B === 'number')
|
|
553
|
+
eq = A === B;
|
|
554
|
+
else if (typeof A === typeof B)
|
|
555
|
+
eq = A === B || String(A) === String(B);
|
|
556
|
+
else
|
|
557
|
+
eq = String(A) === String(B); // Velocity compares toString() of different types
|
|
558
|
+
return op === '==' ? eq : !eq;
|
|
559
|
+
}
|
|
560
|
+
const x = num(A);
|
|
561
|
+
const y = num(B);
|
|
562
|
+
if (x === null || y === null) {
|
|
563
|
+
if (typeof A === 'string' && typeof B === 'string') {
|
|
564
|
+
const c = A < B ? -1 : A > B ? 1 : 0;
|
|
565
|
+
return op === '<' ? c < 0 : op === '>' ? c > 0 : op === '<=' ? c <= 0 : c >= 0;
|
|
566
|
+
}
|
|
567
|
+
return false;
|
|
568
|
+
}
|
|
569
|
+
return op === '<' ? x < y : op === '>' ? x > y : op === '<=' ? x <= y : x >= y;
|
|
570
|
+
}
|
|
571
|
+
// ---------------------------------------------------------------- references
|
|
572
|
+
/** Finds the end of a reference starting at src[i] === '$'. */
|
|
573
|
+
function readRef(src, i) {
|
|
574
|
+
let j = i + 1;
|
|
575
|
+
let quiet = false;
|
|
576
|
+
if (src[j] === '!') {
|
|
577
|
+
quiet = true;
|
|
578
|
+
j++;
|
|
579
|
+
}
|
|
580
|
+
let formal = false;
|
|
581
|
+
if (src[j] === '{') {
|
|
582
|
+
formal = true;
|
|
583
|
+
j++;
|
|
584
|
+
}
|
|
585
|
+
if (!/[A-Za-z_]/.test(src[j] ?? ''))
|
|
586
|
+
return null;
|
|
587
|
+
j++;
|
|
588
|
+
while (/[A-Za-z0-9_-]/.test(src[j] ?? '')) {
|
|
589
|
+
if (src[j] === '-' && !/[A-Za-z0-9_]/.test(src[j + 1] ?? ''))
|
|
590
|
+
break;
|
|
591
|
+
j++;
|
|
592
|
+
}
|
|
593
|
+
// .prop, .method(args), [index]
|
|
594
|
+
for (;;) {
|
|
595
|
+
if (src[j] === '.' && /[A-Za-z_]/.test(src[j + 1] ?? '')) {
|
|
596
|
+
let k = j + 1;
|
|
597
|
+
while (/[A-Za-z0-9_]/.test(src[k] ?? ''))
|
|
598
|
+
k++;
|
|
599
|
+
if (src[k] === '(') {
|
|
600
|
+
const close = matchParen(src, k);
|
|
601
|
+
if (close < 0)
|
|
602
|
+
return null;
|
|
603
|
+
k = close + 1;
|
|
604
|
+
}
|
|
605
|
+
j = k;
|
|
606
|
+
continue;
|
|
607
|
+
}
|
|
608
|
+
if (src[j] === '[') {
|
|
609
|
+
const close = src.indexOf(']', j);
|
|
610
|
+
if (close < 0)
|
|
611
|
+
return null;
|
|
612
|
+
j = close + 1;
|
|
613
|
+
continue;
|
|
614
|
+
}
|
|
615
|
+
break;
|
|
616
|
+
}
|
|
617
|
+
if (formal) {
|
|
618
|
+
if (src[j] !== '}')
|
|
619
|
+
return null;
|
|
620
|
+
j++;
|
|
621
|
+
}
|
|
622
|
+
return { end: j, quiet, formal };
|
|
623
|
+
}
|
|
624
|
+
function matchParen(src, open) {
|
|
625
|
+
let depth = 0;
|
|
626
|
+
let q = null;
|
|
627
|
+
for (let k = open; k < src.length; k++) {
|
|
628
|
+
const c = src[k];
|
|
629
|
+
if (q) {
|
|
630
|
+
if (c === q)
|
|
631
|
+
q = null;
|
|
632
|
+
continue;
|
|
633
|
+
}
|
|
634
|
+
if (c === '"' || c === "'")
|
|
635
|
+
q = c;
|
|
636
|
+
else if (c === '(')
|
|
637
|
+
depth++;
|
|
638
|
+
else if (c === ')') {
|
|
639
|
+
depth--;
|
|
640
|
+
if (depth === 0)
|
|
641
|
+
return k;
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
return -1;
|
|
645
|
+
}
|
|
646
|
+
function splitArgs(s) {
|
|
647
|
+
const out = [];
|
|
648
|
+
let depth = 0;
|
|
649
|
+
let q = null;
|
|
650
|
+
let cur = '';
|
|
651
|
+
for (const c of s) {
|
|
652
|
+
if (q) {
|
|
653
|
+
cur += c;
|
|
654
|
+
if (c === q)
|
|
655
|
+
q = null;
|
|
656
|
+
continue;
|
|
657
|
+
}
|
|
658
|
+
if (c === '"' || c === "'")
|
|
659
|
+
q = c;
|
|
660
|
+
if (c === '(' || c === '[')
|
|
661
|
+
depth++;
|
|
662
|
+
if (c === ')' || c === ']')
|
|
663
|
+
depth--;
|
|
664
|
+
if (c === ',' && depth === 0) {
|
|
665
|
+
out.push(cur);
|
|
666
|
+
cur = '';
|
|
667
|
+
continue;
|
|
668
|
+
}
|
|
669
|
+
cur += c;
|
|
670
|
+
}
|
|
671
|
+
if (cur.trim())
|
|
672
|
+
out.push(cur);
|
|
673
|
+
return out;
|
|
674
|
+
}
|
|
675
|
+
function parseTemplate(src) {
|
|
676
|
+
// Velocity 2 "lines" space gobbling: a line holding only a directive loses its indentation and newline.
|
|
677
|
+
src = src.replace(/^[ \t]*(#\{?(?:set|if|elseif)\b[^\n]*\)|#\{?(?:else|end)\}?)[ \t]*\r?\n/gm, '$1');
|
|
678
|
+
src = src.replace(/#\*[\s\S]*?\*#/g, '').replace(/##[^\n]*/g, '');
|
|
679
|
+
const root = [];
|
|
680
|
+
const stack = [];
|
|
681
|
+
const cur = () => (stack.length ? stack[stack.length - 1].body : root);
|
|
682
|
+
let i = 0;
|
|
683
|
+
let text = '';
|
|
684
|
+
const flush = () => {
|
|
685
|
+
if (text)
|
|
686
|
+
cur().push({ k: 'text', v: text });
|
|
687
|
+
text = '';
|
|
688
|
+
};
|
|
689
|
+
while (i < src.length) {
|
|
690
|
+
const c = src[i];
|
|
691
|
+
if (c === '\\' && (src[i + 1] === '$' || src[i + 1] === '#')) {
|
|
692
|
+
text += src[i + 1];
|
|
693
|
+
i += 2;
|
|
694
|
+
continue;
|
|
695
|
+
}
|
|
696
|
+
if (c === '$') {
|
|
697
|
+
const r = readRef(src, i);
|
|
698
|
+
if (r) {
|
|
699
|
+
flush();
|
|
700
|
+
cur().push({ k: 'ref', src: src.slice(i, r.end), quiet: r.quiet });
|
|
701
|
+
i = r.end;
|
|
702
|
+
continue;
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
if (c === '#') {
|
|
706
|
+
const m = /^#\{?([a-zA-Z]+)\}?/.exec(src.slice(i));
|
|
707
|
+
const name = m?.[1];
|
|
708
|
+
if (name === 'set' || name === 'if' || name === 'elseif') {
|
|
709
|
+
const open = src.indexOf('(', i);
|
|
710
|
+
const close = open < 0 ? -1 : matchParen(src, open);
|
|
711
|
+
if (close < 0)
|
|
712
|
+
throw new TemplateError(`#${name} without matching )`);
|
|
713
|
+
const inner = src.slice(open + 1, close);
|
|
714
|
+
flush();
|
|
715
|
+
if (name === 'set') {
|
|
716
|
+
const eq = inner.indexOf('=');
|
|
717
|
+
const lhs = inner.slice(0, eq).trim();
|
|
718
|
+
if (!/^\$!?\{?[A-Za-z_][A-Za-z0-9_]*\}?$/.test(lhs))
|
|
719
|
+
throw new UnsupportedError(`#set of "${lhs}" is not available offline`);
|
|
720
|
+
cur().push({ k: 'set', name: lhs.replace(/[${}!]/g, ''), expr: inner.slice(eq + 1) });
|
|
721
|
+
}
|
|
722
|
+
else if (name === 'if') {
|
|
723
|
+
const node = { k: 'if', branches: [{ cond: inner, body: [] }] };
|
|
724
|
+
cur().push(node);
|
|
725
|
+
stack.push({ node, body: node.branches[0].body });
|
|
726
|
+
}
|
|
727
|
+
else {
|
|
728
|
+
const top = stack[stack.length - 1];
|
|
729
|
+
if (!top)
|
|
730
|
+
throw new TemplateError('#elseif without #if');
|
|
731
|
+
const b = { cond: inner, body: [] };
|
|
732
|
+
top.node.branches.push(b);
|
|
733
|
+
top.body = b.body;
|
|
734
|
+
}
|
|
735
|
+
i = close + 1;
|
|
736
|
+
continue;
|
|
737
|
+
}
|
|
738
|
+
if (name === 'else' || name === 'end') {
|
|
739
|
+
flush();
|
|
740
|
+
const top = stack[stack.length - 1];
|
|
741
|
+
if (!top)
|
|
742
|
+
throw new TemplateError(`#${name} without #if`);
|
|
743
|
+
if (name === 'else') {
|
|
744
|
+
const b = { cond: null, body: [] };
|
|
745
|
+
top.node.branches.push(b);
|
|
746
|
+
top.body = b.body;
|
|
747
|
+
}
|
|
748
|
+
else
|
|
749
|
+
stack.pop();
|
|
750
|
+
i += m[0].length;
|
|
751
|
+
continue;
|
|
752
|
+
}
|
|
753
|
+
if (name && ['foreach', 'macro', 'parse', 'include', 'evaluate', 'define', 'break', 'stop'].includes(name))
|
|
754
|
+
throw new UnsupportedError(`#${name} is not available offline`);
|
|
755
|
+
}
|
|
756
|
+
text += c;
|
|
757
|
+
i++;
|
|
758
|
+
}
|
|
759
|
+
flush();
|
|
760
|
+
if (stack.length)
|
|
761
|
+
throw new TemplateError('#if without #end');
|
|
762
|
+
return root;
|
|
763
|
+
}
|
|
764
|
+
export class Evaluator {
|
|
765
|
+
ctx;
|
|
766
|
+
constructor(ctx) {
|
|
767
|
+
this.ctx = ctx;
|
|
768
|
+
}
|
|
769
|
+
/** Renders a template string. */
|
|
770
|
+
render(src) {
|
|
771
|
+
if (!src.includes('$') && !src.includes('#'))
|
|
772
|
+
return src;
|
|
773
|
+
return this.run(parseTemplate(src));
|
|
774
|
+
}
|
|
775
|
+
run(nodes) {
|
|
776
|
+
let out = '';
|
|
777
|
+
for (const n of nodes) {
|
|
778
|
+
if (n.k === 'text')
|
|
779
|
+
out += n.v;
|
|
780
|
+
else if (n.k === 'ref') {
|
|
781
|
+
const v = this.refValue(n.src);
|
|
782
|
+
out += v === UNDEF || v === null || v === undefined ? (n.quiet ? '' : n.src) : show(v);
|
|
783
|
+
}
|
|
784
|
+
else if (n.k === 'set') {
|
|
785
|
+
const v = this.expr(n.expr);
|
|
786
|
+
this.ctx[n.name] = v;
|
|
787
|
+
}
|
|
788
|
+
else {
|
|
789
|
+
for (const b of n.branches) {
|
|
790
|
+
if (b.cond === null || truthy(this.exprRaw(b.cond))) {
|
|
791
|
+
out += this.run(b.body);
|
|
792
|
+
break;
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
return out;
|
|
798
|
+
}
|
|
799
|
+
expr(src) {
|
|
800
|
+
const p = new ExprParser(lexExpr(src), this);
|
|
801
|
+
const v = p.parse();
|
|
802
|
+
if (!p.done())
|
|
803
|
+
throw new TemplateError(`could not read "${src.trim()}"`);
|
|
804
|
+
return v;
|
|
805
|
+
}
|
|
806
|
+
exprRaw(src) {
|
|
807
|
+
const p = new ExprParser(lexExpr(src), this);
|
|
808
|
+
const v = p.parse();
|
|
809
|
+
if (!p.done())
|
|
810
|
+
throw new TemplateError(`could not read "${src.trim()}"`);
|
|
811
|
+
return v === null ? null : v;
|
|
812
|
+
}
|
|
813
|
+
/** Value of a reference like $a.b.c(1)[0]; UNDEF when it does not resolve. */
|
|
814
|
+
refValue(ref) {
|
|
815
|
+
let s = ref.slice(1);
|
|
816
|
+
if (s.startsWith('!'))
|
|
817
|
+
s = s.slice(1);
|
|
818
|
+
if (s.startsWith('{'))
|
|
819
|
+
s = s.slice(1, -1);
|
|
820
|
+
const head = /^[A-Za-z_][A-Za-z0-9_-]*/.exec(s)[0];
|
|
821
|
+
let v = head in this.ctx ? this.ctx[head] : UNDEF;
|
|
822
|
+
let i = head.length;
|
|
823
|
+
while (i < s.length) {
|
|
824
|
+
if (v === UNDEF)
|
|
825
|
+
return UNDEF;
|
|
826
|
+
if (s[i] === '.') {
|
|
827
|
+
const m = /^[A-Za-z_][A-Za-z0-9_]*/.exec(s.slice(i + 1));
|
|
828
|
+
const name = m[0];
|
|
829
|
+
let k = i + 1 + name.length;
|
|
830
|
+
if (s[k] === '(') {
|
|
831
|
+
const close = matchParen(s, k);
|
|
832
|
+
const args = splitArgs(s.slice(k + 1, close)).map((a) => this.expr(a));
|
|
833
|
+
v = callMethod(v, name, args);
|
|
834
|
+
k = close + 1;
|
|
835
|
+
}
|
|
836
|
+
else
|
|
837
|
+
v = property(v, name);
|
|
838
|
+
i = k;
|
|
839
|
+
}
|
|
840
|
+
else if (s[i] === '[') {
|
|
841
|
+
const close = s.indexOf(']', i);
|
|
842
|
+
const idx = this.expr(s.slice(i + 1, close));
|
|
843
|
+
if (Array.isArray(v)) {
|
|
844
|
+
const n = num(idx);
|
|
845
|
+
if (n === null || n < 0 || n >= v.length)
|
|
846
|
+
throw new TemplateError(`IndexOutOfBoundsException: ${idx}`);
|
|
847
|
+
v = v[n];
|
|
848
|
+
}
|
|
849
|
+
else if (v && typeof v === 'object')
|
|
850
|
+
v = v[String(idx)] ?? UNDEF;
|
|
851
|
+
else
|
|
852
|
+
v = UNDEF;
|
|
853
|
+
i = close + 1;
|
|
854
|
+
}
|
|
855
|
+
else
|
|
856
|
+
break;
|
|
857
|
+
}
|
|
858
|
+
return v;
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
function show(v) {
|
|
862
|
+
if (v instanceof Date)
|
|
863
|
+
return formatJavaDate(v, 'EEE MMM dd HH:mm:ss zzz yyyy');
|
|
864
|
+
if (Array.isArray(v))
|
|
865
|
+
return `[${v.map(show).join(', ')}]`;
|
|
866
|
+
if (typeof v === 'object' && v)
|
|
867
|
+
return v.__tool ? `org.apache.velocity.tools.generic.${v.__tool}` : JSON.stringify(v);
|
|
868
|
+
return String(v);
|
|
869
|
+
}
|
|
870
|
+
/** Renders one template with variables (plus the ISC tools). */
|
|
871
|
+
export function renderTemplate(src, vars, now = new Date()) {
|
|
872
|
+
return new Evaluator({ ...tools(now), ...vars }).render(src);
|
|
873
|
+
}
|
|
874
|
+
//# sourceMappingURL=velocity.js.map
|