Skip to content

Instantly share code, notes, and snippets.

@iHateYourGlasses
Last active June 14, 2020 22:30
Show Gist options
  • Save iHateYourGlasses/9772632489c711a16e5208cbe1c0bad3 to your computer and use it in GitHub Desktop.
Save iHateYourGlasses/9772632489c711a16e5208cbe1c0bad3 to your computer and use it in GitHub Desktop.
mu parser
const pipe = (...fns) => x => fns.reduce((y, f) => f(y), x);
const wordsCounter = text => text.split(' ').length;
const removeMultipleSpaces = text => text.replace(/\s\s+/gim, '');
const removeStartEndNonAlphabetic = text =>
text.replace(/^[^A-Za-zА-Яа-я0-9]|[^A-Za-zА-Яа-я0-9]$/gim, '');
const removeStartEndSpace = text => text.replace(/^\s|\s$/gim, '');
const toLowerCase = text => text.toLowerCase();
const removeRating = text => text.replace(/^(\s+)?\d{0,2}(\.|\)| - |-)/gim, '');
const cleanUpWord = pipe(
removeMultipleSpaces,
removeStartEndNonAlphabetic,
removeStartEndSpace,
toLowerCase,
);
const parse = () => {
let result = {};
const posts = Array.from(document.querySelectorAll('.post__message '));
posts.forEach(post =>
post.childNodes.forEach(words => {
if (words.nodeType !== 3 || !words.textContent) {
return;
}
const textHasBandAndSong = words.textContent.includes('-');
if (!textHasBandAndSong) {
return;
}
const bandAndSong = removeRating(words.textContent);
let [band, song] = bandAndSong.split(' - ');
if (!song) {
[band, song] = bandAndSong.split('-');
if (!song) {
return;
}
}
if (wordsCounter(band) > 12 || wordsCounter(song) > 12) {
return;
}
band = cleanUpWord(band);
song = cleanUpWord(song);
result = {
...result,
[band]: {
...(result[band] || {}),
[song]: ((result[band] || [])[song] || 0) + 1,
_totalSongs: ((result[band] || {})._totalSongs || 0) + 1,
},
};
}),
);
return result;
};
const parsedData = parse();
const sortedArtists = Object.keys(parsedData)
.sort((a, b) => parsedData[b]._totalSongs - parsedData[a]._totalSongs)
.map(band => `${band} - ${parsedData[band]._totalSongs}`);
console.log('FULL DATA - ', parsedData);
console.log('SORTED ARTISTS - ', sortedArtists);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment