UNPKG

lingva-scraper-update

Version:
50 lines (49 loc) 2.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.undefinedFields = exports.list = exports.pronunciation = exports.typo = exports.detected = void 0; const language_1 = require("./language"); const detected = ([source, target, detected, extra]) => { const code = source?.[2] ?? target?.[3] ?? detected ?? extra?.[8] ?? extra?.[5]?.[0]?.[0]?.[3]; return code ? (0, language_1.mapLingvaCode)(code) : undefined; }; exports.detected = detected; const typo = ([source]) => (source?.[1]?.[0]?.[4] ?? undefined); exports.typo = typo; exports.pronunciation = { query: ([source]) => (source?.[0] ?? undefined), translation: ([, target]) => (target?.[0]?.[0]?.[1] ?? undefined) }; exports.list = { definitions: ({ 3: extra }) => (extra?.[1]?.[0]?.map(([type, defList]) => ({ type, list: defList?.map(({ 0: definition, 1: example, 4: fieldWrapper, 5: synList }) => ({ definition, example, field: fieldWrapper?.[0]?.[0], synonyms: synList ?.flatMap(synItem => synItem?.[0]?.map(([item]) => item)) ?.filter((item) => !!item) ?? [] })) ?? [] })) ?? []), examples: ({ 3: extra }) => (extra?.[2]?.[0]?.map(([, item]) => item) ?? []), similar: ({ 3: extra }) => (extra?.[3]?.[0] ?? []), extraTranslations: ({ 3: extra }) => (extra?.[5]?.[0]?.map(([type, transList]) => ({ type, list: transList?.map(([word, article, meanings, frequency]) => ({ word, article: article ?? undefined, meanings, frequency: 4 - frequency // Reverse the frequency ranking })) ?? [] })) ?? []), // Added extraTranslations here }; const isObject = (value) => typeof value === "object"; const undefinedFields = (obj) => { if (Array.isArray(obj)) return obj.filter((item) => !!item).map(item => isObject(item) ? (0, exports.undefinedFields)(item) : item); const entries = Object.entries(obj) .filter((entry) => !!entry[1]) .map(([key, value]) => [key, isObject(value) ? (0, exports.undefinedFields)(value) : value]); return Object.fromEntries(entries); }; exports.undefinedFields = undefinedFields;