noitu
Version: 
A Vietnamese word connection game library with word validation and next word finding capabilities
122 lines • 5.32 kB
JavaScript
;
/**
 * Vietnamese Word Connection Game Library
 *
 * A comprehensive library for creating word connection games in Vietnamese.
 * Supports word validation, next word finding, and game state management.
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
    for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NextWordFinder = exports.WordChecker = void 0;
exports.createWordChecker = createWordChecker;
exports.createNextWordFinder = createNextWordFinder;
exports.loadDictionary = loadDictionary;
exports.validateDictionary = validateDictionary;
const wordChecker_1 = require("./wordChecker");
const nextWordFinder_1 = require("./nextWordFinder");
var wordChecker_2 = require("./wordChecker");
Object.defineProperty(exports, "WordChecker", { enumerable: true, get: function () { return wordChecker_2.WordChecker; } });
var nextWordFinder_2 = require("./nextWordFinder");
Object.defineProperty(exports, "NextWordFinder", { enumerable: true, get: function () { return nextWordFinder_2.NextWordFinder; } });
__exportStar(require("./types"), exports);
/**
 * Create a new WordChecker instance
 * @param dictionary - The word dictionary to use for validation
 * @returns A new WordChecker instance
 */
function createWordChecker(dictionary) {
    return new wordChecker_1.WordChecker(dictionary);
}
/**
 * Create a new NextWordFinder instance
 * @param dictionary - The word dictionary to use for finding next words
 * @returns A new NextWordFinder instance
 */
function createNextWordFinder(dictionary) {
    return new nextWordFinder_1.NextWordFinder(dictionary);
}
/**
 * Utility function to load dictionary from JSON file
 * @param customDictionaryPath - Optional path to a custom JSON dictionary file to extend the built-in dictionary
 * @returns Promise resolving to the loaded dictionary (built-in + custom merged, built-in takes priority)
 */
async function loadDictionary(customDictionaryPath) {
    try {
        const fs = require('fs/promises');
        const path = require('path');
        // Always load the built-in Vietnamese dictionary first
        const builtInPath = path.join(__dirname, '..', 'assets', 'wordPairs.json');
        const builtInData = await fs.readFile(builtInPath, 'utf-8');
        const builtInDictionary = JSON.parse(builtInData);
        // If custom dictionary path provided, load and merge it
        if (customDictionaryPath) {
            try {
                const customData = await fs.readFile(customDictionaryPath, 'utf-8');
                const customDictionary = JSON.parse(customData);
                // Merge dictionaries: built-in takes priority, custom extends it
                const mergedDictionary = { ...builtInDictionary };
                // Add custom entries for keys that don't exist in built-in, or merge arrays
                for (const [key, values] of Object.entries(customDictionary)) {
                    if (!mergedDictionary[key]) {
                        // Key doesn't exist in built-in, add from custom
                        mergedDictionary[key] = [...values];
                    }
                    else {
                        // Key exists in both, merge arrays without duplicates
                        const existingValues = new Set(mergedDictionary[key]);
                        for (const value of values) {
                            if (!existingValues.has(value)) {
                                mergedDictionary[key].push(value);
                            }
                        }
                    }
                }
                return mergedDictionary;
            }
            catch (customError) {
                console.warn(`Warning: Failed to load custom dictionary from ${customDictionaryPath}: ${customError}`);
                console.warn('Using built-in dictionary only.');
                // Fall back to built-in dictionary only
            }
        }
        return builtInDictionary;
    }
    catch (error) {
        throw new Error(`Failed to load dictionary: ${error}`);
    }
}
/**
 * Utility function to validate dictionary format
 * @param dictionary - The dictionary to validate
 * @returns True if dictionary is valid, false otherwise
 */
function validateDictionary(dictionary) {
    if (!dictionary || typeof dictionary !== 'object') {
        return false;
    }
    for (const [key, value] of Object.entries(dictionary)) {
        if (typeof key !== 'string' || !Array.isArray(value)) {
            return false;
        }
        for (const item of value) {
            if (typeof item !== 'string') {
                return false;
            }
        }
    }
    return true;
}
//# sourceMappingURL=index.js.map