Skip to content

Add customisation for default response from jsonMapper #73

@Siemienik

Description

@Siemienik

Actual

The jsonMapper described in #40 and implemented in #43 has fallback mechanism that return null if parsing json failed.

Expected

Default response should be customisable by calling jsonMapper.default<TDefaultResult>(defaultResult: TDefaultResult) which produce mapper with return type TJsonResult | TDefaultResult.

Example usage:

const statusMapper = jsonMapper.default<Stats>({views: 0, unique: 0, stars: 0})

// returns default = {views: 0, unique:0, starts:0} for invalide
const stats = statusMapper<Stats>('invalid');

// returns parsed = {views: 1234, unique:10, starts:40}
const stats2 = statusMapper<Stats>('{"views": 123, "unique":10, "starts":40}');

// in a config: (column definition for vertical list)
// {key:"stats", index:5, mapper: jsonMapper.default<Stats>({views: 0, unique: 0, stars: 0})} 

TODO:

  1. Introduce factory similar like in splitMapper:
    • define JsonMapper type
    • wrap mapper into factory function
    • implement setting default
    • produce and export default jsonMapper created by the factory
  2. prove that it works by covering new changes by unit tests
  3. describe jsonMapper api in a Readme.md

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Status

    To Release

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions