question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use defaults for undefined arguments in shorthand call to swal

See original GitHub issue

I found some unexpected behavior when working on #979

jsfiddle

main()
async function main () {
  swal.setDefaults({type: 'question'})
  await swal({ title: 'I am the question type...'})
  await swal('but I am not?')
}

I would expect the second swal to also have type of 'question', but it does not.

It’s due to this line of code, which assigns the values to params even when they are undefined

https://github.com/sweetalert2/sweetalert2/blob/13d90857418f69094c7c259596323cd510b6c970/src/sweetalert2.js#L104

I propose that we treat undefined arguments as such, and use the parameter defaults in these cases. What do y’all think?

I’m not sure if this would be considered a fix or a breaking change. I mean, I’m pretty sure it’s technically a breaking change, but bug fixes usually are. How likely is it that people are relying on the current behavior?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
zenflowcommented, Mar 5, 2018

Bug-fix IMO.

Good. Yeah I agree.

I was thinking of something like this:

function argsToParams(args) {
  const params = {}
  ;['title', 'html', 'type'].forEach((name, index) => {
    if (args[index] !== undefined) {
      params[name] = args[index]
    }
  })
  return params
}

I’ll put in another PR after #992 settles

1reaction
toveruxcommented, Mar 5, 2018

Aha, it’s amusing to see that we always agree on complex subjects and general direction but always fail to find a compromise on the smallest details 😆

I don’t see how it is complex.

I don’t feel comfortable copy-pasting code for the sake of reducing function calls.

It’s not code duplication at this point 😃 You are a bit extreme here. It’s just a small, pure-JS pattern applied to three variables. Our brain likes those visual layouts and patterns. Those LoCs don’t need to be fully read, parsed and analyzed. At the contrary, the array, the function, the loop, the array access syntax, the if branch, the two levels of indentation: they make this code look complicated. I need to read those lines when scanning the code. It’s all about cognitive load (athough the performance is also reduced, but I agree it’s not that relevant here).

But don’t worry, I’m not asking you to change it if you don’t agree, I don’t care that much about that. I’m just saying that I would have not written it like that. And I’m sorry to have started another unproductive debate 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

Be careful when using || to set default values in JavaScript
When no value is passed into a function the value of the parameter will be undefined . Using OR however means that the...
Read more >
Destructuring assignment - JavaScript - MDN Web Docs
Each destructured property can have a default value. The default value is used when the property is not present, or has value undefined...
Read more >
30 Awesome JavaScript Shorthand Techniques That You ...
The shorthand techniques in any programming language help you write code in a much cleaner, optimized, and shorter form.
Read more >
Is there a way to check for both `null` and `undefined`?
Using a juggling-check, you can test both null and undefined in one hit: if (x == null) {. If you use a strict-check,...
Read more >
Functions - Elixir School
Using anonymous functions is such a common practice in Elixir there is ... As you probably already guessed, in the shorthand version our...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found