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 | 24x 30x 24x 3947x 3947x 3944x 10351x 70x 70x 4017x 3x 24x 3944x 10672x 488x 924x 270x 252x 151x 65x 61x 1238x 335x 22x 3450x 24x 138x 84x 1x 83x 25x 10x 1x 9x 257x 92x 184x 184x 497x 24x 404x 404x 404x 1591x 1186x 4720x 4720x 45x 156x 156x 162x 279x 16x 37x 404x 24x 78x 78x 78x 78x 77x 1x 1x 13x 8x 5x 4x 22x 22x 80x 23x 10x 9x 31x 116x 3x 78x 24x 1403x 101x 50x 2321x 318x 1403x | /** * デフォルト値を持つ値を提供します. */ export interface ValueSupplier<T> { /** * 値を取得します. * * 値が存在しないときはデフォルト値を返します. */ get (): T /** * デフォルト値を取得します. */ default (): T /** * 値が有効な場合、格納します. * * @param value 格納する値. */ setIf (value: T): void /** * 値が有効な場合、デフォルト値として格納します. * * @param value 格納する値. */ defaultIf (value: T): void } export abstract class ValueValidator<T> { abstract isInvalid (value: T): boolean getMessage (value: T): string { return `値 "${typeof value === 'object' ? JSON.stringify(value) : value}" は設定できません.` } } export class DefaultValueProvider<T> { private value: T constructor (initial: T, private validator?: ValueValidator<T>) { this.throwsIf(initial) this.value = initial } get (): T { return this.value } set (value: T): void { this.throwsIf(value) this.value = value } private throwsIf (value: T): void { if (this.validator?.isInvalid(value)) { throw new Error(this.validator.getMessage(value)) } } } export class PrimitiveValueSupplier<T> implements ValueSupplier<T> { protected _value?: T // eslint-disable-next-line no-useless-constructor protected constructor (protected defaultValue: DefaultValueProvider<T>, protected validator?: ValueValidator<T>) {} get (): T { return this._value ?? this.defaultValue.get() } default (): T { return this.defaultValue.get() } setIf (value?: T): void { if (this.isValid(value)) { this.throwsIf(value) this._value = value } } defaultIf (value?: T): void { if (this.isValid(value)) { this.throwsIf(value) this.defaultValue.set(value) } } protected isValid (value?: T): value is T { return value !== undefined && value !== null } protected throwsIf (value: T): void { if (this.validator?.isInvalid(value)) { throw new Error(this.validator.getMessage(value)) } } static create <V> (initial: V, validator?: ValueValidator<V>): PrimitiveValueSupplier<V> { return new PrimitiveValueSupplier(new DefaultValueProvider<V>(initial, validator), validator) } } export class ObjectSupplier<T> extends PrimitiveValueSupplier<T> implements ValueSupplier<T> { override setIf (obj?: Partial<T>): void { if (this.isValid(obj)) { if (this.validator?.isInvalid(obj)) { throw new Error(this.validator.getMessage(obj)) } this._value = this.parseForcibly(obj) } } override defaultIf (obj?: Partial<T>): void { if (this.isValid(obj)) { if (this.validator?.isInvalid(obj)) { throw new Error(this.validator.getMessage(obj)) } this.defaultValue.set(this.parseForcibly(obj)) } } protected override isValid (obj?: Partial<T>): obj is T { return super.isValid(obj as T) && !Object.keys(this.defaultValue.get() as object).some(key => !Object.prototype.hasOwnProperty.call(obj, key)) } private parseForcibly (obj?: Partial<T>): T { return Object.keys(this.defaultValue.get() as object).reduce((_obj, current) => { _obj[current] = (obj as Record<string, unknown>)[current] return _obj }, {} as Record<string, unknown>) as T } static override create <V> (initial: V, validator?: ValueValidator<V>): ObjectSupplier<V> { return new ObjectSupplier<V>(new DefaultValueProvider<V>(initial, validator), validator) } } export class RecordSupplier<K extends string, V> implements ValueSupplier<Record<K, V>> { private readonly store = new Map<K, PrimitiveValueSupplier<V>>() protected constructor (initial: Record<K, V>, private validator?: ValueValidator<V>) { for (const key of Object.keys(initial) as K[]) { this.store.set(key, PrimitiveValueSupplier.create(initial[key], validator)) } } get (): Record<K, V> { return [...this.store.entries()].reduce((obj, [key, supplier]) => { obj[key] = supplier.get() return obj }, {} as Record<K, V>) } default (): Record<K, V> { return [...this.store.entries()].reduce((obj, [key, supplier]) => { obj[key] = supplier.default() return obj }, {} as Record<K, V>) } setIf (obj: Partial<Record<K, V>>) { for (const key of Object.keys(obj) as K[]) { this.store.get(key)?.setIf(obj[key]) } } defaultIf (obj: Partial<Record<K, V>>) { for (const key of Object.keys(obj) as K[]) { this.store.get(key)?.defaultIf(obj[key]) } } static create <K extends string, V> (initial: Record<K, V>, validator?: ValueValidator<V>): RecordSupplier<K, V> { return new RecordSupplier<K, V>(initial, validator) } } export class ArraySupplier<T> implements ValueSupplier<T[]> { private defaultStore: T[] = [] private store: T[] = [] // eslint-disable-next-line no-useless-constructor protected constructor (protected defaultValue: T[], protected validator?: ValueValidator<T[]>) { this.throwsIf(defaultValue) this.defaultStore = [...defaultValue] } addDefault (value: T): void { this.throwsIf([...this.defaultStore, value]) this.defaultStore.push(value) } default (): T[] { return [...this.defaultStore] } defaultIf (value?: T[]): void { if (this.isValid(value)) { this.throwsIf(value) this.defaultStore = [...value] } } add (value: T): void { this.throwsIf([...this.store, value]) this.store.push(value) } get (): T[] { return [...this.defaultStore, ...this.store] } setIf (value?: T[]): void { if (this.isValid(value)) { this.throwsIf(value) this.store = [...value] } } protected isValid (value?: T[]): value is T[] { return value !== undefined && value !== null } protected throwsIf (value: T[]): void { if (this.validator?.isInvalid(value)) { throw new Error(this.validator.getMessage(value)) } } static create <V> (initial: V[], validator?: ValueValidator<V[]>): ArraySupplier<V> { return new ArraySupplier(initial, validator) } } export class OptionalValueSupplier<T> implements ValueSupplier<T | undefined> { private _value?: T // eslint-disable-next-line no-useless-constructor constructor (private _default?: T) {} default (): T | undefined { return this._default } defaultIf (value: T | undefined) { this._default = value } get (): T | undefined { return this._value ?? this._default } setIf (value: T | undefined) { this._value = value } static create <V> (initial?: V): OptionalValueSupplier<V> { return new OptionalValueSupplier<V>(initial) } } |