@nojaja/greputil 1.0.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/.babelrc +7 -0
- package/.dependency-cruiser.js +89 -0
- package/.eslintignore +6 -0
- package/.eslintrc.cjs +48 -0
- package/.github/skills/completion-mandatory-quality-gates.md +26 -0
- package/.github/skills/nodejs-project-quality-guardrails.md +417 -0
- package/.github/workflows/release.yml +44 -0
- package/.github/workflows/webpack.yml +28 -0
- package/LICENSE +21 -0
- package/MIGRATION_REPORT.md +77 -0
- package/README.md +336 -0
- package/jest.config.js +16 -0
- package/jest.unit.config.js +31 -0
- package/package.json +43 -0
- package/src/BufferPatternMatcher.ts +61 -0
- package/src/RegExpArray.ts +209 -0
- package/src/index.ts +15 -0
- package/tests/unit/RegExpArray.spec.ts +205 -0
- package/tsconfig.json +22 -0
- package/typedoc.js +16 -0
- package/webpack.config.js +32 -0
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 処理名: 正規表現配列クラス
|
|
3
|
+
*
|
|
4
|
+
* 処理概要:
|
|
5
|
+
* 複数の正規表現パターンを配列として管理し、文字列に対して順次適用するためのクラス。
|
|
6
|
+
* 文字列、RegExpオブジェクト、[パターン, フラグ]配列の混在をサポートする。
|
|
7
|
+
*
|
|
8
|
+
* 実装理由:
|
|
9
|
+
* 標準のJavaScript正規表現APIでは単一パターンのみ扱えるため、
|
|
10
|
+
* 複数パターンの一括処理を簡潔に記述できるユーティリティクラスが必要。
|
|
11
|
+
* grepのような複数パターンマッチングを効率的に実装するため。
|
|
12
|
+
*/
|
|
13
|
+
export class RegExpArray {
|
|
14
|
+
/**
|
|
15
|
+
* 正規表現インスタンスのリスト
|
|
16
|
+
*/
|
|
17
|
+
private regExpInstanceList: RegExp[];
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* ソースパターンとフラグのリスト
|
|
21
|
+
*/
|
|
22
|
+
private sourceLiat: Array<[string, string]>;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 最後に実行された正規表現
|
|
26
|
+
*/
|
|
27
|
+
public last: RegExp | null;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 処理名: RegExpArrayコンストラクタ
|
|
31
|
+
*
|
|
32
|
+
* 処理概要:
|
|
33
|
+
* 複数の正規表現パターンからインスタンスを作成する。
|
|
34
|
+
* 文字列、RegExpオブジェクト、[パターン, フラグ]配列の混在可能。
|
|
35
|
+
*
|
|
36
|
+
* 実装理由:
|
|
37
|
+
* 柔軟なパターン指定を可能にし、様々な入力形式に対応するため。
|
|
38
|
+
* 配列でない単一パターンも自動的に配列化して統一的に処理する。
|
|
39
|
+
* @param patternList - 正規表現パターンのリスト(文字列、RegExp、[パターン, フラグ]の配列)
|
|
40
|
+
* @returns RegExpArrayインスタンス
|
|
41
|
+
*/
|
|
42
|
+
constructor(patternList?: RegExp | string | Array<string> | Array<RegExp | string | Array<string>>) {
|
|
43
|
+
const _patternList = (!patternList)
|
|
44
|
+
? []
|
|
45
|
+
: (!Array.isArray(patternList))
|
|
46
|
+
? [patternList]
|
|
47
|
+
: patternList;
|
|
48
|
+
|
|
49
|
+
this.regExpInstanceList = _patternList.map((pattern) => {
|
|
50
|
+
if (pattern instanceof RegExp) {
|
|
51
|
+
return pattern;
|
|
52
|
+
} else if (Array.isArray(pattern)) {
|
|
53
|
+
return new RegExp(pattern[0], pattern[1]);
|
|
54
|
+
} else {
|
|
55
|
+
return new RegExp(pattern);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
this.sourceLiat = this.regExpInstanceList.map((regExp) =>
|
|
60
|
+
[regExp.source, regExp.flags]
|
|
61
|
+
);
|
|
62
|
+
this.last = null;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* 処理名: 順次マッチング実行
|
|
67
|
+
*
|
|
68
|
+
* 処理概要:
|
|
69
|
+
* 各正規表現を順次実行し、すべてのマッチ結果を配列で返す。
|
|
70
|
+
* グローバルフラグ使用時はステートフルに動作する。
|
|
71
|
+
*
|
|
72
|
+
* 実装理由:
|
|
73
|
+
* 複数パターンのマッチ結果を一度に取得するため。
|
|
74
|
+
* reduceを使用して効率的に結果を集約する。
|
|
75
|
+
* @param string - マッチング対象の文字列
|
|
76
|
+
* @returns マッチした文字列の配列
|
|
77
|
+
*/
|
|
78
|
+
exec(string: string): string[] {
|
|
79
|
+
return this.regExpInstanceList.reduce((previousValue, regexp) => {
|
|
80
|
+
this.last = regexp;
|
|
81
|
+
const result = regexp.exec(string);
|
|
82
|
+
return (result) ? previousValue.concat([...result]) : previousValue;
|
|
83
|
+
}, [] as string[]);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 処理名: 最初のマッチ取得
|
|
88
|
+
*
|
|
89
|
+
* 処理概要:
|
|
90
|
+
* 最初にマッチした正規表現の結果を返す。
|
|
91
|
+
* マッチしない場合はnullを返す。
|
|
92
|
+
*
|
|
93
|
+
* 実装理由:
|
|
94
|
+
* 全パターンをテストする必要がなく、最初のマッチで処理を終了できるため効率的。
|
|
95
|
+
* testメソッドの内部実装としても使用される。
|
|
96
|
+
* @param string - マッチング対象の文字列
|
|
97
|
+
* @returns マッチ結果の配列、またはnull
|
|
98
|
+
*/
|
|
99
|
+
firstMatch(string: string): RegExpExecArray | null {
|
|
100
|
+
for (const regexp of this.regExpInstanceList) {
|
|
101
|
+
this.last = regexp;
|
|
102
|
+
const result = regexp.exec(string);
|
|
103
|
+
if (result) return result;
|
|
104
|
+
}
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* 処理名: マッチテスト
|
|
110
|
+
*
|
|
111
|
+
* 処理概要:
|
|
112
|
+
* いずれかの正規表現がマッチすればtrueを返す。
|
|
113
|
+
* グローバルフラグ使用時はステートフルに動作する。
|
|
114
|
+
*
|
|
115
|
+
* 実装理由:
|
|
116
|
+
* 真偽値のみが必要な場合に、マッチ結果の詳細を取得するオーバーヘッドを避けるため。
|
|
117
|
+
* firstMatchの結果を利用して効率的に判定する。
|
|
118
|
+
* @param string - テスト対象の文字列
|
|
119
|
+
* @returns マッチする場合true、しない場合false
|
|
120
|
+
*/
|
|
121
|
+
test(string: string): boolean {
|
|
122
|
+
return this.firstMatch(string) !== null;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* 処理名: RegExp配列取得
|
|
127
|
+
*
|
|
128
|
+
* 処理概要:
|
|
129
|
+
* 内部で保持している正規表現オブジェクトの配列を返す。
|
|
130
|
+
*
|
|
131
|
+
* 実装理由:
|
|
132
|
+
* インスタンス内部の正規表現を外部から参照・検査できるようにするため。
|
|
133
|
+
* デバッグやテスト時に有用。
|
|
134
|
+
* @returns 正規表現オブジェクトの配列
|
|
135
|
+
*/
|
|
136
|
+
toArray(): RegExp[] {
|
|
137
|
+
return this.regExpInstanceList;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* 処理名: 全マッチ結果取得(静的メソッド)
|
|
142
|
+
*
|
|
143
|
+
* 処理概要:
|
|
144
|
+
* 複数の正規表現パターンに対するすべてのマッチ結果を二次元配列で返す。
|
|
145
|
+
* regexpsがnullの場合はnullを返す。
|
|
146
|
+
*
|
|
147
|
+
* 実装理由:
|
|
148
|
+
* インスタンスを作成せずに一時的なマッチング処理を実行できるようにするため。
|
|
149
|
+
* String.prototype.matchAllを活用して効率的にマッチングを行う。
|
|
150
|
+
* @param string - マッチング対象の文字列
|
|
151
|
+
* @param regexps - 正規表現パターン(単一または配列)、nullの場合はnullを返す
|
|
152
|
+
* @returns マッチ結果の二次元配列、またはnull
|
|
153
|
+
* @throws エラーが発生した場合は例外を投げる
|
|
154
|
+
*/
|
|
155
|
+
static matchAll(string: string, regexps: RegExp | string | Array<RegExp | string> | null): Array<Array<string>> | null {
|
|
156
|
+
if (regexps === null) return null;
|
|
157
|
+
const _regexps = (!Array.isArray(regexps)) ? [regexps] : regexps;
|
|
158
|
+
return _regexps.reduce((previousValue, regexp) => {
|
|
159
|
+
const matches = Array.from(string.matchAll(regexp instanceof RegExp ? regexp : new RegExp(regexp, 'g')));
|
|
160
|
+
return previousValue.concat(matches.map(m => Array.from(m)));
|
|
161
|
+
}, [] as Array<Array<string>>);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* 処理名: 最初のマッチ取得(静的メソッド)
|
|
166
|
+
*
|
|
167
|
+
* 処理概要:
|
|
168
|
+
* 複数の正規表現パターンから最初にマッチした結果を返す。
|
|
169
|
+
* regexpsがnullの場合はnullを返す。
|
|
170
|
+
*
|
|
171
|
+
* 実装理由:
|
|
172
|
+
* インスタンスを作成せずに一時的なマッチング処理を実行できるようにするため。
|
|
173
|
+
* 最初のマッチで処理を終了するため効率的。
|
|
174
|
+
* @param string - マッチング対象の文字列
|
|
175
|
+
* @param regexps - 正規表現パターン(単一または配列)、nullの場合はnullを返す
|
|
176
|
+
* @returns マッチ結果の配列、またはnull
|
|
177
|
+
* @throws エラーが発生した場合は例外を投げる
|
|
178
|
+
*/
|
|
179
|
+
static firstMatch(string: string, regexps: RegExp | string | Array<RegExp | string> | null): RegExpExecArray | null {
|
|
180
|
+
if (regexps === null) return null;
|
|
181
|
+
const _regexps = (!Array.isArray(regexps)) ? [regexps] : regexps;
|
|
182
|
+
for (const regexp of _regexps) {
|
|
183
|
+
const _regexp = regexp instanceof RegExp ? regexp : new RegExp(regexp);
|
|
184
|
+
const result = _regexp.exec(string);
|
|
185
|
+
if (result) return result;
|
|
186
|
+
}
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* 処理名: マッチテスト(静的メソッド)
|
|
192
|
+
*
|
|
193
|
+
* 処理概要:
|
|
194
|
+
* 複数の正規表現パターンのいずれかがマッチするか判定する。
|
|
195
|
+
* 内部でfirstMatchを使用して効率的に判定。
|
|
196
|
+
*
|
|
197
|
+
* 実装理由:
|
|
198
|
+
* インスタンスを作成せずに真偽値判定を行えるようにするため。
|
|
199
|
+
* firstMatchの結果を利用して簡潔に実装。
|
|
200
|
+
* @param string - テスト対象の文字列
|
|
201
|
+
* @param regexps - 正規表現パターン(単一または配列)
|
|
202
|
+
* @returns マッチする場合true、しない場合false
|
|
203
|
+
*/
|
|
204
|
+
static test(string: string, regexps: RegExp | string | Array<RegExp | string> | null): boolean {
|
|
205
|
+
return RegExpArray.firstMatch(string, regexps) !== null;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export default RegExpArray;
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 処理名: ライブラリエントリポイント
|
|
3
|
+
*
|
|
4
|
+
* 処理概要:
|
|
5
|
+
* NodeGrepUtilライブラリの公開APIをエクスポートする。
|
|
6
|
+
* RegExpArrayとBufferPatternMatcherクラスを提供。
|
|
7
|
+
*
|
|
8
|
+
* 実装理由:
|
|
9
|
+
* ライブラリ利用者が必要なクラスを簡単にインポートできるようにするため。
|
|
10
|
+
* 単一のエントリポイントから全機能にアクセス可能にする。
|
|
11
|
+
*/
|
|
12
|
+
import { RegExpArray } from './RegExpArray';
|
|
13
|
+
import { BufferPatternMatcher } from './BufferPatternMatcher';
|
|
14
|
+
|
|
15
|
+
export { RegExpArray, BufferPatternMatcher };
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { RegExpArray } from '../../src/RegExpArray';
|
|
2
|
+
import { BufferPatternMatcher } from '../../src/BufferPatternMatcher';
|
|
3
|
+
|
|
4
|
+
describe('RegExpArray', () => {
|
|
5
|
+
describe('constructor', () => {
|
|
6
|
+
it('文字列配列から正規表現を生成できること', () => {
|
|
7
|
+
const regExpArray = new RegExpArray(['test.']);
|
|
8
|
+
expect(regExpArray.toArray()[0].source).toBe('test.');
|
|
9
|
+
expect(regExpArray.toArray()[0].flags).toBe('');
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it('RegExpオブジェクト配列から正規表現を生成できること', () => {
|
|
13
|
+
const regExpArray = new RegExpArray([/t(e)(st(\d?))/g]);
|
|
14
|
+
expect(regExpArray.toArray()[0].source).toBe('t(e)(st(\\d?))');
|
|
15
|
+
expect(regExpArray.toArray()[0].flags).toBe('g');
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('文字列から正規表現を生成できること', () => {
|
|
19
|
+
const regExpArray = new RegExpArray(['t(e)(st(\\d?))']);
|
|
20
|
+
expect(regExpArray.toArray()[0].source).toBe('t(e)(st(\\d?))');
|
|
21
|
+
expect(regExpArray.toArray()[0].flags).toBe('');
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('[パターン, フラグ]配列から正規表現を生成できること', () => {
|
|
25
|
+
const regExpArray = new RegExpArray([['t(e)(st(\\d?))', 'g']]);
|
|
26
|
+
expect(regExpArray.toArray()[0].source).toBe('t(e)(st(\\d?))');
|
|
27
|
+
expect(regExpArray.toArray()[0].flags).toBe('g');
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('空の配列でインスタンス化できること', () => {
|
|
31
|
+
const regExpArray = new RegExpArray([]);
|
|
32
|
+
expect(regExpArray.toArray()).toEqual([]);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('パターンなしでインスタンス化できること', () => {
|
|
36
|
+
const regExpArray = new RegExpArray();
|
|
37
|
+
expect(regExpArray.toArray()).toEqual([]);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
describe('exec', () => {
|
|
42
|
+
it('グローバルフラグ付き正規表現で順次マッチできること', () => {
|
|
43
|
+
const regExpArray = new RegExpArray([/t(e)(st(\d?))/g]);
|
|
44
|
+
expect(regExpArray.exec('test1test2')).toEqual(['test1', 'e', 'st1', '1']);
|
|
45
|
+
expect(regExpArray.exec('test1test2')).toEqual(['test2', 'e', 'st2', '2']);
|
|
46
|
+
expect(regExpArray.exec('test1test2')).toEqual([]);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('複数パターンのマッチ結果を返すこと', () => {
|
|
50
|
+
const regExpArray = new RegExpArray([/hello/, /world/]);
|
|
51
|
+
const result = regExpArray.exec('hello world');
|
|
52
|
+
expect(result).toContain('hello');
|
|
53
|
+
expect(result).toContain('world');
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('マッチしない場合は空配列を返すこと', () => {
|
|
57
|
+
const regExpArray = new RegExpArray([/xyz/]);
|
|
58
|
+
expect(regExpArray.exec('abc')).toEqual([]);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
describe('firstMatch', () => {
|
|
63
|
+
it('最初のマッチ結果を返すこと', () => {
|
|
64
|
+
const regExpArray = new RegExpArray([/test\d/, /hello/]);
|
|
65
|
+
const result = regExpArray.firstMatch('test1 hello');
|
|
66
|
+
expect(result).not.toBeNull();
|
|
67
|
+
expect(result![0]).toBe('test1');
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('マッチしない場合はnullを返すこと', () => {
|
|
71
|
+
const regExpArray = new RegExpArray([/xyz/]);
|
|
72
|
+
expect(regExpArray.firstMatch('abc')).toBeNull();
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
describe('test', () => {
|
|
77
|
+
it('グローバルフラグ付き正規表現でマッチ判定できること', () => {
|
|
78
|
+
const regExpArray = new RegExpArray([/t(e)(st(\d?))/g]);
|
|
79
|
+
expect(regExpArray.test('test1test2')).toBe(true);
|
|
80
|
+
expect(regExpArray.test('test1test2')).toBe(true);
|
|
81
|
+
expect(regExpArray.test('test1test2')).toBe(false);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('マッチする場合trueを返すこと', () => {
|
|
85
|
+
const regExpArray = new RegExpArray([/hello/]);
|
|
86
|
+
expect(regExpArray.test('hello world')).toBe(true);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('マッチしない場合falseを返すこと', () => {
|
|
90
|
+
const regExpArray = new RegExpArray([/xyz/]);
|
|
91
|
+
expect(regExpArray.test('abc')).toBe(false);
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
describe('toArray', () => {
|
|
96
|
+
it('RegExpオブジェクトの配列を返すこと', () => {
|
|
97
|
+
const regExpArray = new RegExpArray([/test/, /hello/]);
|
|
98
|
+
const result = regExpArray.toArray();
|
|
99
|
+
expect(result).toHaveLength(2);
|
|
100
|
+
expect(result[0]).toBeInstanceOf(RegExp);
|
|
101
|
+
expect(result[1]).toBeInstanceOf(RegExp);
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
describe('matchAll (static)', () => {
|
|
106
|
+
it('すべてのマッチ結果を二次元配列で返すこと', () => {
|
|
107
|
+
const result = RegExpArray.matchAll('test1test2', [/t(e)(st(\d?))/g]);
|
|
108
|
+
expect(result).toEqual([['test1', 'e', 'st1', '1'], ['test2', 'e', 'st2', '2']]);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('文字列パターンでマッチできること', () => {
|
|
112
|
+
const result = RegExpArray.matchAll('test1test2', 'test.');
|
|
113
|
+
expect(result).toHaveLength(2);
|
|
114
|
+
expect(result![0][0]).toMatch(/test\d/);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('マッチしない場合は空配列を返すこと', () => {
|
|
118
|
+
const result = RegExpArray.matchAll('test1test2', 'string');
|
|
119
|
+
expect(result).toEqual([]);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('nullを渡した場合はnullを返すこと', () => {
|
|
123
|
+
const result = RegExpArray.matchAll('test', null);
|
|
124
|
+
expect(result).toBeNull();
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
describe('firstMatch (static)', () => {
|
|
129
|
+
it('最初のマッチ結果を返すこと', () => {
|
|
130
|
+
const result = RegExpArray.firstMatch('test1 hello', [/test\d/, /hello/]);
|
|
131
|
+
expect(result).not.toBeNull();
|
|
132
|
+
expect(result![0]).toBe('test1');
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('マッチしない場合はnullを返すこと', () => {
|
|
136
|
+
const result = RegExpArray.firstMatch('abc', [/xyz/]);
|
|
137
|
+
expect(result).toBeNull();
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it('nullを渡した場合はnullを返すこと', () => {
|
|
141
|
+
const result = RegExpArray.firstMatch('test', null);
|
|
142
|
+
expect(result).toBeNull();
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
describe('test (static)', () => {
|
|
147
|
+
it('マッチする場合trueを返すこと', () => {
|
|
148
|
+
expect(RegExpArray.test('hello world', [/hello/])).toBe(true);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it('マッチしない場合falseを返すこと', () => {
|
|
152
|
+
expect(RegExpArray.test('abc', [/xyz/])).toBe(false);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('nullを渡した場合falseを返すこと', () => {
|
|
156
|
+
expect(RegExpArray.test('test', null)).toBe(false);
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
describe('BufferPatternMatcher', () => {
|
|
162
|
+
describe('compareBuf', () => {
|
|
163
|
+
it('パターンにマッチする場合trueを返すこと', () => {
|
|
164
|
+
const matcher = new BufferPatternMatcher();
|
|
165
|
+
const buffer = Buffer.from([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A]);
|
|
166
|
+
const patterns = [Buffer.from([0x89, 0x50, 0x4E, 0x47])]; // PNGシグネチャ
|
|
167
|
+
|
|
168
|
+
expect(matcher.compareBuf(buffer, patterns)).toBe(true);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('パターンにマッチしない場合falseを返すこと', () => {
|
|
172
|
+
const matcher = new BufferPatternMatcher();
|
|
173
|
+
const buffer = Buffer.from([0xFF, 0xD8, 0xFF, 0xE0]);
|
|
174
|
+
const patterns = [Buffer.from([0x89, 0x50, 0x4E, 0x47])]; // PNGシグネチャ
|
|
175
|
+
|
|
176
|
+
expect(matcher.compareBuf(buffer, patterns)).toBe(false);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it('複数パターンの1つにマッチする場合trueを返すこと', () => {
|
|
180
|
+
const matcher = new BufferPatternMatcher();
|
|
181
|
+
const buffer = Buffer.from([0xFF, 0xD8, 0xFF, 0xE0]);
|
|
182
|
+
const patterns = [
|
|
183
|
+
Buffer.from([0x89, 0x50, 0x4E, 0x47]), // PNGシグネチャ
|
|
184
|
+
Buffer.from([0xFF, 0xD8, 0xFF]) // JPEGシグネチャ
|
|
185
|
+
];
|
|
186
|
+
|
|
187
|
+
expect(matcher.compareBuf(buffer, patterns)).toBe(true);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it('patternsがnullの場合nullを返すこと', () => {
|
|
191
|
+
const matcher = new BufferPatternMatcher();
|
|
192
|
+
const buffer = Buffer.from([0x89, 0x50, 0x4E, 0x47]);
|
|
193
|
+
|
|
194
|
+
expect(matcher.compareBuf(buffer, null)).toBeNull();
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it('バッファがパターンより短い場合falseを返すこと', () => {
|
|
198
|
+
const matcher = new BufferPatternMatcher();
|
|
199
|
+
const buffer = Buffer.from([0x89, 0x50]);
|
|
200
|
+
const patterns = [Buffer.from([0x89, 0x50, 0x4E, 0x47])];
|
|
201
|
+
|
|
202
|
+
expect(matcher.compareBuf(buffer, patterns)).toBe(false);
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "ES2022",
|
|
5
|
+
"moduleResolution": "node",
|
|
6
|
+
"lib": ["ES2022"],
|
|
7
|
+
"outDir": "./dist",
|
|
8
|
+
"rootDir": "./src",
|
|
9
|
+
"strict": true,
|
|
10
|
+
"esModuleInterop": true,
|
|
11
|
+
"skipLibCheck": true,
|
|
12
|
+
"forceConsistentCasingInFileNames": true,
|
|
13
|
+
"declaration": true,
|
|
14
|
+
"declarationMap": true,
|
|
15
|
+
"sourceMap": true,
|
|
16
|
+
"types": ["node", "jest"],
|
|
17
|
+
"resolveJsonModule": true,
|
|
18
|
+
"allowSyntheticDefaultImports": true
|
|
19
|
+
},
|
|
20
|
+
"include": ["src/**/*.ts"],
|
|
21
|
+
"exclude": ["node_modules", "dist", "tests"]
|
|
22
|
+
}
|
package/typedoc.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
entryPoints: ['./src/index.ts'],
|
|
3
|
+
entryPointStrategy: 'expand',
|
|
4
|
+
out: './docs/typedoc-md',
|
|
5
|
+
plugin: ['typedoc-plugin-markdown'],
|
|
6
|
+
tsconfig: './tsconfig.json',
|
|
7
|
+
exclude: ['**/node_modules/**', '**/tests/**', '**/dist/**'],
|
|
8
|
+
excludePrivate: false,
|
|
9
|
+
excludeProtected: false,
|
|
10
|
+
includeVersion: true,
|
|
11
|
+
readme: 'none',
|
|
12
|
+
disableSources: false,
|
|
13
|
+
categorizeByGroup: true,
|
|
14
|
+
categoryOrder: ['Classes', 'Functions', 'Variables', '*'],
|
|
15
|
+
sort: ['source-order']
|
|
16
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
|
|
3
|
+
module.exports = {
|
|
4
|
+
mode: 'production',
|
|
5
|
+
devtool: 'source-map',
|
|
6
|
+
target: 'node',
|
|
7
|
+
entry: './src/index.ts',
|
|
8
|
+
output: {
|
|
9
|
+
filename: 'GrepUtil.bundle.js',
|
|
10
|
+
path: path.resolve(__dirname, 'dist'),
|
|
11
|
+
publicPath: '',
|
|
12
|
+
library: {
|
|
13
|
+
name: 'GrepUtil',
|
|
14
|
+
type: 'umd',
|
|
15
|
+
export: 'default'
|
|
16
|
+
},
|
|
17
|
+
globalObject: 'this'
|
|
18
|
+
},
|
|
19
|
+
module: {
|
|
20
|
+
rules: [
|
|
21
|
+
{
|
|
22
|
+
test: /\.ts$/,
|
|
23
|
+
use: 'ts-loader',
|
|
24
|
+
exclude: /node_modules/
|
|
25
|
+
}
|
|
26
|
+
]
|
|
27
|
+
},
|
|
28
|
+
resolve: {
|
|
29
|
+
extensions: ['.ts', '.js'],
|
|
30
|
+
modules: ['node_modules']
|
|
31
|
+
}
|
|
32
|
+
};
|