UNPKG

cambridge-dictionary-api

Version:

API for Cambridge Dictionary

42 lines (41 loc) 1.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.extractDefinitions = extractDefinitions; function extractDefinitions($) { const definitions = []; $(".pr.entry-body__el").each((_, entry) => { const posElement = $(entry).find(".pos.dpos").first(); if (!posElement.length) return; const partOfSpeech = posElement.text().trim(); $(entry) .find(".def-block.ddef_block") .each((_, defBlock) => { const definitionElement = $(defBlock).find(".def.ddef_d.db"); if (!definitionElement.length) return; const definition = definitionElement.text().trim(); const levelElement = $(defBlock).find(".epp-xref.dxref"); const level = levelElement.length ? levelElement.text().trim() : undefined; const examples = []; $(defBlock) .find(".examp.dexamp .eg.deg") .each((_, ex) => { const text = $(ex).text().trim(); if (text) examples.push(text); }); if (examples.length >= 2) { definitions.push({ definition, partOfSpeech, level, examples, }); } }); }); return definitions; }