Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add customisation for default response from jsonMapper #73

Closed
7 tasks done
Siemienik opened this issue Oct 25, 2020 · 0 comments · Fixed by #201
Closed
7 tasks done

Add customisation for default response from jsonMapper #73

Siemienik opened this issue Oct 25, 2020 · 0 comments · Fixed by #201
Labels
accepted It is accepted to do good first issue Good for newcomers

Comments

@Siemienik
Copy link
Owner

Siemienik commented Oct 25, 2020

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted It is accepted to do good first issue Good for newcomers
Projects
Status: To Release
Development

Successfully merging a pull request may close this issue.

1 participant