Created
November 19, 2024 13:28
-
-
Save nemesisqp/939df960644b60791e53bf42c56aa705 to your computer and use it in GitHub Desktop.
GPT classify price
This file contains hidden or 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
type Currency = | |
| "USD" | "EUR" | "GBP" | "JPY" | "AUD" | "CAD" | "CHF" | "CNY" | "SEK" | "NZD" | |
| "MXN" | "SGD" | "HKD" | "NOK" | "KRW" | "TRY" | "INR" | "RUB" | "BRL" | "ZAR" | |
| "DKK" | "PLN" | "THB" | "IDR" | "MYR" | "PHP" | "CZK" | "HUF" | "ILS" | "SAR" | |
| "AED" | "CLP" | "COP" | "ARS" | "VND" | "EGP" | "BDT" | "PKR" | "NGN" | "GHS"; | |
interface PriceRange { | |
low: number; // Lower bound for "low" in USD | |
medium: number; // Lower bound for "medium" in USD | |
high: number; // Lower bound for "high" in USD | |
} | |
const conversionRates: Record<Currency, number> = { | |
USD: 1, | |
EUR: 1.1, | |
GBP: 1.25, | |
JPY: 0.007, | |
AUD: 0.65, | |
CAD: 0.75, | |
CHF: 1.1, | |
CNY: 0.14, | |
SEK: 0.095, | |
NZD: 0.6, | |
MXN: 0.05, | |
SGD: 0.73, | |
HKD: 0.13, | |
NOK: 0.095, | |
KRW: 0.00076, | |
TRY: 0.036, | |
INR: 0.012, | |
RUB: 0.01, | |
BRL: 0.2, | |
ZAR: 0.05, | |
DKK: 0.15, | |
PLN: 0.24, | |
THB: 0.028, | |
IDR: 0.000065, | |
MYR: 0.21, | |
PHP: 0.017, | |
CZK: 0.042, | |
HUF: 0.0027, | |
ILS: 0.25, | |
SAR: 0.27, | |
AED: 0.27, | |
CLP: 0.0011, | |
COP: 0.00025, | |
ARS: 0.0027, | |
VND: 0.000041, | |
EGP: 0.032, | |
BDT: 0.0092, | |
PKR: 0.0035, | |
NGN: 0.0013, | |
GHS: 0.08, | |
}; | |
function classifyPrice( | |
input: string, | |
priceRange: PriceRange, | |
defaultCurrency: Currency = "USD" | |
): string { | |
// Handle predefined classifications ($, $$, $$$) | |
if (["$", "$$", "$$$"].includes(input.trim())) { | |
return input.trim(); | |
} | |
// Extract numeric range and currency | |
const currencyRegex = /[€£¥₹₽₩₺฿₴₦₱]/; | |
const rangeRegex = /(\d+)(?:–|-|to)?(\d+)?|\d+\+/; | |
const currencySymbol = input.match(currencyRegex)?.[0]; | |
const rangeMatch = input.match(rangeRegex); | |
const lowerStr = rangeMatch?.[1]; | |
const upperStr = rangeMatch?.[2]; | |
const lower = lowerStr ? parseFloat(lowerStr) : undefined; | |
const upper = upperStr ? parseFloat(upperStr) : undefined; | |
const isOpenEnded = input.includes("+"); | |
const currency = getCurrencyFromSymbol(currencySymbol) || defaultCurrency; | |
// Convert prices to USD | |
const conversionRate = conversionRates[currency] || 1; | |
const lowerUSD = lower !== undefined ? lower * conversionRate : undefined; | |
const upperUSD = upper !== undefined ? upper * conversionRate : undefined; | |
// Determine the price value to compare against ranges | |
const priceToEvaluate = isOpenEnded ? (lowerUSD || 0) : ((lowerUSD || 0) + (upperUSD || lowerUSD || 0)) / 2; | |
// Classify the price | |
if (priceToEvaluate < priceRange.low) return "$"; | |
if (priceToEvaluate < priceRange.medium) return "$$"; | |
return "$$$"; | |
} | |
function getCurrencyFromSymbol(symbol?: string): Currency | undefined { | |
const symbolToCurrency: Record<string, Currency> = { | |
"$": "USD", | |
"€": "EUR", | |
"£": "GBP", | |
"¥": "JPY", | |
"₹": "INR", | |
"₽": "RUB", | |
"₩": "KRW", | |
"₺": "TRY", | |
"฿": "THB", | |
"₴": "UAH", | |
"₦": "NGN", | |
"₱": "PHP", | |
}; | |
return symbol ? symbolToCurrency[symbol] : undefined; | |
} | |
// Example Usage | |
const priceRange: PriceRange = { low: 10, medium: 50, high: 100 }; | |
const inputs = [ | |
"$$", "€10–20", "€20–30", "€10–20", "€10–20", "€10–20", | |
"$$$", "€20–40", "€10–20", "€20–30", "$$$", "€10–20", | |
"$$", "$$$", "€10–20", "$$$", "€20–30", "$$", "€100+", "€10–20" | |
]; | |
inputs.forEach(input => { | |
console.log(`${input} => ${classifyPrice(input, priceRange)}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
more optimized ver