@burger-editor/utils 4.0.0-alpha.4 → 4.0.0-alpha.43

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.
@@ -0,0 +1,5 @@
1
+ /**
2
+ *
3
+ * @param str
4
+ */
5
+ export declare function camelCase(str: string): string;
@@ -0,0 +1,13 @@
1
+ const camelCaseCache = new Map();
2
+ /**
3
+ *
4
+ * @param str
5
+ */
6
+ export function camelCase(str) {
7
+ if (camelCaseCache.has(str)) {
8
+ return camelCaseCache.get(str);
9
+ }
10
+ const result = str.replaceAll(/-([a-z])/g, (_, c) => c.toUpperCase());
11
+ camelCaseCache.set(str, result);
12
+ return result;
13
+ }
package/dist/index.d.ts CHANGED
@@ -1,2 +1,7 @@
1
+ export * from './camel-case.js';
2
+ export * from './kebab-case.js';
1
3
  export * from './markdown.js';
4
+ export * from './merge-items.js';
5
+ export * from './normalize-html.js';
6
+ export * from './replace-comment-with-html.js';
2
7
  export * from './utils.js';
package/dist/index.js CHANGED
@@ -1,2 +1,7 @@
1
+ export * from './camel-case.js';
2
+ export * from './kebab-case.js';
1
3
  export * from './markdown.js';
4
+ export * from './merge-items.js';
5
+ export * from './normalize-html.js';
6
+ export * from './replace-comment-with-html.js';
2
7
  export * from './utils.js';
@@ -0,0 +1,5 @@
1
+ /**
2
+ *
3
+ * @param str
4
+ */
5
+ export declare function kebabCase(str: string): string;
@@ -0,0 +1,13 @@
1
+ const kebabCaseCache = new Map();
2
+ /**
3
+ *
4
+ * @param str
5
+ */
6
+ export function kebabCase(str) {
7
+ if (kebabCaseCache.has(str)) {
8
+ return kebabCaseCache.get(str);
9
+ }
10
+ const result = str.replaceAll(/([A-Z])/g, '-$1').toLowerCase();
11
+ kebabCaseCache.set(str, result);
12
+ return result;
13
+ }
@@ -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,8 @@
1
+ /**
2
+ * HTMLの構造を正規化して比較する
3
+ * 空白や属性順序の違いを無視し、タグ構造のみを比較する
4
+ * @param html1 比較対象のHTML文字列1
5
+ * @param html2 比較対象のHTML文字列2
6
+ * @returns 構造が同じ場合true、異なる場合false
7
+ */
8
+ export declare function normalizeHtmlStructure(html1: string, html2: string): boolean;
@@ -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,8 @@
1
+ /**
2
+ * HTMLの先頭行のインデントを検出し、全行から同じインデントを削除する
3
+ *
4
+ * CRLF (\r\n) と LF (\n) の両方に対応
5
+ * @param html - 正規化するHTML文字列
6
+ * @returns インデントが正規化され、前後の空白行が削除されたHTML文字列
7
+ */
8
+ export declare function normalizeIndent(html: string): string;
@@ -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
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ *
3
+ * @param html
4
+ * @param templateSeed
5
+ * @param dataConverter
6
+ */
7
+ export declare function replaceCommentWithHTML<T extends string>(html: string, templateSeed: {
8
+ [key in T]: string;
9
+ }, dataConverter?: (key: string, html: string, data: any) => string): string;
@@ -0,0 +1,37 @@
1
+ import { kebabCase } from './kebab-case.js';
2
+ /**
3
+ *
4
+ * @param html
5
+ * @param templateSeed
6
+ * @param dataConverter
7
+ */
8
+ export function replaceCommentWithHTML(html, templateSeed,
9
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
10
+ dataConverter) {
11
+ let iteration = 0;
12
+ const replacements = new Map();
13
+ for (const [key, itemTemplate] of Object.entries(templateSeed)) {
14
+ const _key = kebabCase(key);
15
+ const placeholder = new RegExp(`<!-- ${_key}({[^}]*})? -->`, 'g');
16
+ html = html.replaceAll(placeholder, (_, attr) => {
17
+ const marker = `<!-- __MARKER__${iteration} -->`;
18
+ replacements.set(marker, [key, itemTemplate, attr]);
19
+ iteration++;
20
+ return marker;
21
+ });
22
+ }
23
+ html = html.replaceAll(/<!-- __MARKER__(\d+) -->/g, (_, iteration) => {
24
+ const replacement = replacements.get(`<!-- __MARKER__${iteration} -->`);
25
+ if (!replacement) {
26
+ return '';
27
+ }
28
+ const [key, itemTemplate, attr] = replacement;
29
+ let itemHtml = itemTemplate;
30
+ if (dataConverter) {
31
+ const data = attr ? JSON.parse(`${attr}`) : {};
32
+ itemHtml = dataConverter(key, itemTemplate, data);
33
+ }
34
+ return itemHtml;
35
+ });
36
+ return html;
37
+ }
package/package.json CHANGED
@@ -1,13 +1,12 @@
1
1
  {
2
2
  "name": "@burger-editor/utils",
3
- "version": "4.0.0-alpha.4",
3
+ "version": "4.0.0-alpha.43",
4
4
  "description": "BurgerEditor Editor Utility Functions",
5
5
  "author": "D-ZERO",
6
- "license": "MIT",
6
+ "license": "(MIT OR Apache-2.0)",
7
7
  "repository": {
8
8
  "url": "https://github.com/d-zero-dev/BurgerEditor.git"
9
9
  },
10
- "private": false,
11
10
  "publishConfig": {
12
11
  "access": "public"
13
12
  },
@@ -27,12 +26,12 @@
27
26
  "clean": "tsc --build --clean"
28
27
  },
29
28
  "dependencies": {
30
- "dayjs": "1.11.13",
31
- "marked": "15.0.7",
32
- "turndown": "7.2.0"
29
+ "dayjs": "1.11.19",
30
+ "marked": "17.0.1",
31
+ "turndown": "7.2.2"
33
32
  },
34
33
  "devDependencies": {
35
- "@types/turndown": "5.0.5"
34
+ "@types/turndown": "5.0.6"
36
35
  },
37
- "gitHead": "823486d58579275ae556942c5d7a4aef4e1f634e"
36
+ "gitHead": "3293443ba2bf45b1c80d8b424f16b77cefe65cf1"
38
37
  }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2017-2024 D-ZERO Co., Ltd.
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.