All files string.ts

89.47% Statements 85/95
76.08% Branches 35/46
95.23% Functions 20/21
89.24% Lines 83/93

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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 3102x 2x               2x 9x           2x                                                             2x 22x   22x       22x 48x 37x     11x     11x                 2x 21x   21x       21x 30x 20x     10x     11x                 2x 16x   16x       16x     2x 13x 4x       4x     9x     2x   8x                     2x 11x 3x     8x     2x 9x 3x     6x                                                                 37x 37x           37x       37x                                 20x       11x       6x             7x 7x 1x   6x   7x 5x     2x       7x 1x 1x     6x 2x       4x       7x 1x       6x 2x       5x       7x 1x       6x 1x       6x       9x 9x 9x 9x 6x           3x     2x 37x   7x     7x     7x     7x     9x            
import * as types from './types';
import * as regex from './regex';
 
/**
 * 非空文字列(ホワイトスペース構成は除く)か。
 *
 * @param s
 * @returns
 */
export function isNotWhiteSpace(s: string | null | undefined): s is string {
	return types.isString(s) && trim(s).length !== 0;
}
 
/**
 * トリムの未指定時の対象文字。
 */
const TrimCharacters: ReadonlySet<string> = new Set([
	"\u{0009}",
	"\u{000a}",
	"\u{000b}",
	"\u{000c}",
	"\u{000d}",
	"\u{0085}",
	"\u{00a0}",
	"\u{0020}",
	"\u{2000}",
	"\u{2001}",
	"\u{2002}",
	"\u{2003}",
	"\u{2004}",
	"\u{2005}",
	"\u{2006}",
	"\u{2007}",
	"\u{2008}",
	"\u{2009}",
	"\u{200A}",
	"\u{202F}",
	"\u{205F}",
	"\u{3000}",
]);
 
/**
 * 先頭文字列のトリム処理。
 * @param s
 * @param characters
 * @returns
 */
export function trimStart(s: string, characters: ReadonlySet<string> | null = null): string {
	characters ??= TrimCharacters;
 
	Iif (!characters.size) {
		return s;
	}
 
	for (let i = 0; i < s.length; i++) {
		if (characters.has(s[i])) {
			continue;
		}
 
		return s.substring(i);
	}
 
	return '';
}
 
/**
 * 終端文字列のトリム処理。
 * @param s
 * @param characters
 * @returns
 */
export function trimEnd(s: string, characters: ReadonlySet<string> | null = null): string {
	characters ??= TrimCharacters;
 
	Iif (!characters.size) {
		return s;
	}
 
	for (let i = 0; i < s.length; i++) {
		if (characters.has(s[s.length - i - 1])) {
			continue;
		}
 
		return s.substring(0, s.length - i);
	}
 
	return '';
}
 
/**
 * 前後のトリム処理。
 * @param s
 * @param characters
 * @returns
 */
export function trim(s: string, characters: ReadonlySet<string> | null = null): string {
	characters ??= TrimCharacters;
 
	Iif (!characters.size) {
		return s;
	}
 
	return trimEnd(trimStart(s, characters), characters);
}
 
export function replaceAllImpl(source: string, searchValue: string | RegExp, replaceValue: string): string {
	if (searchValue instanceof RegExp) {
		const flags = searchValue.flags.includes('g')
			? searchValue.flags
			: searchValue.flags + 'g'
			;
		return source.replace(new RegExp(searchValue.source, flags), replaceValue);
	}
 
	return source.replace(new RegExp(regex.escape(searchValue), 'g'), replaceValue);
}
 
export function replaceAll(source: string, searchValue: string | RegExp, replaceValue: string): string {
	//if (!String.prototype.replaceAll) {
		return replaceAllImpl(source, searchValue, replaceValue);
	//}
 
	//return source.replaceAll(searchValue, replaceValue);
}
 
/**
 * 改行分割。
 * @param source
 * @returns
 */
export function splitLines(source: string | null | undefined): Array<string> {
	if (!source) {
		return [];
	}
 
	return source.split(/\r\n|\n|\r/);
}
 
export function toBoolean(s: string | null | undefined): boolean {
	if (!s) {
		return false;
	}
 
	return s.toLowerCase() === 'true';
}
 
export const enum MatchMode {
	Partial,
	Forward,
	Backward,
	Perfect,
	Regex,
}
 
export interface MatchResult {
	//#region property
 
	readonly matched: boolean;
	readonly isRegex: boolean;
	readonly regex: RegExp;
 
	//#endregion
 
	//#region function
	//#endregion
}
 
class MatchResultImpl {
	//#region variable
 
	private _matched: boolean;
	private _regex: RegExp | null;
 
	//#endregion
 
	private constructor(matched: boolean, regex: RegExp | null) {
		this._matched = matched;
		this._regex = regex;
	}
 
	//#region MatchResult
 
	public get matched(): boolean {
		return this._matched;
	}
 
	public get isRegex(): boolean {
		return this._regex !== null;
	}
 
	public get regex(): RegExp {
		Iif (!this._regex) {
			throw new Error('regex');
		}
 
		return this._regex;
	}
 
	//#endregion
 
 
	//#region function
 
	public static none(): MatchResultImpl {
		return new MatchResultImpl(false, null);
	}
 
	public static matchedPlain(): MatchResultImpl {
		return new MatchResultImpl(true, null);
	}
 
	public static matchedRegex(regex: RegExp): MatchResultImpl {
		return new MatchResultImpl(true, regex);
	}
 
	//#endregion
}
 
function matchPartial(input: string, pattern: string, ignoreCase: boolean): MatchResultImpl {
	let index = -1;
	if (ignoreCase) {
		index = input.toLowerCase().indexOf(pattern.toLowerCase());
	} else {
		index = input.indexOf(pattern);
	}
	if (index !== -1) {
		return MatchResultImpl.matchedPlain();
	}
 
	return MatchResultImpl.none();
}
 
function matchForward(input: string, pattern: string, ignoreCase: boolean): MatchResultImpl {
	if (ignoreCase) {
		if (input.toLowerCase().startsWith(pattern.toLowerCase())) {
			return MatchResultImpl.matchedPlain();
		}
	} else {
		if (input.startsWith(pattern)) {
			return MatchResultImpl.matchedPlain();
		}
	}
 
	return MatchResultImpl.none();
}
 
function matchBackward(input: string, pattern: string, ignoreCase: boolean): MatchResultImpl {
	if (ignoreCase) {
		Iif (input.toLowerCase().endsWith(pattern.toLowerCase())) {
			return MatchResultImpl.matchedPlain();
		}
	} else {
		if (input.endsWith(pattern)) {
			return MatchResultImpl.matchedPlain();
		}
	}
 
	return MatchResultImpl.none();
}
 
function matchPerfect(input: string, pattern: string, ignoreCase: boolean): MatchResultImpl {
	if (ignoreCase) {
		Iif (input.toLowerCase() === pattern.toLowerCase()) {
			return MatchResultImpl.matchedPlain();
		}
	} else {
		if (input === pattern) {
			return MatchResultImpl.matchedPlain();
		}
	}
 
	return MatchResultImpl.none();
}
 
function matchRegex(input: string, pattern: string, ignoreCase: boolean): MatchResultImpl {
	const flags = ignoreCase ? 'gi' : 'g';
	try {
		const regex = new RegExp(pattern, flags);
		if (regex.test(input)) {
			return MatchResultImpl.matchedRegex(regex);
		}
	} catch (ex) {
		console.warn('matchRegex', ex);
	}
 
	return MatchResultImpl.none();
}
 
export function match(input: string, pattern: string, ignoreCase: boolean, mode: MatchMode): MatchResult {
	switch (mode) {
		case MatchMode.Partial:
			return matchPartial(input, pattern, ignoreCase);
 
		case MatchMode.Forward:
			return matchForward(input, pattern, ignoreCase);
 
		case MatchMode.Backward:
			return matchBackward(input, pattern, ignoreCase);
 
		case MatchMode.Perfect:
			return matchPerfect(input, pattern, ignoreCase);
 
		case MatchMode.Regex:
			return matchRegex(input, pattern, ignoreCase);
 
		default:
			return MatchResultImpl.none();
	}
}