@burger-editor/utils 4.0.0-alpha.5 → 4.0.0-alpha.50
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.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/merge-items.d.ts +35 -0
- package/dist/merge-items.js +57 -0
- package/dist/normalize-html.d.ts +8 -0
- package/dist/normalize-html.js +109 -0
- package/dist/normalize-indent.d.ts +8 -0
- package/dist/normalize-indent.js +18 -0
- package/package.json +6 -6
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* マージ操作が可能な汎用型
|
|
3
|
+
* 任意のオブジェクト型にdeleteプロパティを追加する
|
|
4
|
+
*/
|
|
5
|
+
export type Mergeable<T extends Record<string, unknown>> = T & {
|
|
6
|
+
readonly delete?: boolean;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* 汎用的なマージ関数
|
|
10
|
+
* 指定されたキーフィールドを基準にアイテムのマージを行う
|
|
11
|
+
* @param defaultItems デフォルトのアイテム配列
|
|
12
|
+
* @param mergeItems マージ設定配列
|
|
13
|
+
* @param keyField キーとなるフィールド名
|
|
14
|
+
* @param isValidForAdd 新規追加時のバリデーション関数
|
|
15
|
+
* @returns マージ後のアイテム配列
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* // SelectableValue型での使用例
|
|
19
|
+
* const result = mergeItems(
|
|
20
|
+
* [{ value: 'a', label: 'A' }],
|
|
21
|
+
* [{ value: 'b', label: 'B' }],
|
|
22
|
+
* 'value',
|
|
23
|
+
* (item) => Boolean(item.label)
|
|
24
|
+
* );
|
|
25
|
+
*
|
|
26
|
+
* // 独自型での使用例
|
|
27
|
+
* interface User { id: string; name: string; }
|
|
28
|
+
* const users = mergeItems(
|
|
29
|
+
* [{ id: '1', name: 'Alice' }],
|
|
30
|
+
* [{ id: '2', name: 'Bob' }],
|
|
31
|
+
* 'id'
|
|
32
|
+
* );
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export declare function mergeItems<T extends Record<string, unknown>, K extends keyof T & string>(defaultItems: readonly T[], mergeItems: readonly Mergeable<Partial<T> & Pick<T, K>>[] | undefined, keyField: K, isValidForAdd?: (item: Mergeable<Partial<T> & Pick<T, K>>) => boolean): T[];
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 汎用的なマージ関数
|
|
3
|
+
* 指定されたキーフィールドを基準にアイテムのマージを行う
|
|
4
|
+
* @param defaultItems デフォルトのアイテム配列
|
|
5
|
+
* @param mergeItems マージ設定配列
|
|
6
|
+
* @param keyField キーとなるフィールド名
|
|
7
|
+
* @param isValidForAdd 新規追加時のバリデーション関数
|
|
8
|
+
* @returns マージ後のアイテム配列
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* // SelectableValue型での使用例
|
|
12
|
+
* const result = mergeItems(
|
|
13
|
+
* [{ value: 'a', label: 'A' }],
|
|
14
|
+
* [{ value: 'b', label: 'B' }],
|
|
15
|
+
* 'value',
|
|
16
|
+
* (item) => Boolean(item.label)
|
|
17
|
+
* );
|
|
18
|
+
*
|
|
19
|
+
* // 独自型での使用例
|
|
20
|
+
* interface User { id: string; name: string; }
|
|
21
|
+
* const users = mergeItems(
|
|
22
|
+
* [{ id: '1', name: 'Alice' }],
|
|
23
|
+
* [{ id: '2', name: 'Bob' }],
|
|
24
|
+
* 'id'
|
|
25
|
+
* );
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export function mergeItems(defaultItems, mergeItems, keyField, isValidForAdd) {
|
|
29
|
+
// マージ設定がない場合はデフォルトをそのまま返す
|
|
30
|
+
if (!mergeItems) {
|
|
31
|
+
return [...defaultItems];
|
|
32
|
+
}
|
|
33
|
+
let result = [...defaultItems];
|
|
34
|
+
for (const mergeItem of mergeItems) {
|
|
35
|
+
if (mergeItem.delete) {
|
|
36
|
+
// 削除: キーが一致するものを除去
|
|
37
|
+
const keyValue = mergeItem[keyField];
|
|
38
|
+
result = result.filter((item) => item[keyField] !== keyValue);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
// 追加または更新
|
|
42
|
+
const keyValue = mergeItem[keyField];
|
|
43
|
+
const existingIndex = result.findIndex((item) => item[keyField] === keyValue);
|
|
44
|
+
if (existingIndex === -1) {
|
|
45
|
+
// 新規追加: バリデーション通過が必要
|
|
46
|
+
if (!isValidForAdd || isValidForAdd(mergeItem)) {
|
|
47
|
+
result.push(mergeItem);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
// 既存の更新: マージする
|
|
52
|
+
result[existingIndex] = { ...result[existingIndex], ...mergeItem };
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return result;
|
|
57
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTMLの構造を正規化して比較する
|
|
3
|
+
* 空白や属性順序の違いを無視し、タグ構造のみを比較する
|
|
4
|
+
* @param html1 比較対象のHTML文字列1
|
|
5
|
+
* @param html2 比較対象のHTML文字列2
|
|
6
|
+
* @returns 構造が同じ場合true、異なる場合false
|
|
7
|
+
*/
|
|
8
|
+
export function normalizeHtmlStructure(html1, html2) {
|
|
9
|
+
const doc1 = new Range().createContextualFragment(html1);
|
|
10
|
+
const doc2 = new Range().createContextualFragment(html2);
|
|
11
|
+
return compareElements(doc1, doc2);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* 2つの要素の構造を比較する
|
|
15
|
+
* @param el1 比較対象の要素1
|
|
16
|
+
* @param el2 比較対象の要素2
|
|
17
|
+
* @returns 構造が同じ場合true、異なる場合false
|
|
18
|
+
*/
|
|
19
|
+
function compareElements(el1, el2) {
|
|
20
|
+
// 子要素の数を取得(テキストノードは無視)
|
|
21
|
+
const children1 = [...el1.children];
|
|
22
|
+
const children2 = [...el2.children];
|
|
23
|
+
// 子要素の数が異なる場合は構造が異なる
|
|
24
|
+
if (children1.length !== children2.length) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
// 子要素がない場合(テキストのみまたは空要素)
|
|
28
|
+
if (children1.length === 0) {
|
|
29
|
+
// テキスト内容を正規化して比較
|
|
30
|
+
const text1 = normalizeText(el1.textContent ?? '');
|
|
31
|
+
const text2 = normalizeText(el2.textContent ?? '');
|
|
32
|
+
return text1 === text2;
|
|
33
|
+
}
|
|
34
|
+
// 各子要素を比較
|
|
35
|
+
for (const [i, child1] of children1.entries()) {
|
|
36
|
+
const child2 = children2[i];
|
|
37
|
+
if (!child1 || !child2 || !compareElementStructure(child1, child2)) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* 2つの要素の構造を詳細に比較する
|
|
45
|
+
* @param el1 比較対象の要素1
|
|
46
|
+
* @param el2 比較対象の要素2
|
|
47
|
+
* @returns 構造が同じ場合true、異なる場合false
|
|
48
|
+
*/
|
|
49
|
+
function compareElementStructure(el1, el2) {
|
|
50
|
+
// タグ名が異なる場合は構造が異なる
|
|
51
|
+
if (el1.tagName !== el2.tagName) {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
// 属性を比較(順序は無視)
|
|
55
|
+
const attrs1 = getNormalizedAttributes(el1);
|
|
56
|
+
const attrs2 = getNormalizedAttributes(el2);
|
|
57
|
+
if (!compareAttributes(attrs1, attrs2)) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
// 子要素を再帰的に比較
|
|
61
|
+
return compareElements(el1, el2);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* 要素の属性を正規化して取得する
|
|
65
|
+
* @param el 対象の要素
|
|
66
|
+
* @returns 属性名をキー、属性値を値とするオブジェクト
|
|
67
|
+
*/
|
|
68
|
+
function getNormalizedAttributes(el) {
|
|
69
|
+
const attrs = {};
|
|
70
|
+
for (const attr of el.attributes) {
|
|
71
|
+
attrs[attr.name] = attr.value;
|
|
72
|
+
}
|
|
73
|
+
return attrs;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* 2つの属性オブジェクトを比較する
|
|
77
|
+
* @param attrs1 比較対象の属性1
|
|
78
|
+
* @param attrs2 比較対象の属性2
|
|
79
|
+
* @returns 属性が同じ場合true、異なる場合false
|
|
80
|
+
*/
|
|
81
|
+
function compareAttributes(attrs1, attrs2) {
|
|
82
|
+
const keys1 = Object.keys(attrs1).toSorted();
|
|
83
|
+
const keys2 = Object.keys(attrs2).toSorted();
|
|
84
|
+
// 属性名の数が異なる場合は異なる
|
|
85
|
+
if (keys1.length !== keys2.length) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
// 各属性を比較
|
|
89
|
+
for (const [i, key1] of keys1.entries()) {
|
|
90
|
+
const key2 = keys2[i];
|
|
91
|
+
// 属性名が異なる場合は異なる
|
|
92
|
+
if (key1 !== key2) {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
// 属性値が異なる場合は異なる
|
|
96
|
+
if (attrs1[key1] !== attrs2[key2]) {
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* テキストを正規化する(空白文字を統一)
|
|
104
|
+
* @param text 対象のテキスト
|
|
105
|
+
* @returns 正規化されたテキスト
|
|
106
|
+
*/
|
|
107
|
+
function normalizeText(text) {
|
|
108
|
+
return text.replaceAll(/\s+/g, ' ').trim();
|
|
109
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTMLの先頭行のインデントを検出し、全行から同じインデントを削除する
|
|
3
|
+
*
|
|
4
|
+
* CRLF (\r\n) と LF (\n) の両方に対応
|
|
5
|
+
* @param html - 正規化するHTML文字列
|
|
6
|
+
* @returns インデントが正規化され、前後の空白行が削除されたHTML文字列
|
|
7
|
+
*/
|
|
8
|
+
export function normalizeIndent(html) {
|
|
9
|
+
// 先頭の改行を削除
|
|
10
|
+
const trimmedHtml = html.replace(/^(?:\r?\n)+/, '');
|
|
11
|
+
const lines = trimmedHtml.split(/\r?\n/);
|
|
12
|
+
const firstLineIndent = lines[0]?.match(/^\s+/)?.[0] ?? '';
|
|
13
|
+
if (firstLineIndent.length === 0) {
|
|
14
|
+
return trimmedHtml;
|
|
15
|
+
}
|
|
16
|
+
const normalizedLines = lines.map((line) => line.replace(firstLineIndent, ''));
|
|
17
|
+
return normalizedLines.join('\n');
|
|
18
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@burger-editor/utils",
|
|
3
|
-
"version": "4.0.0-alpha.
|
|
3
|
+
"version": "4.0.0-alpha.50",
|
|
4
4
|
"description": "BurgerEditor Editor Utility Functions",
|
|
5
5
|
"author": "D-ZERO",
|
|
6
6
|
"license": "(MIT OR Apache-2.0)",
|
|
@@ -26,12 +26,12 @@
|
|
|
26
26
|
"clean": "tsc --build --clean"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"dayjs": "1.11.
|
|
30
|
-
"marked": "
|
|
31
|
-
"turndown": "7.2.
|
|
29
|
+
"dayjs": "1.11.19",
|
|
30
|
+
"marked": "17.0.1",
|
|
31
|
+
"turndown": "7.2.2"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"@types/turndown": "5.0.
|
|
34
|
+
"@types/turndown": "5.0.6"
|
|
35
35
|
},
|
|
36
|
-
"gitHead": "
|
|
36
|
+
"gitHead": "ffcf57b6dad3edd130b4d66a2d9378b164812937"
|
|
37
37
|
}
|