All files / src/builder fieldConfigure.ts

100% Statements 13/13
100% Branches 5/5
100% Functions 5/5
100% Lines 11/11

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  22x                                     22x       168x 168x 168x               10x 7x 7x   3x             167x 167x      
import { FieldConfig, FieldConfigSupplier } from '../value/fieldConfig'
import { Field, FieldImpl } from '../model/field'
 
/**
 * {@link Field} を新規作成する際の各種設定を保持します.
 */
export interface FieldConfigure {
  /**
   * 作成する Field のライブラリ利用者が自由に使えるフィールドを取得します.
   */
  vars (): unknown
 
  /**
   * 作成する Field のライブラリ利用者が自由に使えるフィールドを設定します.
   *
   * @param vars ライブラリ利用者が自由に使えるフィールド
   */
  vars (vars: unknown): FieldConfigure
}
 
export class FieldConfigureImpl implements FieldConfigure {
  private readonly getter: () => FieldConfig
  private readonly setter: (obj: Partial<FieldConfig>) => void
 
  constructor (isDefault: boolean, private readonly config: FieldConfigSupplier) {
    this.getter = () => isDefault ? config.default() : config.get()
    this.setter = obj => isDefault ? config.defaultIf(obj) : config.setIf(obj)
  }
 
  vars (): unknown
 
  vars (vars: unknown): FieldConfigure
 
  vars (args?: unknown): unknown | FieldConfigure {
    if (arguments.length > 0) {
      this.setter({ vars: args })
      return this
    }
    return this.getter().vars
  }
 
  /**
   * Field を作成します.
   */
  build (): Field {
    const config = this.config.get()
    return new FieldImpl({ vars: config.vars })
  }
}