@cardenelabs/dragon 0.46.0 → 0.46.3

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