Skip to content

Instantly share code, notes, and snippets.

@SamadiPour
Last active May 27, 2024 07:23
Show Gist options
  • Save SamadiPour/44f9dfe9ec8918c04dd86b2c055baa4c to your computer and use it in GitHub Desktop.
Save SamadiPour/44f9dfe9ec8918c04dd86b2c055baa4c to your computer and use it in GitHub Desktop.
ADSL TCI plan sorter
const planCards = document.querySelectorAll('#service-list .uk-card');
const cardDetails = [];
function convertPersianNumbersToEnglish(text) {
return text.replace(/[۰-۹]/g, (char) => String.fromCharCode(char.charCodeAt(0) - '۰'.charCodeAt(0) + '0'.charCodeAt(0)));
}
planCards.forEach(card => {
const title = convertPersianNumbersToEnglish(card.querySelector('.uk-card-title').textContent.trim());
const description = convertPersianNumbersToEnglish(card.querySelector('.uk-card-body p').textContent.trim());
const price = convertPersianNumbersToEnglish(card.querySelector('.price h3').textContent.trim());
const numericPrice = Number(price.replace(/[^\d.]/g, '')) / 10;
const gb = Number(title.replace(/[^\d.]/g, ''));
const monthMatch = description.match(/(\d+)\s*ماهه/);
const months = monthMatch ? Number(monthMatch[1]) : null;
const ratio = numericPrice / gb;
const gbPerMonth = gb / months;
cardDetails.push({ card, ratio, numericPrice, gb, months, gbPerMonth });
});
text = ''
cardDetails.sort((a, b) => a.ratio - b.ratio);
cardDetails.forEach(({ card, ratio, numericPrice, gb, months, gbPerMonth }) => {
text += `Ratio: ${ratio}\n`;
text += `Price: ${numericPrice}\n`;
text += `GB: ${gb}\n`;
text += `Months: ${months} (${gbPerMonth} per month)\n`;
text += `---------------------------\n`;
});
console.log(text);
@hosni
Copy link

hosni commented Dec 18, 2023

I suggest using IIFE to be able to run over and over.
Just put the above code in this block:

(function() {
# CODE
})()
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment