Skip to content

Commit

Permalink
馃帹 Code upgrades
Browse files Browse the repository at this point in the history
- 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`
  • Loading branch information
helmasaur committed Jan 13, 2020
1 parent a782240 commit 7932b14
Showing 1 changed file with 31 additions and 23 deletions.
54 changes: 31 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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;
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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 = [];
Expand Down

0 comments on commit 7932b14

Please sign in to comment.