| 26 | * @param config The configuration for the automatically selected Unit. |
| 27 | */ |
| 28 | export function createUnit<T>( |
| 29 | initialValue: T, |
| 30 | config?: Exclude<UnitConfig<T>, 'initialValue'> |
| 31 | ): ValueToUnitType<T> { |
| 32 | const unitConfig: UnitConfig<T> = {...config, initialValue}; |
| 33 | const typeOfValue = typeof initialValue; |
| 34 | |
| 35 | switch (true) { |
| 36 | case initialValue == null: |
| 37 | default: |
| 38 | return new GenericUnit(unitConfig as any) as any; |
| 39 | case typeOfValue === 'boolean': |
| 40 | return new BoolUnit(unitConfig as any) as any; |
| 41 | case typeOfValue === 'number': |
| 42 | return new NumUnit(unitConfig as any) as any; |
| 43 | case typeOfValue === 'string': |
| 44 | return new StringUnit(unitConfig as any) as any; |
| 45 | case Array.isArray(initialValue): |
| 46 | return new ListUnit(unitConfig as any) as any; |
| 47 | case isDict(initialValue): |
| 48 | return new DictUnit(unitConfig as any) as any; |
| 49 | } |
| 50 | } |