@jxsuite/parser 2.0.0 → 3.0.0

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/content.js CHANGED
@@ -1,4 +1,139 @@
1
+ import { createRequire } from "node:module";
2
+ var __create = Object.create;
3
+ var __getProtoOf = Object.getPrototypeOf;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ function __accessProp(key) {
8
+ return this[key];
9
+ }
10
+ var __toESMCache_node;
11
+ var __toESMCache_esm;
12
+ var __toESM = (mod, isNodeMode, target) => {
13
+ var canCache = mod != null && typeof mod === "object";
14
+ if (canCache) {
15
+ var cache = isNodeMode ? __toESMCache_node ??= new WeakMap : __toESMCache_esm ??= new WeakMap;
16
+ var cached = cache.get(mod);
17
+ if (cached)
18
+ return cached;
19
+ }
20
+ target = mod != null ? __create(__getProtoOf(mod)) : {};
21
+ const to = isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
22
+ if (mod && typeof mod === "object" || typeof mod === "function") {
23
+ for (let key of __getOwnPropNames(mod))
24
+ if (!__hasOwnProp.call(to, key))
25
+ __defProp(to, key, {
26
+ get: __accessProp.bind(mod, key),
27
+ enumerable: true
28
+ });
29
+ }
30
+ if (canCache)
31
+ cache.set(mod, to);
32
+ return to;
33
+ };
34
+ var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
35
+ var __returnValue = (v) => v;
36
+ function __exportSetter(name, newValue) {
37
+ this[name] = __returnValue.bind(null, newValue);
38
+ }
39
+ var __export = (target, all) => {
40
+ for (var name in all)
41
+ __defProp(target, name, {
42
+ get: all[name],
43
+ enumerable: true,
44
+ configurable: true,
45
+ set: __exportSetter.bind(all, name)
46
+ });
47
+ };
48
+ var __esm = (fn, res, err) => () => {
49
+ if (fn)
50
+ try {
51
+ res = fn(fn = 0);
52
+ } catch (e) {
53
+ err = [e];
54
+ }
55
+ if (err)
56
+ throw err[0];
57
+ return res;
58
+ };
59
+ var __require = /* @__PURE__ */ createRequire(import.meta.url);
60
+
61
+ // ../../packages/schema/src/guards.ts
62
+ function isString(value) {
63
+ return typeof value === "string";
64
+ }
65
+ function isNumber(value) {
66
+ return typeof value === "number";
67
+ }
68
+ function isBoolean(value) {
69
+ return typeof value === "boolean";
70
+ }
71
+ function isNonNullObject(value) {
72
+ return typeof value === "object" && value !== null;
73
+ }
74
+ function isJsonObject(value) {
75
+ return typeof value === "object" && value !== null && !Array.isArray(value);
76
+ }
77
+ function isRef(value) {
78
+ return isJsonObject(value) && typeof value.$ref === "string";
79
+ }
80
+ function isNodeObject(value) {
81
+ return isJsonObject(value);
82
+ }
83
+ function isPlainTagName(tagName) {
84
+ return typeof tagName === "string";
85
+ }
86
+ function isTagExpression(tagName) {
87
+ if (typeof tagName !== "object" || tagName === null || Array.isArray(tagName) || !isJsonObject(tagName.$expression)) {
88
+ return false;
89
+ }
90
+ const expression = tagName.$expression;
91
+ if (expression.operator === "?:") {
92
+ return typeof expression.value === "string" && typeof expression.initial === "string";
93
+ }
94
+ if (expression.operator === "switch") {
95
+ return isJsonObject(expression.cases) && Object.values(expression.cases).every((tag) => isString(tag)) && typeof expression.default === "string";
96
+ }
97
+ return false;
98
+ }
99
+ function tagNameCandidates(tagName) {
100
+ if (isPlainTagName(tagName)) {
101
+ return [tagName];
102
+ }
103
+ if (!isTagExpression(tagName)) {
104
+ return [];
105
+ }
106
+ const expression = tagName.$expression;
107
+ if (expression.operator === "?:") {
108
+ return [...new Set([expression.value, expression.initial])];
109
+ }
110
+ return [...new Set([...Object.values(expression.cases), expression.default])];
111
+ }
112
+ var SCHEMA_KEYWORDS;
113
+ var init_guards = __esm(() => {
114
+ SCHEMA_KEYWORDS = new Set([
115
+ "type",
116
+ "enum",
117
+ "minimum",
118
+ "maximum",
119
+ "minLength",
120
+ "maxLength",
121
+ "pattern",
122
+ "format",
123
+ "items",
124
+ "properties",
125
+ "required",
126
+ "description",
127
+ "title",
128
+ "$comment"
129
+ ]);
130
+ });
131
+
132
+ // src/content.ts
133
+ init_guards();
134
+
1
135
  // ../../packages/schema/src/locale.ts
136
+ init_guards();
2
137
  var RTL_SCRIPTS = new Set([
3
138
  "Adlm",
4
139
  "Arab",
@@ -41,7 +176,7 @@ var RTL_SCRIPTS = new Set([
41
176
  "Yezi"
42
177
  ]);
43
178
  function canonicalizeLocale(tag) {
44
- if (typeof tag !== "string" || tag.trim() === "") {
179
+ if (!isString(tag) || tag.trim() === "") {
45
180
  return null;
46
181
  }
47
182
  try {
@@ -54,7 +189,7 @@ var DEFAULT_ROUTING = "prefix-except-default";
54
189
  function resolveI18n(projectConfig) {
55
190
  const raw = projectConfig.i18n;
56
191
  const errors = [];
57
- if (!raw || typeof raw !== "object") {
192
+ if (!isNonNullObject(raw)) {
58
193
  return { errors, i18n: null };
59
194
  }
60
195
  const declared = Array.isArray(raw.locales) ? raw.locales : [];
@@ -97,10 +232,16 @@ function localeUrlPrefix(locale, i18n) {
97
232
  return `/${canonical.toLowerCase()}`;
98
233
  }
99
234
 
235
+ // src/content-data.ts
236
+ async function entriesOf(data, name) {
237
+ return await data?.load?.(name) ?? data?.get(name);
238
+ }
239
+
100
240
  // src/dates.ts
241
+ init_guards();
101
242
  var DATE_FORMATS = ["date", "date-time"];
102
243
  function isDateFormat(format) {
103
- return typeof format === "string" && DATE_FORMATS.includes(format);
244
+ return DATE_FORMATS.some((candidate) => candidate === format);
104
245
  }
105
246
  var DATE_ONLY = /^\d{4}-\d{2}-\d{2}$/;
106
247
  var RFC_3339 = /^\d{4}-\d{2}-\d{2}[Tt ]\d{2}:\d{2}(:\d{2}(\.\d+)?)?([Zz]|[+-]\d{2}:\d{2})?$/;
@@ -117,7 +258,7 @@ function parseDateValue(value, format) {
117
258
  if (value instanceof Date) {
118
259
  return Number.isNaN(value.getTime()) ? null : format === "date" ? toDateOnly(value) : toInstant(value);
119
260
  }
120
- if (typeof value !== "string") {
261
+ if (!isString(value)) {
121
262
  return null;
122
263
  }
123
264
  const text = value.trim();
@@ -150,7 +291,7 @@ function coerceEntryDates(entries, schema, contentTypeName) {
150
291
  if (!properties) {
151
292
  return warnings;
152
293
  }
153
- const dateFields = Object.entries(properties).map(([field, def]) => [field, def.format]).filter((pair) => isDateFormat(pair[1]));
294
+ const dateFields = Object.entries(properties).flatMap(([field, def]) => isDateFormat(def.format) ? [[field, def.format]] : []);
154
295
  if (dateFields.length === 0) {
155
296
  return warnings;
156
297
  }
@@ -181,15 +322,16 @@ function coerceEntryDates(entries, schema, contentTypeName) {
181
322
  return warnings;
182
323
  }
183
324
  function parseComparable(a, b) {
184
- const aDate = typeof a === "string" && (DATE_ONLY.test(a) || RFC_3339.test(a));
185
- const bDate = typeof b === "string" && (DATE_ONLY.test(b) || RFC_3339.test(b));
186
- if (aDate && bDate) {
325
+ if (isString(a) && isCoercedDate(a) && isString(b) && isCoercedDate(b)) {
187
326
  return [a, b];
188
327
  }
189
328
  return [Number(a), Number(b)];
190
329
  }
191
330
 
192
331
  // src/content.ts
332
+ function includesValue(items, value) {
333
+ return items.includes(value);
334
+ }
193
335
  function evaluateFilterRule(rule, entry) {
194
336
  const actual = rule.field === "id" ? entry.id : entry.data[rule.field];
195
337
  switch (rule.op) {
@@ -207,15 +349,15 @@ function evaluateFilterRule(rule, entry) {
207
349
  }
208
350
  case "contains": {
209
351
  if (Array.isArray(actual)) {
210
- return actual.includes(rule.value);
352
+ return includesValue(actual, rule.value);
211
353
  }
212
- return typeof actual === "string" && actual.includes(String(rule.value ?? ""));
354
+ return isString(actual) && actual.includes(String(rule.value ?? ""));
213
355
  }
214
356
  case "not contains": {
215
357
  if (Array.isArray(actual)) {
216
- return !actual.includes(rule.value);
358
+ return !includesValue(actual, rule.value);
217
359
  }
218
- return typeof actual === "string" && !actual.includes(String(rule.value ?? ""));
360
+ return isString(actual) && !actual.includes(String(rule.value ?? ""));
219
361
  }
220
362
  case ">": {
221
363
  const [a, b] = parseComparable(actual, rule.value);
@@ -244,7 +386,7 @@ function queryContentType(entries, query = {}) {
244
386
  let rules;
245
387
  if (Array.isArray(query.filter)) {
246
388
  rules = query.filter;
247
- } else if (typeof query.filter === "object") {
389
+ } else if (isNonNullObject(query.filter)) {
248
390
  rules = Object.entries(query.filter).map(([field, value]) => ({
249
391
  field,
250
392
  op: "==",
@@ -281,29 +423,35 @@ function findEntry(entries, id) {
281
423
  }
282
424
 
283
425
  class ContentCollection {
426
+ static projectOnDemand = true;
284
427
  config;
285
428
  constructor(config) {
286
429
  this.config = config;
287
430
  }
288
- resolve() {
431
+ async resolve() {
289
432
  const { contentType, filter, sort, limit, _project } = this.config;
290
- const entries = _project?.content?.get(contentType ?? "");
433
+ const entries = await entriesOf(_project?.content, contentType ?? "");
291
434
  if (!entries) {
292
435
  return [];
293
436
  }
294
- return queryContentType(entries, {
295
- ...filter != null && {
296
- filter
297
- },
298
- ...sort != null && {
299
- sort
300
- },
301
- ...limit != null && { limit }
302
- });
437
+ const query = {};
438
+ if (filter != null) {
439
+ query.filter = filter;
440
+ }
441
+ if (sort != null) {
442
+ query.sort = sort;
443
+ }
444
+ if (limit != null) {
445
+ query.limit = limit;
446
+ }
447
+ const result = queryContentType(entries, query);
448
+ await _project?.content?.materialize?.(result);
449
+ return result;
303
450
  }
304
451
  }
305
452
 
306
453
  class ContentEntry {
454
+ static projectOnDemand = true;
307
455
  config;
308
456
  constructor(config) {
309
457
  this.config = config;
@@ -311,7 +459,7 @@ class ContentEntry {
311
459
  #pageRoute() {
312
460
  const { _project, _document } = this.config;
313
461
  let path = _document?.route?.urlPattern;
314
- if (typeof path !== "string") {
462
+ if (!isString(path)) {
315
463
  return;
316
464
  }
317
465
  const { i18n } = resolveI18n(_project?.config ?? {});
@@ -321,21 +469,26 @@ class ContentEntry {
321
469
  }
322
470
  return path.replace(/\/+$/, "") || "/";
323
471
  }
324
- resolve() {
472
+ async resolve() {
473
+ const found = await this.#find();
474
+ await this.config._project?.content?.materialize?.([found]);
475
+ return found;
476
+ }
477
+ async#find() {
325
478
  const { contentType, id, field, _project, _document } = this.config;
326
- const entries = _project?.content?.get(contentType ?? "");
479
+ const entries = await entriesOf(_project?.content, contentType ?? "");
327
480
  if (!entries) {
328
481
  return null;
329
482
  }
330
483
  const wanted = _document?.route?.locale;
331
484
  const localized = entries.some((e) => e._meta?.locale !== undefined);
332
- const scoped = localized && typeof wanted === "string" ? entries.filter((e) => e._meta?.locale === wanted) : entries;
485
+ const scoped = localized && isString(wanted) ? entries.filter((e) => e._meta?.locale === wanted) : entries;
333
486
  if (id === undefined && field === undefined) {
334
487
  const route = this.#pageRoute();
335
488
  return route === undefined ? null : scoped.find((e) => e._meta?.route === route) ?? null;
336
489
  }
337
490
  let resolvedId = id;
338
- if (resolvedId && typeof resolvedId === "object" && resolvedId.$ref?.startsWith("#/$params/")) {
491
+ if (isRef(resolvedId) && resolvedId.$ref.startsWith("#/$params/")) {
339
492
  const paramName = resolvedId.$ref.replace("#/$params/", "");
340
493
  resolvedId = _document?.route?._pathParams?.[paramName];
341
494
  }
@@ -345,7 +498,7 @@ class ContentEntry {
345
498
  if (field && field !== "id") {
346
499
  return scoped.find((e) => e.data[field] === resolvedId) ?? null;
347
500
  }
348
- return findEntry(scoped, resolvedId);
501
+ return isString(resolvedId) ? findEntry(scoped, resolvedId) : null;
349
502
  }
350
503
  }
351
504
  export {