2019-05-21 20:14:00 +02:00
|
|
|
import { getCookie } from './StackOverflow/cookie';
|
2019-05-03 18:34:11 +02:00
|
|
|
|
2019-05-04 00:10:41 +02:00
|
|
|
export function getNextId() {
|
|
|
|
const lastId = window.currentDictionary.words.reduce((highestId, word) => {
|
|
|
|
return (word.wordId && word.wordId) > highestId ? word.wordId : highestId;
|
|
|
|
}, 0);
|
|
|
|
return lastId + 1;
|
|
|
|
}
|
|
|
|
|
2019-05-03 18:34:11 +02:00
|
|
|
export function getWordsStats() {
|
2019-05-03 20:29:53 +02:00
|
|
|
const {words, partsOfSpeech} = window.currentDictionary;
|
|
|
|
const {caseSensitive} = window.currentDictionary.settings;
|
2019-05-03 18:34:11 +02:00
|
|
|
|
|
|
|
const wordStats = {
|
|
|
|
numberOfWords: [
|
|
|
|
{
|
|
|
|
name: 'Total',
|
|
|
|
value: words.length,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
wordLength: {
|
|
|
|
shortest: 0,
|
|
|
|
longest: 0,
|
|
|
|
average: 0,
|
|
|
|
},
|
|
|
|
letterDistribution: [
|
|
|
|
/* {
|
|
|
|
letter: '',
|
|
|
|
number: 0,
|
|
|
|
percentage: 0.00,
|
|
|
|
} */
|
|
|
|
],
|
|
|
|
totalLetters: 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
partsOfSpeech.forEach(partOfSpeech => {
|
|
|
|
const wordsWithPartOfSpeech = words.filter(word => word.partOfSpeech === partOfSpeech);
|
|
|
|
wordStats.numberOfWords.push({
|
|
|
|
name: partOfSpeech,
|
|
|
|
value: wordsWithPartOfSpeech.length,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
wordStats.numberOfWords.push({
|
|
|
|
name: 'Unclassified',
|
|
|
|
value: words.filter(word => !partsOfSpeech.includes(word.partOfSpeech)).length,
|
|
|
|
});
|
|
|
|
|
|
|
|
let totalLetters = 0;
|
|
|
|
const numberOfLetters = {};
|
|
|
|
|
|
|
|
words.forEach(word => {
|
|
|
|
const shortestWord = wordStats.wordLength.shortest;
|
|
|
|
const longestWord = wordStats.wordLength.longest;
|
|
|
|
const wordLetters = word.name.split('');
|
|
|
|
const lettersInWord = wordLetters.length;
|
|
|
|
|
|
|
|
totalLetters += lettersInWord;
|
|
|
|
|
|
|
|
if (shortestWord === 0 || lettersInWord < shortestWord) {
|
|
|
|
wordStats.wordLength.shortest = lettersInWord;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (longestWord === 0 || lettersInWord > longestWord) {
|
|
|
|
wordStats.wordLength.longest = lettersInWord;
|
|
|
|
}
|
|
|
|
|
|
|
|
wordLetters.forEach(letter => {
|
|
|
|
const letterToUse = caseSensitive ? letter : letter.toLowerCase();
|
|
|
|
if (!numberOfLetters.hasOwnProperty(letterToUse)) {
|
|
|
|
numberOfLetters[letterToUse] = 1;
|
|
|
|
} else {
|
|
|
|
numberOfLetters[letterToUse]++;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
wordStats.totalLetters = totalLetters;
|
2019-05-04 05:52:19 +02:00
|
|
|
wordStats.wordLength.average = words.length > 0 ? Math.round(totalLetters / words.length) : 0;
|
2019-05-03 18:34:11 +02:00
|
|
|
|
|
|
|
for (const letter in numberOfLetters) {
|
|
|
|
if (numberOfLetters.hasOwnProperty(letter)) {
|
|
|
|
const number = numberOfLetters[letter];
|
|
|
|
wordStats.letterDistribution.push({
|
|
|
|
letter,
|
|
|
|
number,
|
|
|
|
percentage: number / totalLetters,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wordStats.letterDistribution.sort((a, b) => {
|
|
|
|
if (a.percentage === b.percentage) return 0;
|
|
|
|
return (a.percentage > b.percentage) ? -1 : 1;
|
|
|
|
});
|
|
|
|
|
|
|
|
return wordStats;
|
|
|
|
}
|
2019-05-03 19:46:11 +02:00
|
|
|
|
|
|
|
export function wordExists(word, returnId = false) {
|
|
|
|
const { currentDictionary } = window;
|
|
|
|
const { caseSensitive } = currentDictionary.settings;
|
|
|
|
const foundWord = currentDictionary.words.find(existingWord => {
|
|
|
|
return caseSensitive ? existingWord.name === word : existingWord.name.toLowerCase() === word.toLowerCase();
|
|
|
|
});
|
|
|
|
return foundWord ? (returnId ? foundWord.wordId : true) : false;
|
2019-05-03 20:02:46 +02:00
|
|
|
}
|
2019-05-07 00:28:51 +02:00
|
|
|
|
2019-05-29 07:45:32 +02:00
|
|
|
export function getHomonymnIndexes(word) {
|
|
|
|
const { currentDictionary } = window;
|
|
|
|
const { caseSensitive } = currentDictionary.settings;
|
|
|
|
const foundIndexes = [];
|
|
|
|
currentDictionary.words.forEach((existingWord, index) => {
|
|
|
|
if (existingWord.wordId !== word.wordId
|
|
|
|
&& (caseSensitive ? existingWord.name === word.name : existingWord.name.toLowerCase() === word.name.toLowerCase())) {
|
|
|
|
foundIndexes.push(index);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return foundIndexes;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getHomonymnNumber(word) {
|
|
|
|
const homonyms = getHomonymnIndexes(word);
|
|
|
|
if (homonyms.length > 0) {
|
|
|
|
const index = window.currentDictionary.words.findIndex(w => w.wordId === word.wordId);
|
|
|
|
let number = 1;
|
|
|
|
|
|
|
|
for (let i = 0; i < homonyms.length; i++) {
|
|
|
|
if (index < homonyms[i]) break;
|
|
|
|
number++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return number;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-23 23:24:56 +02:00
|
|
|
export function addMessage(messageText, time = 5000, extraClass = false) {
|
2019-05-09 01:13:23 +02:00
|
|
|
const messagingSection = document.getElementById('messagingSection');
|
|
|
|
const element = document.createElement('div');
|
|
|
|
element.classList.add('message');
|
2019-05-23 23:24:56 +02:00
|
|
|
if (extraClass !== false) {
|
|
|
|
element.classList.add(extraClass);
|
|
|
|
}
|
2019-05-25 02:02:04 +02:00
|
|
|
element.innerHTML = `<a class="close-button" style="animation-duration: ${time / 1000}s;">×︎</a>` + messageText;
|
2019-05-09 01:13:23 +02:00
|
|
|
messagingSection.appendChild(element);
|
|
|
|
|
|
|
|
const closeButton = element.querySelector('.close-button');
|
|
|
|
const closeMessage = () => {
|
|
|
|
closeButton.removeEventListener('click', closeMessage);
|
2019-06-08 06:36:00 +02:00
|
|
|
fadeOutElement(element);
|
2019-05-09 01:13:23 +02:00
|
|
|
};
|
|
|
|
closeButton.addEventListener('click', closeMessage);
|
|
|
|
|
2019-05-24 19:45:08 +02:00
|
|
|
if (time > 0) {
|
|
|
|
setTimeout(closeMessage, time);
|
|
|
|
}
|
2019-05-09 01:13:23 +02:00
|
|
|
}
|
2019-05-21 20:14:00 +02:00
|
|
|
|
2019-06-08 06:36:00 +02:00
|
|
|
export function fadeOutElement(element) {
|
|
|
|
element.classList.add('fadeout');
|
|
|
|
setTimeout(() => {
|
|
|
|
element.parentElement.removeChild(element);
|
|
|
|
}, 300);
|
|
|
|
}
|
|
|
|
|
2019-05-24 03:20:38 +02:00
|
|
|
export function hideAllModals() {
|
|
|
|
const permanentModals = ['#searchModal', '#settingsModal', '#editModal'];
|
|
|
|
const hideModals = document.querySelectorAll(permanentModals.join(',')),
|
|
|
|
removeModals = document.querySelectorAll('.modal:not(' + permanentModals.join('):not(') + ')');
|
|
|
|
Array.from(hideModals).forEach(modal => modal.style.display = 'none');
|
|
|
|
Array.from(removeModals).forEach(modal => modal.parentElement.removeChild(modal));
|
|
|
|
}
|
|
|
|
|
2019-05-21 20:14:00 +02:00
|
|
|
export function hasToken() {
|
2019-06-06 01:34:56 +02:00
|
|
|
return window.isOffline !== true && getCookie('token') !== '';
|
2019-05-21 20:14:00 +02:00
|
|
|
}
|
2019-07-10 01:03:52 +02:00
|
|
|
|
|
|
|
export function objectValuesAreDifferent(newObject, oldObject) {
|
|
|
|
let valuesAreDifferent = false;
|
|
|
|
for (let property in newObject) {
|
|
|
|
if (!oldObject.hasOwnProperty(property) || JSON.stringify(newObject[property]) !== JSON.stringify(oldObject[property])) {
|
|
|
|
valuesAreDifferent = true;
|
|
|
|
}
|
|
|
|
if (typeof newObject[property] === 'object' && !Array.isArray(newObject[property])) {
|
|
|
|
valuesAreDifferent = objectValuesAreDifferent(newObject[property], oldObject[property]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (valuesAreDifferent) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return valuesAreDifferent;
|
|
|
|
}
|