Skip to content

add getDefaults method #584

Answered by franky47
NurbekGithub asked this question in Ideas
Jul 2, 2024 · 1 comments · 5 replies
Discussion options

You must be logged in to vote

Your first code example can be done with a simple function (simple at runtime, but harder to make type-safe; PR #578 might help here):

function getDefaultValues(schema: Record<string, ParserBuilder<any>) {
  const result = {}
  for (const [key, parser] of Object.entries(schema)) {
    result[key] = parser.defaultValue
  }
  return result
}

// Or if you prefer a functional programming approach:
function getDefaultValues(schema: Record<string, ParserBuilder<any>) {
  return Object.entries(schema)
    .reduce((acc, [key, parser]) => ({
      ...acc,
      [key]: parser.defaultValue
    }), {})
}

Now, your second code example could work, but going through the serializer could be expensive.

I…

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@NurbekGithub
Comment options

@franky47
Comment options

@NurbekGithub
Comment options

@franky47
Comment options

Answer selected by NurbekGithub
@NurbekGithub
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet
2 participants