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

Passing an incorrect builder option in create breaks things #103

Open
vjpr opened this issue Sep 8, 2021 · 0 comments
Open

Passing an incorrect builder option in create breaks things #103

vjpr opened this issue Sep 8, 2021 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@vjpr
Copy link

vjpr commented Sep 8, 2021

The function param overloading is confusing, and broken.

If you make a typo in the argument to builderOptions, then it will use defaults.

Validation should be added and overloading should be removed.

export function convert(p1: XMLBuilderCreateOptions | string | ExpandObject,
  p2?: string | ExpandObject | WriterOptions, p3?: WriterOptions): XMLSerializedValue {

  let builderOptions: XMLBuilderCreateOptions
  let contents: string | ExpandObject
  let convertOptions: WriterOptions | undefined
  if (isXMLBuilderCreateOptions(p1) && p2 !== undefined) {
    builderOptions = p1
    contents = p2
    convertOptions = p3
  } else {
    builderOptions = DefaultBuilderOptions
    contents = p1
    convertOptions = p2 as WriterOptions || undefined
  }

  return create(builderOptions, contents).end(convertOptions as any)
}
function isXMLBuilderCreateOptions(obj: any): obj is XMLBuilderCreateOptions {
  if (!isPlainObject(obj)) return false

  for (const key in obj) {
    /* istanbul ignore else */
    if (obj.hasOwnProperty(key)) {
      if (!XMLBuilderOptionKeys.has(key)) return false
    }
  }

  return true
}
@vjpr vjpr added the bug Something isn't working label Sep 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants