UNPKG

jeuxdemots

Version:

Module package for JeuxDeMots.org.

39 lines (33 loc) 1.3 kB
'use strict'; const Dump = require('./lib/Dump'); const http = require('http'); /** * A module to retrieve JeuxDeMots data. * @module jeuxdemots */ /** * Search for a word in Rézo Dump. * @param word {string} word to search for. * @param [options] {{outrelation?: boolean, inrelation?: boolean, relationTypes?: string[]}} default: {outrelation: true, inrelation: true} * search options. OutRelation is for including or excluding outcoming relation and InRelation for incoming relations. * @returns {Promise<Dump>} * @api public */ exports.search = function (word, options) { return new Promise((resolve, reject) => { http.get(`http://www.jeuxdemots.org/rezo-dump.php?gotermsubmit=Chercher&gotermrel=test&rel=?gotermsubmit=Chercher&gotermrel=${word}&rel=`, function (res) { res.setEncoding('latin1'); let data = ''; // called when a data chunk is received. res.on('data', (chunk) => { data += chunk; }); res.on('end', () => { resolve(new Dump(data.slice(data.indexOf(`<code>`) + 6, data.indexOf(`</code>`)), options)); }); }).on("error", (err) => { reject(err); }); }); }