@kensnzk/koyu 0.15.0 → 0.17.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/README.ja.md +14 -10
- package/README.md +16 -13
- package/dist/cli.js +162 -84
- package/dist/cli.js.map +1 -1
- package/dist/core/derive.d.ts +264 -0
- package/dist/core/derive.d.ts.map +1 -0
- package/dist/core/derive.js +576 -0
- package/dist/core/derive.js.map +1 -0
- package/dist/{check.d.ts → core/diagnose.d.ts} +15 -15
- package/dist/core/diagnose.d.ts.map +1 -0
- package/dist/{check.js → core/diagnose.js} +269 -223
- package/dist/core/diagnose.js.map +1 -0
- package/dist/{diff.d.ts → core/diff.d.ts} +3 -2
- package/dist/core/diff.d.ts.map +1 -0
- package/dist/{diff.js → core/diff.js} +89 -43
- package/dist/core/diff.js.map +1 -0
- package/dist/{fabric.d.ts → core/fabric.d.ts} +5 -3
- package/dist/core/fabric.d.ts.map +1 -0
- package/dist/{fabric.js → core/fabric.js} +12 -11
- package/dist/core/fabric.js.map +1 -0
- package/dist/{graph.d.ts → core/graph.d.ts} +2 -4
- package/dist/core/graph.d.ts.map +1 -0
- package/dist/{graph.js → core/graph.js} +53 -54
- package/dist/core/graph.js.map +1 -0
- package/dist/core/light.d.ts +24 -0
- package/dist/core/light.d.ts.map +1 -0
- package/dist/{light.js → core/light.js} +17 -12
- package/dist/core/light.js.map +1 -0
- package/dist/{model.d.ts → core/model.d.ts} +178 -18
- package/dist/core/model.d.ts.map +1 -0
- package/dist/{model.js → core/model.js} +307 -68
- package/dist/core/model.js.map +1 -0
- package/dist/core/parse.d.ts.map +1 -0
- package/dist/{parse.js → core/parse.js} +546 -133
- package/dist/core/parse.js.map +1 -0
- package/dist/{poly.d.ts → core/poly.d.ts} +3 -6
- package/dist/core/poly.d.ts.map +1 -0
- package/dist/{poly.js → core/poly.js} +8 -13
- package/dist/core/poly.js.map +1 -0
- package/dist/core/site.d.ts.map +1 -0
- package/dist/{site.js → core/site.js} +2 -2
- package/dist/core/site.js.map +1 -0
- package/dist/core/tolerance.d.ts +26 -0
- package/dist/core/tolerance.d.ts.map +1 -0
- package/dist/core/tolerance.js +48 -0
- package/dist/core/tolerance.js.map +1 -0
- package/dist/{vertical.d.ts → core/vertical.d.ts} +48 -12
- package/dist/core/vertical.d.ts.map +1 -0
- package/dist/{vertical.js → core/vertical.js} +53 -105
- package/dist/core/vertical.js.map +1 -0
- package/dist/core/vocabulary.d.ts +40 -0
- package/dist/core/vocabulary.d.ts.map +1 -0
- package/dist/core/vocabulary.js +176 -0
- package/dist/core/vocabulary.js.map +1 -0
- package/dist/{axo.d.ts → draw/axo.d.ts} +1 -1
- package/dist/draw/axo.d.ts.map +1 -0
- package/dist/{axo.js → draw/axo.js} +53 -81
- package/dist/draw/axo.js.map +1 -0
- package/dist/draw/index.d.ts +3 -0
- package/dist/draw/index.d.ts.map +1 -0
- package/dist/draw/index.js +11 -0
- package/dist/draw/index.js.map +1 -0
- package/dist/draw/plan.d.ts +10 -0
- package/dist/draw/plan.d.ts.map +1 -0
- package/dist/draw/plan.js +284 -0
- package/dist/draw/plan.js.map +1 -0
- package/dist/index.d.ts +16 -11
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +55 -12
- package/dist/index.js.map +1 -1
- package/dist/mcp.js +112 -50
- package/dist/mcp.js.map +1 -1
- package/dist/parse-file.d.ts +1 -1
- package/dist/parse-file.d.ts.map +1 -1
- package/dist/parse-file.js +19 -4
- package/dist/parse-file.js.map +1 -1
- package/dist/validate/access.d.ts +6 -0
- package/dist/validate/access.d.ts.map +1 -0
- package/dist/validate/access.js +248 -0
- package/dist/validate/access.js.map +1 -0
- package/dist/validate/envelope.d.ts +4 -0
- package/dist/validate/envelope.d.ts.map +1 -0
- package/dist/validate/envelope.js +55 -0
- package/dist/validate/envelope.js.map +1 -0
- package/dist/validate/index.d.ts +60 -0
- package/dist/validate/index.d.ts.map +1 -0
- package/dist/validate/index.js +77 -0
- package/dist/validate/index.js.map +1 -0
- package/dist/validate/light.d.ts +6 -0
- package/dist/validate/light.d.ts.map +1 -0
- package/dist/validate/light.js +28 -0
- package/dist/validate/light.js.map +1 -0
- package/dist/validate/runs.d.ts +16 -0
- package/dist/validate/runs.d.ts.map +1 -0
- package/dist/validate/runs.js +60 -0
- package/dist/validate/runs.js.map +1 -0
- package/dist/validate/site.d.ts +6 -0
- package/dist/validate/site.d.ts.map +1 -0
- package/dist/validate/site.js +63 -0
- package/dist/validate/site.js.map +1 -0
- package/editors/vscode/.vscodeignore +2 -0
- package/editors/vscode/README.md +28 -0
- package/editors/vscode/extension.js +150 -0
- package/editors/vscode/language-configuration.json +19 -0
- package/editors/vscode/package.json +57 -0
- package/editors/vscode/syntaxes/koyu.tmLanguage.json +226 -0
- package/examples/basement/main.muro +3 -3
- package/examples/complex/main.muro +1 -1
- package/examples/complex/podium.muro +1 -1
- package/examples/complex/site.muro +6 -6
- package/examples/house/L2.muro +1 -1
- package/examples/house/main.muro +2 -2
- package/examples/house/site.muro +4 -4
- package/examples/house.muro +7 -7
- package/examples/mansion.muro +3 -3
- package/examples/office.muro +4 -4
- package/examples/steps/01-one-room.muro +1 -1
- package/examples/steps/02-two-rooms.muro +1 -1
- package/examples/steps/03-door.muro +1 -1
- package/examples/steps/04-exterior.muro +2 -2
- package/examples/steps/05-two-storeys.muro +2 -2
- package/examples/steps/06-finished.muro +3 -3
- package/examples/tower/L2.muro +1 -1
- package/examples/tower/main.muro +1 -1
- package/examples/tower/site.muro +4 -4
- package/examples/twin/main.muro +2 -2
- package/examples/twin/podium.muro +4 -4
- package/examples/twin/site.muro +8 -8
- package/examples/two-rooms.canonical.json +6 -4
- package/examples/two-rooms.muro +3 -3
- package/package.json +18 -3
- package/src/cli.ts +173 -83
- package/src/core/derive.ts +869 -0
- package/src/{check.ts → core/diagnose.ts} +313 -235
- package/src/{diff.ts → core/diff.ts} +91 -43
- package/src/{fabric.ts → core/fabric.ts} +22 -12
- package/src/{graph.ts → core/graph.ts} +64 -63
- package/src/core/light.ts +69 -0
- package/src/{model.ts → core/model.ts} +361 -74
- package/src/{parse.ts → core/parse.ts} +618 -135
- package/src/{poly.ts → core/poly.ts} +8 -16
- package/src/{site.ts → core/site.ts} +2 -2
- package/src/core/tolerance.ts +55 -0
- package/src/{vertical.ts → core/vertical.ts} +73 -116
- package/src/core/vocabulary.ts +210 -0
- package/src/{axo.ts → draw/axo.ts} +51 -94
- package/src/draw/index.ts +11 -0
- package/src/draw/plan.ts +344 -0
- package/src/index.ts +201 -34
- package/src/mcp.ts +119 -50
- package/src/parse-file.ts +19 -5
- package/src/validate/access.ts +291 -0
- package/src/validate/envelope.ts +59 -0
- package/src/validate/index.ts +107 -0
- package/src/validate/light.ts +43 -0
- package/src/validate/runs.ts +87 -0
- package/src/validate/site.ts +84 -0
- package/dist/axo.d.ts.map +0 -1
- package/dist/axo.js.map +0 -1
- package/dist/check.d.ts.map +0 -1
- package/dist/check.js.map +0 -1
- package/dist/diff.d.ts.map +0 -1
- package/dist/diff.js.map +0 -1
- package/dist/fabric.d.ts.map +0 -1
- package/dist/fabric.js.map +0 -1
- package/dist/graph.d.ts.map +0 -1
- package/dist/graph.js.map +0 -1
- package/dist/light.d.ts +0 -15
- package/dist/light.d.ts.map +0 -1
- package/dist/light.js.map +0 -1
- package/dist/model.d.ts.map +0 -1
- package/dist/model.js.map +0 -1
- package/dist/parse.d.ts.map +0 -1
- package/dist/parse.js.map +0 -1
- package/dist/plan.d.ts +0 -10
- package/dist/plan.d.ts.map +0 -1
- package/dist/plan.js +0 -354
- package/dist/plan.js.map +0 -1
- package/dist/poly.d.ts.map +0 -1
- package/dist/poly.js.map +0 -1
- package/dist/site.d.ts.map +0 -1
- package/dist/site.js.map +0 -1
- package/dist/vertical.d.ts.map +0 -1
- package/dist/vertical.js.map +0 -1
- package/src/light.ts +0 -63
- package/src/plan.ts +0 -472
- /package/dist/{parse.d.ts → core/parse.d.ts} +0 -0
- /package/dist/{site.d.ts → core/site.d.ts} +0 -0
|
@@ -7,10 +7,21 @@ import * as poly from "./poly.js";
|
|
|
7
7
|
export type AttrValue = string | number;
|
|
8
8
|
export type Attrs = Record<string, AttrValue>;
|
|
9
9
|
|
|
10
|
-
/**
|
|
11
|
-
|
|
10
|
+
/**
|
|
11
|
+
* このツールが受理する言語版 (ADR-0017)。旧版は意味保存の場合のみ受理される (checkが検査する)。
|
|
12
|
+
* **この並びが版の新旧の順である** — 版の比較はここの添字で行う (辞書順では 0.5 が 1.0 より後になる)
|
|
13
|
+
*/
|
|
14
|
+
export const SUPPORTED_LANGUAGE_VERSIONS: readonly string[] = ["0.1", "0.2", "0.3", "0.4", "0.5", "1.0", "1.1"];
|
|
12
15
|
/** 版宣言を省略したときの解釈 — 常に最新版の意味論 (省略はツール版を跨いで意味安定ではない) */
|
|
13
|
-
export const DEFAULT_LANGUAGE_VERSION = "
|
|
16
|
+
export const DEFAULT_LANGUAGE_VERSION = "1.1";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 機械形式 (正準JSON) が自分を名乗る版 (ADR-0036)。**言語版でもツール版でもない** —
|
|
20
|
+
* 数えるのは綴りだけである。minorはキーが増えたとき (増えたキーを持たない文書のバイトは変わらない)、
|
|
21
|
+
* majorは既存のキーの名前・並び・照合順・正規化・数の綴りが変わったとき (既存の文書のバイトが変わる)。
|
|
22
|
+
* 意味論の版は muro が持つので、`koyu` キー (書かれた版宣言の素通し) とは別の面である
|
|
23
|
+
*/
|
|
24
|
+
export const CANONICAL_FORMAT = "koyu-canonical/1.1";
|
|
14
25
|
|
|
15
26
|
/** 方位。edge指定は「最初に書いた空間」の矩形から見た辺。N=+Y, S=-Y, E=+X, W=-X */
|
|
16
27
|
export type Edge = "N" | "E" | "S" | "W";
|
|
@@ -72,13 +83,17 @@ export interface Space {
|
|
|
72
83
|
* パスの第一義は集計の階層 — レベルは既定で先頭セグメントから読むが、
|
|
73
84
|
* 階を跨ぐくくり (メゾネット) は level: 属性で明示する (ADR-0008) */
|
|
74
85
|
path: string;
|
|
75
|
-
/**
|
|
76
|
-
|
|
86
|
+
/**
|
|
87
|
+
* 自由なラベル。**core はこの語を一切読まない**ので、綴りは何の判定にも効かない。
|
|
88
|
+
* 集計の軸と図面の刷り字にだけ現れる。任意 — 書かなければ何も無い。
|
|
89
|
+
* 構成の事実 (外部・吹抜け) は `outside:` / `void:` の宣言の側にある
|
|
90
|
+
*/
|
|
91
|
+
type?: string;
|
|
77
92
|
/** 所属レベル名 (パス先頭セグメント、または level: 属性) */
|
|
78
93
|
level?: string;
|
|
79
94
|
/** グリッド参照。複数矩形の合併でL字などを表す (rectsと同順) */
|
|
80
95
|
grids: GridRef[];
|
|
81
|
-
/** グリッド解決後のmm
|
|
96
|
+
/** グリッド解決後のmm矩形の合併。`outside:1` の空間などは空。**書かれた割付** (セル) であって形ではない */
|
|
82
97
|
rects: Rect[];
|
|
83
98
|
/**
|
|
84
99
|
* 導出された領域 — 凸片の集合 (ADR-0022)。既定は rects をそのまま写したもので、
|
|
@@ -186,6 +201,18 @@ export interface DrawnLine {
|
|
|
186
201
|
a: Pt;
|
|
187
202
|
b: Pt;
|
|
188
203
|
line: number;
|
|
204
|
+
/**
|
|
205
|
+
* 切り分けの帰結 — **導出したその場で記録する** (ADR-0041)。
|
|
206
|
+
*
|
|
207
|
+
* `derivePieces` は割付から形を起こしながら線を順に適用するので、その時点でしか
|
|
208
|
+
* 「この線が実際に何かを切ったか」を正しくは言えない。診断が後から計算し直すと、
|
|
209
|
+
* **既に切られた形**を相手に窓を組み立てることになり、母集団が食い違う。
|
|
210
|
+
* 判定と操作を同じ場所に置く — 同じ関数を通すだけでなく、同じ母集団を見る (ADR-0041)。
|
|
211
|
+
*
|
|
212
|
+
* `undetermined` = 残す側が決まらない (LIN01) / `nothing` = 何も切らない (LIN03)。
|
|
213
|
+
* 正準JSONには出ない — 書かれた構成ではなく導出の帰結だからである。
|
|
214
|
+
*/
|
|
215
|
+
effect?: "cut" | "nothing" | "undetermined";
|
|
189
216
|
}
|
|
190
217
|
|
|
191
218
|
/** 境界はどちらの空間にも属さない。二つの空間パスを結ぶ第一級の関係 */
|
|
@@ -216,7 +243,7 @@ export interface Boundary {
|
|
|
216
243
|
|
|
217
244
|
/** エラー・警告の位置表記 — 合成時はどのレイヤーのことかを言葉にする (ADR-0010) */
|
|
218
245
|
export function srcRef(line: number, file?: string): string {
|
|
219
|
-
return `${file ? `${file}:` : ""}${line}
|
|
246
|
+
return `${file ? `${file}:` : ""}line ${line}`;
|
|
220
247
|
}
|
|
221
248
|
|
|
222
249
|
export interface Model {
|
|
@@ -234,10 +261,41 @@ export interface Model {
|
|
|
234
261
|
polygons: Map<string, SitePolygon>;
|
|
235
262
|
/** 柱の宣言 (ADR-0023)。位置は書かれない — 通り芯の交点から導出される */
|
|
236
263
|
columns: ColumnDecl[];
|
|
237
|
-
/**
|
|
264
|
+
/**
|
|
265
|
+
* 合成に参加したレイヤー (ローダーのキー)。**この並びが層の強度順序である** (docs/reference/muro/import.md)。
|
|
266
|
+
* entry が添字0で最も弱く、**後の層ほど強い**。単一ソースの parse では空。
|
|
267
|
+
*
|
|
268
|
+
* 並びは import 行を深さ優先で平坦化した順で、同じ層が二度現れれば最初の位置を保つ。
|
|
269
|
+
* 著者が import 行を並べる行為が強度の宣言であり、暗黙の解決はどこにも無い。
|
|
270
|
+
*/
|
|
238
271
|
layers: string[];
|
|
272
|
+
/**
|
|
273
|
+
* 属性ごとの出所 (合成の規則6 — 出所が追える)。
|
|
274
|
+
* キーは `<種別>:<対象>:<属性キー>`、値は `layers` の添字。
|
|
275
|
+
*
|
|
276
|
+
* 「最終的な値を、どの層のどの行が与えたか」を言えるようにするためにある。
|
|
277
|
+
* `over` はここを読んで**強い層の意見だけを通す** — 走査の順ではなく強度で決まる。
|
|
278
|
+
*/
|
|
279
|
+
attrSrc: Map<string, number>;
|
|
239
280
|
/** koyu版が明示宣言されたか (base層でのみ・一度だけ — ADR-0017の合成規則の管理用) */
|
|
240
281
|
versionDeclared?: boolean;
|
|
282
|
+
/**
|
|
283
|
+
* muro 1.0 で入った合成の語 (`over` / `drop` / `over` 直下の `+` `-` `=`) が書かれた箇所 (ADR-0038)。
|
|
284
|
+
* **上書きの跡は機械形式に残らない** (合成の規則5) ので、正準JSONにも合成後のモデルにも
|
|
285
|
+
* 「上書きが書かれたかどうか」は残らない。VER04 が旧版の宣言を捕まえるには宣言の出所が要るので、
|
|
286
|
+
* ここだけが走査の順に持つ。診断のためだけの列であり、導出も正準形もこれを読まない
|
|
287
|
+
*/
|
|
288
|
+
compositionEdits: CompositionEdit[];
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/** `over` / `drop` / 集合編集の宣言 (VER04 のための出所 — ADR-0038) */
|
|
292
|
+
export interface CompositionEdit {
|
|
293
|
+
/** 書かれた語そのもの — `over` / `drop` / `+` / `-` / `=` */
|
|
294
|
+
word: string;
|
|
295
|
+
/** 何に向けられた宣言か (メッセージに出す。`over /L1/a` の `/L1/a` など) */
|
|
296
|
+
subject: string;
|
|
297
|
+
line: number;
|
|
298
|
+
file?: string;
|
|
241
299
|
}
|
|
242
300
|
|
|
243
301
|
/** 平面上の点 (mm) */
|
|
@@ -292,7 +350,7 @@ export interface SitePolygon {
|
|
|
292
350
|
file?: string;
|
|
293
351
|
}
|
|
294
352
|
|
|
295
|
-
/** 多角形の面積 ㎡。シューレースの実体は poly.ts にひとつだけある
|
|
353
|
+
/** 多角形の面積 ㎡。シューレースの実体は poly.ts にひとつだけある */
|
|
296
354
|
export const polygonAreaM2 = (points: Pt[]): number => poly.area(points) / 1e6;
|
|
297
355
|
|
|
298
356
|
/** 線分同士が内部で交差するか (端点・境界上の接触は交差としない、許容誤差eps mm)。交点を返す */
|
|
@@ -323,14 +381,6 @@ export function polygonSelfIntersection(poly: Pt[], eps = 1): Pt | undefined {
|
|
|
323
381
|
return undefined;
|
|
324
382
|
}
|
|
325
383
|
|
|
326
|
-
/**
|
|
327
|
-
* 矩形が多角形からはみ出す点 (完全に内側ならundefined)。凹多角形にも正しい:
|
|
328
|
-
* 四隅の内包に加え、多角形の頂点の矩形内への入り込みと、辺同士の内部交差を検査する
|
|
329
|
-
*/
|
|
330
|
-
export function rectEscapesPolygon(r: Rect, poly: Pt[], eps = 1): Pt | undefined {
|
|
331
|
-
return shapeEscapesPolygon(rectToPoly(r), poly, eps);
|
|
332
|
-
}
|
|
333
|
-
|
|
334
384
|
/** 凸片が多角形からはみ出す点 (導出された領域を敷地形状と照合する — ADR-0022) */
|
|
335
385
|
export function shapeEscapesPolygon(shape: Pt[], poly: Pt[], eps = 1): Pt | undefined {
|
|
336
386
|
for (const c of shape) if (!pointInPolygon(c, poly, eps)) return c;
|
|
@@ -363,13 +413,13 @@ export class SourceError extends Error {
|
|
|
363
413
|
/** 出所ファイル (合成時) */
|
|
364
414
|
public file?: string,
|
|
365
415
|
) {
|
|
366
|
-
super(`${file ? `${file}:` : ""}${line}
|
|
416
|
+
super(`${file ? `${file}:` : ""}line ${line}: ${raw}`);
|
|
367
417
|
this.name = "SourceError";
|
|
368
418
|
}
|
|
369
419
|
}
|
|
370
420
|
|
|
371
421
|
/**
|
|
372
|
-
* 空間の導出された領域 (ADR-0022 /
|
|
422
|
+
* 空間の導出された領域 (ADR-0022 / docs/reference/form/index.md)。**形を読むときの唯一の入口**。
|
|
373
423
|
* parse の出口で必ず埋まるので、割付への退避は「未parseのModelを手で組んだとき」だけに効く。
|
|
374
424
|
* この式が各所に散っていたことが、rects と pieces の取り違えを四度生んだ根である。
|
|
375
425
|
*/
|
|
@@ -386,29 +436,6 @@ export function areaM2(s: Space): number | undefined {
|
|
|
386
436
|
/** 矩形を頂点列へ (反時計回り) */
|
|
387
437
|
export const rectToPoly = poly.rectToPoly;
|
|
388
438
|
|
|
389
|
-
/** 凸多角形を半平面で切る (Sutherland–Hodgman)。
|
|
390
|
-
* 半平面は有向線分 a→b の左側 (外積>0)。切り落とされて空なら [] */
|
|
391
|
-
export function clipHalfPlane(poly: Pt[], a: Pt, b: Pt, keepLeft: boolean, eps = 1e-6): Pt[] {
|
|
392
|
-
const side = (p: Pt): number => {
|
|
393
|
-
const v = (b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x);
|
|
394
|
-
return keepLeft ? v : -v;
|
|
395
|
-
};
|
|
396
|
-
const out: Pt[] = [];
|
|
397
|
-
for (let i = 0; i < poly.length; i++) {
|
|
398
|
-
const p = poly[i]!;
|
|
399
|
-
const q = poly[(i + 1) % poly.length]!;
|
|
400
|
-
const sp = side(p);
|
|
401
|
-
const sq = side(q);
|
|
402
|
-
if (sp >= -eps) out.push(p);
|
|
403
|
-
if ((sp > eps && sq < -eps) || (sp < -eps && sq > eps)) {
|
|
404
|
-
const t = sp / (sp - sq);
|
|
405
|
-
out.push({ x: p.x + t * (q.x - p.x), y: p.y + t * (q.y - p.y) });
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
// 退化 (面積ゼロ) は捨てる
|
|
409
|
-
return out.length >= 3 && polygonAreaM2(out) > 1e-9 ? out : [];
|
|
410
|
-
}
|
|
411
|
-
|
|
412
439
|
/** 頂点列の外接矩形 */
|
|
413
440
|
export const polyBounds = poly.bounds;
|
|
414
441
|
|
|
@@ -427,8 +454,8 @@ export function columnSites(
|
|
|
427
454
|
const floors = [...model.spaces.values()].filter(
|
|
428
455
|
(s) =>
|
|
429
456
|
s.level === level &&
|
|
430
|
-
s
|
|
431
|
-
s
|
|
457
|
+
!isOutside(s) &&
|
|
458
|
+
!isVoid(s) &&
|
|
432
459
|
s.rects.length > 0 &&
|
|
433
460
|
// 空しか支えない床には柱を立てない (ADR-0030): 半屋外で上に床も無い
|
|
434
461
|
// 屋上庭園・テラスは、柱が持ち上げるものを持たない
|
|
@@ -449,7 +476,7 @@ export function columnSites(
|
|
|
449
476
|
pointInPolygon({ x: gx.v, y: gy.v }, p, 1),
|
|
450
477
|
),
|
|
451
478
|
);
|
|
452
|
-
if (inside) out.push({ x: gx.v, y: gy.v, grid: `${gx.n}
|
|
479
|
+
if (inside) out.push({ x: gx.v, y: gy.v, grid: `${gx.n}/${gy.n}` });
|
|
453
480
|
}
|
|
454
481
|
}
|
|
455
482
|
return out;
|
|
@@ -457,7 +484,7 @@ export function columnSites(
|
|
|
457
484
|
|
|
458
485
|
/**
|
|
459
486
|
* そのレベルに立つ柱を導く (ADR-0023)。
|
|
460
|
-
* 通り芯の交点のうち、床のある空間 (
|
|
487
|
+
* 通り芯の交点のうち、床のある空間 (`outside:1`・`void:1` を除く) の内側にあるものへ柱を置く。
|
|
461
488
|
* 位置はどこにも書かれていない — 通りと床という既にある事実の交わりから現れる
|
|
462
489
|
*/
|
|
463
490
|
export function columnsFor(model: Model, level: string): Column[] {
|
|
@@ -484,7 +511,23 @@ export function columnsFor(model: Model, level: string): Column[] {
|
|
|
484
511
|
}
|
|
485
512
|
|
|
486
513
|
/**
|
|
487
|
-
*
|
|
514
|
+
* 建物の外部か。**空間の型ではなく `outside:1` の宣言で決まる。**
|
|
515
|
+
*
|
|
516
|
+
* 型の位置は開かれた語彙であって、そこから構成の事実を読むと綴りが意味を持ってしまう。
|
|
517
|
+
* `exteriorr` の一字で外部が屋内になり、延床が倍になりながら check は緑だった。
|
|
518
|
+
* 宣言なら台帳が綴りを守る (ATT03/ATT02) — 開かれた語彙を殺さずに塞ぐ唯一の形である。
|
|
519
|
+
*/
|
|
520
|
+
export function isOutside(s: Space): boolean {
|
|
521
|
+
return s.attrs["outside"] === 1;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
/** 吹抜けか — 床が無いので、面積にも通行にも数えない。[[isOutside]] と同じ理由で宣言である */
|
|
525
|
+
export function isVoid(s: Space): boolean {
|
|
526
|
+
return s.attrs["void"] === 1;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
/**
|
|
530
|
+
* 半屋外か — 宣言ではなく導出。外部 (outside:1) に対して
|
|
488
531
|
* open または air:1 (手すり等、遮蔽しない物) の境界で接する空間は半屋外である (ADR-0007)
|
|
489
532
|
*/
|
|
490
533
|
export function isSemiOutdoor(model: Model, s: Space): boolean {
|
|
@@ -493,7 +536,8 @@ export function isSemiOutdoor(model: Model, s: Space): boolean {
|
|
|
493
536
|
if (b.kind !== "open" && !b.air) continue;
|
|
494
537
|
const other = b.a === s.path ? b.b : b.b === s.path ? b.a : undefined;
|
|
495
538
|
if (!other) continue;
|
|
496
|
-
|
|
539
|
+
const o = model.spaces.get(other);
|
|
540
|
+
if (o && isOutside(o)) return true;
|
|
497
541
|
}
|
|
498
542
|
return false;
|
|
499
543
|
}
|
|
@@ -552,7 +596,7 @@ export function zoneAreaM2(model: Model, zonePath: string): number {
|
|
|
552
596
|
*/
|
|
553
597
|
export function isIndoor(model: Model, s: Space): boolean {
|
|
554
598
|
if (s.rects.length === 0) return false;
|
|
555
|
-
if (s
|
|
599
|
+
if (isOutside(s) || isVoid(s)) return false;
|
|
556
600
|
return !isSemiOutdoor(model, s);
|
|
557
601
|
}
|
|
558
602
|
|
|
@@ -591,10 +635,85 @@ export function displayName(s: Space): string {
|
|
|
591
635
|
return typeof n === "string" ? n : (s.path.split("/").pop() ?? s.path);
|
|
592
636
|
}
|
|
593
637
|
|
|
638
|
+
/**
|
|
639
|
+
* 開口が主張する同一性の名 (ADR-0039)。
|
|
640
|
+
*
|
|
641
|
+
* 開口の同一性は「含む対象 + その中で一意な名」から導かれる (docs/reference/scope.md)。だが
|
|
642
|
+
* `name:` はアセットからも流れ込む — そしてアセットの `name` は**型の名**である
|
|
643
|
+
* (`asset W1 window … name:掃き出し窓`)。同じ建具を一枚の壁に二枚並べるのは
|
|
644
|
+
* ごく普通の設計であり、型の名を同一性の主張として読めば、それが衝突になってしまう。
|
|
645
|
+
*
|
|
646
|
+
* **主張は、その開口の行に書かれた名だけである。**参照したアセットの名と同じ値なら、
|
|
647
|
+
* それは継いだ型の名であって主張ではない (undefined)。
|
|
648
|
+
*/
|
|
649
|
+
export function openingIdentity(model: Model, o: Opening): string | undefined {
|
|
650
|
+
const n = o.attrs["name"];
|
|
651
|
+
if (n === undefined || n === "") return undefined;
|
|
652
|
+
const name = String(n);
|
|
653
|
+
const inherited = o.ref === undefined ? undefined : model.assets.get(o.ref)?.attrs["name"];
|
|
654
|
+
if (inherited !== undefined && String(inherited) === name) return undefined;
|
|
655
|
+
return name;
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
// ---- 同一性の生成 (ADR-0039) ----
|
|
659
|
+
|
|
660
|
+
/**
|
|
661
|
+
* 生成される uid の綴り — 接頭辞 `u-` + 16字、合わせて18字。
|
|
662
|
+
*
|
|
663
|
+
* 字母は Crockford base32 の小文字 (`i` `l` `o` `u` を持たない)。**接頭辞の `u` は
|
|
664
|
+
* 字母に無い**ので、生成された uid は必ず一つだけ `u` を持ち、それが先頭である。
|
|
665
|
+
* 16字 × 5ビット = **80ビット**。
|
|
666
|
+
*
|
|
667
|
+
* 接頭辞があるのは、数字だけの綴りを構造的に不可能にするためである (UID01 —
|
|
668
|
+
* 数値化でトークンの区別が失われる)。種別 (space / zone) は綴りに入れない —
|
|
669
|
+
* uid は不透明であり、綴りから何かが読めてはならない。
|
|
670
|
+
*/
|
|
671
|
+
const UID_ALPHABET = "0123456789abcdefghjkmnpqrstvwxyz";
|
|
672
|
+
const UID_LENGTH = 16;
|
|
673
|
+
const UID_PREFIX = "u-";
|
|
674
|
+
|
|
675
|
+
/**
|
|
676
|
+
* 新しい uid を作る (ADR-0039)。**パスからもモデルの中身からも導出しない** —
|
|
677
|
+
* 導出すれば改名でトークンが変わり、uid の意味 (改名を跨ぐ) が消える。
|
|
678
|
+
*
|
|
679
|
+
* 保証は二段である。
|
|
680
|
+
*
|
|
681
|
+
* 1. **合成済みのこのモデルとは衝突しない。**検査して作るので確実である
|
|
682
|
+
* 2. **まだ合成されていない層・他のリポジトリとは、確率でしか衝突しない。**
|
|
683
|
+
* 80ビットの乱数なので、100万個を集めても衝突確率は 10⁻¹² を下回る。
|
|
684
|
+
* **確実さが要るなら、合成して `check` を通すこと** — UID03 だけが実際に一意性を証明する
|
|
685
|
+
*
|
|
686
|
+
* 付与は明示の行為である。この関数を呼ばないかぎり、どのツールも uid を書かない (opt-in)。
|
|
687
|
+
*/
|
|
688
|
+
export function newUids(model: Model, count = 1): string[] {
|
|
689
|
+
if (!Number.isInteger(count) || count < 1) throw new RangeError(`count is a positive integer: ${count}`);
|
|
690
|
+
const taken = new Set<string>();
|
|
691
|
+
for (const s of model.spaces.values()) {
|
|
692
|
+
const v = s.attrs["uid"];
|
|
693
|
+
if (v !== undefined) taken.add(String(v));
|
|
694
|
+
}
|
|
695
|
+
for (const z of model.zones.values()) {
|
|
696
|
+
const v = z.attrs["uid"];
|
|
697
|
+
if (v !== undefined) taken.add(String(v));
|
|
698
|
+
}
|
|
699
|
+
const out: string[] = [];
|
|
700
|
+
const buf = new Uint8Array(UID_LENGTH);
|
|
701
|
+
while (out.length < count) {
|
|
702
|
+
globalThis.crypto.getRandomValues(buf);
|
|
703
|
+
// 1バイト (0..255) の下位5ビットは 0..31 に一様である (256 = 32 × 8) — 剰余の偏りは無い
|
|
704
|
+
let token = UID_PREFIX;
|
|
705
|
+
for (const b of buf) token += UID_ALPHABET[b & 31];
|
|
706
|
+
if (taken.has(token)) continue;
|
|
707
|
+
taken.add(token);
|
|
708
|
+
out.push(token);
|
|
709
|
+
}
|
|
710
|
+
return out;
|
|
711
|
+
}
|
|
712
|
+
|
|
594
713
|
/** 正準JSONの空間エントリ (書かれた表記・正準順)。semantic diff (ADR-0018) が比較基底として共有する */
|
|
595
714
|
export function canonicalSpaceEntry(s: Space): Record<string, unknown> {
|
|
596
715
|
return {
|
|
597
|
-
type: s.type,
|
|
716
|
+
...(s.type !== undefined ? { type: s.type } : {}),
|
|
598
717
|
// 明示の level: (パス先頭セグメントと異なる所属 — メゾネット等) は書かれた構成として保存する。
|
|
599
718
|
// これが無いとJSONだけでは所属レベル (垂直検査・集計・既定境界の前提) を復元できない
|
|
600
719
|
...(s.level !== undefined && s.path.split("/")[1] !== s.level ? { level: s.level } : {}),
|
|
@@ -645,7 +764,7 @@ export function canonicalSegEntry(g: Seg): Record<string, unknown> {
|
|
|
645
764
|
/** 正準JSONの境界エントリ。a/bの向きは書かれた表記 (aキー) として保存する — edge/swingはa側から読む */
|
|
646
765
|
export function canonicalBoundaryEntry(b: Boundary): Record<string, unknown> {
|
|
647
766
|
return {
|
|
648
|
-
between: [b.a, b.b].sort(),
|
|
767
|
+
between: [b.a, b.b].sort(compareCanonical),
|
|
649
768
|
a: b.a,
|
|
650
769
|
kind: b.kind,
|
|
651
770
|
...(b.t !== undefined ? { t: b.t } : {}),
|
|
@@ -661,11 +780,90 @@ export function canonicalBoundaryEntry(b: Boundary): Record<string, unknown> {
|
|
|
661
780
|
};
|
|
662
781
|
}
|
|
663
782
|
|
|
783
|
+
/**
|
|
784
|
+
* 境界を正準の順に並べる (ADR-0041)。
|
|
785
|
+
*
|
|
786
|
+
* **宣言順は正準JSONが捨てる情報である。**捨てられる情報が形を変えてはならないので、
|
|
787
|
+
* 境界を順に読む導出 — 線の切り分け (`derivePieces`) と、関係の同一性の綴り
|
|
788
|
+
* (`derive` の `a|b@i`) — はこの並びを使う。
|
|
789
|
+
*
|
|
790
|
+
* 既定境界 (derived) は正準JSONに出ないが、`model.spaces` の並び (= 宣言順) から
|
|
791
|
+
* 導かれるので、同じ規則で並べ直さないと同じ病を持つ。
|
|
792
|
+
*/
|
|
793
|
+
export function canonicalBoundaryOrder(model: Model): Boundary[] {
|
|
794
|
+
return [...model.boundaries]
|
|
795
|
+
.map((b, i) => ({ b, key: JSON.stringify(canonicalBoundaryEntry(b)), i }))
|
|
796
|
+
.sort((p, q) => compareCanonical(p.key, q.key) || p.i - q.i)
|
|
797
|
+
.map((x) => x.b);
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
/**
|
|
801
|
+
* The openings of one boundary in canonical order, and its `seg`s likewise.
|
|
802
|
+
*
|
|
803
|
+
* **The same disease as `canonicalBoundaryOrder`, one level down.** The identity of an opening is
|
|
804
|
+
* `<the boundary's ref>/<index within the boundary>`, and that index used to be the declaration
|
|
805
|
+
* order — which the canonical form discards, since it sorts openings by content
|
|
806
|
+
* (`canonicalBoundaryEntry`). So two sources with byte-identical canonical JSON gave the same
|
|
807
|
+
* spelling to *different* openings: `…@4/0` was the door in one and the window in the other.
|
|
808
|
+
*
|
|
809
|
+
* A generated pair found this — the hand-written witnesses never swapped two openings on one
|
|
810
|
+
* boundary, and neither did any conformance case.
|
|
811
|
+
*/
|
|
812
|
+
export function canonicalOpeningOrder(b: Boundary): Opening[] {
|
|
813
|
+
return [...b.openings]
|
|
814
|
+
.map((o, i) => ({ o, key: JSON.stringify(canonicalOpeningEntry(o)), i }))
|
|
815
|
+
.sort((p, q) => compareCanonical(p.key, q.key) || p.i - q.i)
|
|
816
|
+
.map((x) => x.o);
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
export function canonicalSegOrder(b: Boundary): Seg[] {
|
|
820
|
+
return [...b.segs]
|
|
821
|
+
.map((g, i) => ({ g, key: JSON.stringify(canonicalSegEntry(g)), i }))
|
|
822
|
+
.sort((p, q) => compareCanonical(p.key, q.key) || p.i - q.i)
|
|
823
|
+
.map((x) => x.g);
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
/**
|
|
827
|
+
* Spaces in canonical order (path collation).
|
|
828
|
+
*
|
|
829
|
+
* **Declaration order is information the canonical form discards** — `toCanonical` sorts the
|
|
830
|
+
* `spaces` keys by collation. So every derivation that reads spaces in order, such as the
|
|
831
|
+
* ordering of `Form.spaces`, uses this. The `Map` insertion order itself is left alone:
|
|
832
|
+
* diagnostics are contractually in **scan order** (ADR-0028), so another reader needs
|
|
833
|
+
* declaration order.
|
|
834
|
+
*/
|
|
835
|
+
export function canonicalSpaceOrder(model: Model): Space[] {
|
|
836
|
+
return [...model.spaces.keys()].sort(compareCanonical).map((p) => model.spaces.get(p)!);
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
/**
|
|
840
|
+
* Puts the spelling of every region into canonical order.
|
|
841
|
+
*
|
|
842
|
+
* **The order the parts of a `+` union were written in is information the canonical form
|
|
843
|
+
* discards** — `canonicalSpaceEntry` runs `at` through `sortBySerial`. Discarded information
|
|
844
|
+
* must not change the shape, so everything that reads `rects` — convex pieces, boundary
|
|
845
|
+
* segments, slabs, plan entities, and the anchor of a room label when two pieces have equal
|
|
846
|
+
* area — is kept off the written order by sorting the spelling. `grids` and `rects` stay
|
|
847
|
+
* parallel.
|
|
848
|
+
*/
|
|
849
|
+
export function normalizeRegionOrder(model: Model): void {
|
|
850
|
+
for (const s of model.spaces.values()) {
|
|
851
|
+
if (s.grids.length < 2 || s.grids.length !== s.rects.length) continue;
|
|
852
|
+
const order = s.grids
|
|
853
|
+
.map((g, i) => [JSON.stringify([g.xa, g.ya, g.xb, g.yb]), i] as const)
|
|
854
|
+
.sort(([x], [y]) => compareCanonical(x, y))
|
|
855
|
+
.map(([, i]) => i);
|
|
856
|
+
s.grids = order.map((i) => s.grids[i]!);
|
|
857
|
+
s.rects = order.map((i) => s.rects[i]!);
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
|
|
664
861
|
/** 正準JSON — 機械形式。差分とレイヤー合成の土台 (キーは安定順) */
|
|
665
862
|
export function toCanonical(model: Model): string {
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
863
|
+
// Keys taken from the source go in a Map so the collation order survives serialisation
|
|
864
|
+
const spaces = new Map<string, unknown>();
|
|
865
|
+
for (const p of [...model.spaces.keys()].sort(compareCanonical)) {
|
|
866
|
+
spaces.set(p, canonicalSpaceEntry(model.spaces.get(p)!));
|
|
669
867
|
}
|
|
670
868
|
// 境界: 宣言順は意味を持たないため、並びは内容の正準順 (betweenの辞書順、同一betweenは直列化順)。
|
|
671
869
|
// 既定境界 (derived — ADR-0014) は出さない: 正準JSONは書かれた構成のみで、意味は導出後のModelが持つ
|
|
@@ -673,23 +871,28 @@ export function toCanonical(model: Model): string {
|
|
|
673
871
|
[...model.boundaries].filter((b) => !b.derived).map(canonicalBoundaryEntry),
|
|
674
872
|
);
|
|
675
873
|
|
|
676
|
-
const zones
|
|
677
|
-
for (const p of [...model.zones.keys()].sort()) {
|
|
874
|
+
const zones = new Map<string, unknown>();
|
|
875
|
+
for (const p of [...model.zones.keys()].sort(compareCanonical)) {
|
|
678
876
|
const z = model.zones.get(p)!;
|
|
679
|
-
zones
|
|
877
|
+
zones.set(p, Object.keys(z.attrs).length ? { attrs: sortObj(z.attrs) } : {});
|
|
680
878
|
}
|
|
681
|
-
const assets
|
|
682
|
-
for (const n of [...model.assets.keys()].sort()) {
|
|
879
|
+
const assets = new Map<string, unknown>();
|
|
880
|
+
for (const n of [...model.assets.keys()].sort(compareCanonical)) {
|
|
683
881
|
const a = model.assets.get(n)!;
|
|
684
|
-
assets
|
|
882
|
+
assets.set(n, { kind: a.kind, ...(Object.keys(a.attrs).length ? { attrs: sortObj(a.attrs) } : {}) });
|
|
685
883
|
}
|
|
686
|
-
const polygons
|
|
687
|
-
for (const p of [...model.polygons.keys()].sort()) {
|
|
688
|
-
polygons
|
|
884
|
+
const polygons = new Map<string, number[][]>();
|
|
885
|
+
for (const p of [...model.polygons.keys()].sort(compareCanonical)) {
|
|
886
|
+
polygons.set(p, model.polygons.get(p)!.points.map((pt) => [pt.x, pt.y]));
|
|
689
887
|
}
|
|
690
888
|
|
|
691
889
|
const doc = {
|
|
692
|
-
koyu
|
|
890
|
+
// **文書が最初に名乗るのは自分の綴りの版である** (ADR-0036)。`koyu` は言語版であって
|
|
891
|
+
// 形式版ではなく、しかも「書かれた版宣言の素通し」なので、宣言の無いファイルには出ない —
|
|
892
|
+
// 出せば、その版を著者は書いていないのに書いたことになり、しかもツールの既定が動いた日に
|
|
893
|
+
// 同じ入力のバイトが変わる。決定性は形式の側の約束なので、ツールの既定に預けない
|
|
894
|
+
format: CANONICAL_FORMAT,
|
|
895
|
+
...(model.versionDeclared ? { koyu: model.version } : {}),
|
|
693
896
|
...(model.name ? { name: model.name } : {}),
|
|
694
897
|
unit: model.unit,
|
|
695
898
|
grid: { X: model.grid.X.coords, Y: model.grid.Y.coords },
|
|
@@ -706,8 +909,8 @@ export function toCanonical(model: Model): string {
|
|
|
706
909
|
]),
|
|
707
910
|
),
|
|
708
911
|
),
|
|
709
|
-
...(
|
|
710
|
-
...(
|
|
912
|
+
...(assets.size ? { assets } : {}),
|
|
913
|
+
...(polygons.size ? { polygons } : {}),
|
|
711
914
|
// **柱の宣言順は意味である。**同じ交点に二本は立たず、先の宣言が勝つ (ADR-0023) ので、
|
|
712
915
|
// 並べ替えると別の建物が同一のJSONになる。並べ替えてよいのは宣言の**中**の、
|
|
713
916
|
// 順序に意味の無い通り名の列だけである (ADR-0029)
|
|
@@ -723,11 +926,11 @@ export function toCanonical(model: Model): string {
|
|
|
723
926
|
})),
|
|
724
927
|
}
|
|
725
928
|
: {}),
|
|
726
|
-
...(
|
|
929
|
+
...(zones.size ? { zones } : {}),
|
|
727
930
|
spaces,
|
|
728
931
|
boundaries,
|
|
729
932
|
};
|
|
730
|
-
return
|
|
933
|
+
return canonicalStringify(doc) + "\n";
|
|
731
934
|
}
|
|
732
935
|
|
|
733
936
|
/**
|
|
@@ -736,8 +939,36 @@ export function toCanonical(model: Model): string {
|
|
|
736
939
|
* 書き順は綴りの揺れである (規則1)。綴り自体は通り参照のまま保つ (規則3)
|
|
737
940
|
*/
|
|
738
941
|
function canonicalLineEnds(d: DrawnLine): [string, string] {
|
|
739
|
-
|
|
740
|
-
|
|
942
|
+
return drawnIsForward(d) ? [d.aRef, d.bRef] : [d.bRef, d.aRef];
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
/**
|
|
946
|
+
* 描かれた線の正準の向き — 解決座標の (x, then y) 昇順。
|
|
947
|
+
*
|
|
948
|
+
* **線分は向きを持たない。**同じ二点を結ぶ線は、どちらの端から書いても同じ線である。
|
|
949
|
+
* 正準JSONはこの規則で端点の対を並べ替える。したがって**書き順は形を変えてはならない** —
|
|
950
|
+
* `canonicalizeDrawn` が parse の出口でモデルの側もこの向きに揃える (ADR-0041)。
|
|
951
|
+
*/
|
|
952
|
+
export function drawnIsForward(d: DrawnLine): boolean {
|
|
953
|
+
return d.a.x < d.b.x || (d.a.x === d.b.x && d.a.y <= d.b.y);
|
|
954
|
+
}
|
|
955
|
+
|
|
956
|
+
/**
|
|
957
|
+
* 描かれた線を正準の向きへ揃える。綴り (`aRef`/`bRef`) も一緒に入れ替えるので、
|
|
958
|
+
* 診断が引用する綴りは書かれたとおりのまま入れ替わる。
|
|
959
|
+
*
|
|
960
|
+
* これをしないと、**正準JSONがバイト同一のまま扉が別の位置に出る** — 実測で
|
|
961
|
+
* `line X1,Y1+2000 X2,Y1+4000` と `line X2,Y1+4000 X1,Y1+2000` が sha256 一致のまま
|
|
962
|
+
* 扉を (1500,2500) と (4500,3500) に置いていた。開口の `at:` は線分の始端からの比なので、
|
|
963
|
+
* 始端が書き順で決まる限り、形は正準形の関数にならない。
|
|
964
|
+
*/
|
|
965
|
+
export function canonicalizeDrawn(d: DrawnLine): void {
|
|
966
|
+
if (drawnIsForward(d)) return;
|
|
967
|
+
const { a, b, aRef, bRef } = d;
|
|
968
|
+
d.a = b;
|
|
969
|
+
d.b = a;
|
|
970
|
+
d.aRef = bRef;
|
|
971
|
+
d.bRef = aRef;
|
|
741
972
|
}
|
|
742
973
|
|
|
743
974
|
/** 通り名の列を通りの並び順に整える — `x:X2,X1` と `x:X1,X2` は同じ構成である */
|
|
@@ -745,13 +976,69 @@ function sortGridNames(axis: string[], names: string[]): string[] {
|
|
|
745
976
|
return [...names].sort((p, q) => {
|
|
746
977
|
const ip = axis.indexOf(p);
|
|
747
978
|
const iq = axis.indexOf(q);
|
|
748
|
-
if (ip < 0 || iq < 0) return p
|
|
979
|
+
if (ip < 0 || iq < 0) return compareCanonical(p, q); // 未宣言の通りは照合順で安定させる
|
|
749
980
|
return ip - iq;
|
|
750
981
|
});
|
|
751
982
|
}
|
|
752
983
|
|
|
753
|
-
|
|
754
|
-
|
|
984
|
+
/**
|
|
985
|
+
* 正準形の照合順 — **Unicode符号位置の昇順**であり、これは出力される UTF-8 バイトの昇順に等しい
|
|
986
|
+
* (ADR-0036)。**JavaScript の `<` と既定の `sort` は使えない** — あれは UTF-16 コード単位順で、
|
|
987
|
+
* 符号位置順と一致しない。実測: 𠮟 (U+20B9F) は代用対 D842,DF9F なので `<` では 﨑 (U+FA11) より
|
|
988
|
+
* 小さいが、UTF-8 では F0 A0 AE 9F と EF A8 91 で 﨑 が先である。どちらも日本語の実在の字なので、
|
|
989
|
+
* 差は理論上のものではない。**規範は「JSの既定」ではなく「この形式自身のバイト」に置く** —
|
|
990
|
+
* 別の言語で書かれた実装が素直に書けば同じ並びになる側を選ぶ。
|
|
991
|
+
*
|
|
992
|
+
* 実装は代用対の持ち上げによる: 符号位置順では U+E000..U+FFFF が代用対 (U+10000以上) より
|
|
993
|
+
* 手前に来るので、コード単位を写して比べる。
|
|
994
|
+
*/
|
|
995
|
+
export function compareCanonical(a: string, b: string): number {
|
|
996
|
+
if (a === b) return 0;
|
|
997
|
+
const n = Math.min(a.length, b.length);
|
|
998
|
+
for (let i = 0; i < n; i++) {
|
|
999
|
+
const x = a.charCodeAt(i);
|
|
1000
|
+
const y = b.charCodeAt(i);
|
|
1001
|
+
if (x !== y) return utf8Order(x) < utf8Order(y) ? -1 : 1;
|
|
1002
|
+
}
|
|
1003
|
+
return a.length === b.length ? 0 : a.length < b.length ? -1 : 1;
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
/** UTF-16コード単位を、符号位置 (= UTF-8バイト) の順に並ぶ数へ写す */
|
|
1007
|
+
function utf8Order(u: number): number {
|
|
1008
|
+
if (u >= 0xd800 && u <= 0xdfff) return u + 0x2000; // 代用対 = U+10000以上 — 最上位へ
|
|
1009
|
+
if (u >= 0xe000) return u - 0x800; // 代用領域の穴を詰める
|
|
1010
|
+
return u;
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
function sortObj<T>(o: Record<string, T>): Map<string, T> {
|
|
1014
|
+
return new Map(Object.entries(o).sort(([a], [b]) => compareCanonical(a, b)));
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
/**
|
|
1018
|
+
* Serialises a canonical document, with the same bytes `JSON.stringify(value, null, 2)` produces —
|
|
1019
|
+
* plus `Map`, whose entries are emitted **in insertion order**.
|
|
1020
|
+
*
|
|
1021
|
+
* A plain object cannot carry the collation order: JavaScript keeps integer-like keys
|
|
1022
|
+
* (`"2"`, `"10"`) ahead of the rest, in ascending numeric order, whatever order they were inserted
|
|
1023
|
+
* in. So a level or asset named `2` came out before one named `10`, while collation order — which
|
|
1024
|
+
* this format promises — puts `10` first. `Object.fromEntries` silently undid a correct sort.
|
|
1025
|
+
* A `Map` has no such rule, so keys taken from the source are carried in one.
|
|
1026
|
+
*/
|
|
1027
|
+
function canonicalStringify(value: unknown, depth = 0): string {
|
|
1028
|
+
const pad = " ".repeat(depth + 1);
|
|
1029
|
+
const close = " ".repeat(depth);
|
|
1030
|
+
if (value === null || typeof value !== "object") return JSON.stringify(value) ?? "null";
|
|
1031
|
+
if (Array.isArray(value)) {
|
|
1032
|
+
if (value.length === 0) return "[]";
|
|
1033
|
+
const items = value.map((v) => pad + canonicalStringify(v, depth + 1));
|
|
1034
|
+
return `[\n${items.join(",\n")}\n${close}]`;
|
|
1035
|
+
}
|
|
1036
|
+
const entries: Array<[string, unknown]> =
|
|
1037
|
+
value instanceof Map ? [...value.entries()] : Object.entries(value);
|
|
1038
|
+
const kept = entries.filter(([, v]) => v !== undefined);
|
|
1039
|
+
if (kept.length === 0) return "{}";
|
|
1040
|
+
const items = kept.map(([k, v]) => `${pad}${JSON.stringify(k)}: ${canonicalStringify(v, depth + 1)}`);
|
|
1041
|
+
return `{\n${items.join(",\n")}\n${close}}`;
|
|
755
1042
|
}
|
|
756
1043
|
|
|
757
1044
|
/**
|
|
@@ -764,6 +1051,6 @@ function sortObj<T>(o: Record<string, T>): Record<string, T> {
|
|
|
764
1051
|
export function sortBySerial<T>(items: T[]): T[] {
|
|
765
1052
|
return items
|
|
766
1053
|
.map((it) => [JSON.stringify(it), it] as const)
|
|
767
|
-
.sort(([x], [y]) => (x
|
|
1054
|
+
.sort(([x], [y]) => compareCanonical(x, y))
|
|
768
1055
|
.map(([, it]) => it);
|
|
769
1056
|
}
|