Last active
May 27, 2024 07:23
-
-
Save SamadiPour/44f9dfe9ec8918c04dd86b2c055baa4c to your computer and use it in GitHub Desktop.
ADSL TCI plan sorter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I suggest using
IIFE
to be able to run over and over.Just put the above code in this block: