All files number.ts

97.05% Statements 33/34
87.5% Branches 7/8
100% Functions 5/5
97.05% Lines 33/34

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 1023x                       3x 121x 1x   120x 1x     119x     119x 20x   99x 99x 99x                   3x 11x   11x 3x     8x                     3x 9x 1x     8x   8x 1x     7x                 3x 7x   7x       7x                 3x 3x   3x 2x     1x    
import * as throws from '../core/throws';
 
/**
 * 数値埋め処理。
 *
 * なぜこれを作ったのか、謎い。
 *
 * @param input 数値。
 * @param width 幅数。
 * @param c 埋め文字。
 * @returns
 */
export function padding(input: number, width: number, c: string): string {
	if (input < 0) {
		throw new Error('input is negative');
	}
	if (c.length != 1) {
		throw new Error('c.length is ' + c.length);
	}
 
	const numberValue = input.toString();
 
	// 埋める余地がない場合はそのまま返す
	if (width <= numberValue.length) {
		return numberValue;
	}
	const count = width - numberValue.length;
	const result = c.repeat(count) + numberValue;
	return result;
}
 
/**
 * `parseInt` ラッパー。
 * @param input 数値。
 * @param radix 基数(未指定で10進数)。
 * @returns 整数。
 * @throws {throws.ParseError} パース失敗。
 */
export function parseInt(input: string, radix?: number | undefined) {
	const value = globalThis.parseInt(input, radix);
 
	if (isNaN(value)) {
		throw new throws.ParseError(`input: ${input}, radix: ${radix}`);
	}
 
	return value;
}
 
/**
 * `parseInt` ラッパー。
 * @param input 数値。
 * @param fallback パース失敗時の戻り値。
 * @param radix 基数(未指定で10進数)。
 * @returns 整数。
 * @throws {throws.ArgumentError} `fallback` が整数ではない。
 */
export function parseIntOr(input: string, fallback: number, radix?: number | undefined) {
	if (!Number.isInteger(fallback)) {
		throw new throws.ArgumentError('fallback: Number.isInteger');
	}
 
	const value = globalThis.parseInt(input, radix);
 
	if (isNaN(value)) {
		return fallback;
	}
 
	return value;
}
 
/**
 * `parseFloat` ラッパー。
 * @param input 数値。
 * @returns 実数。
 * @throws {throws.ParseError} パース失敗。
 */
export function parseFloat(input: string) {
	const value = globalThis.parseFloat(input);
 
	Iif (isNaN(value)) {
		throw new throws.ParseError(`input: ${input}`);
	}
 
	return value;
}
 
/**
 * `parseFloat` ラッパー。
 * @param input 数値。
 * @param fallback パース失敗時の戻り値。
 * @returns 実数。
 */
export function parseFloatOr(input: string, fallback: number) {
	const value = globalThis.parseFloat(input);
 
	if (isNaN(value)) {
		return fallback;
	}
 
	return value;
}