@gueripep/wordle-solver
Version:
A Wordle solver using entropy maximization and information theory
86 lines • 3.08 kB
JavaScript
/**
* Service for fetching and managing daily Wordle puzzles from NYTimes
*/
import { solveWordle } from "./solver.js";
export class DailyWordleService {
/**
* Fetch today's Wordle puzzle
*/
static async getTodaysWordleData() {
const today = new Date().toISOString().split('T')[0]; // YYYY-MM-DD format
const url = `${this.BASE_URL}${today}.json`;
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to fetch daily Wordle: ${response.status} ${response.statusText}`);
}
const data = await response.json();
// Validate the response
if (!data.solution || data.solution.length !== 5) {
throw new Error('Invalid Wordle data: solution is missing or not 5 letters');
}
return data;
}
catch (error) {
if (error instanceof Error) {
throw new Error(`Error fetching daily Wordle: ${error.message}`);
}
throw new Error('Unknown error fetching daily Wordle');
}
}
/**
* Fetch today's wordle solution
*/
static async getTodaysWordleSolution() {
const data = await this.getTodaysWordleData();
return data.solution.toUpperCase();
}
static async solveTodaysWordle() {
const todaysWordle = await this.getTodaysWordleSolution();
const result = solveWordle(todaysWordle);
return result;
}
/**
* Fetch Wordle puzzle for a specific date
*/
static async getWordleForDate(date) {
// Validate date format (YYYY-MM-DD)
const dateRegex = /^\d{4}-\d{2}-\d{2}$/;
if (!dateRegex.test(date)) {
throw new Error('Date must be in YYYY-MM-DD format');
}
const url = `${this.BASE_URL}${date}.json`;
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to fetch Wordle for ${date}: ${response.status} ${response.statusText}`);
}
const data = await response.json();
// Validate the response
if (!data.solution || data.solution.length !== 5) {
throw new Error('Invalid Wordle data: solution is missing or not 5 letters');
}
return data;
}
catch (error) {
if (error instanceof Error) {
throw new Error(`Error fetching Wordle for ${date}: ${error.message}`);
}
throw new Error(`Unknown error fetching Wordle for ${date}`);
}
}
/**
* Format date for display
*/
static formatDate(dateString) {
const date = new Date(dateString);
return date.toLocaleDateString('en-US', {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric'
});
}
}
DailyWordleService.BASE_URL = 'https://www.nytimes.com/svc/wordle/v2/';
//# sourceMappingURL=daily-wordle.js.map