@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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cardenelabs/dragon",
3
- "version": "0.46.0",
3
+ "version": "0.46.1",
4
4
  "description": "Dragon — 動く図を、人は行ごとの記法で、AI は JSON で書く。時系列図と構成図を段ごとに動かし、mp4・GIF・HTML・PNG に書き出す。",
5
5
  "license": "MIT",
6
6
  "author": "cardene777",
@@ -23,6 +23,8 @@ export interface FieldMessages {
23
23
  additionalProperties: (key: string) => string;
24
24
  required: (key: string) => string;
25
25
  enum: (values: readonly string[]) => string;
26
+ /** 知らないアイコン名 `value` と、それに一番近い名前 `near` (#3315) */
27
+ unknownIcon: (value: string, near: string) => string;
26
28
  type: (type: string) => string;
27
29
  minLength: (limit: number) => string;
28
30
  maxLength: (limit: number) => string;
@@ -80,6 +82,7 @@ const ja: FieldMessages = {
80
82
  additionalProperties: (key) => `${key} という欄は書けない`,
81
83
  required: (key) => `${key} の欄が要る`,
82
84
  enum: (values) => `${values.join(" / ")} のどれかにする`,
85
+ unknownIcon: (value, near) => `${value} という icon は無い。近い名前は ${near}`,
83
86
  type: (type) => TYPES_JA[type] ?? `${type} にする`,
84
87
  minLength: (limit) => `${limit} 字以上書く`,
85
88
  maxLength: (limit) => `${limit} 字までにする`,
@@ -107,6 +110,7 @@ const en: FieldMessages = {
107
110
  additionalProperties: (key) => `The ${key} field is not allowed`,
108
111
  required: (key) => `The ${key} field is required`,
109
112
  enum: (values) => `Use one of ${values.join(" / ")}`,
113
+ unknownIcon: (value, near) => `There is no icon named ${value}. The closest is ${near}`,
110
114
  type: (type) => TYPES_EN[type] ?? `Use ${type}`,
111
115
  minLength: (limit) => `Write at least ${plural(limit, "character")}`,
112
116
  maxLength: (limit) => `Write at most ${plural(limit, "character")}`,
package/src/messages.ts CHANGED
@@ -4,6 +4,7 @@
4
4
  * 記法の語は英語だけで file から言葉が決まらないため、誤りの文は呼ぶ側が渡した言葉
5
5
  * (渡さなければ日本語) で出す。文の中の記法の語は語の表から引き、表と文で語が食い違わないようにする。
6
6
  */
7
+ import { nearest } from "./nearest";
7
8
  import { COLORS, ICON_NAMES } from "./schema";
8
9
  import { CARRIES, DURATION_UNITS, HEADS, KINDS, type NotationLang, SIDES, SPAN_SHAPES, VIEWS } from "./words";
9
10
 
@@ -57,7 +58,8 @@ export interface Messages {
57
58
  attrTwice: (key: string) => string;
58
59
  badAttr: (key: string, allowed: readonly string[]) => string;
59
60
  badColor: string;
60
- badIcon: string;
61
+ /** 知らないアイコン名 `value`。近い名前が在ればそれだけを、無ければ使える名前を全て挙げる (#3315) */
62
+ badIcon: (value: string) => string;
61
63
  badView: string;
62
64
  badCarry: string;
63
65
  badSide: string;
@@ -163,6 +165,11 @@ export interface Messages {
163
165
  }
164
166
 
165
167
  const list = (words: readonly string[]): string => words.join(" / ");
168
+ /** 書いたアイコン名に近い名前が在れば `near` の文、無ければ `none` の文 (#3315) */
169
+ const withNear = (value: string, near: (name: string) => string, none: string): string => {
170
+ const name = nearest(value, ICON_NAMES);
171
+ return name === undefined ? none : near(name);
172
+ };
166
173
 
167
174
  const ja = (): Messages => {
168
175
  const shapes: Record<Shape, string> = {
@@ -224,7 +231,7 @@ const ja = (): Messages => {
224
231
  attrTwice: (key) => `${key} は 1 つの行に 1 つまで`,
225
232
  badAttr: (key, allowed) => `${key} はこの行に書けない属性。書けるのは ${list(allowed)}`,
226
233
  badColor: `color は ${list(COLORS)} のどれか`,
227
- badIcon: `icon は ${list(ICON_NAMES)} のどれか`,
234
+ badIcon: (value) => withNear(value, (near) => `${value} という icon は無い。近い名前は ${near}`, `icon は ${list(ICON_NAMES)} のどれか`),
228
235
  badView: `view は ${list(VIEWS)} のどれか`,
229
236
  badCarry: `carry は ${list(CARRIES)} のどれか。札は tag: <値> で書く`,
230
237
  badSide: `side は ${list(SIDES)} のどれか`,
@@ -364,7 +371,7 @@ const en = (): Messages => {
364
371
  attrTwice: (key) => `Write ${key} only once on a line`,
365
372
  badAttr: (key, allowed) => `${key} cannot be written on this line. Write ${list(allowed)}`,
366
373
  badColor: `color is one of ${list(COLORS)}`,
367
- badIcon: `icon is one of ${list(ICON_NAMES)}`,
374
+ badIcon: (value) => withNear(value, (near) => `There is no icon named ${value}. The closest is ${near}`, `icon is one of ${list(ICON_NAMES)}`),
368
375
  badView: `view is one of ${list(VIEWS)}`,
369
376
  badCarry: `carry is one of ${list(CARRIES)}. Write a tag as tag: <value>`,
370
377
  badSide: `side is one of ${list(SIDES)}`,
package/src/nearest.ts ADDED
@@ -0,0 +1,37 @@
1
+ /**
2
+ * 書いた語に一番近い語 (#3315)。誤りの文で、書き間違えた語の代わりに 1 つだけ候補を示す。
3
+ *
4
+ * 大文字と小文字を区別しない編集距離 (1 字の足し、消し、置き換えを 1 と数える) で比べる。
5
+ * 距離が 2 以下で、書いた語の字数より小さい時だけ近いとみなす。
6
+ * 字数より小さいことを求めるのは、1 字の語がどの 1 字の語とも距離 1 になり、何を書いても候補が出るのを避けるため。
7
+ * 同じ距離の語が 2 つ以上ある時は `candidates` の並びで先の語を返し、近い語が無ければ `undefined` を返す。
8
+ */
9
+ const MAX_DISTANCE = 2;
10
+
11
+ export function nearest(word: string, candidates: readonly string[]): string | undefined {
12
+ const written = [...word.toLowerCase()];
13
+ let limit = Math.min(MAX_DISTANCE, written.length - 1);
14
+ let best: string | undefined;
15
+ for (const candidate of candidates) {
16
+ const d = distance(written, [...candidate.toLowerCase()]);
17
+ if (d > limit) continue;
18
+ best = candidate;
19
+ // 後の語は、今の語より近い時だけ置き換える
20
+ limit = d - 1;
21
+ }
22
+ return best;
23
+ }
24
+
25
+ /** 2 つの字の並びの編集距離 */
26
+ function distance(a: readonly string[], b: readonly string[]): number {
27
+ let previous = Array.from({ length: b.length + 1 }, (_, j) => j);
28
+ for (let i = 1; i <= a.length; i++) {
29
+ const current = [i];
30
+ for (let j = 1; j <= b.length; j++) {
31
+ const replace = (previous[j - 1] as number) + (a[i - 1] === b[j - 1] ? 0 : 1);
32
+ current.push(Math.min((previous[j] as number) + 1, (current[j - 1] as number) + 1, replace));
33
+ }
34
+ previous = current;
35
+ }
36
+ return previous[b.length] as number;
37
+ }
package/src/parser.ts CHANGED
@@ -326,7 +326,7 @@ function lookOf(source: SourceLine, attrs: Map<string, Attr>, m: Messages): { no
326
326
  const color = attrs.get("color");
327
327
  if (color && !isOneOf(COLORS, color.value)) throw fail(source, color.at, m.badColor);
328
328
  const icon = attrs.get("icon");
329
- if (icon && !ICON_NAMES.includes(icon.value)) throw fail(source, icon.at, m.badIcon);
329
+ if (icon && !ICON_NAMES.includes(icon.value)) throw fail(source, icon.at, m.badIcon(icon.value));
330
330
  const note = attrs.get("note")?.value;
331
331
  return {
332
332
  ...(note !== undefined ? { note } : {}),
package/src/schema.ts CHANGED
@@ -11,6 +11,7 @@
11
11
  */
12
12
  import type { ErrorObject } from "ajv";
13
13
  import { type Allowed, FIELD_MESSAGES, type FieldMessages, type Forbidden } from "./field-messages";
14
+ import { nearest } from "./nearest";
14
15
  import { ICONS } from "./render/icons";
15
16
  import validate from "./schemas/validate-diagram.js";
16
17
  import { type NotationLang, STATE_ENDS } from "./words";
@@ -295,9 +296,22 @@ const RATIO = /%\)\$$/u;
295
296
 
296
297
  const escape = (key: string): string => key.replaceAll("~", "~0").replaceAll("/", "~1");
297
298
 
299
+ /** JSON Pointer `pointer` が指す `input` の中の値。辿れない時は `undefined` */
300
+ function valueAt(input: unknown, pointer: string): unknown {
301
+ let value = input;
302
+ for (const raw of pointer.split("/").slice(1)) {
303
+ if (typeof value !== "object" || value === null) return undefined;
304
+ value = (value as Record<string, unknown>)[raw.replaceAll("~1", "/").replaceAll("~0", "~")];
305
+ }
306
+ return value;
307
+ }
308
+
298
309
  /** 箱の飛び先と段の出典の URL の決まりの場所 (#3199) */
299
310
  const URL_RULE = "#/definitions/url/pattern";
300
311
 
312
+ /** 箱と区画のアイコン名の決まりの場所 (#3311)。近い名前が在れば一覧の代わりにそれを示す (#3315) */
313
+ const ICON_RULE = "#/definitions/icon/enum";
314
+
301
315
  /** commit の hash の決まり (`^[0-9a-f]{<最小>,<最大>}$`)。字の数の幅を読む */
302
316
  const HEX = /^\^\[0-9a-f\]\{(\d+),(\d+)\}\$$/u;
303
317
 
@@ -318,7 +332,7 @@ const branchesOf = (anyOf: ErrorObject, errors: readonly ErrorObject[]): ErrorOb
318
332
  (error.instancePath === anyOf.instancePath || error.instancePath.startsWith(`${anyOf.instancePath}/`)),
319
333
  );
320
334
 
321
- function toFieldError(error: ErrorObject, errors: readonly ErrorObject[], m: FieldMessages): FieldError {
335
+ function toFieldError(error: ErrorObject, errors: readonly ErrorObject[], m: FieldMessages, input: unknown): FieldError {
322
336
  const params = error.params as Record<string, unknown>;
323
337
  const at = error.instancePath;
324
338
  switch (error.keyword) {
@@ -351,8 +365,12 @@ function toFieldError(error: ErrorObject, errors: readonly ErrorObject[], m: Fie
351
365
  const tag = String(params.tag);
352
366
  return { pointer: `${at}/${escape(tag)}`, message: m.discriminator(tag, EVENT_TYPES) };
353
367
  }
354
- case "enum":
368
+ case "enum": {
369
+ const written = error.schemaPath === ICON_RULE ? valueAt(input, at) : undefined;
370
+ const near = typeof written === "string" ? nearest(written, ICON_NAMES) : undefined;
371
+ if (typeof written === "string" && near !== undefined) return { pointer: at, message: m.unknownIcon(written, near) };
355
372
  return { pointer: at, message: m.enum((params.allowedValues as unknown[]).map(String)) };
373
+ }
356
374
  case "type":
357
375
  return { pointer: at, message: m.type(String(params.type)) };
358
376
  case "minLength":
@@ -391,6 +409,6 @@ export function validateDiagram(input: unknown, options: CheckOptions = {}): Che
391
409
  const inBranch = new Set(all.filter((error) => error.keyword === "anyOf").flatMap((anyOf) => branchesOf(anyOf, all)));
392
410
  const m = FIELD_MESSAGES[options.lang ?? "ja"];
393
411
  // 条件付きの決まり (if / then) は中で落ちた決まりの誤りを別に出すので、包む側の if の誤りは出さない
394
- const errors = all.filter((error) => error.keyword !== "if" && !inBranch.has(error)).map((error) => toFieldError(error, all, m));
412
+ const errors = all.filter((error) => error.keyword !== "if" && !inBranch.has(error)).map((error) => toFieldError(error, all, m, input));
395
413
  return { ok: false, errors };
396
414
  }