From 5064eff2e4e8e5a56362e136b5f9bda01fe8a350 Mon Sep 17 00:00:00 2001 From: Hossam Tarek Date: Tue, 26 Oct 2021 20:56:21 +0200 Subject: [PATCH] Better way to check for property in options Using !key would lead to false positive when sending empty string in title or artist --- lib/utils/index.js | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/lib/utils/index.js b/lib/utils/index.js index cede5b3..14ec8b9 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -1,22 +1,21 @@ const checkOptions = (options) => { - let { apiKey, title, artist } = options; - if (!apiKey) { - throw '"apiKey" property is missing from options'; - } else if (!title) { - throw '"title" property is missing from options'; - } else if (!artist) { - throw '"artist" property is missing from options'; - } + if (!("apiKey" in options) || !options.apiKey) { + throw '"apiKey" property is missing from options'; + } else if (!("title" in options)) { + throw '"title" property is missing from options'; + } else if (!("artist" in options)) { + throw '"artist" property is missing from options'; + } }; const getTitle = (title, artist) => { - return `${title} ${artist}` - .toLowerCase() - .replace(/ *\([^)]*\) */g, '') - .replace(/ *\[[^\]]*]/, '') - .replace(/feat.|ft./g, '') - .replace(/\s+/g, ' ') - .trim(); + return `${title} ${artist}` + .toLowerCase() + .replace(/ *\([^)]*\) */g, "") + .replace(/ *\[[^\]]*]/, "") + .replace(/feat.|ft./g, "") + .replace(/\s+/g, " ") + .trim(); }; module.exports = { checkOptions, getTitle };