@first-ch/tools-mcp 0.4.0 → 0.24.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/lib.mjs CHANGED
@@ -158,3 +158,106 @@ export function buildJsonLd(type, fields = {}) {
158
158
  const snippet = `<script type="application/ld+json">\n${JSON.stringify(json, null, 2)}\n</script>`;
159
159
  return { json, snippet };
160
160
  }
161
+
162
+ // ---- 文字コード・改行コード変換(site/encoding/app.js と同一アルゴリズム・2箇所ルール対象) ----
163
+ // ブラウザ側は TextDecoder/TextEncoder、Node側は Buffer で同じ結果を返す。
164
+ // Shift_JIS のエンコードは Node の標準APIに無いため、変換表を持たず「判定と UTF-8 化」までを担当する。
165
+
166
+ const BOM_UTF8 = [0xef, 0xbb, 0xbf];
167
+
168
+ /** 先頭バイトからBOMを判定する。戻り値: 'utf-8' | 'utf-16le' | 'utf-16be' | null */
169
+ export function detectBom(bytes) {
170
+ const b = Array.from(bytes.slice(0, 3));
171
+ if (b[0] === 0xef && b[1] === 0xbb && b[2] === 0xbf) return 'utf-8';
172
+ if (b[0] === 0xff && b[1] === 0xfe) return 'utf-16le';
173
+ if (b[0] === 0xfe && b[1] === 0xff) return 'utf-16be';
174
+ return null;
175
+ }
176
+
177
+ /** 改行コードを数える。CRLF は CR+LF を1件として扱う */
178
+ export function detectNewline(text) {
179
+ const crlf = (text.match(/\r\n/g) || []).length;
180
+ const lf = (text.match(/(?<!\r)\n/g) || []).length;
181
+ const cr = (text.match(/\r(?!\n)/g) || []).length;
182
+ const kinds = [
183
+ ['CRLF', crlf],
184
+ ['LF', lf],
185
+ ['CR', cr],
186
+ ].filter(([, n]) => n > 0);
187
+ return {
188
+ crlf,
189
+ lf,
190
+ cr,
191
+ dominant: kinds.length ? kinds.sort((a, b) => b[1] - a[1])[0][0] : 'none',
192
+ mixed: kinds.length > 1,
193
+ };
194
+ }
195
+
196
+ /** 改行コードを揃える。newline: 'LF' | 'CRLF' | 'CR' */
197
+ export function convertNewline(text, newline) {
198
+ const lf = String(text).replace(/\r\n/g, '\n').replace(/\r/g, '\n');
199
+ if (newline === 'CRLF') return lf.replace(/\n/g, '\r\n');
200
+ if (newline === 'CR') return lf.replace(/\n/g, '\r');
201
+ return lf;
202
+ }
203
+
204
+ /** UTF-8 かどうかを厳密に判定する(不正バイト列なら false) */
205
+ export function isValidUtf8(bytes) {
206
+ try {
207
+ new TextDecoder('utf-8', { fatal: true }).decode(Uint8Array.from(bytes));
208
+ return true;
209
+ } catch {
210
+ return false;
211
+ }
212
+ }
213
+
214
+ /**
215
+ * バイト列を解析して文字コード・BOM・改行コードを返す。
216
+ * encoding は 'utf-8' | 'shift_jis' を明示できる。未指定なら UTF-8 として妥当かで自動判定する
217
+ * (日本語CSVは実務上ほぼ UTF-8 か Shift_JIS の二択のため、この2つに絞る)。
218
+ */
219
+ export function analyzeEncoding(bytes, encoding) {
220
+ const arr = Uint8Array.from(bytes);
221
+ const bom = detectBom(arr);
222
+ const body = bom === 'utf-8' ? arr.slice(3) : arr;
223
+ const enc = encoding || (isValidUtf8(body) ? 'utf-8' : 'shift_jis');
224
+ let text = '';
225
+ let decodable = true;
226
+ try {
227
+ text = new TextDecoder(enc, { fatal: true }).decode(body);
228
+ } catch {
229
+ decodable = false;
230
+ text = new TextDecoder(enc).decode(body); // 置換文字込みで返す
231
+ }
232
+ return {
233
+ encoding: enc,
234
+ encoding_detected: !encoding,
235
+ bom: bom,
236
+ has_bom: bom !== null,
237
+ decodable,
238
+ newline: detectNewline(text),
239
+ bytes: arr.length,
240
+ chars: text.length,
241
+ text,
242
+ };
243
+ }
244
+
245
+ /**
246
+ * 変換して UTF-8 のバイト列を返す。
247
+ * opts: { encoding?, newline?: 'LF'|'CRLF'|'CR', bom?: boolean }
248
+ * Shift_JIS への書き出しは Node 標準では不可能なため、出力は UTF-8 固定にしている。
249
+ */
250
+ export function convertEncoding(bytes, opts = {}) {
251
+ const info = analyzeEncoding(bytes, opts.encoding);
252
+ let text = info.text;
253
+ if (opts.newline) text = convertNewline(text, opts.newline);
254
+ const out = new TextEncoder().encode(text);
255
+ const withBom = opts.bom ? Uint8Array.from([...BOM_UTF8, ...out]) : out;
256
+ return {
257
+ from: { encoding: info.encoding, has_bom: info.has_bom, newline: info.newline.dominant },
258
+ to: { encoding: 'utf-8', has_bom: !!opts.bom, newline: opts.newline || info.newline.dominant },
259
+ bytes: withBom.length,
260
+ base64: Buffer.from(withBom).toString('base64'),
261
+ text,
262
+ };
263
+ }