@cardenelabs/dragon 0.46.0 → 0.46.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.cjs CHANGED
@@ -836,8 +836,8 @@ function longest(points) {
836
836
  }
837
837
  return { a: best.a, b: best.b, vertical: Math.abs(best.b[0] - best.a[0]) < Math.abs(best.b[1] - best.a[1]) };
838
838
  }
839
- function along(points, distance) {
840
- let left = distance;
839
+ function along(points, distance2) {
840
+ let left = distance2;
841
841
  for (let i = 1; i < points.length; i++) {
842
842
  const a = points[i - 1];
843
843
  const b = points[i];
@@ -3974,6 +3974,33 @@ function extractDiagrams(markdown) {
3974
3974
  return fences;
3975
3975
  }
3976
3976
 
3977
+ // src/nearest.ts
3978
+ var MAX_DISTANCE = 2;
3979
+ function nearest(word, candidates) {
3980
+ const written = [...word.toLowerCase()];
3981
+ let limit = Math.min(MAX_DISTANCE, written.length - 1);
3982
+ let best;
3983
+ for (const candidate of candidates) {
3984
+ const d = distance(written, [...candidate.toLowerCase()]);
3985
+ if (d > limit) continue;
3986
+ best = candidate;
3987
+ limit = d - 1;
3988
+ }
3989
+ return best;
3990
+ }
3991
+ function distance(a, b) {
3992
+ let previous = Array.from({ length: b.length + 1 }, (_, j) => j);
3993
+ for (let i = 1; i <= a.length; i++) {
3994
+ const current = [i];
3995
+ for (let j = 1; j <= b.length; j++) {
3996
+ const replace = previous[j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1);
3997
+ current.push(Math.min(previous[j] + 1, current[j - 1] + 1, replace));
3998
+ }
3999
+ previous = current;
4000
+ }
4001
+ return previous[b.length];
4002
+ }
4003
+
3977
4004
  // src/field-messages.ts
3978
4005
  var TYPES_JA = {
3979
4006
  string: "\u6587\u5B57\u5217\u306B\u3059\u308B",
@@ -3998,6 +4025,7 @@ var ja2 = {
3998
4025
  additionalProperties: (key2) => `${key2} \u3068\u3044\u3046\u6B04\u306F\u66F8\u3051\u306A\u3044`,
3999
4026
  required: (key2) => `${key2} \u306E\u6B04\u304C\u8981\u308B`,
4000
4027
  enum: (values) => `${values.join(" / ")} \u306E\u3069\u308C\u304B\u306B\u3059\u308B`,
4028
+ unknownIcon: (value2, near) => `${value2} \u3068\u3044\u3046 icon \u306F\u7121\u3044\u3002\u8FD1\u3044\u540D\u524D\u306F ${near}`,
4001
4029
  type: (type) => TYPES_JA[type] ?? `${type} \u306B\u3059\u308B`,
4002
4030
  minLength: (limit) => `${limit} \u5B57\u4EE5\u4E0A\u66F8\u304F`,
4003
4031
  maxLength: (limit) => `${limit} \u5B57\u307E\u3067\u306B\u3059\u308B`,
@@ -4022,6 +4050,7 @@ var en = {
4022
4050
  additionalProperties: (key2) => `The ${key2} field is not allowed`,
4023
4051
  required: (key2) => `The ${key2} field is required`,
4024
4052
  enum: (values) => `Use one of ${values.join(" / ")}`,
4053
+ unknownIcon: (value2, near) => `There is no icon named ${value2}. The closest is ${near}`,
4025
4054
  type: (type) => TYPES_EN[type] ?? `Use ${type}`,
4026
4055
  minLength: (limit) => `Write at least ${plural(limit, "character")}`,
4027
4056
  maxLength: (limit) => `Write at most ${plural(limit, "character")}`,
@@ -10395,7 +10424,16 @@ var WIRED_EVENTS = ["edge", "action"];
10395
10424
  var DURATION = /^\^\[0-9\]\+\(\\\.\[0-9\]\+\)\?\(([a-z|]+)\)\$$/u;
10396
10425
  var RATIO = /%\)\$$/u;
10397
10426
  var escape = (key2) => key2.replaceAll("~", "~0").replaceAll("/", "~1");
10427
+ function valueAt2(input, pointer) {
10428
+ let value2 = input;
10429
+ for (const raw of pointer.split("/").slice(1)) {
10430
+ if (typeof value2 !== "object" || value2 === null) return void 0;
10431
+ value2 = value2[raw.replaceAll("~1", "/").replaceAll("~0", "~")];
10432
+ }
10433
+ return value2;
10434
+ }
10398
10435
  var URL_RULE = "#/definitions/url/pattern";
10436
+ var ICON_RULE = "#/definitions/icon/enum";
10399
10437
  var HEX = /^\^\[0-9a-f\]\{(\d+),(\d+)\}\$$/u;
10400
10438
  function forbiddenOf(pattern) {
10401
10439
  const inside = /^\^\[\^(.+)\]\+\$$/u.exec(pattern)?.[1];
@@ -10406,7 +10444,7 @@ function forbiddenOf(pattern) {
10406
10444
  var branchesOf = (anyOf, errors) => errors.filter(
10407
10445
  (error) => error !== anyOf && error.schemaPath.startsWith(`${anyOf.schemaPath}/`) && (error.instancePath === anyOf.instancePath || error.instancePath.startsWith(`${anyOf.instancePath}/`))
10408
10446
  );
10409
- function toFieldError(error, errors, m) {
10447
+ function toFieldError(error, errors, m, input) {
10410
10448
  const params = error.params;
10411
10449
  const at = error.instancePath;
10412
10450
  switch (error.keyword) {
@@ -10439,8 +10477,12 @@ function toFieldError(error, errors, m) {
10439
10477
  const tag = String(params.tag);
10440
10478
  return { pointer: `${at}/${escape(tag)}`, message: m.discriminator(tag, EVENT_TYPES) };
10441
10479
  }
10442
- case "enum":
10480
+ case "enum": {
10481
+ const written = error.schemaPath === ICON_RULE ? valueAt2(input, at) : void 0;
10482
+ const near = typeof written === "string" ? nearest(written, ICON_NAMES) : void 0;
10483
+ if (typeof written === "string" && near !== void 0) return { pointer: at, message: m.unknownIcon(written, near) };
10443
10484
  return { pointer: at, message: m.enum(params.allowedValues.map(String)) };
10485
+ }
10444
10486
  case "type":
10445
10487
  return { pointer: at, message: m.type(String(params.type)) };
10446
10488
  case "minLength":
@@ -10476,12 +10518,16 @@ function validateDiagram(input, options = {}) {
10476
10518
  const all = validate_diagram_default.errors ?? [];
10477
10519
  const inBranch = new Set(all.filter((error) => error.keyword === "anyOf").flatMap((anyOf) => branchesOf(anyOf, all)));
10478
10520
  const m = FIELD_MESSAGES[options.lang ?? "ja"];
10479
- const errors = all.filter((error) => error.keyword !== "if" && !inBranch.has(error)).map((error) => toFieldError(error, all, m));
10521
+ const errors = all.filter((error) => error.keyword !== "if" && !inBranch.has(error)).map((error) => toFieldError(error, all, m, input));
10480
10522
  return { ok: false, errors };
10481
10523
  }
10482
10524
 
10483
10525
  // src/messages.ts
10484
10526
  var list = (words) => words.join(" / ");
10527
+ var withNear = (value2, near, none) => {
10528
+ const name2 = nearest(value2, ICON_NAMES);
10529
+ return name2 === void 0 ? none : near(name2);
10530
+ };
10485
10531
  var ja3 = () => {
10486
10532
  const shapes = {
10487
10533
  sequence: "sequence <\u984C>",
@@ -10542,7 +10588,7 @@ var ja3 = () => {
10542
10588
  attrTwice: (key2) => `${key2} \u306F 1 \u3064\u306E\u884C\u306B 1 \u3064\u307E\u3067`,
10543
10589
  badAttr: (key2, allowed) => `${key2} \u306F\u3053\u306E\u884C\u306B\u66F8\u3051\u306A\u3044\u5C5E\u6027\u3002\u66F8\u3051\u308B\u306E\u306F ${list(allowed)}`,
10544
10590
  badColor: `color \u306F ${list(COLORS)} \u306E\u3069\u308C\u304B`,
10545
- badIcon: `icon \u306F ${list(ICON_NAMES)} \u306E\u3069\u308C\u304B`,
10591
+ badIcon: (value2) => withNear(value2, (near) => `${value2} \u3068\u3044\u3046 icon \u306F\u7121\u3044\u3002\u8FD1\u3044\u540D\u524D\u306F ${near}`, `icon \u306F ${list(ICON_NAMES)} \u306E\u3069\u308C\u304B`),
10546
10592
  badView: `view \u306F ${list(VIEWS)} \u306E\u3069\u308C\u304B`,
10547
10593
  badCarry: `carry \u306F ${list(CARRIES)} \u306E\u3069\u308C\u304B\u3002\u672D\u306F tag: <\u5024> \u3067\u66F8\u304F`,
10548
10594
  badSide: `side \u306F ${list(SIDES)} \u306E\u3069\u308C\u304B`,
@@ -10681,7 +10727,7 @@ var en2 = () => {
10681
10727
  attrTwice: (key2) => `Write ${key2} only once on a line`,
10682
10728
  badAttr: (key2, allowed) => `${key2} cannot be written on this line. Write ${list(allowed)}`,
10683
10729
  badColor: `color is one of ${list(COLORS)}`,
10684
- badIcon: `icon is one of ${list(ICON_NAMES)}`,
10730
+ badIcon: (value2) => withNear(value2, (near) => `There is no icon named ${value2}. The closest is ${near}`, `icon is one of ${list(ICON_NAMES)}`),
10685
10731
  badView: `view is one of ${list(VIEWS)}`,
10686
10732
  badCarry: `carry is one of ${list(CARRIES)}. Write a tag as tag: <value>`,
10687
10733
  badSide: `side is one of ${list(SIDES)}`,
@@ -10942,7 +10988,7 @@ function lookOf(source, attrs2, m) {
10942
10988
  const color = attrs2.get("color");
10943
10989
  if (color && !isOneOf(COLORS, color.value)) throw fail(source, color.at, m.badColor);
10944
10990
  const icon = attrs2.get("icon");
10945
- if (icon && !ICON_NAMES.includes(icon.value)) throw fail(source, icon.at, m.badIcon);
10991
+ if (icon && !ICON_NAMES.includes(icon.value)) throw fail(source, icon.at, m.badIcon(icon.value));
10946
10992
  const note = attrs2.get("note")?.value;
10947
10993
  return {
10948
10994
  ...note !== void 0 ? { note } : {},