From 7932b14b82d283b82307372eebcafb90c2a29e85 Mon Sep 17 00:00:00 2001 From: Helmasaur Date: Mon, 13 Jan 2020 03:03:43 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Code=20upgrades?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - the date argument is now and object of two values (day and month) instead of two arguments (#3) - helpers are at the end of the file (until they are moved to an other file #12) - values checking for `signName` and `signSymbol` --- index.js | 54 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/index.js b/index.js index e4acf87..baa633b 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,9 @@ module.exports = defaultLanguage => { return { - getSignByDate: (day, month, language = defaultLanguage) => { - return getSignByDate(day, month, language); + getSignByDate: ({ day, month} = {day: new Date().getDate(), month: new Date().getMonth() + 1 }, language = defaultLanguage) => { + return getSignByDate({ day, month }, language); }, - getSignByName: (signName = getSignByDate().name.toLowerCase(), language = defaultLanguage) => { + getSignByName: (signName, language = defaultLanguage) => { return getSignByName(signName, language); }, getSignBySymbol: (signSymbol, language = defaultLanguage) => { @@ -21,7 +21,7 @@ module.exports = defaultLanguage => { } }; -const getSignByDate = (day = new Date().getDate(), month = new Date().getMonth() + 1, language) => { +const getSignByDate = ({ day, month }, language) => { const date = new Date(`2000-${month}-${day}`); if (date.toString() === 'Invalid Date') { return -1; @@ -51,35 +51,24 @@ const getSignByDate = (day = new Date().getDate(), month = new Date().getMonth() }; const getSignByName = (signName, language) => { + if (signName === null || !(typeof signName === 'string')) { + return -2; + } + const index = getNames().indexOf(signName.charAt(0).toUpperCase() + signName.slice(1)); return getSignByIndex(index, language); }; const getSignBySymbol = (signSymbol, language) => { - const index = getSymbols().indexOf(signSymbol); - - return getSignByIndex(index, language); -}; - -const getSignByIndex = (index, language) => { - if (index === -1) { + if (signSymbol === null || !(typeof signSymbol === 'string')) { return -2; } - const signsData = Object.values(require('./data/zodiac.json')); - let signsLocale; - try { - signsLocale = Object.values(require(`./locales/${language}/zodiac.json`)); - } catch { - signsLocale = Object.values(require('./locales/en/zodiac.json')); - } - - let sign = Object.assign(signsLocale[index], signsData[index]); - sign = getElement(sign, language); + const index = getSymbols().indexOf(signSymbol); - return sign; -} + return getSignByIndex(index, language); +}; const getSymbols = () => { const signsData = require('./data/zodiac.json'); @@ -109,6 +98,25 @@ const getElements = (language) => { return elementsData; }; +const getSignByIndex = (index, language) => { + if (index === -1) { + return -2; + } + + const signsData = Object.values(require('./data/zodiac.json')); + let signsLocale; + try { + signsLocale = Object.values(require(`./locales/${language}/zodiac.json`)); + } catch { + signsLocale = Object.values(require('./locales/en/zodiac.json')); + } + + let sign = Object.assign(signsLocale[index], signsData[index]); + sign = getElement(sign, language); + + return sign; +}; + const getListValue = (key, data) => { data = Object.values(data); const values = [];