@nojaja/greputil 1.0.0 → 1.0.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/README.md CHANGED
@@ -1,336 +1,336 @@
1
- # NodeGrepUtil
2
-
3
- 複数の正規表現パターンを効率的に扱うためのJavaScriptユーティリティライブラリです。文字列に対して複数の正規表現を一度に適用し、マッチング結果を取得できます。また、バッファパターンマッチング機能も提供します。
4
-
5
- ## プロジェクト概要
6
-
7
- NodeGrepUtilは、複数の正規表現パターンを配列として管理し、それらを文字列に対して順次適用するための`RegExpArray`クラスを提供します。標準のJavaScript正規表現APIを拡張し、複数パターンの一括処理を簡潔に記述できます。さらに、`BufferPatternMatcher`クラスによるバイナリデータのパターンマッチング機能も備えています。
8
-
9
- ## プロジェクト構造
10
-
11
- ```
12
- NodeGrepUtil/
13
- ├── src/
14
- │ ├── index.js # ライブラリエントリポイント(RegExpArray、BufferPatternMatcherのエクスポート)
15
- │ ├── RegExpArray.js # RegExpArrayクラスの実装
16
- │ └── BufferPatternMatcher.js # BufferPatternMatcherクラスの実装
17
- ├── tests/
18
- │ └── unit/
19
- │ └── RegExpArray.spec.js # ユニットテスト
20
- ├── dist/ # ビルド出力ディレクトリ(webpack)
21
- ├── jest.config.js # Jestテスト設定
22
- ├── webpack.config.js # Webpackビルド設定(UMD形式)
23
- ├── package.json # プロジェクトメタデータと依存関係
24
- ├── LICENSE # MITライセンス
25
- └── README.md # プロジェクトドキュメント
26
- ```
27
-
28
- ## 技術スタック
29
-
30
- - **言語**: JavaScript (ES6+)
31
- - **ビルドツール**: Webpack 5
32
- - **テストフレームワーク**: Jest 29
33
- - **トランスパイラ**: Babel (Jest用)
34
- - **出力形式**: UMD (Universal Module Definition)
35
-
36
- ## 機能
37
-
38
- ### 完了している機能
39
-
40
- #### RegExpArray(正規表現配列)
41
- - ✅ 複数の正規表現パターンを配列で管理
42
- - ✅ 正規表現の文字列表記、配列表記、RegExpオブジェクトの混在をサポート
43
- - ✅ `exec()` - 複数パターンでの順次マッチング実行
44
- - ✅ `test()` - 複数パターンでのテスト(いずれかがマッチすればtrue)
45
- - ✅ `firstMatch()` - 最初にマッチした結果を返すインスタンスメソッド
46
- - ✅ `toArray()` - RegExpオブジェクトの配列を取得
47
- - ✅ `RegExpArray.matchAll()` - 静的メソッドによる全マッチ結果の取得
48
- - ✅ `RegExpArray.firstMatch()` - 静的メソッドで最初のマッチを取得
49
- - ✅ `RegExpArray.test()` - 静的メソッドでマッチテスト(修正済み)
50
-
51
- #### BufferPatternMatcher(バッファパターンマッチング)
52
- - ✅ `compareBuf()` - バイナリデータ(Buffer)に対するパターンマッチング
53
- - ✅ 複数のバッファパターンとの比較
54
- - ✅ エラーハンドリングとthrowによる例外送出
55
-
56
- #### 共通機能
57
- - ✅ UMDバンドルによるNode.js/ブラウザ両対応
58
- - ✅ null入力時の適切な処理(nullを返す)
59
- - ✅ 改善されたエラーハンドリング(throw使用)
60
-
61
- ## セットアップ
62
-
63
- ### インストール
64
-
65
- ```powershell
66
- # リポジトリのクローン
67
- git clone https://github.com/nojaja/NodeGrepUtil.git
68
- cd NodeGrepUtil
69
-
70
- # 依存関係のインストール
71
- npm install
72
- ```
73
-
74
- ### ビルド
75
-
76
- ```powershell
77
- # UMDバンドルのビルド (dist/GrepUtil.bundle.js)
78
- npm run build
79
- ```
80
-
81
- ## 使用方法
82
-
83
- ### ライブラリとしての使用
84
-
85
- #### インストール
86
-
87
- ```powershell
88
- npm install @nojaja/greputil
89
- ```
90
-
91
- #### API リファレンス
92
-
93
- ##### RegExpArray クラス
94
-
95
- | メソッド/関数 | パラメータ | 戻り値 | 説明 |
96
- |--------------|-----------|--------|------|
97
- | `new RegExpArray(patternList)` | `patternList: string[] \| RegExp[] \| Array<[string, string]>` | `RegExpArray` | 複数の正規表現パターンからインスタンスを作成。文字列、RegExpオブジェクト、[パターン, フラグ]配列の混在可 |
98
- | `exec(string)` | `string: string` | `Array<string>` | 各正規表現を順次実行し、すべてのマッチ結果を配列で返す(グローバルフラグ使用時はステートフル) |
99
- | `firstMatch(string)` | `string: string` | `RegExpExecArray \| null` | 最初にマッチした結果を返す。マッチしない場合はnull |
100
- | `test(string)` | `string: string` | `boolean` | いずれかの正規表現がマッチすればtrue(firstMatchを内部使用) |
101
- | `toArray()` | なし | `Array<RegExp>` | RegExpオブジェクトの配列を返す |
102
- | `RegExpArray.matchAll(string, regexps)` | `string: string`, `regexps: string \| RegExp \| Array \| null` | `Array<Array<string>> \| null` | 静的メソッド。すべてのマッチ結果を二次元配列で返す。regexpsがnullの場合はnullを返す |
103
- | `RegExpArray.firstMatch(string, regexps)` | `string: string`, `regexps: string \| RegExp \| Array \| null` | `RegExpExecArray \| null` | 静的メソッド。最初のマッチ結果を返す。regexpsがnullの場合はnullを返す |
104
- | `RegExpArray.test(string, regexps)` | `string: string`, `regexps: string \| RegExp \| Array \| null` | `boolean` | 静的メソッド。マッチするかテスト(内部でfirstMatchを使用) |
105
-
106
- ##### BufferPatternMatcher クラス
107
-
108
- | メソッド/関数 | パラメータ | 戻り値 | 説明 |
109
- |--------------|-----------|--------|------|
110
- | `new BufferPatternMatcher()` | なし | `BufferPatternMatcher` | BufferPatternMatcherのインスタンスを作成 |
111
- | `compareBuf(buffer, patterns)` | `buffer: Buffer`, `patterns: Array<Buffer>` | `boolean \| null` | バッファを複数のパターンと比較。マッチすればtrue、マッチしなければfalse、patternsがnullの場合はnull |
112
-
113
- #### 基本的な使用例
114
-
115
- ##### RegExpArray の使用
116
-
117
- ```javascript
118
- import { RegExpArray } from '@nojaja/greputil';
119
-
120
- // 1. インスタンス作成 - 複数のパターンを配列で指定
121
- const util = new RegExpArray([
122
- /test\d/g, // RegExpオブジェクト
123
- 'hello', // 文字列
124
- ['world', 'i'] // [パターン, フラグ]配列
125
- ]);
126
-
127
- // 2. exec() - 順次マッチング実行
128
- const result1 = util.exec('test1 hello test2');
129
- // => ["test1", "hello"]
130
-
131
- // 3. firstMatch() - 最初のマッチを取得
132
- const result2 = util.firstMatch('test1 hello test2');
133
- // => ["test1"] (配列形式で詳細含む)
134
-
135
- // 4. test() - いずれかがマッチするか判定
136
- const result3 = util.test('test1 hello');
137
- // => true
138
-
139
- // 5. toArray() - RegExpオブジェクト配列を取得
140
- const regexps = util.toArray();
141
- // => [/test\d/g, /hello/, /world/i]
142
-
143
- // 6. 静的メソッド matchAll() - すべてのマッチを取得
144
- const matches = RegExpArray.matchAll('test1test2', [/t(e)(st(\d?))/g]);
145
- // => [["test1","e","st1","1"], ["test2","e","st2","2"]]
146
-
147
- // 7. 静的メソッド firstMatch() - 最初のマッチのみ
148
- const firstMatch = RegExpArray.firstMatch('test1test2', [/test\d/g]);
149
- // => ["test1"]
150
- ```
151
-
152
- ##### BufferPatternMatcher の使用
153
-
154
- ```javascript
155
- import { BufferPatternMatcher } from '@nojaja/greputil';
156
-
157
- // バイナリデータのパターンマッチング
158
- const matcher = new BufferPatternMatcher();
159
-
160
- // PNG/JPEGファイルの判定例
161
- const fileBuffer = Buffer.from([0x89, 0x50, 0x4E, 0x47, ...]);
162
- const patterns = [
163
- Buffer.from([0x89, 0x50, 0x4E, 0x47]), // PNGシグネチャ
164
- Buffer.from([0xFF, 0xD8, 0xFF]) // JPEGシグネチャ
165
- ];
166
-
167
- const isMatch = matcher.compareBuf(fileBuffer, patterns);
168
- // => true (PNGパターンにマッチ)
169
- ```
170
-
171
- #### 実用例 - ログファイルの解析
172
-
173
- ```javascript
174
- import { RegExpArray } from '@nojaja/greputil';
175
-
176
- const logText = `
177
- 2026-01-01 ERROR: Database connection failed
178
- 2026-01-01 WARN: Slow query detected
179
- 2026-01-02 ERROR: Timeout occurred
180
- `;
181
-
182
- // ERRORとWARNの両方を抽出
183
- const patterns = [
184
- /\d{4}-\d{2}-\d{2} ERROR: .+/g,
185
- /\d{4}-\d{2}-\d{2} WARN: .+/g
186
- ];
187
-
188
- const matches = RegExpArray.matchAll(logText, patterns);
189
- console.log(matches);
190
- // => [
191
- // ["2026-01-01 ERROR: Database connection failed"],
192
- // ["2026-01-01 WARN: Slow query detected"],
193
- // ["2026-01-02 ERROR: Timeout occurred"]
194
- // ]
195
-
196
- // 最初のエラーのみを取得
197
- const firstError = RegExpArray.firstMatch(logText, [/ERROR: .+/]);
198
- // => ["ERROR: Database connection failed"]
199
- ```
200
-
201
- #### エラーハンドリング
202
-
203
- ```javascript
204
- import { RegExpArray, BufferPatternMatcher } from '@nojaja/greputil';
205
-
206
- // RegExpArray: エラーは例外として送出される
207
- try {
208
- const matches = RegExpArray.matchAll('test', [/[/]); // 不正な正規表現
209
- } catch (error) {
210
- console.error('正規表現エラー:', error);
211
- // エラーがthrowされるため、適切にキャッチする必要がある
212
- }
213
-
214
- // null入力時の処理
215
- const result = RegExpArray.matchAll('test', null);
216
- // => null(エラーではなくnullを返す)
217
-
218
- // BufferPatternMatcher: エラーは例外として送出される
219
- try {
220
- const matcher = new BufferPatternMatcher();
221
- const isMatch = matcher.compareBuf(invalidBuffer, patterns);
222
- } catch (error) {
223
- console.error('バッファ比較エラー:', error);
224
- }
225
- ```
226
-
227
- ### 開発セットアップ
228
-
229
- 開発環境のセットアップと一般的な開発コマンド:
230
-
231
- ```powershell
232
- # リポジトリをクローン
233
- git clone https://github.com/nojaja/NodeGrepUtil.git
234
- cd NodeGrepUtil
235
-
236
- # 依存関係をインストール
237
- npm install
238
-
239
- # テストを実行
240
- npm run test
241
-
242
- # ビルドを実行(dist/GrepUtil.bundle.js を生成)
243
- npm run build
244
- ```
245
-
246
- #### 開発ワークフロー
247
-
248
- 1. `src/RegExpArray.js` または `src/BufferPatternMatcher.js` でコードを修正
249
- 2. `tests/unit/RegExpArray.spec.js` でテストを追加・更新
250
- 3. `npm run test` でテスト実行
251
- 4. `npm run build` でUMDバンドルをビルド
252
- 5. コミット&プッシュ
253
-
254
- ## 技術的な詳細
255
-
256
- ### パターン指定の柔軟性
257
-
258
- `RegExpArray`のコンストラクタは、以下の3つの形式を受け付けます:
259
-
260
- ```javascript
261
- new RegExpArray([
262
- /pattern/g, // 1. RegExpオブジェクト
263
- 'string pattern', // 2. 文字列(フラグなし)
264
- ['pattern', 'gi'] // 3. [パターン, フラグ]配列
265
- ]);
266
- ```
267
-
268
- ### ステートフルな動作
269
-
270
- グローバルフラグ(`g`)を使用した正規表現は、`exec()`や`test()`呼び出し時にステートを保持します:
271
-
272
- ```javascript
273
- const util = new RegExpArray([/test\d/g]);
274
-
275
- util.exec('test1 test2'); // => ["test1"]
276
- util.exec('test1 test2'); // => ["test2"] (次のマッチに進む)
277
- util.exec('test1 test2'); // => [] (マッチなし)
278
- ```
279
-
280
- 最後に使用された正規表現は`util.last`で参照可能です。
281
-
282
- ### null入力時の動作
283
-
284
- `RegExpArray`の静的メソッドは、null入力に対して適切に処理します:
285
-
286
- ```javascript
287
- RegExpArray.matchAll('test', null); // => null
288
- RegExpArray.firstMatch('test', null); // => null
289
- RegExpArray.test('test', null); // => false
290
- ```
291
-
292
- `BufferPatternMatcher.compareBuf()`もpatternsがnullの場合はnullを返します。
293
-
294
- ### UMDバンドル
295
-
296
- Webpackで生成される`dist/GrepUtil.bundle.js`は、以下の環境で使用可能:
297
-
298
- - **Node.js**: `require('@nojaja/greputil')`
299
- - **ブラウザ(グローバル)**: `<script>`タグで読み込み後、グローバル変数として利用
300
- - **AMD/RequireJS**: AMD形式で読み込み可能
301
-
302
- ## 現在のステータス
303
-
304
- ### 実装済み
305
-
306
- - ✅ RegExpArrayコアAPIの実装完了(`constructor`, `exec`, `firstMatch`, `test`, `toArray`, 静的メソッド)
307
- - ✅ BufferPatternMatcherの実装完了(`compareBuf`)
308
- - ✅ ユニットテスト実装(カバレッジ: 主要機能)
309
- - ✅ Webpackビルド設定完了(UMD出力)
310
- - ✅ エラーハンドリング改善(throw使用)
311
- - ✅ null入力時の適切な処理
312
- - ✅ 静的メソッド`test()`の修正(firstMatch使用)
313
- - ✅ ドキュメント整備(この README.md)
314
-
315
- ### 未実装・検討中
316
-
317
- - ❌ TypeScript型定義ファイル(`.d.ts`)
318
- - ❌ ESM形式での配布対応
319
- - ❌ より詳細なエラーハンドリングとバリデーション
320
- - ❌ パフォーマンス最適化(大量パターン処理時)
321
-
322
- ## パフォーマンス・目標
323
-
324
- - **目標**: 100個以上の正規表現パターンを同時に扱える設計
325
- - **現状**: 小〜中規模のパターン配列(10〜50個)での動作を想定した実装
326
-
327
- ## ライセンス & 作者
328
-
329
- - **ライセンス**: MIT License
330
- - **作者**: nojaja <free.riccia@gmail.com>
331
- - **リポジトリ**: [https://github.com/nojaja/NodeGrepUtil](https://github.com/nojaja/NodeGrepUtil)
332
- - **バージョン**: 1.0.0
333
-
334
- ## バグ報告・機能要望
335
-
1
+ # NodeGrepUtil
2
+
3
+ 複数の正規表現パターンを効率的に扱うためのJavaScriptユーティリティライブラリです。文字列に対して複数の正規表現を一度に適用し、マッチング結果を取得できます。また、バッファパターンマッチング機能も提供します。
4
+
5
+ ## プロジェクト概要
6
+
7
+ NodeGrepUtilは、複数の正規表現パターンを配列として管理し、それらを文字列に対して順次適用するための`RegExpArray`クラスを提供します。標準のJavaScript正規表現APIを拡張し、複数パターンの一括処理を簡潔に記述できます。さらに、`BufferPatternMatcher`クラスによるバイナリデータのパターンマッチング機能も備えています。
8
+
9
+ ## プロジェクト構造
10
+
11
+ ```
12
+ NodeGrepUtil/
13
+ ├── src/
14
+ │ ├── index.js # ライブラリエントリポイント(RegExpArray、BufferPatternMatcherのエクスポート)
15
+ │ ├── RegExpArray.js # RegExpArrayクラスの実装
16
+ │ └── BufferPatternMatcher.js # BufferPatternMatcherクラスの実装
17
+ ├── tests/
18
+ │ └── unit/
19
+ │ └── RegExpArray.spec.js # ユニットテスト
20
+ ├── dist/ # ビルド出力ディレクトリ(webpack)
21
+ ├── jest.config.js # Jestテスト設定
22
+ ├── webpack.config.js # Webpackビルド設定(UMD形式)
23
+ ├── package.json # プロジェクトメタデータと依存関係
24
+ ├── LICENSE # MITライセンス
25
+ └── README.md # プロジェクトドキュメント
26
+ ```
27
+
28
+ ## 技術スタック
29
+
30
+ - **言語**: JavaScript (ES6+)
31
+ - **ビルドツール**: Webpack 5
32
+ - **テストフレームワーク**: Jest 29
33
+ - **トランスパイラ**: Babel (Jest用)
34
+ - **出力形式**: UMD (Universal Module Definition)
35
+
36
+ ## 機能
37
+
38
+ ### 完了している機能
39
+
40
+ #### RegExpArray(正規表現配列)
41
+ - ✅ 複数の正規表現パターンを配列で管理
42
+ - ✅ 正規表現の文字列表記、配列表記、RegExpオブジェクトの混在をサポート
43
+ - ✅ `exec()` - 複数パターンでの順次マッチング実行
44
+ - ✅ `test()` - 複数パターンでのテスト(いずれかがマッチすればtrue)
45
+ - ✅ `firstMatch()` - 最初にマッチした結果を返すインスタンスメソッド
46
+ - ✅ `toArray()` - RegExpオブジェクトの配列を取得
47
+ - ✅ `RegExpArray.matchAll()` - 静的メソッドによる全マッチ結果の取得
48
+ - ✅ `RegExpArray.firstMatch()` - 静的メソッドで最初のマッチを取得
49
+ - ✅ `RegExpArray.test()` - 静的メソッドでマッチテスト(修正済み)
50
+
51
+ #### BufferPatternMatcher(バッファパターンマッチング)
52
+ - ✅ `compareBuf()` - バイナリデータ(Buffer)に対するパターンマッチング
53
+ - ✅ 複数のバッファパターンとの比較
54
+ - ✅ エラーハンドリングとthrowによる例外送出
55
+
56
+ #### 共通機能
57
+ - ✅ UMDバンドルによるNode.js/ブラウザ両対応
58
+ - ✅ null入力時の適切な処理(nullを返す)
59
+ - ✅ 改善されたエラーハンドリング(throw使用)
60
+
61
+ ## セットアップ
62
+
63
+ ### インストール
64
+
65
+ ```powershell
66
+ # リポジトリのクローン
67
+ git clone https://github.com/nojaja/NodeGrepUtil.git
68
+ cd NodeGrepUtil
69
+
70
+ # 依存関係のインストール
71
+ npm install
72
+ ```
73
+
74
+ ### ビルド
75
+
76
+ ```powershell
77
+ # UMDバンドルのビルド (dist/GrepUtil.bundle.js)
78
+ npm run build
79
+ ```
80
+
81
+ ## 使用方法
82
+
83
+ ### ライブラリとしての使用
84
+
85
+ #### インストール
86
+
87
+ ```powershell
88
+ npm install @nojaja/greputil
89
+ ```
90
+
91
+ #### API リファレンス
92
+
93
+ ##### RegExpArray クラス
94
+
95
+ | メソッド/関数 | パラメータ | 戻り値 | 説明 |
96
+ |--------------|-----------|--------|------|
97
+ | `new RegExpArray(patternList)` | `patternList: string[] \| RegExp[] \| Array<[string, string]>` | `RegExpArray` | 複数の正規表現パターンからインスタンスを作成。文字列、RegExpオブジェクト、[パターン, フラグ]配列の混在可 |
98
+ | `exec(string)` | `string: string` | `Array<string>` | 各正規表現を順次実行し、すべてのマッチ結果を配列で返す(グローバルフラグ使用時はステートフル) |
99
+ | `firstMatch(string)` | `string: string` | `RegExpExecArray \| null` | 最初にマッチした結果を返す。マッチしない場合はnull |
100
+ | `test(string)` | `string: string` | `boolean` | いずれかの正規表現がマッチすればtrue(firstMatchを内部使用) |
101
+ | `toArray()` | なし | `Array<RegExp>` | RegExpオブジェクトの配列を返す |
102
+ | `RegExpArray.matchAll(string, regexps)` | `string: string`, `regexps: string \| RegExp \| Array \| null` | `Array<Array<string>> \| null` | 静的メソッド。すべてのマッチ結果を二次元配列で返す。regexpsがnullの場合はnullを返す |
103
+ | `RegExpArray.firstMatch(string, regexps)` | `string: string`, `regexps: string \| RegExp \| Array \| null` | `RegExpExecArray \| null` | 静的メソッド。最初のマッチ結果を返す。regexpsがnullの場合はnullを返す |
104
+ | `RegExpArray.test(string, regexps)` | `string: string`, `regexps: string \| RegExp \| Array \| null` | `boolean` | 静的メソッド。マッチするかテスト(内部でfirstMatchを使用) |
105
+
106
+ ##### BufferPatternMatcher クラス
107
+
108
+ | メソッド/関数 | パラメータ | 戻り値 | 説明 |
109
+ |--------------|-----------|--------|------|
110
+ | `new BufferPatternMatcher()` | なし | `BufferPatternMatcher` | BufferPatternMatcherのインスタンスを作成 |
111
+ | `compareBuf(buffer, patterns)` | `buffer: Buffer`, `patterns: Array<Buffer>` | `boolean \| null` | バッファを複数のパターンと比較。マッチすればtrue、マッチしなければfalse、patternsがnullの場合はnull |
112
+
113
+ #### 基本的な使用例
114
+
115
+ ##### RegExpArray の使用
116
+
117
+ ```javascript
118
+ import { RegExpArray } from '@nojaja/greputil';
119
+
120
+ // 1. インスタンス作成 - 複数のパターンを配列で指定
121
+ const util = new RegExpArray([
122
+ /test\d/g, // RegExpオブジェクト
123
+ 'hello', // 文字列
124
+ ['world', 'i'] // [パターン, フラグ]配列
125
+ ]);
126
+
127
+ // 2. exec() - 順次マッチング実行
128
+ const result1 = util.exec('test1 hello test2');
129
+ // => ["test1", "hello"]
130
+
131
+ // 3. firstMatch() - 最初のマッチを取得
132
+ const result2 = util.firstMatch('test1 hello test2');
133
+ // => ["test1"] (配列形式で詳細含む)
134
+
135
+ // 4. test() - いずれかがマッチするか判定
136
+ const result3 = util.test('test1 hello');
137
+ // => true
138
+
139
+ // 5. toArray() - RegExpオブジェクト配列を取得
140
+ const regexps = util.toArray();
141
+ // => [/test\d/g, /hello/, /world/i]
142
+
143
+ // 6. 静的メソッド matchAll() - すべてのマッチを取得
144
+ const matches = RegExpArray.matchAll('test1test2', [/t(e)(st(\d?))/g]);
145
+ // => [["test1","e","st1","1"], ["test2","e","st2","2"]]
146
+
147
+ // 7. 静的メソッド firstMatch() - 最初のマッチのみ
148
+ const firstMatch = RegExpArray.firstMatch('test1test2', [/test\d/g]);
149
+ // => ["test1"]
150
+ ```
151
+
152
+ ##### BufferPatternMatcher の使用
153
+
154
+ ```javascript
155
+ import { BufferPatternMatcher } from '@nojaja/greputil';
156
+
157
+ // バイナリデータのパターンマッチング
158
+ const matcher = new BufferPatternMatcher();
159
+
160
+ // PNG/JPEGファイルの判定例
161
+ const fileBuffer = Buffer.from([0x89, 0x50, 0x4E, 0x47, ...]);
162
+ const patterns = [
163
+ Buffer.from([0x89, 0x50, 0x4E, 0x47]), // PNGシグネチャ
164
+ Buffer.from([0xFF, 0xD8, 0xFF]) // JPEGシグネチャ
165
+ ];
166
+
167
+ const isMatch = matcher.compareBuf(fileBuffer, patterns);
168
+ // => true (PNGパターンにマッチ)
169
+ ```
170
+
171
+ #### 実用例 - ログファイルの解析
172
+
173
+ ```javascript
174
+ import { RegExpArray } from '@nojaja/greputil';
175
+
176
+ const logText = `
177
+ 2026-01-01 ERROR: Database connection failed
178
+ 2026-01-01 WARN: Slow query detected
179
+ 2026-01-02 ERROR: Timeout occurred
180
+ `;
181
+
182
+ // ERRORとWARNの両方を抽出
183
+ const patterns = [
184
+ /\d{4}-\d{2}-\d{2} ERROR: .+/g,
185
+ /\d{4}-\d{2}-\d{2} WARN: .+/g
186
+ ];
187
+
188
+ const matches = RegExpArray.matchAll(logText, patterns);
189
+ console.log(matches);
190
+ // => [
191
+ // ["2026-01-01 ERROR: Database connection failed"],
192
+ // ["2026-01-01 WARN: Slow query detected"],
193
+ // ["2026-01-02 ERROR: Timeout occurred"]
194
+ // ]
195
+
196
+ // 最初のエラーのみを取得
197
+ const firstError = RegExpArray.firstMatch(logText, [/ERROR: .+/]);
198
+ // => ["ERROR: Database connection failed"]
199
+ ```
200
+
201
+ #### エラーハンドリング
202
+
203
+ ```javascript
204
+ import { RegExpArray, BufferPatternMatcher } from '@nojaja/greputil';
205
+
206
+ // RegExpArray: エラーは例外として送出される
207
+ try {
208
+ const matches = RegExpArray.matchAll('test', [/[/]); // 不正な正規表現
209
+ } catch (error) {
210
+ console.error('正規表現エラー:', error);
211
+ // エラーがthrowされるため、適切にキャッチする必要がある
212
+ }
213
+
214
+ // null入力時の処理
215
+ const result = RegExpArray.matchAll('test', null);
216
+ // => null(エラーではなくnullを返す)
217
+
218
+ // BufferPatternMatcher: エラーは例外として送出される
219
+ try {
220
+ const matcher = new BufferPatternMatcher();
221
+ const isMatch = matcher.compareBuf(invalidBuffer, patterns);
222
+ } catch (error) {
223
+ console.error('バッファ比較エラー:', error);
224
+ }
225
+ ```
226
+
227
+ ### 開発セットアップ
228
+
229
+ 開発環境のセットアップと一般的な開発コマンド:
230
+
231
+ ```powershell
232
+ # リポジトリをクローン
233
+ git clone https://github.com/nojaja/NodeGrepUtil.git
234
+ cd NodeGrepUtil
235
+
236
+ # 依存関係をインストール
237
+ npm install
238
+
239
+ # テストを実行
240
+ npm run test
241
+
242
+ # ビルドを実行(dist/GrepUtil.bundle.js を生成)
243
+ npm run build
244
+ ```
245
+
246
+ #### 開発ワークフロー
247
+
248
+ 1. `src/RegExpArray.js` または `src/BufferPatternMatcher.js` でコードを修正
249
+ 2. `tests/unit/RegExpArray.spec.js` でテストを追加・更新
250
+ 3. `npm run test` でテスト実行
251
+ 4. `npm run build` でUMDバンドルをビルド
252
+ 5. コミット&プッシュ
253
+
254
+ ## 技術的な詳細
255
+
256
+ ### パターン指定の柔軟性
257
+
258
+ `RegExpArray`のコンストラクタは、以下の3つの形式を受け付けます:
259
+
260
+ ```javascript
261
+ new RegExpArray([
262
+ /pattern/g, // 1. RegExpオブジェクト
263
+ 'string pattern', // 2. 文字列(フラグなし)
264
+ ['pattern', 'gi'] // 3. [パターン, フラグ]配列
265
+ ]);
266
+ ```
267
+
268
+ ### ステートフルな動作
269
+
270
+ グローバルフラグ(`g`)を使用した正規表現は、`exec()`や`test()`呼び出し時にステートを保持します:
271
+
272
+ ```javascript
273
+ const util = new RegExpArray([/test\d/g]);
274
+
275
+ util.exec('test1 test2'); // => ["test1"]
276
+ util.exec('test1 test2'); // => ["test2"] (次のマッチに進む)
277
+ util.exec('test1 test2'); // => [] (マッチなし)
278
+ ```
279
+
280
+ 最後に使用された正規表現は`util.last`で参照可能です。
281
+
282
+ ### null入力時の動作
283
+
284
+ `RegExpArray`の静的メソッドは、null入力に対して適切に処理します:
285
+
286
+ ```javascript
287
+ RegExpArray.matchAll('test', null); // => null
288
+ RegExpArray.firstMatch('test', null); // => null
289
+ RegExpArray.test('test', null); // => false
290
+ ```
291
+
292
+ `BufferPatternMatcher.compareBuf()`もpatternsがnullの場合はnullを返します。
293
+
294
+ ### UMDバンドル
295
+
296
+ Webpackで生成される`dist/GrepUtil.bundle.js`は、以下の環境で使用可能:
297
+
298
+ - **Node.js**: `require('@nojaja/greputil')`
299
+ - **ブラウザ(グローバル)**: `<script>`タグで読み込み後、グローバル変数として利用
300
+ - **AMD/RequireJS**: AMD形式で読み込み可能
301
+
302
+ ## 現在のステータス
303
+
304
+ ### 実装済み
305
+
306
+ - ✅ RegExpArrayコアAPIの実装完了(`constructor`, `exec`, `firstMatch`, `test`, `toArray`, 静的メソッド)
307
+ - ✅ BufferPatternMatcherの実装完了(`compareBuf`)
308
+ - ✅ ユニットテスト実装(カバレッジ: 主要機能)
309
+ - ✅ Webpackビルド設定完了(UMD出力)
310
+ - ✅ エラーハンドリング改善(throw使用)
311
+ - ✅ null入力時の適切な処理
312
+ - ✅ 静的メソッド`test()`の修正(firstMatch使用)
313
+ - ✅ ドキュメント整備(この README.md)
314
+
315
+ ### 未実装・検討中
316
+
317
+ - ❌ TypeScript型定義ファイル(`.d.ts`)
318
+ - ❌ ESM形式での配布対応
319
+ - ❌ より詳細なエラーハンドリングとバリデーション
320
+ - ❌ パフォーマンス最適化(大量パターン処理時)
321
+
322
+ ## パフォーマンス・目標
323
+
324
+ - **目標**: 100個以上の正規表現パターンを同時に扱える設計
325
+ - **現状**: 小〜中規模のパターン配列(10〜50個)での動作を想定した実装
326
+
327
+ ## ライセンス & 作者
328
+
329
+ - **ライセンス**: MIT License
330
+ - **作者**: nojaja <free.riccia@gmail.com>
331
+ - **リポジトリ**: [https://github.com/nojaja/NodeGrepUtil](https://github.com/nojaja/NodeGrepUtil)
332
+ - **バージョン**: 1.0.0
333
+
334
+ ## バグ報告・機能要望
335
+
336
336
  問題や機能要望は [GitHub Issues](https://github.com/nojaja/NodeGrepUtil/issues) までお願いします。