From a7b3f1dcf93c05b5a33eb9a889aa0d2e4f881347 Mon Sep 17 00:00:00 2001 From: The Jared Wilcurt Date: Thu, 8 Apr 2021 08:25:25 -0400 Subject: [PATCH] DRY entry point There was a simple repeating pattern that could be made more DRY with a map. --- index.js | 63 +++++++++++++++++++++++++------------------------------- 1 file changed, 28 insertions(+), 35 deletions(-) diff --git a/index.js b/index.js index a28f9c7..a55298a 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,3 @@ -var os = require('os'); -var utils = require('./lib/utils'); - // All notifiers var NotifySend = require('./notifiers/notifysend'); var NotificationCenter = require('./notifiers/notificationcenter'); @@ -10,40 +7,36 @@ var WindowsBalloon = require('./notifiers/balloon'); var options = { withFallback: true }; -var osType = utils.isWSL() ? 'WSL' : os.type(); - -switch (osType) { - case 'Linux': - module.exports = new NotifySend(options); - module.exports.Notification = NotifySend; - break; - case 'Darwin': - module.exports = new NotificationCenter(options); - module.exports.Notification = NotificationCenter; - break; - case 'Windows_NT': - if (utils.isLessThanWin8()) { - module.exports = new WindowsBalloon(options); - module.exports.Notification = WindowsBalloon; - } else { - module.exports = new WindowsToaster(options); - module.exports.Notification = WindowsToaster; - } - break; - case 'WSL': - module.exports = new WindowsToaster(options); - module.exports.Notification = WindowsToaster; - break; - default: - if (os.type().match(/BSD$/)) { - module.exports = new NotifySend(options); - module.exports.Notification = NotifySend; - } else { - module.exports = new Growl(options); - module.exports.Notification = Growl; - } +function getOsType () { + var utils = require('./lib/utils'); + var osType = require('os').type(); + + if (osType.match(/BSD$/)) { + osType === 'BSD'; + } else if (osType === 'Windows_NT' && utils.isLessThanWin8()) { + osType === 'Windows_7_and_Below'; + } else if (utils.isWSL()) { + osType = 'WSL'; + } + + return osType.toLowerCase(); } +var osMap = { + bsd: NotifySend, + darwin: NotificationCenter, + linux: NotifySend, + windows_7_and_below: WindowsBalloon, + windows_nt: WindowsToaster, + wsl: WindowsToaster +}; + +var osNotifier = osMap[getOsType()] || Growl; + +// Set the notifier specific to the current OS +module.exports = new osNotifier(options); +module.exports.Notification = osNotifier; + // Expose notifiers to give full control. module.exports.NotifySend = NotifySend; module.exports.NotificationCenter = NotificationCenter;