caroso1222 / notyf

👻 A minimalistic, responsive, vanilla JavaScript library to show toast notifications.
https://carlosroso.com/notyf/
MIT License
2.66k stars 197 forks source link

Small optimizations + improvements #126

Open juliyvchirkov opened 2 years ago

juliyvchirkov commented 2 years ago

The proposed update

  1. Optimizes functions success and error to get rid of redundant constant declaration
  2. Improves function normalizeOptions, implementing strict type checking for payload object. The method typeof payload === 'object', used before, fails to detect the type clearly, if passed payload inherits Object prototype being de facto non classic Object. This can lead to possible uncaught runime errors and at least results in displaying empty notification. For example, notyf.error(Array.of('They see me rolling')), notyf.error(new String('They see me rolling')) or notyf.success(null), since the check typeof Array.of('They see me rolling'), typeof new String('They see me rolling') and typeof null will identify these Array, String and Null instances as object
  3. Improves function normalizeOptions, correcting the logic of workflow (although it may be subject for discussion) by swapping the order of payload and options during the merge. (options = { ...options, ...payload } => options = { ...payload, ...options }). The point is to assure the notification, produced by options normalization subroutine, is exactly of type passed in 1st argument to normalizeOptions(type, payload) function regardless of properties of payload object. The logic, used before swapping, could result in curious things like invocation of notyf.error({ type : 'success', message : 'They see me rolling' }), which despite the name produces the notification of type success
  4. Improves function open, covering the original code with additional check to assure the argument passed to notyf.open(options) function is of expected classic Object type and this option object contains property message with value of type String. The point is to avoid arrival of empty notifications if for some reason the options hasn't been combined properly. This improvement has an impact only on accidental failures and one still is able to push an empty notification like notyf.error('') or notyf.open({ type : 'success', message : '' }), declaring message as empty string