All files wildcard.ts

100% Statements 10/10
100% Branches 0/0
100% Functions 3/3
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 691x 1x             1x                                 4x   4x           4x           4x                         4x 4x                   4x          
import * as regex from './regex';
import * as string from './string';
 
/**
 * ワイルドカード処理。
 *
 * `?`/`*` に全てをかけた存在。
 */
export default class Wildcard {
	//#region variable
 
	private _regex: RegExp;
 
	//#endregion
 
	/**
	 * 生成。
	 *
	 * @param pattern ワイルドカードパターン。
	 */
	public constructor(pattern: string) {
		// const regexPattern = regex.escape(pattern)
		// 	.replaceAll("\\?", '.')
		// 	.replaceAll("\\*", '.*')
		// 	;
		const a = regex.escape(pattern);
 
		const b = string.replaceAll(
			a,
			"\\?",
			'.'
		);
 
		const c = string.replaceAll(
			b,
			"\\*",
			'.*'
		);
 
		this._regex = new RegExp('^' + c + '$');
	}
 
	//#region function
 
	/**
	 * インスタンスを使用せずにワイルドカード判定。
	 *
	 * @param input 入力。
	 * @param pattern ワイルドカードパターン。
	 * @returns
	 */
	public static test(input: string, pattern: string): boolean {
		const wildcard = new Wildcard(pattern);
		return wildcard.match(input);
	}
 
	/**
	 * ワイルドカード判定。
	 *
	 * @param input 入力。
	 * @returns
	 */
	public match(input: string): boolean {
		return this._regex.test(input);
	}
 
	//#endregion
}